Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions src/firmware/app.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ static uint16_t lvc_voltage_x100;
static uint16_t lvc_ramp_down_start_voltage_x100;
static uint16_t lvc_ramp_down_end_voltage_x100;

static uint16_t full_voltage_range_x100;
static uint16_t padded_voltage_range_x100;
static uint16_t low_voltage_pad_x100;
static uint16_t high_voltage_pad_x100;

static assist_level_data_t assist_level_data;
static uint16_t speed_limit_ramp_interval_rpm_x10;

Expand Down Expand Up @@ -90,15 +95,17 @@ void app_init()

lvc_voltage_x100 = g_config.low_cut_off_v * 100u;

uint16_t full_voltage_range_x100 =
EXPAND_U16(g_config.max_battery_x100v_u16h, g_config.max_battery_x100v_u16l) - lvc_voltage_x100;
uint16_t padded_voltage_range_x100 = (uint16_t)(full_voltage_range_x100 *
(100 - BATTERY_FULL_OFFSET_PERCENT - BATTERY_EMPTY_OFFSET_PERCENT) / 100);
full_voltage_range_x100 = EXPAND_U16(g_config.max_battery_x100v_u16h, g_config.max_battery_x100v_u16l) - lvc_voltage_x100;
low_voltage_pad_x100 = full_voltage_range_x100 * BATTERY_EMPTY_OFFSET_PERCENT / 100;
high_voltage_pad_x100 = full_voltage_range_x100 * BATTERY_FULL_OFFSET_PERCENT / 100;
padded_voltage_range_x100 = full_voltage_range_x100 - low_voltage_pad_x100 - high_voltage_pad_x100;

// The LVC ramp down end is at 0% battery, which is LVC + the low padding value.
lvc_ramp_down_end_voltage_x100 = lvc_voltage_x100 + low_voltage_pad_x100;

lvc_ramp_down_end_voltage_x100 = (uint16_t)(lvc_voltage_x100 +
(full_voltage_range_x100 * BATTERY_EMPTY_OFFSET_PERCENT / 100));
lvc_ramp_down_start_voltage_x100 = (uint16_t)(lvc_ramp_down_end_voltage_x100 +
((padded_voltage_range_x100 * LVC_RAMP_DOWN_OFFSET_PERCENT) / 100));
// The LVC ramp down starts at LVC_RAMP_DOWN_OFFSET_PERCENT battery, using the padded range.
uint16_t lvc_ramp_down_offset_x100 = padded_voltage_range_x100 * LVC_RAMP_DOWN_OFFSET_PERCENT / 100;
lvc_ramp_down_start_voltage_x100 = lvc_ramp_down_end_voltage_x100 + lvc_ramp_down_offset_x100;

global_speed_limit_rpm = 0;
global_throttle_speed_limit_rpm_x10 = 0;
Expand Down Expand Up @@ -724,17 +731,18 @@ bool apply_low_voltage_limit(uint8_t* target_current)
}

// Ramp down power until LVC_LOW_CURRENT_PERCENT when approaching LVC
uint8_t tmp = (uint8_t)MAP32(
int8_t tmp = (int8_t)MAP32(
voltage_x100, // value
lvc_ramp_down_end_voltage_x100, // in_min
lvc_ramp_down_start_voltage_x100, // in_max
LVC_LOW_CURRENT_PERCENT, // out_min
100 // out_max
);
uint8_t tmp2 = (uint8_t)CLAMP(tmp, 0, 100);

if (*target_current > tmp)
if (*target_current > tmp2)
{
*target_current = tmp;
*target_current = tmp2;
return true;
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/firmware/battery.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,11 @@ void battery_init()
EXPAND_U16(g_config.max_battery_x100v_u16h, g_config.max_battery_x100v_u16l);

uint16_t battery_range_x100v = battery_max_voltage_x100v - battery_min_voltage_x100v;

battery_full_x100v = battery_max_voltage_x100v -
((BATTERY_FULL_OFFSET_PERCENT * battery_range_x100v) / 100);

battery_empty_x100v = battery_min_voltage_x100v +
((BATTERY_EMPTY_OFFSET_PERCENT * battery_range_x100v) / 100);
uint16_t battery_full_pad_x100v = battery_range_x100v * BATTERY_FULL_OFFSET_PERCENT / 100;
uint16_t battery_empty_pad_x100v = battery_range_x100v * BATTERY_EMPTY_OFFSET_PERCENT / 100;

battery_full_x100v = battery_max_voltage_x100v - battery_full_pad_x100v;
battery_empty_x100v = battery_min_voltage_x100v + battery_empty_pad_x100v;
}

void battery_process()
Expand Down