Skip to content

Commit e8dfa8f

Browse files
committed
Add interpolated position pin to Quadrature encoder module
1 parent ab3b2d6 commit e8dfa8f

3 files changed

Lines changed: 29 additions & 2 deletions

File tree

docs/man/man9/hostmot2.9

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,14 @@ Number of encoder counts since the previous reset.
290290
(float out) position
291291
Encoder position in position units (count / scale).
292292

293+
.TP
294+
(float out) position-interpolated
295+
Encoder interpolated position in position units (count / scale).
296+
Only valid when velocity is approximately constant and the time
297+
between counts is less than the velocity timeout parameter value.
298+
Do not use for position control. Useful for spindle synchronized
299+
moves with low resolution encoders.
300+
293301
.TP
294302
(float out) position-latched
295303
Encoder latched position in position units (count / scale).

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,13 @@ int hm2_encoder_parse_md(hostmot2_t *hm2, int md_index) {
537537
goto fail1;
538538
}
539539

540+
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.position-interpolated", hm2->llio->name, i);
541+
r = hal_pin_float_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.position_interpolated), hm2->llio->comp_id);
542+
if (r < 0) {
543+
HM2_ERR("error adding pin '%s', aborting\n", name);
544+
goto fail1;
545+
}
546+
540547
rtapi_snprintf(name, sizeof(name), "%s.encoder.%02d.position-latched", hm2->llio->name, i);
541548
r = hal_pin_float_new(name, HAL_OUT, &(hm2->encoder.instance[i].hal.pin.position_latch), hm2->llio->comp_id);
542549
if (r < 0) {
@@ -824,6 +831,10 @@ static void hm2_encoder_instance_update_rawcounts_and_handle_index(hostmot2_t *h
824831

825832
e->zero_offset = prev_rawcounts + reg_count_diff;
826833
*e->hal.pin.index_enable = 0;
834+
// may need to update interpolated position after index event because
835+
// this _may_ happen without a count but its pretty ugly...
836+
// *e->hal.pin.count = *e->hal.pin.rawcounts - e->zero_offset;
837+
//*e->hal.pin.position_interpolated = *e->hal.pin.count / e->hal.param.scale;
827838
}
828839
} else if(e->prev_control & HM2_ENCODER_LATCH_ON_PROBE) {
829840
if (hm2->encoder.firmware_supports_probe) {
@@ -888,6 +899,8 @@ static void hm2_encoder_instance_update_position(hostmot2_t *hm2, int instance)
888899

889900
if (*e->hal.pin.reset) {
890901
e->zero_offset = *e->hal.pin.rawcounts;
902+
*e->hal.pin.position_interpolated = *e->hal.pin.position;
903+
891904
}
892905

893906

@@ -1000,6 +1013,7 @@ static void hm2_encoder_instance_process_tram_read(hostmot2_t *hm2, int instance
10001013
if (dT_s >= e->hal.param.vel_timeout) {
10011014
*e->hal.pin.velocity = 0.0;
10021015
*e->hal.pin.velocity_rpm = 0.0;
1016+
*e->hal.pin.position_interpolated = *e->hal.pin.position;
10031017
e->state = HM2_ENCODER_STOPPED;
10041018
break;
10051019
}
@@ -1009,6 +1023,8 @@ static void hm2_encoder_instance_process_tram_read(hostmot2_t *hm2, int instance
10091023
} else {
10101024
dS_counts = -1;
10111025
}
1026+
// if no counts, calculate interpolated position
1027+
*e->hal.pin.position_interpolated = *e->hal.pin.position + (dT_s * *e->hal.pin.velocity);
10121028

10131029
dS_pos_units = dS_counts / e->hal.param.scale;
10141030

@@ -1039,7 +1055,7 @@ static void hm2_encoder_instance_process_tram_read(hostmot2_t *hm2, int instance
10391055

10401056
hm2_encoder_instance_update_rawcounts_and_handle_index(hm2, instance);
10411057
hm2_encoder_instance_update_position(hm2, instance);
1042-
1058+
*e->hal.pin.position_interpolated = *e->hal.pin.position;
10431059
time_of_interest = hm2_encoder_get_reg_timestamp(hm2, instance);
10441060
if (time_of_interest < e->prev_time_of_interest) {
10451061
// tsc rollover!
@@ -1059,6 +1075,8 @@ static void hm2_encoder_instance_process_tram_read(hostmot2_t *hm2, int instance
10591075
) {
10601076
*e->hal.pin.velocity = 0.0;
10611077
*e->hal.pin.velocity_rpm = 0.0;
1078+
*e->hal.pin.position_interpolated = *e->hal.pin.position;
1079+
10621080
} else {
10631081
dT_clocks = (time_of_interest - e->prev_event_reg_timestamp) + (e->tsc_num_rollovers << 16);
10641082
dT_s = (double)dT_clocks * hm2->encoder.seconds_per_tsdiv_clock;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ typedef struct {
281281
hal_s32_t *count_latch; // (rawlatch - zero_offset)
282282
hal_float_t *position;
283283
hal_float_t *position_latch;
284+
hal_float_t *position_interpolated;
284285
hal_float_t *velocity;
285286
hal_float_t *velocity_rpm;
286287
hal_bit_t *reset;
@@ -338,7 +339,7 @@ typedef struct {
338339
hal_u32_t *sample_frequency;
339340
hal_u32_t *skew;
340341
hal_s32_t *dpll_timer_num;
341-
hal_bit_t *hires_timestamp;
342+
hal_bit_t *hires_timestamp;
342343

343344
} pin;
344345
} hm2_encoder_module_global_t;

0 commit comments

Comments
 (0)