forked from hzeller/beagleg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim-firmware.c
334 lines (300 loc) · 11.5 KB
/
sim-firmware.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* (c) 2013, 2014 Henner Zeller <h.zeller@acm.org>
*
* This file is part of BeagleG. http://github.com/hzeller/beagleg
*
* BeagleG is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BeagleG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BeagleG. If not, see <http://www.gnu.org/licenses/>.
*/
/*
Gnuplot X11 output does not do dashes by default, so this
should be in the ~/.Xdefaults
! gnuplot settings
gnuplot*dashed: on
gnuplot*borderDashes: 0
gnuplot*axisDashes: 16
gnuplot*line1Dashes: 0
gnuplot*line2Dashes: 42
gnuplot*line3Dashes: 13
gnuplot*line4Dashes: 44
gnuplot*line5Dashes: 15
gnuplot*line6Dashes: 4441
gnuplot*line7Dashes: 42
gnuplot*line8Dashes: 13
*/
/*
Gnuplot, showing speed and steps on the left axis, acceleration on
the right axis.
set grid # easier to follow
set ytics nomirror # Don't intervene with y2tics
set y2tics
set ylabel "steps & steps/s (velocity)"
set y2label "steps/s^2 (acceleration)"
# euclid axis
set style line 1 linetype 1 linecolor rgb "black" lw 2 # velocity
set style line 2 linetype 2 linecolor rgb "black" lw 1 # accel
# first axis steps/speed/velocity (X)
set style line 10 linetype 5 linecolor rgb "red" lw 1 # steps
set style line 11 linetype 1 linecolor rgb "red" lw 2 # velocity
set style line 12 linetype 2 linecolor rgb "red" lw 1 # accel
# Y
set style line 20 linetype 5 linecolor rgb "blue" lw 1
set style line 21 linetype 1 linecolor rgb "blue" lw 2
set style line 22 linetype 2 linecolor rgb "blue" lw 1
# Z
set style line 30 linetype 5 linecolor rgb "green" lw 1
set style line 31 linetype 1 linecolor rgb "green" lw 2
set style line 32 linetype 2 linecolor rgb "green" lw 1
# Plotting X,Y axis
plot "/tmp/foo.data" \
using 1:11 title "steps X" with lines ls 10, \
'' using 1:12 title "velocity X" with lines ls 11,\
'' using 1:13 title "accel X" axes x1y2 with lines ls 12, \
'' using 1:14 title "steps Y" with lines ls 20, \
'' using 1:15 title "velocity Y" with lines ls 21,\
'' using 1:16 title "accel Y" axes x1y2 with lines ls 22
#.. as one-liner
plot "/tmp/foo.data" using 1:11 title "steps X" with lines ls 10, '' using 1:12 title "velocity X" with lines ls 11, '' using 1:13 title "accel X" axes x1y2 with lines ls 12,'' using 1:14 title "steps Y" with lines ls 20,'' using 1:15 title "velocity Y" with lines ls 21,'' using 1:16 title "accel Y" axes x1y2 with lines ls 22
# Euclid space
plot "/tmp/foo.data" using 1:3 title "velocity Euclid" with lines ls 1, '' using 1:4 title "accel Euclid" axes x1y2 with lines ls 2
*/
#include "sim-firmware.h"
#include <math.h>
#include <stdint.h>
#include <strings.h>
#include <stdio.h>
#include "motion-queue.h"
#include "motor-interface-constants.h"
#define LOOPS_PER_STEP (1 << 1)
/*
* Due to the timer accuracy, velocity is quantized (sometimes, adjacent steps have the
* exact velocity followed by a step in velocity)
* Calculating the acceleration requires some smoothing moving average over
* a couple of measurements.
* The minimum is 2 to look at two adjacent values.
*/
#define AVERAGE_RINBGUFFER_SIZE 10
static double avg_ringbuffer[AVERAGE_RINBGUFFER_SIZE];
static double avg_dt_sum = 0;
static uint32_t avg_pos = 0;
static void avg_reset() {
avg_dt_sum = 0;
avg_pos = 0;
bzero(&avg_ringbuffer, AVERAGE_RINBGUFFER_SIZE * sizeof(double));
}
static double avg_get_acceleration() {
if (avg_pos < 2)
return 0;
// We go back as far as the ringbuffer reaches. In the beginning, that is not far.
int back = AVERAGE_RINBGUFFER_SIZE - 1;
if (back >= avg_pos) {
back = avg_pos - 1;
}
double dt0 = avg_ringbuffer[(avg_pos + AVERAGE_RINBGUFFER_SIZE - back) % AVERAGE_RINBGUFFER_SIZE];
double dt1 = avg_ringbuffer[avg_pos % AVERAGE_RINBGUFFER_SIZE];
if (dt0 <= 0 || dt1 <= 0)
return 0;
double v0 = (1 / dt0) / LOOPS_PER_STEP;
double v1 = (1 / dt1) / LOOPS_PER_STEP;
return (v1 - v0) / (avg_dt_sum - dt1);
}
static void avg_push_delta_time(double t) {
int next_pos = (avg_pos + 1) % AVERAGE_RINBGUFFER_SIZE;
avg_dt_sum -= avg_ringbuffer[next_pos];
avg_ringbuffer[next_pos] = t;
avg_dt_sum += t;
avg_pos++;
}
struct HardwareState {
// Internal state
uint32_t m[MOTION_MOTOR_COUNT];
};
static double sim_time;
static int sim_steps[MOTION_MOTOR_COUNT]; // we are only looking at the defining axis steps.
static struct HardwareState state;
// Default mapping of our motors to axis (see kChannelLayout)
enum {
X_MOTOR = 2,
Y_MOTOR = 3,
Z_MOTOR = 1,
};
static double euclid(double x, double y, double z) {
return sqrt(x*x + y*y + z*z);
}
// This simulates what happens in the PRU. For testing purposes.
static void sim_enqueue(struct MotionSegment *segment) {
if (segment->state == STATE_EXIT)
return;
// setting output direction according to segment->direction_bits;
bzero(&state, sizeof(state));
// For convenience, this is the relative speed of each motor.
double motor_speeds[MOTION_MOTOR_COUNT];
double div = 1.0 * 2147483647u;
for (int i = 0; i < MOTION_MOTOR_COUNT; ++i) {
motor_speeds[i] = segment->fractions[i] / div;
}
const double euklid_factor = euclid(motor_speeds[X_MOTOR],
motor_speeds[Y_MOTOR],
motor_speeds[Z_MOTOR]);
#if JERK_EXPERIMENT
uint32_t jerk_index = 1;
#endif
char is_first = 1;
uint32_t remainder = 0;
const char *msg = "";
for (;;) {
// Increment by motor fraction.
for (int i = 0; i < MOTION_MOTOR_COUNT; ++i) {
int before = (state.m[i] & 0x80000000) != 0;
state.m[i] += segment->fractions[i];
// Top bit is our step bit. Collect all of these and output to hardware.
int after = (state.m[i] & 0x80000000) != 0;
if (!before && after) { // transition 0->1
sim_steps[i] += ((1 << i) & segment->direction_bits) ? -1 : 1;
}
}
msg = "";
sim_time += 160e-9; // Updating the motor takes this time.
uint32_t delay_loops = 0;
// Higher resolution delay if we had fractional counts. Used to better calculate acceleration
// for display purposes.
double hires_delay = 0;
#if JERK_EXPERIMENT
if (segment->jerk_start > 0) {
if (is_first) {
msg = "# jerk";
fprintf(stderr, "jerk start: jerk-timer-cycles=%.3f\n",
segment->jerk_motion);
is_first = 0;
}
// TODO: index 0 ?
// TODO: is it 2* ?
segment->jerk_motion -= 2*segment->jerk_motion / ((3 * jerk_index) + 1);
--segment->jerk_start;
++jerk_index;
delay_loops = segment->jerk_motion;
hires_delay = segment->jerk_motion;
if (hires_delay < 0) {
fprintf(stderr, "Got less than 0 at index %d\n", jerk_index);
segment->jerk_start = 0;
hires_delay = delay_loops = 30000;
}
if (segment->jerk_start == 0) {
fprintf(stderr, "jerk end : jerk-timer-cycles=%.3f\n",
segment->jerk_motion);
is_first = 1;
}
}
else
#endif
if (segment->loops_accel > 0) {
if (is_first) {
msg = "# accel.";
fprintf(stderr, "SIM: Accel start: accel-series-idx=%5u, "
"accel-timer-cycles=%.3f (%d half-steps)\n",
segment->accel_series_index,
1.0 * segment->hires_accel_cycles / (1<<DELAY_CYCLE_SHIFT),
segment->loops_accel);
is_first = 0;
}
if (segment->accel_series_index != 0) {
const uint32_t divident = (segment->hires_accel_cycles << 1) + remainder;
const uint32_t divisor = (segment->accel_series_index << 2) + 1;
segment->hires_accel_cycles -= (divident / divisor);
remainder = divident % divisor;
}
++segment->accel_series_index;
--segment->loops_accel;
delay_loops = segment->hires_accel_cycles >> DELAY_CYCLE_SHIFT;
hires_delay = 1.0 * segment->hires_accel_cycles / (1<<DELAY_CYCLE_SHIFT);
if (segment->loops_accel == 0) {
fprintf(stderr, "SIM: Accel end : accel-series-idx=%5u, accel-timer-cycles=%.3f\n",
segment->accel_series_index,
1.0 * segment->hires_accel_cycles / (1<<DELAY_CYCLE_SHIFT));
}
}
else if (segment->loops_travel > 0) {
delay_loops = segment->travel_delay_cycles;
hires_delay = segment->travel_delay_cycles;
if (is_first) {
msg = "# travel.";
fprintf(stderr, "SIM: travel. timer-delay-cycles=%u (%d half-steps)\n", delay_loops,
segment->loops_travel);
is_first = 0;
}
--segment->loops_travel;
}
else if (segment->loops_decel > 0) {
if (is_first) {
msg = "# decel.";
fprintf(stderr, "SIM: Decel start: accel-series-idx=%5u, "
"decel-timer-cycles=%.3f (%d half-steps)\n",
segment->accel_series_index,
1.0 * segment->hires_accel_cycles / (1<<DELAY_CYCLE_SHIFT),
segment->loops_decel);
is_first = 0;
}
const uint32_t divident = (segment->hires_accel_cycles << 1) + remainder;
const uint32_t divisor = (segment->accel_series_index << 2) - 1;
segment->hires_accel_cycles += (divident / divisor);
remainder = divident % divisor;
--segment->accel_series_index;
--segment->loops_decel;
delay_loops = segment->hires_accel_cycles >> DELAY_CYCLE_SHIFT;
hires_delay = 1.0 * segment->hires_accel_cycles / (1<<DELAY_CYCLE_SHIFT);
if (segment->loops_decel == 0) {
fprintf(stderr, "SIM: Decel end : accel-series-idx=%5u, decel-timer-cycles=%.3f\n",
segment->accel_series_index,
1.0 * segment->hires_accel_cycles / (1<<DELAY_CYCLE_SHIFT));
}
}
else {
break; // done.
}
double wait_time = 1.0 * delay_loops / TIMER_FREQUENCY;
avg_push_delta_time(1.0 * hires_delay / TIMER_FREQUENCY);
double acceleration = avg_get_acceleration();
sim_time += wait_time;
double velocity = (1 / wait_time) / LOOPS_PER_STEP; // in Hz.
// Total time; speed; acceleration; delay_loops. [steps walked for all motors].
printf("%12.8f %10d %12.4f %12.4f ",
sim_time, delay_loops,
euklid_factor * velocity,
euklid_factor * acceleration);
for (int i = 0; i < MOTION_MOTOR_COUNT; ++i) {
printf("%5d %8.4f %8.4f ", sim_steps[i],
motor_speeds[i] * velocity,
motor_speeds[i] * acceleration);
}
printf("%s\n", msg);
}
}
static void sim_wait_queue_empty() {}
static void sim_motor_enable(char on) {}
static void sim_shutdown(char do_flush) {}
void init_sim_motion_queue(struct MotionQueue *queue) {
bzero(&state, sizeof(state));
sim_time = 0;
bzero(&sim_steps, sizeof(&sim_steps));
avg_reset();
queue->enqueue = &sim_enqueue;
queue->wait_queue_empty = &sim_wait_queue_empty;
queue->motor_enable = &sim_motor_enable;
queue->shutdown = &sim_shutdown;
// Total time; speed; acceleration; delay_loops. [steps walked for all motors].
printf("%12s %10s %12s %12s ", "time", "timer-loop", "Euclid-speed", "Euclid-accel");
for (int i = 0; i < MOTION_MOTOR_COUNT; ++i) {
printf("%4s%d %7s%d %7s%d ", "s", i, "v", i, "a", i);
}
printf("\n");
}