From e49106590a8b346fa4df19a55fa242a9075aa7b4 Mon Sep 17 00:00:00 2001 From: NicobobinusX <71988810+NicobobinusX@users.noreply.github.com> Date: Sun, 27 Sep 2020 14:02:56 -0700 Subject: [PATCH] Update motor.c My attempt to apply the fix suggested by djwlindenaar in "https://github.com/OpenSource-EBike-firmware/TSDZ2-Smart-EBike/pull/128" to the current V1.0 code. Seems to run smoothly and performance appears equivalent whether lights are on or off. Other past issues where similar issues are referenced: 1. https://github.com/OpenSource-EBike-firmware/TSDZ2-Smart-EBike/issues/71 2. https://github.com/OpenSource-EBike-firmware/TSDZ2-Smart-EBike/issues/94 3. https://github.com/OpenSource-EBike-firmware/Color_LCD/issues/104 --- src/motor.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/motor.c b/src/motor.c index 4cf19cb2..c350feb0 100755 --- a/src/motor.c +++ b/src/motor.c @@ -478,13 +478,23 @@ void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER) while (!(ADC1->CSR & ADC1_FLAG_EOC)) ; ui16_g_adc_battery_current = UI16_ADC_10_BIT_BATTERY_CURRENT; + // we ignore low values of the battery current < 5 to avoid issues with other consumers than the motor + // Piecewise linear is better than a step, to avoid limit cycles. + // in --> out + // 0 - 5 --> 0 - 0 + // 5 - 15 --> 0 - 15 + if (ui16_g_adc_battery_current <= 5) + ui16_g_adc_battery_current = 0; + else if (ui16_g_adc_battery_current <= 15) + ui16_g_adc_battery_current = (ui16_g_adc_battery_current + ui16_g_adc_battery_current >> 1) - 5; + // this shoud work but does not....... // ui16_g_adc_battery_current = (((uint16_t) ADC1->DRH) << 8) | ((uint16_t) ADC1->DRL); // calculate motor current ADC value if (ui8_g_duty_cycle > 0) { - ui16_g_adc_motor_current = ((ui16_g_adc_battery_current << 8) / ((uint16_t) ui8_g_duty_cycle)); + ui16_g_adc_motor_current = ((ui16_g_adc_battery_current << 6) / ((uint16_t) ui8_g_duty_cycle)); } else {