Skip to content

Commit f4e6146

Browse files
authored
Merge pull request #4743 from BsAtHome/hal64_counter-encoder
hal: Update all accesses to 64-bit and make counting 64-bit clean
2 parents 6660aee + 42afee3 commit f4e6146

3 files changed

Lines changed: 73 additions & 73 deletions

File tree

‎src/hal/components/counter.c‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ typedef struct {
7676
hal_real_t pos_scale; /* pin: scaling factor for pos */
7777
double old_scale; /* stored scale value */
7878
double scale; /* reciprocal value used for scaling */
79-
rtapi_s32 last_count;
80-
rtapi_s32 last_index_count;
79+
rtapi_sint last_count;
80+
rtapi_sint last_index_count;
8181
} counter_t;
8282

8383
/* pointer to array of counter_t structs in shmem, 1 per counter */
@@ -138,8 +138,8 @@ int rtapi_app_main(void)
138138
counter_array[n].oldZ = 0;
139139
counter_array[n].oldA = 0;
140140
counter_array[n].reset_on_index = 0;
141-
hal_set_si32(counter_array[n].raw_count, 0);
142-
hal_set_si32(counter_array[n].count, 0);
141+
hal_set_sint(counter_array[n].raw_count, 0);
142+
hal_set_sint(counter_array[n].count, 0);
143143
hal_set_real(counter_array[n].pos, 0.0);
144144
hal_set_real(counter_array[n].pos_scale, 1.0);
145145
counter_array[n].old_scale = 1.0;
@@ -186,12 +186,12 @@ static void update(void *arg, long period)
186186
for (cntr = arg, n = 0; n < num_chan; cntr++, n++) {
187187
// count on rising edge
188188
if(!cntr->oldA && hal_get_bool(cntr->phaseA))
189-
hal_set_si32(cntr->raw_count, hal_get_si32(cntr->raw_count) + 1);
189+
hal_set_sint(cntr->raw_count, hal_get_sint(cntr->raw_count) + 1);
190190
cntr->oldA = hal_get_bool(cntr->phaseA);
191191

192192
// reset on rising edge
193193
if(cntr->reset_on_index && !cntr->oldZ && hal_get_bool(cntr->phaseZ)) {
194-
cntr->last_index_count = hal_get_si32(cntr->raw_count);
194+
cntr->last_index_count = hal_get_sint(cntr->raw_count);
195195
hal_set_bool(cntr->index_ena, 0);
196196
}
197197
cntr->oldZ = hal_get_bool(cntr->phaseZ);
@@ -205,17 +205,17 @@ static void capture(void *arg, long period)
205205

206206
for (cntr = arg, n = 0; n < num_chan; cntr++, n++) {
207207
/* check reset input */
208-
int raw_count;
209-
int counts;
208+
rtapi_sint raw_count;
209+
rtapi_sint counts;
210210
if (hal_get_bool(cntr->reset)) {
211211
/* reset is active, reset the counter */
212-
hal_set_si32(cntr->raw_count, 0);
212+
hal_set_sint(cntr->raw_count, 0);
213213
cntr->last_index_count = 0;
214214
cntr->last_count = 0;
215215
}
216216
/* capture raw counts to latches */
217-
raw_count = hal_get_si32(cntr->raw_count);
218-
hal_set_si32(cntr->count, raw_count - cntr->last_index_count);
217+
raw_count = hal_get_sint(cntr->raw_count);
218+
hal_set_sint(cntr->count, raw_count - cntr->last_index_count);
219219
counts = (raw_count - cntr->last_count);
220220
cntr->last_count = raw_count;
221221

@@ -232,7 +232,7 @@ static void capture(void *arg, long period)
232232
cntr->scale = 1.0 / hal_get_real(cntr->pos_scale);
233233
}
234234
/* scale count to make floating point position */
235-
hal_set_real(cntr->pos, hal_get_si32(cntr->count) * cntr->scale);
235+
hal_set_real(cntr->pos, hal_get_sint(cntr->count) * cntr->scale);
236236
/* scale counts to make floating point velocity */
237237
hal_set_real(cntr->vel, counts * cntr->scale * 1e9 / period);
238238

@@ -273,9 +273,9 @@ static int export_counter(int num, counter_t * addr)
273273
/* export pin for the reset input */
274274
CHK(hal_pin_new_bool(comp_id, HAL_IN, &addr->reset, 0, "counter.%d.reset", num));
275275
/* export parameter for raw counts */
276-
CHK(hal_pin_new_si32(comp_id, HAL_OUT, &addr->raw_count, 0, "counter.%d.rawcounts", num));
276+
CHK(hal_pin_new_sint(comp_id, HAL_OUT, &addr->raw_count, 0, "counter.%d.rawcounts", num));
277277
/* export pin for counts captured by capture() */
278-
CHK(hal_pin_new_si32(comp_id, HAL_OUT, &addr->count, 0, "counter.%d.counts", num));
278+
CHK(hal_pin_new_sint(comp_id, HAL_OUT, &addr->count, 0, "counter.%d.counts", num));
279279
/* export pin for scaled position captured by capture() */
280280
CHK(hal_pin_new_real(comp_id, HAL_OUT, &addr->pos, 0.0, "counter.%d.position", num));
281281
/* export pin for scaled velocity captured by capture() */

‎src/hal/components/encoder.c‎

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,10 @@ typedef struct {
8888
char count_detected;
8989
char index_detected;
9090
char latch_detected;
91-
rtapi_s32 raw_count;
92-
rtapi_u32 timestamp;
93-
rtapi_s32 index_count;
94-
rtapi_s32 latch_count;
91+
rtapi_sint raw_count;
92+
rtapi_uint timestamp;
93+
rtapi_sint index_count;
94+
rtapi_sint latch_count;
9595
} atomic;
9696

9797
/* this structure contains the runtime data for a single counter
@@ -120,10 +120,10 @@ typedef struct {
120120
hal_bool_t latch_in; /* c:r counter latch input */
121121
hal_bool_t latch_rising; /* u:r latch on rising edge? */
122122
hal_bool_t latch_falling; /* u:r latch on falling edge? */
123-
rtapi_s32 raw_count; /* c:rw captured raw_count */
124-
rtapi_u32 timestamp; /* c:rw captured timestamp */
125-
rtapi_s32 index_count; /* c:rw captured index count */
126-
rtapi_s32 latch_count; /* c:rw captured index count */
123+
rtapi_sint raw_count; /* c:rw captured raw_count */
124+
rtapi_uint timestamp; /* c:rw captured timestamp */
125+
rtapi_sint index_count; /* c:rw captured index count */
126+
rtapi_sint latch_count; /* c:rw captured index count */
127127
hal_sint_t count; /* c:w captured binary count value */
128128
hal_sint_t count_latch; /* c:w captured binary count value */
129129
hal_real_t min_speed; /* c:r minimum velocity to estimate nonzero */
@@ -140,7 +140,7 @@ typedef struct {
140140
int gaps; /* gap counter (unlikely to ever be > 1) */
141141
} counter_t;
142142

143-
static rtapi_u32 timebase; /* master timestamp for all counters */
143+
static rtapi_uint timebase; /* master timestamp for all counters */
144144

145145
/* pointer to array of counter_t structs in shmem, 1 per counter */
146146
static counter_t *counter_array;
@@ -273,12 +273,12 @@ int rtapi_app_main(void)
273273
cntr->buf[0].index_detected = 0;
274274
cntr->buf[1].index_detected = 0;
275275
cntr->bp = &(cntr->buf[0]);
276-
hal_set_si32(cntr->raw_counts, 0);
276+
hal_set_sint(cntr->raw_counts, 0);
277277
cntr->raw_count = 0;
278278
cntr->timestamp = 0;
279279
cntr->index_count = 0;
280280
cntr->latch_count = 0;
281-
hal_set_si32(cntr->count, 0);
281+
hal_set_sint(cntr->count, 0);
282282
hal_set_real(cntr->min_speed, 1.0);
283283
hal_set_real(cntr->pos, 0.0);
284284
hal_set_real(cntr->pos_latch, 0.0);
@@ -332,7 +332,7 @@ static void update(void *arg, long period)
332332
cntr = arg;
333333
for (n = 0; n < howmany; n++) {
334334
buf = (atomic *) cntr->bp;
335-
hal_set_si32(cntr->dt, hal_get_si32(cntr->dt) + period);
335+
hal_set_sint(cntr->dt, hal_get_sint(cntr->dt) + period);
336336
/* get state machine current state */
337337
state = cntr->state;
338338
/* add input bits to state code */
@@ -352,15 +352,15 @@ static void update(void *arg, long period)
352352
}
353353
/* should we count? */
354354
if (state & SM_CNT_UP_MASK) {
355-
if (hal_get_si32(cntr->missing_teeth) && hal_get_si32(cntr->dt) > hal_get_si32(cntr->limit_dt)){
355+
if (hal_get_sint(cntr->missing_teeth) && hal_get_sint(cntr->dt) > hal_get_sint(cntr->limit_dt)){
356356
cntr->gaps++;
357357
}
358-
buf->raw_count = hal_set_si32(cntr->raw_counts, hal_get_si32(cntr->raw_counts) + 1);
358+
buf->raw_count = hal_set_sint(cntr->raw_counts, hal_get_sint(cntr->raw_counts) + 1);
359359
buf->timestamp = timebase;
360360
buf->count_detected = 1;
361-
hal_set_si32(cntr->dt, 0);
361+
hal_set_sint(cntr->dt, 0);
362362
} else if (state & SM_CNT_DN_MASK) {
363-
buf->raw_count = hal_set_si32(cntr->raw_counts, hal_get_si32(cntr->raw_counts) - 1);
363+
buf->raw_count = hal_set_sint(cntr->raw_counts, hal_get_sint(cntr->raw_counts) - 1);
364364
buf->timestamp = timebase;
365365
buf->count_detected = 1;
366366
}
@@ -376,7 +376,7 @@ static void update(void *arg, long period)
376376
/* test for index enabled and rising edge on phase Z */
377377
if ((state & cntr->Zmask) == 1) {
378378
/* capture counts, reset Zmask */
379-
buf->index_count = hal_get_si32(cntr->raw_counts);
379+
buf->index_count = hal_get_sint(cntr->raw_counts);
380380
buf->index_detected = 1;
381381
cntr->Zmask = 0;
382382
}
@@ -388,7 +388,7 @@ static void update(void *arg, long period)
388388
if((rising && hal_get_bool(cntr->latch_rising))
389389
|| (falling && hal_get_bool(cntr->latch_falling))) {
390390
buf->latch_detected = 1;
391-
buf->latch_count = hal_get_si32(cntr->raw_counts);
391+
buf->latch_count = hal_get_sint(cntr->raw_counts);
392392
}
393393
cntr->old_latch = latch;
394394

@@ -407,8 +407,8 @@ static void capture(void *arg, long period)
407407
counter_t *cntr;
408408
atomic *buf;
409409
int n;
410-
rtapi_s32 delta_counts;
411-
rtapi_u32 delta_time;
410+
rtapi_sint delta_counts;
411+
rtapi_uint delta_time;
412412
double vel, interp;
413413

414414
cntr = arg;
@@ -465,26 +465,26 @@ static void capture(void *arg, long period)
465465
running count of edges seen since startup. The
466466
public "count" is the difference between raw_count
467467
and index_count, so it will become zero. */
468-
cntr->raw_count = hal_get_si32(cntr->raw_counts);
468+
cntr->raw_count = hal_get_sint(cntr->raw_counts);
469469
cntr->index_count = cntr->raw_count;
470470
}
471471
/* process data from update() */
472472
if ( buf->count_detected ) {
473-
rtapi_s32 missing_teeth = hal_get_si32(cntr->missing_teeth);
473+
rtapi_sint missing_teeth = hal_get_sint(cntr->missing_teeth);
474474
/* one or more counts in the last period */
475475
buf->count_detected = 0;
476476
delta_time = buf->timestamp - cntr->timestamp;
477477
delta_counts = buf->raw_count - cntr->raw_count;
478478
// lowpass the gap detector deliberately ignoring missing teeth
479479
// see https://github.com/LinuxCNC/linuxcnc/issues/2635
480480
if (delta_counts != 0){
481-
rtapi_s32 limit_dt = hal_get_si32(cntr->limit_dt);
481+
rtapi_sint limit_dt = hal_get_sint(cntr->limit_dt);
482482
limit_dt *= 0.9;
483483
limit_dt += 0.1 * ((missing_teeth + 0.5) * (delta_time / delta_counts));
484-
hal_set_si32(cntr->limit_dt, limit_dt);
484+
hal_set_sint(cntr->limit_dt, limit_dt);
485485
}
486486
// correct counts for tooth gap
487-
hal_set_si32(cntr->raw_counts, hal_get_si32(cntr->raw_counts) + missing_teeth * cntr->gaps);
487+
hal_set_sint(cntr->raw_counts, hal_get_sint(cntr->raw_counts) + missing_teeth * cntr->gaps);
488488
cntr->raw_count = buf->raw_count + missing_teeth * cntr->gaps;
489489
delta_counts += missing_teeth * cntr->gaps;
490490
cntr->gaps = 0;
@@ -500,7 +500,7 @@ static void capture(void *arg, long period)
500500
if ( cntr->counts_since_timeout ) {
501501
/* calc time since last count */
502502
delta_time = timebase - cntr->timestamp;
503-
if (hal_get_si32(cntr->missing_teeth) && delta_time > 1.5 * hal_get_si32(cntr->limit_dt)){
503+
if (hal_get_sint(cntr->missing_teeth) && delta_time > 1.5 * hal_get_sint(cntr->limit_dt)){
504504
// dont update the velocity in the tooth gap
505505
} else if ( delta_time < 1e9 / ( hal_get_real(cntr->min_speed) * cntr->scale )) {
506506
/* not to long, estimate vel if a count arrived now */
@@ -527,11 +527,11 @@ static void capture(void *arg, long period)
527527
}
528528
hal_set_real(cntr->vel_rpm, hal_get_real(cntr->vel) * 60.0);
529529
/* compute net counts */
530-
hal_set_si32(cntr->count, cntr->raw_count - cntr->index_count);
531-
hal_set_si32(cntr->count_latch, cntr->latch_count - cntr->index_count);
530+
hal_set_sint(cntr->count, cntr->raw_count - cntr->index_count);
531+
hal_set_sint(cntr->count_latch, cntr->latch_count - cntr->index_count);
532532
/* scale count to make floating point position */
533-
hal_set_real(cntr->pos, hal_get_si32(cntr->count) * cntr->scale);
534-
hal_set_real(cntr->pos_latch, hal_get_si32(cntr->count_latch) * cntr->scale);
533+
hal_set_real(cntr->pos, hal_get_sint(cntr->count) * cntr->scale);
534+
hal_set_real(cntr->pos_latch, hal_get_sint(cntr->count_latch) * cntr->scale);
535535
/* add interpolation value */
536536
delta_time = timebase - cntr->timestamp;
537537
interp = hal_get_real(cntr->vel) * (delta_time * 1e-9);
@@ -604,19 +604,19 @@ static int export_encoder(counter_t * addr,char * prefix)
604604
}
605605

606606
/* export parameter for raw counts */
607-
retval = hal_pin_new_si32(comp_id, HAL_OUT, &(addr->raw_counts), 0,
607+
retval = hal_pin_new_sint(comp_id, HAL_OUT, &(addr->raw_counts), 0,
608608
"%s.rawcounts", prefix);
609609
if (retval != 0) {
610610
return retval;
611611
}
612612
/* export pin for counts captured by capture() */
613-
retval = hal_pin_new_si32(comp_id, HAL_OUT, &(addr->count), 0,
613+
retval = hal_pin_new_sint(comp_id, HAL_OUT, &(addr->count), 0,
614614
"%s.counts", prefix);
615615
if (retval != 0) {
616616
return retval;
617617
}
618618
/* export pin for counts latched by capture() */
619-
retval = hal_pin_new_si32(comp_id, HAL_OUT, &(addr->count_latch), 0,
619+
retval = hal_pin_new_sint(comp_id, HAL_OUT, &(addr->count_latch), 0,
620620
"%s.counts-latched", prefix);
621621
if (retval != 0) {
622622
return retval;
@@ -676,7 +676,7 @@ static int export_encoder(counter_t * addr,char * prefix)
676676
return retval;
677677
}
678678
/* export pin for missing-tooth index */
679-
retval = hal_pin_new_si32(comp_id, HAL_IN, &(addr->missing_teeth), 0,
679+
retval = hal_pin_new_sint(comp_id, HAL_IN, &(addr->missing_teeth), 0,
680680
"%s.missing-teeth", prefix);
681681
if (retval != 0) {
682682
return retval;
@@ -686,13 +686,13 @@ static int export_encoder(counter_t * addr,char * prefix)
686686
#if 0
687687
// These two, 'dt' and 'limit_dt' are used in the code and if they are
688688
// used, then they must be created to allocate storage.
689-
retval = hal_param_new_si32(comp_id, HAL_RO, &(addr->dt), 0,
689+
retval = hal_param_new_sint(comp_id, HAL_RO, &(addr->dt), 0,
690690
"%s.dt", prefix);
691691
if (retval != 0) {
692692
return retval;
693693
}
694694

695-
retval = hal_param_new_si32(comp_id, HAL_RO, &(addr->limit_dt), 0,
695+
retval = hal_param_new_sint(comp_id, HAL_RO, &(addr->limit_dt), 0,
696696
"%s.limit_dt", prefix);
697697
if (retval != 0) {
698698
return retval;

‎src/hal/components/encoder_ratio.c‎

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@
2929
3030
Input Pins:
3131
32-
encoder-ratio.N.master-A (bit) Phase A of master axis encoder
33-
encoder-ratio.N.master-B (bit) Phase B of master axis encoder
34-
encoder-ratio.N.slave-A (bit) Phase A of slave axis encoder
35-
encoder-ratio.N.slave-B (bit) Phase B of slave axis encoder
36-
encoder-ratio.N.enable (bit) Enables master-slave tracking
32+
encoder-ratio.N.master-A (bool) Phase A of master axis encoder
33+
encoder-ratio.N.master-B (bool) Phase B of master axis encoder
34+
encoder-ratio.N.slave-A (bool) Phase A of slave axis encoder
35+
encoder-ratio.N.slave-B (bool) Phase B of slave axis encoder
36+
encoder-ratio.N.enable (bool) Enables master-slave tracking
3737
3838
Output Pins:
3939
40-
encoder-ratio.N.error (float) Position error of slave (in revs)
40+
encoder-ratio.N.error (real) Position error of slave (in revs)
4141
4242
Parameters:
4343
44-
encoder-ratio.N.master-ppr (u32) Master axis PPR
45-
encoder-ratio.N.slave-ppr (u32) Slave axis PPR
46-
encoder-ratio.N.master-teeth (u32) "teeth" on master "gear"
47-
encoder-ratio.N.slave-teeth (u32) "teeth" on slave "gear"
44+
encoder-ratio.N.master-ppr (uint) Master axis PPR
45+
encoder-ratio.N.slave-ppr (uint) Slave axis PPR
46+
encoder-ratio.N.master-teeth (uint) "teeth" on master "gear"
47+
encoder-ratio.N.slave-teeth (uint) "teeth" on slave "gear"
4848
4949
The module also exports two functions. "encoder-ratio.sample"
5050
must be called in a high speed thread, at least twice the maximum
@@ -121,11 +121,11 @@ typedef struct {
121121
hal_bool_t slave_A; /* quadrature input */
122122
hal_bool_t slave_B; /* quadrature input */
123123
hal_bool_t enable; /* enable input */
124-
unsigned master_state; /* quad decode state machine state */
125-
unsigned slave_state; /* quad decode state machine state */
126-
int raw_error; /* internal data */
127-
int master_increment; /* internal data */
128-
int slave_increment; /* internal data */
124+
rtapi_uint master_state; /* quad decode state machine state */
125+
rtapi_uint slave_state; /* quad decode state machine state */
126+
rtapi_sint raw_error; /* internal data */
127+
rtapi_sint master_increment; /* internal data */
128+
rtapi_sint slave_increment; /* internal data */
129129
double output_scale; /* internal data */
130130
hal_real_t error; /* error output */
131131
hal_uint_t master_ppr; /* parameter: master encoder PPR */
@@ -279,7 +279,7 @@ static void sample(void *arg, long period)
279279
(void)period;
280280
encoder_pair_t *pair;
281281
int n;
282-
unsigned state;
282+
rtapi_uint state;
283283

284284
pair = arg;
285285
for (n = 0; n < howmany; n++) {
@@ -346,9 +346,9 @@ static void update(void *arg, long period)
346346
}
347347
/* update scale factors (only needed if params change, but
348348
it's faster to do it every time than to detect changes.) */
349-
pair->master_increment = hal_get_ui32(pair->master_teeth) * hal_get_ui32(pair->slave_ppr);
350-
pair->slave_increment = hal_get_ui32(pair->slave_teeth) * hal_get_ui32(pair->master_ppr);
351-
pair->output_scale = hal_get_ui32(pair->master_ppr) * hal_get_ui32(pair->slave_ppr) * hal_get_ui32(pair->slave_teeth);
349+
pair->master_increment = hal_get_uint(pair->master_teeth) * hal_get_uint(pair->slave_ppr);
350+
pair->slave_increment = hal_get_uint(pair->slave_teeth) * hal_get_uint(pair->master_ppr);
351+
pair->output_scale = hal_get_uint(pair->master_ppr) * hal_get_uint(pair->slave_ppr) * hal_get_uint(pair->slave_teeth);
352352
/* move on to next pair */
353353
pair++;
354354
}
@@ -405,22 +405,22 @@ static int export_encoder_pair(int num, encoder_pair_t * addr, char* prefix)
405405
return retval;
406406
}
407407
/* export pins for config info() */
408-
retval = hal_pin_new_ui32(comp_id, HAL_IO, &(addr->master_ppr), 0,
408+
retval = hal_pin_new_uint(comp_id, HAL_IO, &(addr->master_ppr), 0,
409409
"%s.master-ppr", prefix);
410410
if (retval != 0) {
411411
return retval;
412412
}
413-
retval = hal_pin_new_ui32(comp_id, HAL_IO, &(addr->slave_ppr), 0,
413+
retval = hal_pin_new_uint(comp_id, HAL_IO, &(addr->slave_ppr), 0,
414414
"%s.slave-ppr", prefix);
415415
if (retval != 0) {
416416
return retval;
417417
}
418-
retval = hal_pin_new_ui32(comp_id, HAL_IO, &(addr->master_teeth), 0,
418+
retval = hal_pin_new_uint(comp_id, HAL_IO, &(addr->master_teeth), 0,
419419
"%s.master-teeth", prefix);
420420
if (retval != 0) {
421421
return retval;
422422
}
423-
retval = hal_pin_new_ui32(comp_id, HAL_IO, &(addr->slave_teeth), 0,
423+
retval = hal_pin_new_uint(comp_id, HAL_IO, &(addr->slave_teeth), 0,
424424
"%s.slave-teeth", prefix);
425425
if (retval != 0) {
426426
return retval;

0 commit comments

Comments
 (0)