1
6
7
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
114
115
116
117
118
122
123
124
125
126
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
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
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
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
313
314
315
316
322
323
324
325
326
327
328
329
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
411
412
413
414
415
/* ... */
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "hardware/pio.h"
#include "hardware/timer.h"
#include "hardware/pwm.h"
#include <pico/divider.h>
#include "quadrature_encoder_substep.pio.h"
8 includes
const int dir_pin = 5;
const int pwm_pin = 7;
static void set_pwm(float value)
{
int ivalue = value * 6250;
if (ivalue < 0) {
gpio_put(dir_pin, true);
pwm_set_gpio_level(pwm_pin, 6250+ivalue);
}if (ivalue < 0) { ... } else {
gpio_put(dir_pin, false);
pwm_set_gpio_level(pwm_pin, ivalue);
}else { ... }
}{ ... }
static void init_pwm(void)
{
gpio_init(dir_pin);
gpio_set_dir(dir_pin, true);
gpio_init(pwm_pin);
gpio_set_dir(pwm_pin, true);
pwm_config cfg = pwm_get_default_config();
pwm_config_set_clkdiv_int(&cfg, 1);
pwm_config_set_wrap(&cfg, 6250);
pwm_init(pwm_gpio_to_slice_num(pwm_pin), &cfg, true);
gpio_set_function(pwm_pin, GPIO_FUNC_PWM);
set_pwm(0);
}{ ... }
typedef struct substep_state_t {
uint calibration_data[4];
uint clocks_per_us;
uint idle_stop_samples;
PIO pio;
uint sm;
uint prev_trans_pos, prev_trans_us;
uint prev_step_us;
uint prev_low, prev_high;
uint idle_stop_sample_count;
int speed_2_20;
int stopped;
int speed;
uint position;
uint raw_step;
...} substep_state_t;
static void read_pio_data(substep_state_t *state, uint *step, uint *step_us, uint *transition_us, int *forward)
{
int cycles;
quadrature_encoder_substep_get_counts(state->pio, state->sm, step, &cycles, step_us);
if (cycles < 0) {
cycles = -cycles;
*forward = 1;
}if (cycles < 0) { ... } else {
cycles = 0x80000000 - cycles;
*forward = 0;
}else { ... }
*transition_us = *step_us - ((cycles * 13) / state->clocks_per_us);
}{ ... }
static uint get_step_start_transition_pos(substep_state_t *state, uint step)
{
return ((step << 6) & 0xFFFFFF00) | state->calibration_data[step & 3];
}{ ... }
static int substep_calc_speed(int delta_substep, int delta_us)
{
return ((int64_t) delta_substep << 20) / delta_us;
}{ ... }
static void substep_init_state(PIO pio, int sm, int pin_a, substep_state_t *state)
{
int forward;
memset(state, 0, sizeof(substep_state_t));
state->pio = pio;
state->sm = sm;
quadrature_encoder_substep_program_init(pio, sm, pin_a);
state->calibration_data[0] = 0;
state->calibration_data[1] = 64;
state->calibration_data[2] = 128;
state->calibration_data[3] = 192;
state->idle_stop_samples = 3;
state->stopped = 1;
state->clocks_per_us = (clock_get_hz(clk_sys) + 500000) / 1000000;
read_pio_data(state, &state->raw_step, &state->prev_step_us, &state->prev_trans_us, &forward);
state->position = get_step_start_transition_pos(state, state->raw_step) + 32;
}{ ... }
static void substep_update(substep_state_t *state)
{
uint step, step_us, transition_us, transition_pos, low, high;
int forward, speed_high, speed_low;
read_pio_data(state, &step, &step_us, &transition_us, &forward);
low = get_step_start_transition_pos(state, step);
high = get_step_start_transition_pos(state, step + 1);
if (step == state->raw_step)
state->idle_stop_sample_count++;
else
state->idle_stop_sample_count = 0;
if (!state->stopped && state->idle_stop_sample_count >= state->idle_stop_samples) {
state->speed = 0;
state->speed_2_20 = 0;
state->stopped = 1;
}if (!state->stopped && state->idle_stop_sample_count >= state->idle_stop_samples) { ... }
if (state->raw_step != step) {
transition_pos = forward ? low : high;
if (!state->stopped)
state->speed_2_20 = substep_calc_speed(transition_pos - state->prev_trans_pos, transition_us - state->prev_trans_us);
state->stopped = 0;
state->prev_trans_pos = transition_pos;
state->prev_trans_us = transition_us;
}if (state->raw_step != step) { ... }
if (!state->stopped) {
if (state->prev_trans_us > state->prev_step_us &&
(int)(state->prev_trans_us - state->prev_step_us) > (int)(step_us - state->prev_trans_us)) {
speed_high = substep_calc_speed(state->prev_trans_pos - state->prev_low, state->prev_trans_us - state->prev_step_us);
speed_low = substep_calc_speed(state->prev_trans_pos - state->prev_high, state->prev_trans_us - state->prev_step_us);
}if (state->prev_trans_us > state->prev_step_us && (int)(state->prev_trans_us - state->prev_step_us) > (int)(step_us - state->prev_trans_us)) { ... } else {
speed_high = substep_calc_speed(high - state->prev_trans_pos, step_us - state->prev_trans_us);
speed_low = substep_calc_speed(low - state->prev_trans_pos, step_us - state->prev_trans_us);
}else { ... }
if (state->speed_2_20 > speed_high)
state->speed_2_20 = speed_high;
if (state->speed_2_20 < speed_low)
state->speed_2_20 = speed_low;
state->speed = (state->speed_2_20 * 62500LL) >> 16;
state->position = state->prev_trans_pos + (((int64_t)state->speed_2_20 * (step_us - transition_us)) >> 20);
if ((int)(state->position - high) > 0)
state->position = high;
else if ((int)(state->position - low) < 0)
state->position = low;
}if (!state->stopped) { ... }
state->prev_low = low;
state->prev_high = high;
state->raw_step = step;
state->prev_step_us = step_us;
}{ ... }
static void substep_calibrate_phases(PIO pio, uint sm)
{
#define sample_count 1024
#ifdef SHOW_ALL_SAMPLES
static int result[sample_count];
int i;/* ... */
#endif
int index, cycles, clocks_per_us, calib[4];
uint cur_us, last_us, step_us, step, last_step;
int64_t sum[4], total;
memset(sum, 0, sizeof(sum));
clocks_per_us = (clock_get_hz(clk_sys) + 500000) / 1000000;
last_step = -10;
index = -10;
while (index < sample_count) {
quadrature_encoder_substep_get_counts(pio, sm, &step, &cycles, &step_us);
if (step == last_step)
continue;
if (index < 0 && index > -4 && (step & 3) == 1)
index = 0;
if (cycles > 0) {
printf("error: expected forward motion\n");
return;
}if (cycles > 0) { ... }
cur_us = step_us + (cycles * 13) / clocks_per_us;
if (index >= 0) {
#ifdef SHOW_ALL_SAMPLES
result[index] = cur_us - last_us;
#endif
sum[(step - 1) & 3] += cur_us - last_us;
}if (index >= 0) { ... }
index++;
last_step = step;
last_us = cur_us;
}while (index < sample_count) { ... }
#ifdef SHOW_ALL_SAMPLES
printf("full sample table:\n");
for (i = 0; i < sample_count; i++) {
printf("%d ", result[i]);
if ((i & 3) == 3)
printf("\n");
}for (i = 0; i < sample_count; i++) { ... }
/* ... */#endif
total = sum[0] + sum[1] + sum[2] + sum[3];
calib[0] = (sum[0] * 256 + total / 2) / total;
calib[1] = ((sum[0] + sum[1]) * 256 + total / 2) / total;
calib[2] = ((sum[0] + sum[1] + sum[2]) * 256 + total / 2) / total;
printf("calibration command:\n\n");
printf("\tsubstep_set_calibration_data(&state, %d, %d, %d);\n\n",
calib[0], calib[1], calib[2]);
}{ ... }
static void substep_set_calibration_data(substep_state_t *state, int step0, int step1, int step2)
{
state->calibration_data[0] = 0;
state->calibration_data[1] = step0;
state->calibration_data[2] = step1;
state->calibration_data[3] = step2;
}{ ... }
int main(void)
{
substep_state_t state;
const uint PIN_A = 2;
stdio_init_all();
PIO pio = pio0;
const uint sm = 0;
pio_add_program(pio, &quadrature_encoder_substep_program);
substep_init_state(pio, sm, PIN_A, &state);
substep_set_calibration_data(&state, 64, 128, 192);
uint last_position = 0;
int last_speed = 0;
uint last_raw_step = 0;
while (1) {
substep_update(&state);
if (last_position != state.position || last_speed != state.speed || last_raw_step != state.raw_step) {
printf("pos: %-10d speed: %-10d raw_steps: %-10d\n", state.position, state.speed, state.raw_step);
last_position = state.position;
last_speed = state.speed;
last_raw_step = state.raw_step;
}if (last_position != state.position || last_speed != state.speed || last_raw_step != state.raw_step) { ... }
sleep_ms(10);
}while (1) { ... }
}{ ... }