Skip to content

Commit 3eae1ef

Browse files
committed
refactor(hm2): drop _64 encoder pins, keep 64-bit internal
RFC #3286 narrow option 1: fix float position wrap on high-count encoders via 64-bit internal counters, without exposing new HAL pin types. s32 count/rawcounts/rawlatch/ count-latched remain as truncated views of the internal 64-bit counters. Float position is computed from the full 64-bit value so it does not wrap. Avoids committing four integer pin types (s32/u32/s64/u64) into 2.10 ABI before the getter/setter direction in #3286 is decided. The overflow fix from 8a3ac3f is preserved; only the pin exposure is reverted. Unused prev_event_rawcounts_64 dropped. BREAKING CHANGE: count_64, rawcounts_64, rawlatch_64 and count-latched_64 pins added post-2.9 are removed. HAL surface matches 2.9. Refs #3286 #2331
1 parent a2283f8 commit 3eae1ef

3 files changed

Lines changed: 43 additions & 86 deletions

File tree

docs/src/man/man9/hostmot2.9.adoc

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,7 @@ following pins and parameters:
322322
*Pins:*
323323

324324
count (s32 out)::
325-
Number of encoder counts since the previous reset.
326-
count_64 (s64 out)::
327-
Number of encoder counts since the previous reset (64 bit).
325+
Number of encoder counts since the previous reset. 32-bit truncation of the 64-bit internal counter; position is computed from the full-width internal value so it does not wrap.
328326
position (float out)::
329327
Encoder position in position units (count / scale).
330328
position-interpolated (float out)::
@@ -355,13 +353,9 @@ probe-invert (bit r/w)::
355353
If set to True, the rising edge of the probe input pin triggers the latch event (if probe-enable is True).
356354
If set to False, the falling edge triggers. (only present if supported by firmware)
357355
rawcounts (s32 out)::
358-
Total number of encoder counts since the start, not adjusted for index or reset.
359-
rawcounts_64 (s64 out)::
360-
Total number of encoder counts since the start, not adjusted for index or reset. (64 bit)
356+
Total number of encoder counts since the start, not adjusted for index or reset. Truncated view of the internal 64-bit counter.
361357
count_latch (s32 out)::
362-
Encoder count at latch event. (index or probe)
363-
count_latch_64 (s64 out)::
364-
Encoder count at latch event. (index or probe) (64 bit)
358+
Encoder count at latch event (index or probe). Truncated view of the internal 64-bit latched count.
365359
input-a, input-b, input-index (bit out)::
366360
Real time filtered values of A,B,Index encoder signals
367361
quad-error-enable (bit in)::

src/hal/drivers/mesa-hostmot2/encoder.c

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -510,54 +510,26 @@ int hm2_encoder_parse_md(hostmot2_t *hm2, int md_index) {
510510
goto fail1;
511511
}
512512

513-
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.rawcounts_64", hm2->llio->name, i);
514-
r = hal_pin_s64_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.rawcounts_64), hm2->llio->comp_id);
515-
if (r < 0) {
516-
HM2_ERR("error adding pin '%s', aborting\n", name);
517-
goto fail1;
518-
}
519-
520513
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.rawlatch", hm2->llio->name, i);
521514
r = hal_pin_s32_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.rawlatch), hm2->llio->comp_id);
522515
if (r < 0) {
523516
HM2_ERR("error adding pin '%s', aborting\n", name);
524517
goto fail1;
525518
}
526519

527-
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.rawlatch_64", hm2->llio->name, i);
528-
r = hal_pin_s64_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.rawlatch_64), hm2->llio->comp_id);
529-
if (r < 0) {
530-
HM2_ERR("error adding pin '%s', aborting\n", name);
531-
goto fail1;
532-
}
533-
534520
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.count", hm2->llio->name, i);
535521
r = hal_pin_s32_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.count), hm2->llio->comp_id);
536522
if (r < 0) {
537523
HM2_ERR("error adding pin '%s', aborting\n", name);
538524
goto fail1;
539525
}
540526

541-
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.count_64", hm2->llio->name, i);
542-
r = hal_pin_s64_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.count_64), hm2->llio->comp_id);
543-
if (r < 0) {
544-
HM2_ERR("error adding pin '%s', aborting\n", name);
545-
goto fail1;
546-
}
547-
548527
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.count-latched", hm2->llio->name, i);
549528
r = hal_pin_s32_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.count_latch), hm2->llio->comp_id);
550529
if (r < 0) {
551530
HM2_ERR("error adding pin '%s', aborting\n", name);
552531
goto fail1;
553532
}
554-
555-
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.count-latched_64", hm2->llio->name, i);
556-
r = hal_pin_s64_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.count_latch_64), hm2->llio->comp_id);
557-
if (r < 0) {
558-
HM2_ERR("error adding pin '%s', aborting\n", name);
559-
goto fail1;
560-
}
561533

562534
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.position", hm2->llio->name, i);
563535
r = hal_pin_float_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.position), hm2->llio->comp_id);
@@ -779,13 +751,13 @@ void hm2_encoder_tram_init(hostmot2_t *hm2) {
779751

780752
*hm2->encoder.instance[i].hal.pin.rawcounts = count;
781753
*hm2->encoder.instance[i].hal.pin.rawlatch = count;
782-
*hm2->encoder.instance[i].hal.pin.rawcounts_64 = count;
783-
*hm2->encoder.instance[i].hal.pin.rawlatch_64 = count;
754+
hm2->encoder.instance[i].rawcounts_64 = count;
755+
hm2->encoder.instance[i].rawlatch_64 = count;
784756

785757
*hm2->encoder.instance[i].hal.pin.count = 0;
786758
*hm2->encoder.instance[i].hal.pin.count_latch = 0;
787-
*hm2->encoder.instance[i].hal.pin.count_64 = 0;
788-
*hm2->encoder.instance[i].hal.pin.count_latch_64 = 0;
759+
hm2->encoder.instance[i].count_64 = 0;
760+
hm2->encoder.instance[i].count_latch_64 = 0;
789761
*hm2->encoder.instance[i].hal.pin.position = 0.0;
790762
*hm2->encoder.instance[i].hal.pin.position_latch = 0.0;
791763
*hm2->encoder.instance[i].hal.pin.velocity = 0.0;
@@ -831,29 +803,22 @@ void hm2_encoder_tram_init(hostmot2_t *hm2) {
831803

832804
static void hm2_encoder_instance_update_rawcounts_and_handle_index(hostmot2_t *hm2, int instance) {
833805
rtapi_u16 reg_count;
834-
rtapi_s32 reg_count_diff;
835-
rtapi_s32 prev_rawcounts;
836806
rtapi_s64 prev_rawcounts_64;
837807

838808
hm2_encoder_instance_t *e;
839809

840810
e = &hm2->encoder.instance[instance];
841-
prev_rawcounts = *e->hal.pin.rawcounts;
842-
prev_rawcounts_64 = *e->hal.pin.rawcounts_64;
811+
prev_rawcounts_64 = e->rawcounts_64;
843812

844813

845-
//
814+
//
846815
// figure out current rawcounts accumulated by the driver
847-
//
816+
//
848817

849818
reg_count = hm2_encoder_get_reg_count(hm2, instance);
850819

851-
reg_count_diff = (rtapi_s32)reg_count - (rtapi_s32)e->prev_reg_count;
852-
if (reg_count_diff > 32768) reg_count_diff -= 65536;
853-
if (reg_count_diff < -32768) reg_count_diff += 65536;
854-
855-
*e->hal.pin.rawcounts += reg_count_diff;
856-
*e->hal.pin.rawcounts_64 += reg_count_diff;
820+
e->rawcounts_64 = hal_extend_counter(e->rawcounts_64, reg_count, 16);
821+
*e->hal.pin.rawcounts = (rtapi_s32)e->rawcounts_64;
857822

858823

859824
//
@@ -870,18 +835,17 @@ static void hm2_encoder_instance_update_rawcounts_and_handle_index(hostmot2_t *h
870835
// hm2 reports index event occurred
871836

872837
rtapi_u16 latched_count;
838+
rtapi_s64 latched_64;
873839

874840
latched_count = (latch_ctrl >> 16) & 0xffff;
875841

876-
reg_count_diff = (rtapi_s32)latched_count - (rtapi_s32)e->prev_reg_count;
877-
if (reg_count_diff > 32768) reg_count_diff -= 65536;
878-
if (reg_count_diff < -32768) reg_count_diff += 65536;
842+
latched_64 = hal_extend_counter(prev_rawcounts_64, latched_count, 16);
879843
if (!*(e->hal.pin.no_clear_on_index)) {
880-
e->zero_offset = prev_rawcounts + reg_count_diff;
881-
e->zero_offset_64 = prev_rawcounts_64 + (rtapi_s64)reg_count_diff;
844+
e->zero_offset_64 = latched_64;
845+
e->zero_offset = (rtapi_s32)latched_64;
882846
}
883-
*(e->hal.pin.rawlatch) = prev_rawcounts + reg_count_diff;
884-
*(e->hal.pin.rawlatch_64) = prev_rawcounts_64 + (rtapi_s64)reg_count_diff;
847+
e->rawlatch_64 = latched_64;
848+
*(e->hal.pin.rawlatch) = (rtapi_s32)latched_64;
885849
*e->hal.pin.index_enable = 0;
886850
// may need to update interpolated position after index event because
887851
// this _may_ happen without a count but its pretty ugly...
@@ -897,15 +861,13 @@ static void hm2_encoder_instance_update_rawcounts_and_handle_index(hostmot2_t *h
897861
// hm2 reports probe event occurred
898862

899863
rtapi_u16 latched_count;
864+
rtapi_s64 latched_64;
900865

901866
latched_count = (latch_ctrl >> 16) & 0xffff;
902867

903-
reg_count_diff = (rtapi_s32)latched_count - (rtapi_s32)e->prev_reg_count;
904-
if (reg_count_diff > 32768) reg_count_diff -= 65536;
905-
if (reg_count_diff < -32768) reg_count_diff += 65536;
906-
907-
*(e->hal.pin.rawlatch) = prev_rawcounts + reg_count_diff;
908-
*(e->hal.pin.rawlatch_64) = prev_rawcounts_64 + (rtapi_s64)reg_count_diff;
868+
latched_64 = hal_extend_counter(prev_rawcounts_64, latched_count, 16);
869+
e->rawlatch_64 = latched_64;
870+
*(e->hal.pin.rawlatch) = (rtapi_s32)latched_64;
909871
*e->hal.pin.latch_enable = 0;
910872
}
911873
}
@@ -953,34 +915,35 @@ static void hm2_encoder_instance_update_position(hostmot2_t *hm2, int instance)
953915

954916
if (*e->hal.pin.reset) {
955917
e->zero_offset = *e->hal.pin.rawcounts;
956-
e->zero_offset_64 = *e->hal.pin.rawcounts_64;
918+
e->zero_offset_64 = e->rawcounts_64;
957919
*e->hal.pin.rawlatch = e->zero_offset;
958-
*e->hal.pin.rawlatch_64 = e->zero_offset_64;
959-
*e->hal.pin.position_interpolated = *e->hal.pin.position;
920+
e->rawlatch_64 = e->zero_offset_64;
921+
*e->hal.pin.position_interpolated = *e->hal.pin.position;
960922

961923
}
962924

963925

964-
//
926+
//
965927
// Now we know the current rawcounts and zero_offset, which together
966928
// tell us the current count.
967929
//
968930
// From that we easily compute count and scaled position.
969931
//
970932

971933
*e->hal.pin.count = *e->hal.pin.rawcounts - e->zero_offset;
972-
*e->hal.pin.count_64 = *e->hal.pin.rawcounts_64 - e->zero_offset_64;
934+
e->count_64 = e->rawcounts_64 - e->zero_offset_64;
973935
*e->hal.pin.count_latch = *e->hal.pin.rawlatch - e->zero_offset;
974-
*e->hal.pin.count_latch_64 = *e->hal.pin.rawlatch_64 - e->zero_offset_64;
936+
e->count_latch_64 = e->rawlatch_64 - e->zero_offset_64;
975937

976938

977939
//
978940
// Now we know the current current .count, from this we easily compute
979-
// the scaled position.
941+
// the scaled position. Float position uses the 64-bit internal count
942+
// to avoid wrap on high-resolution encoders.
980943
//
981944

982-
*e->hal.pin.position = *e->hal.pin.count_64 / e->hal.param.scale;
983-
*e->hal.pin.position_latch = *e->hal.pin.count_latch_64 / e->hal.param.scale;
945+
*e->hal.pin.position = e->count_64 / e->hal.param.scale;
946+
*e->hal.pin.position_latch = e->count_latch_64 / e->hal.param.scale;
984947
}
985948

986949

@@ -1023,7 +986,6 @@ static void hm2_encoder_instance_process_tram_read(hostmot2_t *hm2, int instance
1023986
hm2_encoder_instance_update_position(hm2, instance);
1024987

1025988
e->prev_event_rawcounts = *e->hal.pin.rawcounts;
1026-
e->prev_event_rawcounts_64 = *e->hal.pin.rawcounts_64;
1027989
e->prev_event_reg_timestamp = hm2_encoder_get_reg_timestamp(hm2, instance);
1028990
e->prev_dS_counts = 0;
1029991

src/hal/drivers/mesa-hostmot2/hostmot2.h

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,10 @@ typedef struct {
281281
struct {
282282

283283
struct {
284-
hal_s32_t *rawcounts; // raw encoder counts
285-
hal_s32_t *rawlatch; // raw encoder of latch
286-
hal_s32_t *count; // (rawcounts - zero_offset)
287-
hal_s32_t *count_latch; // (rawlatch - zero_offset)
288-
hal_s64_t *rawcounts_64; // raw encoder counts
289-
hal_s64_t *rawlatch_64; // raw encoder of latch
290-
hal_s64_t *count_64; // (rawcounts - zero_offset)
291-
hal_s64_t *count_latch_64; // (rawlatch - zero_offset)
284+
hal_s32_t *rawcounts; // raw encoder counts (truncated view of 64-bit internal)
285+
hal_s32_t *rawlatch; // raw encoder of latch (truncated view)
286+
hal_s32_t *count; // (rawcounts - zero_offset), truncated view
287+
hal_s32_t *count_latch; // (rawlatch - zero_offset), truncated view
292288
hal_float_t *position;
293289
hal_float_t *position_latch;
294290
hal_float_t *position_interpolated;
@@ -321,7 +317,13 @@ typedef struct {
321317
} hal;
322318

323319
rtapi_s32 zero_offset; // *hal.pin.counts == (*hal.pin.rawcounts - zero_offset)
324-
rtapi_s64 zero_offset_64; // *hal.pin.counts_64 == (*hal.pin.rawcounts_64 - zero_offset_64)
320+
// 64-bit internals prevent float position wrap on high-count encoders.
321+
// Not exposed as HAL pins; s32 pins above are truncated views.
322+
rtapi_s64 rawcounts_64;
323+
rtapi_s64 rawlatch_64;
324+
rtapi_s64 count_64;
325+
rtapi_s64 count_latch_64;
326+
rtapi_s64 zero_offset_64;
325327

326328
rtapi_u16 prev_reg_count; // from this and the current count in the register we compute a change-in-counts, which we add to rawcounts
327329

@@ -335,7 +337,6 @@ typedef struct {
335337

336338
// these two are the datapoint last time we moved (only valid if state == HM2_ENCODER_MOVING)
337339
rtapi_s32 prev_event_rawcounts;
338-
rtapi_s64 prev_event_rawcounts_64;
339340
rtapi_u16 prev_event_reg_timestamp;
340341

341342
rtapi_s32 tsc_num_rollovers;

0 commit comments

Comments
 (0)