diff --git a/src/controller/ebike_app.c b/src/controller/ebike_app.c index 7a048b06..afed4f55 100755 --- a/src/controller/ebike_app.c +++ b/src/controller/ebike_app.c @@ -766,7 +766,7 @@ static void ebike_control_motor(void) //voltage based control assumes the winding resistance is constant for simplicity uint32_t smooth_start_voltage_target_x100 = (uint32_t)(uint16_t)(smooth_start_voltage_limit_x100 + smooth_start_voltage_limit_blend_x100); - smooth_start_voltage_target_x100 += (uint32_t)(uint16_t)(ui16_motor_bemf_voltage_x1000 / (10U + SMOOTH_START_BEMF_REDUCE_FACTOR)); + smooth_start_voltage_target_x100 += (uint32_t)(uint16_t)(ui16_motor_bemf_voltage_x1000 / (10U + SMOOTH_START_BEMF_REDUCE_FACTOR));//due to resolution of the pwm duty cycle, BEMF starts to increase the duty only above about 3erps if (smooth_start_voltage_target_x100 > UINT16_MAX){ smooth_start_voltage_target_x100 = UINT16_MAX; } diff --git a/src/controller/main.h b/src/controller/main.h index e2593c2a..2ef8d2db 100644 --- a/src/controller/main.h +++ b/src/controller/main.h @@ -16,9 +16,11 @@ // PWM related values // motor -#define PWM_CYCLES_SECOND 19047U // 52us (PWM period) -#define PWM_CYCLES_COUNTER_MAX 3800U // 5 erps minimum speed -> 1/5 = 200 ms; 200 ms / 50 us = 4000 (3125 at 15.625KHz) -#define DOUBLE_PWM_CYCLES_SECOND 38094 // 25us (2 irq x PWM period) +#define PWM_PERIOD 420U +#define PWM_DOUBLE_PERIOD (2U*PWM_PERIOD) //PWM center aligned mode: counts from 0 to PWM_PERIOD and then down from PWM_PERIOD to 0 +#define PWM_CYCLES_SECOND ((uint16_t)(HSE_VALUE / PWM_DOUBLE_PERIOD)) // 19047Hz - 52us (PWM period) - !! has to be less than 21845 +#define DOUBLE_PWM_CYCLES_SECOND ((uint16_t)(HSE_VALUE / PWM_PERIOD)) // 25us (2 irq x PWM period) +#define PWM_CYCLES_COUNTER_MAX (DOUBLE_PWM_CYCLES_SECOND + 1U) // +1U ensures ui16_motor_speed_erps is 0 when counter is max // ramp up/down PWM cycles count #define PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_CADENCE_OFFSET 60 // PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP offset for cadence assist mode //#define PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_DEFAULT 195 // 160 -> 160 * 64 us for every duty cycle increment at 15.625KHz diff --git a/src/controller/motor.c b/src/controller/motor.c index b05e28b7..b66cb066 100755 --- a/src/controller/motor.c +++ b/src/controller/motor.c @@ -294,7 +294,7 @@ uint16_t ui16_PWM_cycles_counter_a = 3; uint8_t ui8_pwm_counter_valid_b = 0; uint16_t ui16_PWM_cycles_counter_b = 3; uint16_t ui16_PWM_cycles_counter_6 = 3; -uint16_t ui16_PWM_cycles_counter_total = 0xffff; +uint16_t ui16_PWM_cycles_counter_total = PWM_CYCLES_COUNTER_MAX; uint8_t ui8_motor_commutation_type = BLOCK_COMMUTATION; volatile uint16_t ui16_motor_speed_erps = 0; static uint8_t ui8_motor_rotor_absolute_angle; @@ -539,7 +539,7 @@ void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER) ui16_PWM_cycles_counter_6 = 2; ui8_pwm_counter_valid_a = 0; ui8_pwm_counter_valid_b = 0; - ui16_PWM_cycles_counter_total = 0xffff; + ui16_PWM_cycles_counter_total = PWM_CYCLES_COUNTER_MAX; ui8_g_foc_angle = 0; ui8_motor_commutation_type = BLOCK_COMMUTATION; ui8_hall_sensors_state_last = 0; // this way we force execution of hall sensors code next time diff --git a/src/controller/pwm.c b/src/controller/pwm.c index 8b38e965..58c58fbc 100644 --- a/src/controller/pwm.c +++ b/src/controller/pwm.c @@ -39,7 +39,7 @@ void pwm_init_bipolar_4q(void) { TIM1_TimeBaseInit(0, // TIM1_Prescaler = 0 TIM1_COUNTERMODE_CENTERALIGNED3, // clock = 16MHz; counter period = 840; PWM freq = 16MHz / 840 = 19,047kHz; - 420, // PWM center aligned mode: counts from 0 to 420 and then down from 420 to 0 + PWM_PERIOD, // PWM center aligned mode: counts from 0 to PWM_PERIOD and then down from PWM_PERIOD to 0 1);// will fire the TIM1_IT_UPDATE at every PWM period cycle //#define DISABLE_PWM_CHANNELS_1_3