forked from OpenSourceEBike/TSDZ2-Smart-EBike
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from dzid26/fix_E07
Cut PWM above speed limit (fixes E09)
- Loading branch information
Showing
5 changed files
with
175 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import pytest | ||
from sim._tsdz2 import ffi, lib as ebike # module generated from c-code | ||
import numpy as np | ||
from hypothesis import given, assume, strategies as st | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"x, in_min, in_max, out_min, out_max, expected", [ | ||
( 4, 0, 16, 16, 0, 12), | ||
( 1, 0, 2, 3, 0, 1), | ||
( 1, 0, 3, 0, 2, 1), | ||
]) | ||
def test_maps_simple(x, in_min, in_max, out_min, out_max, expected): | ||
map_ui8_result = ebike.map_ui8(x, in_min, in_max, out_min, out_max) | ||
map_ui16_result = ebike.map_ui16(x, in_min, in_max, out_min, out_max) | ||
assert map_ui8_result == pytest.approx(expected, abs=1), f'Expected map_ui8_result {expected}, got {map_ui8_result}' | ||
assert map_ui16_result == expected, f'Expected map_ui16_result {expected}, got {map_ui16_result}' | ||
|
||
|
||
# Parameterized test function with different ticks values | ||
@pytest.mark.parametrize("x", range(20, 45)) | ||
def test_compare_ui8_ui16_map_input_smaller_than_output(x): | ||
in_min = 23 | ||
in_max = 43 | ||
out_min = 5 | ||
out_max = 250 | ||
map_ui8_result = ebike.map_ui8(x, in_min, in_max, out_min, out_max) | ||
map_ui16_result = ebike.map_ui16(x, in_min, in_max, out_min, out_max) | ||
# ! map_ui8 has lower precision so allow for an error of 1 | ||
assert map_ui16_result == pytest.approx(map_ui8_result, abs=1), f'Expected map_ui8_result {map_ui8_result} == map_ui16_result {map_ui16_result}' | ||
|
||
@pytest.mark.parametrize("x", range(20, 90)) | ||
def test_compare_ui8_ui16_map_input_greater_than_output(x): | ||
in_min = 23 | ||
in_max = 87 | ||
out_min = 5 | ||
out_max = 50 | ||
map_ui8_result = ebike.map_ui8(x, in_min, in_max, out_min, out_max) | ||
map_ui16_result = ebike.map_ui16(x, in_min, in_max, out_min, out_max) | ||
|
||
# ! map_ui8 has lower precision so allow for an error of 1 | ||
assert map_ui16_result == pytest.approx(map_ui8_result, abs=1), f'Expected map_ui8_result {map_ui8_result} == map_ui16_result {map_ui16_result}' | ||
|
||
|
||
|
||
# Define the hypothesis test for map_ui8 | ||
@given( | ||
x=st.integers(min_value=0, max_value=65535), | ||
in_min=st.integers(min_value=0, max_value=65535), | ||
in_max=st.integers(min_value=0, max_value=65535), | ||
out_min=st.integers(min_value=0, max_value=65535), | ||
out_max=st.integers(min_value=0, max_value=65535)) | ||
def test_maps_full_ranges(x, in_min, in_max, out_min, out_max): | ||
assume(in_min <= in_max) | ||
|
||
expected = np.interp(x, [in_min, in_max], [out_min, out_max]) | ||
# !test map_ui8 only for 8 bit ranges | ||
if max(x, in_min, in_max, out_min, out_max) < 2^8: | ||
map_ui8_result = ebike.map_ui8(x, in_min, in_max, out_min, out_max) | ||
# ! map_ui8 lowest precision is 1 | ||
assert map_ui8_result == pytest.approx(expected, abs=1), \ | ||
f"map_ui8({x}, {in_min}, {in_max}, {out_min}, {out_max}) returned {map_ui8_result}, expected {expected}" | ||
else: | ||
print(f'x={x}, in_min={in_min}, in_max={in_max}, out_min={out_min}, out_max={out_max}') | ||
|
||
map_ui16_result = ebike.map_ui16(x, in_min, in_max, out_min, out_max) | ||
# ! map_ui16 lowest precision is 0.5 (thanks to nearest rounding) | ||
assert map_ui16_result == pytest.approx(expected, abs=.5), \ | ||
f"map_ui16({x}, {in_min}, {in_max}, {out_min}, {out_max}) returned {map_ui16_result}, expected {expected}" | ||
|
||
|
||
|
||
# Run the tests | ||
if __name__ == '__main__': | ||
pytest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |