Skip to content

Commit

Permalink
renamed motor task frequency #defines
Browse files Browse the repository at this point in the history
Use HSE_VALUE to calculate frequency
  • Loading branch information
dzid26 committed Jul 7, 2024
1 parent 5e29e1a commit c7ae390
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
19 changes: 9 additions & 10 deletions src/ebike_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -1457,13 +1457,12 @@ static void calc_wheel_speed(void)
// calc wheel speed (km/h x10)
if (ui16_wheel_speed_sensor_ticks > 0U) {
uint16_t ui16_tmp = ui16_wheel_speed_sensor_ticks;
// rps = PWM_CYCLES_SECOND / ui16_wheel_speed_sensor_ticks (rev/sec)
// rps = MOTOR_TASK_FREQ / ui16_wheel_speed_sensor_ticks (rev/sec)
// km/h*10 = rps * ui16_wheel_perimeter * ((3600 / (1000 * 1000)) * 10)
// !!!warning if PWM_CYCLES_SECOND is not a multiple of 1000
ui16_wheel_speed_x10 = (uint16_t)(((uint32_t) m_configuration_variables.ui16_wheel_perimeter * ((PWM_CYCLES_SECOND/1000)*36U)) / ui16_tmp);
}
else {
ui16_wheel_speed_x10 = 0;
// !!!warning if MOTOR_TASK_FREQ is not a multiple of 1000
ui16_wheel_speed_x10 = (uint16_t)(((uint32_t) m_configuration_variables.ui16_wheel_perimeter * ((MOTOR_TASK_FREQ/1000)*36U)) / ui16_tmp);
} else {
ui16_wheel_speed_x10 = 0;
}
}

Expand All @@ -1481,9 +1480,9 @@ static void calc_cadence(void)
CADENCE_SENSOR_TICKS_COUNTER_MIN_AT_SPEED);

// calculate cadence in RPM and avoid zero division
// !!!warning if PWM_CYCLES_SECOND > 21845
// !!!warning if MOTOR_TASK_FREQ > 21845
if (ui16_cadence_sensor_ticks_temp > 0U) {
ui8_pedal_cadence_RPM = (uint8_t)((PWM_CYCLES_SECOND * 3U) / ui16_cadence_sensor_ticks_temp);
ui8_pedal_cadence_RPM = (uint8_t)((MOTOR_TASK_FREQ * 3U) / ui16_cadence_sensor_ticks_temp);

if (ui8_pedal_cadence_RPM > 120) {
ui8_pedal_cadence_RPM = 120;
Expand All @@ -1501,9 +1500,9 @@ static void calc_cadence(void)
Formula for calculating the cadence in RPM:
(1) Cadence in RPM = (60 * PWM_CYCLES_SECOND) / CADENCE_SENSOR_NUMBER_MAGNETS) / ticks
(1) Cadence in RPM = (60 * MOTOR_TASK_FREQ) / CADENCE_SENSOR_NUMBER_MAGNETS) / ticks
(2) Cadence in RPM = (PWM_CYCLES_SECOND * 3) / ticks
(2) Cadence in RPM = (MOTOR_TASK_FREQ * 3) / ticks
-------------------------------------------------------------------------------------------------*/
}
Expand Down
36 changes: 18 additions & 18 deletions src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
#define OEM_WHEEL_SPEED_DIVISOR 363 // at 18 KHz
#endif

#define PWM_CYCLES_SECOND (16000000/(PWM_COUNTER_MAX*2)) // 55.5us (PWM period) 18 Khz
#define MOTOR_TASK_FREQ ((uint16_t)(HSE_VALUE / (PWM_COUNTER_MAX*2))) // 55.5us (PWM period) 18 Khz

/*---------------------------------------------------------
NOTE: regarding duty cycle (PWM) ramping
Expand All @@ -70,16 +70,16 @@
---------------------------------------------------------*/

// ramp up/down PWM cycles count
#define PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_DEFAULT (uint8_t)(PWM_CYCLES_SECOND/98)
#define PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_MIN (uint8_t)(PWM_CYCLES_SECOND/781)
#define PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_DEFAULT (uint8_t)(PWM_CYCLES_SECOND/260)
#define PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_MIN (uint8_t)(PWM_CYCLES_SECOND/1953)
#define CRUISE_DUTY_CYCLE_RAMP_UP_INVERSE_STEP (uint8_t)(PWM_CYCLES_SECOND/78)
#define WALK_ASSIST_DUTY_CYCLE_RAMP_UP_INVERSE_STEP (uint8_t)(PWM_CYCLES_SECOND/78)
#define THROTTLE_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_DEFAULT (uint8_t)(PWM_CYCLES_SECOND/78)
#define THROTTLE_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_MIN (uint8_t)(PWM_CYCLES_SECOND/390)

#define MOTOR_OVER_SPEED_ERPS ((PWM_CYCLES_SECOND/29) < 650 ? (PWM_CYCLES_SECOND/29) : 650) // motor max speed | 29 points for the sinewave at max speed (less than PWM_CYCLES_SECOND/29)
#define PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_DEFAULT (uint8_t)(MOTOR_TASK_FREQ/98)
#define PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_MIN (uint8_t)(MOTOR_TASK_FREQ/781)
#define PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_DEFAULT (uint8_t)(MOTOR_TASK_FREQ/260)
#define PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_MIN (uint8_t)(MOTOR_TASK_FREQ/1953)
#define CRUISE_DUTY_CYCLE_RAMP_UP_INVERSE_STEP (uint8_t)(MOTOR_TASK_FREQ/78)
#define WALK_ASSIST_DUTY_CYCLE_RAMP_UP_INVERSE_STEP (uint8_t)(MOTOR_TASK_FREQ/78)
#define THROTTLE_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_DEFAULT (uint8_t)(MOTOR_TASK_FREQ/78)
#define THROTTLE_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_MIN (uint8_t)(MOTOR_TASK_FREQ/390)

#define MOTOR_OVER_SPEED_ERPS ((MOTOR_TASK_FREQ/29) < 650 ? (MOTOR_TASK_FREQ/29) : 650) // motor max speed | 29 points for the sinewave at max speed (less than MOTOR_TASK_FREQ/29)
#define MOTOR_SPEED_FIELD_WEAKENING_MIN 490

// foc angle multiplier
Expand All @@ -92,14 +92,14 @@
#endif

// cadence
#define CADENCE_SENSOR_CALC_COUNTER_MIN (uint16_t)((uint32_t)PWM_CYCLES_SECOND*100U/446U) // 3500 at 15.625KHz
#define CADENCE_SENSOR_TICKS_COUNTER_MIN_AT_SPEED (uint16_t)((uint32_t)PWM_CYCLES_SECOND*10U/558U) // 280 at 15.625KHz
#define CADENCE_TICKS_STARTUP (uint16_t)((uint32_t)PWM_CYCLES_SECOND*10U/25U) // ui16_cadence_sensor_ticks value for startup. About 7-8 RPM (6250 at 15.625KHz)
#define CADENCE_SENSOR_STANDARD_MODE_SCHMITT_TRIGGER_THRESHOLD (uint16_t)((uint32_t)PWM_CYCLES_SECOND*10U/446U) // software based Schmitt trigger to stop motor jitter when at resolution limits (350 at 15.625KHz)
#define CADENCE_SENSOR_CALC_COUNTER_MIN (uint16_t)((uint32_t)MOTOR_TASK_FREQ*100U/446U) // 3500 at 15.625KHz
#define CADENCE_SENSOR_TICKS_COUNTER_MIN_AT_SPEED (uint16_t)((uint32_t)MOTOR_TASK_FREQ*10U/558U) // 280 at 15.625KHz
#define CADENCE_TICKS_STARTUP (uint16_t)((uint32_t)MOTOR_TASK_FREQ*10U/25U) // ui16_cadence_sensor_ticks value for startup. About 7-8 RPM (6250 at 15.625KHz)
#define CADENCE_SENSOR_STANDARD_MODE_SCHMITT_TRIGGER_THRESHOLD (uint16_t)((uint32_t)MOTOR_TASK_FREQ*10U/446U) // software based Schmitt trigger to stop motor jitter when at resolution limits (350 at 15.625KHz)

// Wheel speed sensor
#define WHEEL_SPEED_SENSOR_TICKS_COUNTER_MAX (uint16_t)((uint32_t)PWM_CYCLES_SECOND*10U/1157U) // (135 at 15,625KHz) something like 200 m/h with a 6'' wheel
#define WHEEL_SPEED_SENSOR_TICKS_COUNTER_MIN (uint16_t)((uint32_t)PWM_CYCLES_SECOND*1000U/477U) // 32767@15625KHz could be a bigger number but will make for a slow detection of stopped wheel speed
#define WHEEL_SPEED_SENSOR_TICKS_COUNTER_MAX (uint16_t)((uint32_t)MOTOR_TASK_FREQ*10U/1157U) // (135 at 15,625KHz) something like 200 m/h with a 6'' wheel
#define WHEEL_SPEED_SENSOR_TICKS_COUNTER_MIN (uint16_t)((uint32_t)MOTOR_TASK_FREQ*1000U/477U) // 32767@15625KHz could be a bigger number but will make for a slow detection of stopped wheel speed

#define PWM_DUTY_CYCLE_MAX UINT8_MAX
#define PWM_DUTY_CYCLE_STARTUP 30 // Initial PWM Duty Cycle at motor startup
Expand Down Expand Up @@ -131,7 +131,7 @@ HALL_COUNTER_OFFSET_UP: 29 -> 44
****************************************
*/

#define HALL_COUNTER_OFFSET_DOWN (HALL_COUNTER_FREQ/PWM_CYCLES_SECOND/2 + 17)
#define HALL_COUNTER_OFFSET_DOWN (HALL_COUNTER_FREQ/MOTOR_TASK_FREQ/2 + 17)
#define HALL_COUNTER_OFFSET_UP (HALL_COUNTER_OFFSET_DOWN + 21)
#define FW_HALL_COUNTER_OFFSET_MAX 5 // 5*4=20us max time offset

Expand Down

0 comments on commit c7ae390

Please sign in to comment.