Skip to content

Commit

Permalink
Simpler cadence stop detection
Browse files Browse the repository at this point in the history
  • Loading branch information
dzid26 committed Apr 15, 2024
1 parent 24307dd commit da1b2f0
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions src/controller/motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,6 @@ volatile uint16_t ui16_cadence_sensor_ticks = CADENCE_TICKS_STOP;
static uint16_t ui16_cadence_sensor_ticks_counter_min = CADENCE_SENSOR_CALC_COUNTER_MIN;
static uint8_t ui8_pas_state_old = 4;
static uint16_t ui16_cadence_calc_counter = CADENCE_COUNTER_MAX;
static uint16_t ui16_cadence_stop_counter = 0;
static uint8_t ui8_cadence_calc_ref_state = NO_PAS_REF;
const static uint8_t ui8_pas_old_valid_state[4] = { 0x01, 0x03, 0x00, 0x02 };

Expand Down Expand Up @@ -846,7 +845,7 @@ void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER)
* ui8_pas_state stores the PAS1 and PAS2 state: bit0=PAS1, bit1=PAS2
* Pedal forward ui8_pas_state sequence is: 0x01 -> 0x00 -> 0x02 -> 0x03 -> 0x01
* After a stop, the first forward transition is taken as reference transition
* Following forward transition sets the cadence to 7RPM for immediate startup
* Following forward transition sets the cadence to 1RPM for immediate startup
* Then, starting form the second reference transition, the cadence is calculated based on counter value
* All transitions resets the stop detection counter (much faster stop detection):
*/
Expand All @@ -863,7 +862,7 @@ void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER)

// motor fast stop
if(ui8_pedal_cadence_fast_stop && (ui16_cadence_sensor_ticks != CADENCE_TICKS_STOP)) {
ui16_cadence_sensor_ticks_counter_min = ui16_cadence_sensor_ticks - (CADENCE_SENSOR_STANDARD_MODE_SCHMITT_TRIGGER_THRESHOLD >> 1);
ui16_cadence_sensor_ticks_counter_min = ui16_cadence_sensor_ticks;
}
else {
ui16_cadence_sensor_ticks_counter_min = ui16_cadence_ticks_count_min_speed_adj;
Expand All @@ -873,8 +872,6 @@ void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER)
// ui16_cadence_calc_counter is valid for cadence calculation
ui16_cadence_sensor_ticks = ui16_cadence_calc_counter;
ui16_cadence_calc_counter = CADENCE_COUNTER_RESET;
// software based Schmitt trigger to stop motor jitter when at resolution limits
ui16_cadence_sensor_ticks_counter_min += CADENCE_SENSOR_STANDARD_MODE_SCHMITT_TRIGGER_THRESHOLD;
} else if (ui8_cadence_calc_ref_state == NO_PAS_REF) {
// this is the new reference state for cadence calculation
ui8_cadence_calc_ref_state = ui8_pas_state;
Expand All @@ -887,24 +884,20 @@ void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER)
}

skip_cadence:
// reset the counter used to detect pedal stop
ui16_cadence_stop_counter = 0;
// save current PAS state
ui8_pas_state_old = ui8_pas_state;
}
if (ui16_cadence_calc_counter > ui16_cadence_sensor_ticks) {
// next pulse later than previous - start decaying the speed
ui16_cadence_sensor_ticks = ui16_cadence_calc_counter;
// next pulse later than previous - start decaying the speed
ui16_cadence_sensor_ticks = ui16_cadence_calc_counter;
}

if (++ui16_cadence_stop_counter > ui16_cadence_sensor_ticks_counter_min) {
// pedals stop detected
ui16_cadence_sensor_ticks = CADENCE_TICKS_STOP;
ui16_cadence_stop_counter = 0;
ui8_cadence_calc_ref_state = NO_PAS_REF;
} else if ((ui8_cadence_calc_ref_state != NO_PAS_REF) && (ui16_cadence_calc_counter < CADENCE_COUNTER_MAX)) {
if (ui16_cadence_calc_counter < CADENCE_COUNTER_MAX) {
// increment cadence tick counter
++ui16_cadence_calc_counter;
} else { //detect stop
ui8_cadence_calc_ref_state = NO_PAS_REF;
ui16_cadence_sensor_ticks = CADENCE_TICKS_STOP;
}

/****************************************************************************/
Expand Down

0 comments on commit da1b2f0

Please sign in to comment.