Skip to content

Commit

Permalink
Merge branch 'master' into optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
dzid26 authored Aug 9, 2024
2 parents d8ad996 + cb44428 commit 9db5159
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.0",
"configurations": [
{
"preLaunchTask": "STM8 Build & Flash",
"preLaunchTask": "Build & Flash",
"postDebugTask": "OpenOCD reset target",
"name": "STM8-gdb",
"request": "launch",
Expand Down
4 changes: 3 additions & 1 deletion compile_and_flash_20.bat
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ COPY ..\bin\main.hex "%backup_folder%\TSDZ2-%settings_date%.hex" >NUL 2>NUL
echo Press any key to flash... (Ctl+C to stop)
pause > nul
CALL flash.bat
echo press any key to close...
@ECHO OFF
echo.
echo Press any key to close...
pause > nul

:EXIT
Expand Down
42 changes: 23 additions & 19 deletions src/ebike_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -1434,15 +1434,20 @@ static void apply_temperature_limiting(void)
}


static void apply_speed_limit(void)
{
if (m_configuration_variables.ui8_wheel_speed_max) {
// set battery current target
ui8_adc_battery_current_target = map_ui16(ui16_wheel_speed_x10,
(uint16_t) (((uint8_t)(m_configuration_variables.ui8_wheel_speed_max) * (uint8_t)10U) - (uint8_t)20U),
(uint16_t) (((uint8_t)(m_configuration_variables.ui8_wheel_speed_max) * (uint8_t)10U) + (uint8_t)20U),
static void apply_speed_limit(void) {
if (m_configuration_variables.ui8_wheel_speed_max > 0U) {
uint16_t speed_limit_low = (uint16_t)((uint8_t)(m_configuration_variables.ui8_wheel_speed_max - 2U) * (uint8_t)10U); // casting literal to uint8_t ensures usage of MUL X,A
uint16_t speed_limit_high = (uint16_t)((uint8_t)(m_configuration_variables.ui8_wheel_speed_max + 2U) * (uint8_t)10U);

ui8_adc_battery_current_target = (uint8_t)map_ui16(ui16_wheel_speed_x10,
speed_limit_low,
speed_limit_high,
ui8_adc_battery_current_target,
0);
0U);

if (ui16_wheel_speed_x10 > speed_limit_high) {
ui8_duty_cycle_target = 0;
}
}
}

Expand Down Expand Up @@ -1647,34 +1652,32 @@ static void check_system(void)
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// E09 ERROR_MOTOR_CHECK (E08 blinking for XH18)
// E09 shared with ERROR_WRITE_EEPROM
#define MOTOR_CHECK_TIME_GOES_ALONE_TRESHOLD 60 // 60 * 100ms = 6.0 seconds
#define MOTOR_CHECK_TIME_GOES_ALONE_TRESHOLD 60 // 60 * 100ms = 6.0 seconds
#define MOTOR_CHECK_ERPS_THRESHOLD 20 // 20 ERPS
static uint8_t ui8_riding_torque_mode = 0;
static uint8_t ui8_motor_check_time_goes_alone = 0;
static uint8_t ui8_motor_check_goes_alone_timer = 0U;

// riding modes that use the torque sensor
if (((m_configuration_variables.ui8_riding_mode == POWER_ASSIST_MODE)
||(m_configuration_variables.ui8_riding_mode == TORQUE_ASSIST_MODE)
||(m_configuration_variables.ui8_riding_mode == HYBRID_ASSIST_MODE)
||(m_configuration_variables.ui8_riding_mode == eMTB_ASSIST_MODE))
&& (!ui8_adc_throttle_assist)) {
ui8_riding_torque_mode = 1;
&& (ui8_adc_throttle_assist == 0U)) {
ui8_riding_torque_mode = 1;
}
else {
ui8_riding_torque_mode = 0;
}
// Check if the motor goes alone and with current or duty cycle target = 0 (safety)
if ((ui16_motor_speed_erps > MOTOR_CHECK_ERPS_THRESHOLD)
&&((ui8_riding_torque_mode)
||(m_configuration_variables.ui8_riding_mode == CADENCE_ASSIST_MODE))){
if ((!ui8_adc_battery_current_target || !ui8_duty_cycle_target)) {
ui8_motor_check_time_goes_alone++;
}
&&((ui8_riding_torque_mode) || (m_configuration_variables.ui8_riding_mode == CADENCE_ASSIST_MODE))
&& (ui8_adc_battery_current_target == 0U || ui8_duty_cycle_target == 0U)) {
ui8_motor_check_goes_alone_timer++;
}
else {
ui8_motor_check_time_goes_alone = 0;
ui8_motor_check_goes_alone_timer = 0;
}
if (ui8_motor_check_time_goes_alone > MOTOR_CHECK_TIME_GOES_ALONE_TRESHOLD) {
if (ui8_motor_check_goes_alone_timer > MOTOR_CHECK_TIME_GOES_ALONE_TRESHOLD) {
ui8_system_state = ERROR_MOTOR_CHECK;
}

Expand Down Expand Up @@ -2966,6 +2969,7 @@ static void uart_send_package(void)
if ((ui8_display_fault_code != NO_FAULT)&&(ui8_display_function_code == NO_FUNCTION)) {
#if ENABLE_XH18
if (ui8_display_fault_code == ERROR_WRITE_EEPROM) {
// shared with ERROR_MOTOR_CHECK
// instead of E09, display blinking E08
if (ui8_default_flash_state) {
ui8_tx_buffer[5] = 8;
Expand Down
7 changes: 4 additions & 3 deletions src/motor.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,8 @@ void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER)
// - ramp up/down PWM duty_cycle and/or field weakening angle value

// check if to decrease, increase or maintain duty cycle
if ((ui8_g_duty_cycle > ui8_controller_duty_cycle_target)
|| (ui8_adc_battery_current_filtered > ui8_controller_adc_battery_current_target)
if ( (ui8_controller_duty_cycle_target < ui8_g_duty_cycle)
|| (ui8_controller_adc_battery_current_target < ui8_adc_battery_current_filtered)
|| (ui8_adc_motor_phase_current > ui8_adc_motor_phase_current_max)
|| (ui16_hall_counter_total < (HALL_COUNTER_FREQ / MOTOR_OVER_SPEED_ERPS))
|| (ui16_adc_voltage < ui16_adc_voltage_cut_off)
Expand All @@ -813,7 +813,8 @@ void TIM1_CAP_COM_IRQHandler(void) __interrupt(TIM1_CAP_COM_IRQHANDLER)
}
}
}
else if (ui8_g_duty_cycle < ui8_controller_duty_cycle_target) {
else if ((ui8_controller_duty_cycle_target > ui8_g_duty_cycle)
&& (ui8_controller_adc_battery_current_target > ui8_adc_battery_current_filtered)) {
// reset duty cycle ramp down counter (filter)
ui8_counter_duty_cycle_ramp_down = 0;

Expand Down
41 changes: 41 additions & 0 deletions tests/test_speed_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import pytest
from sim._tsdz2 import ffi, lib as ebike # module generated from c-code
import numpy as np

BATTERY_CURRENT_PER_10_BIT_ADC_STEP_X100 = 16
mA_to_ADC = 100/BATTERY_CURRENT_PER_10_BIT_ADC_STEP_X100 / 1000

# Set up initial values before each test
@pytest.fixture(autouse=True)
def setup_ebike():
# Set up initial values before each test
ebike.m_configuration_variables.ui8_wheel_speed_max = 25
ebike.ui8_duty_cycle_target = 255 # set by assistance function
ebike.ui8_adc_battery_current_target = int(5000 * mA_to_ADC) # 5000mA set by assistance function
yield
# Teardown after each test (optional)


def apply_speed_limit_float(speed):
speed_max = ebike.m_configuration_variables.ui8_wheel_speed_max
speed_lo = speed_max - 2
speed_hi = speed_max + 2
curr_target = ebike.ui8_adc_battery_current_target
current_lim = np.interp(speed, [speed_lo, speed_hi], [curr_target, 0])
return current_lim

# Parameterized test function with different ticks values
@pytest.mark.parametrize("speed", [0, 22.9, 23, 23.5, 24, 24.5, 25, 25.5, 26, 26.5, 27, 27.1, 30])
def test_apply_speed_limit(speed):
ebike.ui16_wheel_speed_x10 = int(speed * 10)

expected = apply_speed_limit_float(speed) # this has to run first
ebike.apply_speed_limit()
result = ebike.ui8_adc_battery_current_target

assert result ==pytest.approx(expected, rel=1e-1, abs=0.1), f'Expected target {expected/mA_to_ADC}mA, got {result/mA_to_ADC}mA'


# Run the tests
if __name__ == '__main__':
pytest.main()

0 comments on commit 9db5159

Please sign in to comment.