Casainho, here is the description and the source code, I hope it is also useful for the main project
I hope that after the automatic translation it is still understandable.
The code to calculate and activate the PWM deceleration ramp is in the ebike_app.c file, this introduces a significant delay when you stop pedaling.
I added other code for this function in motor.c, (PWM frequency).
I tried to do it in a simple way by minimizing the lines of code.
The added code works in parallel with the existing one that has not been deleted.
added code:
![TSDZ2-20beta1-fix_overrun-1.jpg TSDZ2-20beta1-fix_overrun-1.jpg](https://endless-sphere.com/sphere/data/attachments/145/145233-28fccadb43553a99c87cae252d88894e.jpg)
variable declaration:
"ui8_cadence_sensor_stop_flag" state of the pedals stopped.
"ui16_cadence_sensor_ticks_stop" number of pulses of the tick counter beyond which the pedals are considered stationary.
![TSDZ2-20beta1-fix_overrun-2.jpg TSDZ2-20beta1-fix_overrun-2.jpg](https://endless-sphere.com/sphere/data/attachments/145/145234-a5ae9c57ba7e5851a90c76107ed54389.jpg)
Added direct activation of the PWM deceleration ramp with "ui8_cadence_sensor_stop_flag", previously activated in ebike_app.c due to the absence of assistance.
With the pedals stopped "ui8_cadence_sensor_stop_flag", direct assignment ui16_controller_duty_cycle_ramp_down_inverse_step = PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_MIN. Previously the value was calculated as a function of the speed in ebike.c.
The PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_MIN value was used by the brake sensors but always in ebike_app.c. For this I also added "OR ui8_brake_state".
The PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_MIN value in main.h has not changed.
![TSDZ2-20beta1-fix_overrun-4.jpg TSDZ2-20beta1-fix_overrun-4.jpg](https://endless-sphere.com/sphere/data/attachments/145/145236-10c3c2858f119ab4d2ed19cc14549825.jpg)
Calculation of the value of "ui16_cadence_sensor_ticks_stop", in standard and advanced cadence sensor mode. They are different values.
The optimal values were obtained experimentally.
Increasing them increases the overrun time, decreasing them lacks assistance at the start.
![TSDZ2-20beta1-fix_overrun-5.jpg TSDZ2-20beta1-fix_overrun-5.jpg](https://endless-sphere.com/sphere/data/attachments/145/145237-b3088ef0ccb81a2c85a20b54c9f038f0.jpg)
Check the state of the pedals.
When "ui16_cadence_sensor_ticks" has a valid value and "ui16_cadence_sensor_ticks_counter" exceeds the value of "ui16_cadence_sensor_ticks_stop", the pedals are considered stationary.
View attachment motor.c
motor.c source code TSDZ2 20 beta 1 version only for LCD3, with overrun modification.