Skip to content

Commit

Permalink
Modified the torque control mode to a kind of motor current and batte…
Browse files Browse the repository at this point in the history
…ry current mix, which i call watt control mode. please have a look and give it a try.
  • Loading branch information
Nico Ackermann authored and Nico Ackermann committed Sep 4, 2019
1 parent 4e4ece3 commit 02f58d0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/controller/ebike_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ static uint16_t ui16_battery_voltage_filtered_x1000 = 0;
static uint8_t ui8_battery_current_filtered_x10 = 0;
static uint8_t ui8_adc_battery_current_max = ADC_10_BIT_BATTERY_CURRENT_MAX;
static uint8_t ui8_adc_battery_current_target = 0;
static uint8_t ui8_adc_motor_current_target = 0;
static uint8_t ui8_duty_cycle_target = 0;


Expand Down Expand Up @@ -203,6 +204,7 @@ static void ebike_control_motor (void)
ui16_duty_cycle_ramp_up_inverse_step = PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_DEFAULT;
ui16_duty_cycle_ramp_down_inverse_step = PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_DEFAULT;
ui8_adc_battery_current_target = 0;
ui8_adc_motor_current_target = ADC_10_BIT_MOTOR_PHASE_CURRENT_MAX;
ui8_duty_cycle_target = 0;

// reset initialization of Cruise PID controller
Expand Down Expand Up @@ -264,15 +266,19 @@ static void ebike_control_motor (void)
ui16_controller_duty_cycle_ramp_up_inverse_step = PWM_DUTY_CYCLE_RAMP_UP_INVERSE_STEP_DEFAULT;
ui16_controller_duty_cycle_ramp_down_inverse_step = PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_MIN;
ui8_controller_adc_battery_current_target = 0;
ui8_controller_adc_motor_current_target = 0;
ui8_controller_duty_cycle_target = 0;
}
else
{
// limit max current if higher than configured hardware limit (safety)
if (ui8_adc_battery_current_max > ADC_10_BIT_BATTERY_CURRENT_MAX) { ui8_adc_battery_current_max = ADC_10_BIT_BATTERY_CURRENT_MAX; }

// limit target current if higher than max value (safety)
// limit battery target current if higher than max value (safety)
if (ui8_adc_battery_current_target > ui8_adc_battery_current_max) { ui8_adc_battery_current_target = ui8_adc_battery_current_max; }

// limit motor target current if higher than max value (safety)
if (ui8_adc_motor_current_target > ADC_10_BIT_MOTOR_PHASE_CURRENT_MAX) { ui8_adc_motor_current_target = ADC_10_BIT_MOTOR_PHASE_CURRENT_MAX; }

// limit target duty cycle if higher than max value
if (ui8_duty_cycle_target > PWM_DUTY_CYCLE_MAX) { ui8_duty_cycle_target = PWM_DUTY_CYCLE_MAX; }
Expand All @@ -292,6 +298,8 @@ static void ebike_control_motor (void)
// set target battery current in controller
ui8_controller_adc_battery_current_target = ui8_adc_battery_current_target;

ui8_controller_adc_motor_current_target = ui8_adc_motor_current_target;

// set target duty cycle in controller
ui8_controller_duty_cycle_target = ui8_duty_cycle_target;
}
Expand Down Expand Up @@ -357,7 +365,8 @@ static void apply_power_assist()

static void apply_torque_assist()
{
#define TORQUE_ASSIST_FACTOR_DENOMINATOR 110 // scale the torque assist target current
//#define TORQUE_ASSIST_FACTOR_DENOMINATOR 110 // scale the torque assist target current
#define TORQUE_ASSIST_FACTOR_DENOMINATOR 66 // scale the torque assist target current (110 * (90/150) 90 0 max battery current adc and 150 = max motor current adc)

// check for assist without pedal rotation threshold when there is no pedal rotation and standing still
if (ui8_assist_without_pedal_rotation_threshold && !ui8_pedal_cadence_RPM && !ui16_wheel_speed_x10)
Expand All @@ -372,8 +381,9 @@ static void apply_torque_assist()
uint8_t ui8_torque_assist_factor = ui8_riding_mode_parameter;

// calculate torque assist target current
uint16_t ui16_adc_battery_current_target_torque_assist = ((uint16_t) ui16_adc_pedal_torque_delta * ui8_torque_assist_factor) / TORQUE_ASSIST_FACTOR_DENOMINATOR;

//uint16_t ui16_adc_battery_current_target_torque_assist = ((uint16_t) ui16_adc_pedal_torque_delta * ui8_torque_assist_factor) / TORQUE_ASSIST_FACTOR_DENOMINATOR;
uint16_t ui16_adc_motor_current_target_torque_assist = ((uint16_t) ui16_adc_pedal_torque_delta * ui8_torque_assist_factor) / TORQUE_ASSIST_FACTOR_DENOMINATOR;

// set motor acceleration
ui16_duty_cycle_ramp_up_inverse_step = map((uint32_t) ui16_wheel_speed_x10,
(uint32_t) 40, // 40 -> 4 kph
Expand All @@ -388,11 +398,18 @@ static void apply_torque_assist()
(uint32_t) PWM_DUTY_CYCLE_RAMP_DOWN_INVERSE_STEP_MIN);

// set battery current target
if (ui16_adc_battery_current_target_torque_assist > ui8_adc_battery_current_max) { ui8_adc_battery_current_target = ui8_adc_battery_current_max; }
else { ui8_adc_battery_current_target = ui16_adc_battery_current_target_torque_assist; }
if (ui16_adc_motor_current_target_torque_assist > ADC_10_BIT_MOTOR_PHASE_CURRENT_MAX) { ui8_adc_motor_current_target = ADC_10_BIT_MOTOR_PHASE_CURRENT_MAX; }
else { ui8_adc_motor_current_target = ui16_adc_motor_current_target_torque_assist; }

// set max battery current relative to the motor max current, this way it is a smooth transition from motor current control to battery current control
ui8_adc_battery_current_target = fmapf((uint32_t) ui8_adc_motor_current_target,
(uint32_t) 0,
(uint32_t) ADC_10_BIT_MOTOR_PHASE_CURRENT_MAX,
(uint32_t) 0,
(uint32_t) ui8_adc_battery_current_max);

// set duty cycle target
if (ui8_adc_battery_current_target) { ui8_duty_cycle_target = PWM_DUTY_CYCLE_MAX; }
if (ui8_adc_motor_current_target && ui8_adc_battery_current_target) { ui8_duty_cycle_target = PWM_DUTY_CYCLE_MAX; }
else { ui8_duty_cycle_target = 0; }
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/controller/motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ volatile uint16_t ui16_adc_battery_current = 0;
volatile uint8_t ui8_adc_battery_current_filtered = 0;
volatile uint8_t ui8_controller_adc_battery_current = 0;
volatile uint8_t ui8_controller_adc_battery_current_target = 0;
volatile uint8_t ui8_controller_adc_motor_current_target = 0;
volatile uint8_t ui8_g_duty_cycle = 0;
volatile uint8_t ui8_controller_duty_cycle_target = 0;
volatile uint8_t ui8_g_foc_angle = 0;
Expand Down Expand Up @@ -633,6 +634,7 @@ void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER)
if ((ui8_g_duty_cycle > ui8_controller_duty_cycle_target) ||
(ui8_controller_adc_battery_current > ui8_controller_adc_battery_current_target) ||
(ui8_adc_motor_phase_current > ADC_10_BIT_MOTOR_PHASE_CURRENT_MAX) ||
(ui8_adc_motor_phase_current > ui8_controller_adc_motor_current_target) ||
(ui16_motor_speed_erps > ui16_max_motor_speed_erps) ||
(UI8_ADC_BATTERY_VOLTAGE < ui8_adc_battery_voltage_cut_off) ||
(ui8_brake_state))
Expand Down
1 change: 1 addition & 0 deletions src/controller/motor.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern volatile uint16_t ui16_adc_battery_voltage_filtered;
extern volatile uint8_t ui8_adc_battery_voltage_cut_off;
extern volatile uint8_t ui8_adc_battery_current_filtered;
extern volatile uint8_t ui8_controller_adc_battery_current_target;
extern volatile uint8_t ui8_controller_adc_motor_current_target;
extern volatile uint8_t ui8_g_duty_cycle;
extern volatile uint8_t ui8_controller_duty_cycle_target;
extern volatile uint8_t ui8_g_foc_angle;
Expand Down

0 comments on commit 02f58d0

Please sign in to comment.