Skip to content

Commit

Permalink
Added motor current control as torque mode as an option while keeping…
Browse files Browse the repository at this point in the history
… also as an option the power mode. Changed walk assist max speed to 6 km/h while previously was 8 km/h
  • Loading branch information
casainho committed May 12, 2020
1 parent 057b457 commit 4398bc5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
24 changes: 20 additions & 4 deletions src/ebike_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ uint8_t ui8_pas_cadence_rpm = 0;
uint16_t ui16_m_pedal_torque_x10;
uint16_t ui16_m_pedal_torque_x100;
uint16_t ui16_m_pedal_power_x10;
uint16_t ui16_m_pedal_power_fixed_cadence_x10;
uint16_t ui16_m_pedal_power_max_x10;
uint8_t ui8_m_pedal_human_power = 0;
uint8_t ui8_pas_pedal_position_right = 0;
Expand Down Expand Up @@ -209,7 +210,6 @@ static void ebike_control_motor(void)
uint32_t ui32_temp = 0;
uint32_t ui32_pedal_power_no_cadence_x10 = 0;
uint32_t ui32_assist_level_factor_x1000;
uint32_t ui32_current_amps_x10 = 0;
uint8_t ui8_tmp_pas_cadence_rpm;
uint16_t ui16_adc_current;
uint16_t ui16_adc_max_battery_power_current = 0;
Expand All @@ -218,6 +218,10 @@ static void ebike_control_motor(void)
uint16_t ui16_battery_voltage_filtered = calc_filtered_battery_voltage();
uint16_t ui16_adc_battery_current_max = 0;
uint8_t ui8_assist_enable = 0;
uint32_t ui32_current_amps_x10_final;
uint32_t ui32_current_amps_x10;
uint32_t ui32_current_amps_fixed_cadence_x10;
uint32_t ui32_current_amps_no_cadence_x10;

// the ui8_m_brake_is_set is updated here only and used all over ebike_control_motor()
ui8_g_brake_is_set = ui8_g_brakes_state;
Expand All @@ -236,19 +240,29 @@ static void ebike_control_motor(void)
// force a min of 10 RPM cadence
ui32_pedal_power_no_cadence_x10 = (((uint32_t) ui16_m_pedal_torque_x100 * 10) / (uint32_t) 96);

ui32_current_amps_x10_final = 0;
ui32_current_amps_x10 = ((uint32_t) ui16_m_pedal_power_x10 * ui32_assist_level_factor_x1000) / 1000;
ui32_current_amps_fixed_cadence_x10 = ((uint32_t) ui16_m_pedal_power_fixed_cadence_x10 * ui32_assist_level_factor_x1000) / 1000;
ui32_current_amps_no_cadence_x10 = (ui32_pedal_power_no_cadence_x10 * ui32_assist_level_factor_x1000) / 1000;
if (m_config_vars.ui8_motor_assistance_startup_without_pedal_rotation == 0 ||
ui8_pas_cadence_rpm)
{
ui32_current_amps_x10 = ((uint32_t) ui16_m_pedal_power_x10 * ui32_assist_level_factor_x1000) / 1000;
if (m_config_vars.ui8_motor_current_control_mode == 0)
ui32_current_amps_x10_final = ui32_current_amps_x10;
else
ui32_current_amps_x10_final = ui32_current_amps_fixed_cadence_x10;
}
else
{
ui32_current_amps_x10 = (ui32_pedal_power_no_cadence_x10 * ui32_assist_level_factor_x1000) / 1000;
if (m_config_vars.ui8_motor_current_control_mode == 0)
ui32_current_amps_x10_final = ui32_current_amps_no_cadence_x10;
else
ui32_current_amps_x10_final = ui32_current_amps_fixed_cadence_x10;
}

// 6.410 = 1 / 0.156 (each ADC step for current)
// 6.410 * 8 = ~51
ui16_adc_current = (uint16_t) ((ui32_current_amps_x10 * 51) / 80);
ui16_adc_current = (uint16_t) ((ui32_current_amps_x10_final * 51) / 80);
ui16_limit_max(&ui16_adc_current, ui16_m_adc_motor_current_max);

// if user is rotating the pedals, force use the min current value
Expand Down Expand Up @@ -747,6 +761,7 @@ static void communications_process_packages(uint8_t ui8_frame_type)
ui8_g_pedal_cadence_fast_stop = ui8_temp & 1;
ui8_g_field_weakening_enable = (ui8_temp & 2) >> 1;
ui8_g_coast_brake_enable = (ui8_temp & 4) >> 2;
m_config_vars.ui8_motor_current_control_mode = (ui8_temp & 8) >> 3;

// coast brake threshold
ui16_temp = (uint16_t) ui8_rx_buffer[81];
Expand Down Expand Up @@ -1007,6 +1022,7 @@ static void calc_pedal_force_and_torque(void)

ui16_m_pedal_torque_x10 = ui16_m_pedal_torque_x100 / 10;
ui16_m_pedal_power_x10 = (uint16_t) (((uint32_t) ui16_m_pedal_torque_x100 * (uint32_t) ui8_pas_cadence_rpm) / (uint32_t) 96);
ui16_m_pedal_power_fixed_cadence_x10 = (uint16_t) (((uint32_t) ui16_m_pedal_torque_x100 * 70) / (uint32_t) 96);
ui16_m_pedal_power_max_x10 = ui16_m_pedal_power_x10;
}

Expand Down
1 change: 1 addition & 0 deletions src/ebike_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ typedef struct
uint8_t ui8_wheel_max_speed;
uint8_t ui8_motor_type;
uint8_t ui8_motor_assistance_startup_without_pedal_rotation;
uint8_t ui8_motor_current_control_mode;
uint8_t ui8_target_battery_max_power_div25;
uint8_t configuration_variables;
uint8_t ui8_startup_motor_power_boost_feature_enabled;
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@


// walk assist and cruise
#define WALK_ASSIST_CRUISE_THRESHOLD_SPEED_X10 80 // 8.0 km/h
#define WALK_ASSIST_CRUISE_THRESHOLD_SPEED_X10 60 // 6.0 km/h
#define CRUISE_PID_KP 7 // 48 volt motor: 6, 36 volt motor: 7
#define CRUISE_PID_KI 0.35 // 48 volt motor: 0.5, 36 volt motor: 0.35
#define CRUISE_PID_INTEGRAL_LIMIT 1000
Expand Down

0 comments on commit 4398bc5

Please sign in to comment.