From e627bb058638ecd7c0a2faa41d19da9aad092bfb Mon Sep 17 00:00:00 2001 From: peter wallace Date: Tue, 24 Jul 2018 14:18:19 -0700 Subject: [PATCH] BUGFIX: hostmot2 encoder quadrature error reporting bug. Previous to this patch the quadrature errors were not reported correctly and could be lost. This patch requires a firmware change in the the encoder firmware. Writing 1 to the quadrature error bit in the encoder control register will reset the error and writing 0 to it will do nothing. Previous driver had a bug in error handling, where it did not report the quadrature error properly. Using this patch with unpatched firmware is harmless as the encoder quadrature error did not work to start with. --- src/hal/drivers/mesa-hostmot2/encoder.c | 26 +++++++++++++++----------- src/hal/drivers/mesa-hostmot2/hostmot2.h | 4 ++++ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/hal/drivers/mesa-hostmot2/encoder.c b/src/hal/drivers/mesa-hostmot2/encoder.c index 3c9cd83..4f30816 100644 --- a/src/hal/drivers/mesa-hostmot2/encoder.c +++ b/src/hal/drivers/mesa-hostmot2/encoder.c @@ -110,32 +110,36 @@ static void hm2_encoder_update_control_register(hostmot2_t *hm2) { e->hal.param.filter, HM2_ENCODER_FILTER ); + do_flag( + &hm2->encoder.control_reg[i], + e->reset_quadrature_error, + HM2_ENCODER_QUADRATURE_ERROR + ); } } static void hm2_encoder_read_control_register(hostmot2_t *hm2) { int i; - static int last_error_enable = 0; for (i = 0; i < hm2->encoder.num_instances; i ++) { hm2_encoder_instance_t *e = &hm2->encoder.instance[i]; if (*e->hal.pin.quadrature_error_enable) { - if (!last_error_enable) { + e->reset_quadrature_error=0; + if(!e->prev_quadrature_error_enable){ // detect rising edge of pin quadrature_error_enable + e->reset_quadrature_error = 1; hm2_encoder_force_write(hm2); - last_error_enable = 1; - } else { - int state = (hm2->encoder.read_control_reg[i] & HM2_ENCODER_CONTROL_MASK) & HM2_ENCODER_QUADRATURE_ERROR; - if ((*e->hal.pin.quadrature_error == 0) && state) { - HM2_ERR("Encoder %d: quadrature count error", i); - } - *e->hal.pin.quadrature_error = (hal_bit_t) state; } - } else { + int state = (hm2->encoder.read_control_reg[i] & HM2_ENCODER_CONTROL_MASK) & HM2_ENCODER_QUADRATURE_ERROR; + if ((*e->hal.pin.quadrature_error == 0) && state) { + HM2_ERR("Encoder %d: quadrature count error\n", i); + } + *e->hal.pin.quadrature_error = (hal_bit_t) state; + } else{ // quadrature error disabled, set reported error to 0 *e->hal.pin.quadrature_error = 0; - last_error_enable = 0; } + e->prev_quadrature_error_enable = *e->hal.pin.quadrature_error_enable; *e->hal.pin.input_a = hm2->encoder.read_control_reg[i] & HM2_ENCODER_INPUT_A; *e->hal.pin.input_b = hm2->encoder.read_control_reg[i] & HM2_ENCODER_INPUT_B; diff --git a/src/hal/drivers/mesa-hostmot2/hostmot2.h b/src/hal/drivers/mesa-hostmot2/hostmot2.h index 62e7ad2..a8e111c 100755 --- a/src/hal/drivers/mesa-hostmot2/hostmot2.h +++ b/src/hal/drivers/mesa-hostmot2/hostmot2.h @@ -300,6 +300,10 @@ typedef struct { rtapi_u32 prev_control; + hal_bit_t prev_quadrature_error_enable; // shadow for detecting rising edge on the quadrature_error_enable + hal_bit_t reset_quadrature_error; // bit to indicate if we want to reset the quadrature error + + // these two are the datapoint last time we moved (only valid if state == HM2_ENCODER_MOVING) rtapi_s32 prev_event_rawcounts; rtapi_u16 prev_event_reg_timestamp; -- 1.7.10.4