tests.dashboard.main3
We only have 1 DAC rn (because all 8 DACs accidently have the same I2C address) So we will use voltage dividers for 3 pedal inputs and use the 1 DAC for the 4th pedal input.
1""" 2 We only have 1 DAC rn (because all 8 DACs accidently have the same I2C address) 3 So we will use voltage dividers for 3 pedal inputs and use the 1 DAC for the 4th 4 pedal input. 5""" 6 7from typing import Optional 8 9import hil2.hil2 as hil2 10import hil2.component as hil2_comp 11import hil2.can_helper as can_helper 12import mk_assert.mk_assert as mka 13 14import time 15import logging 16 17# MSG_NAME = "raw_throttle_brake" 18MSG_NAME = 144705536 19BRAKE_PERCENT = 0.0 # precent 20BRAKE_TOL = 10 21THROTTLE_TOL = 10 22 23# BRAKE1_RAW = 300 24# BRAKE2_RAW = 300 25# THROTTLE2_RAW = 300 26# V_TO_ADC = lambda v: (v / 5.0) * 4095 27SCALE = lambda x, in_min, in_max, out_min, out_max: (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min 28# V_TO_ADC_INV = lambda v: 4095 - V_TO_ADC(v) 29 30def check_msg(msg: Optional[can_helper.CanMessage], v: float, test_prefix: str): 31 mka.assert_true(msg is not None, f"{test_prefix}: VCAN message received") 32 if msg is None: 33 return 34 35 brake = msg.data["brake"] 36 # brake_right = msg.data["brake_right"] 37 throttle = msg.data["throttle"] 38 # throttle_right = msg.data["throttle_right"] 39 40 adc_exp = SCALE(v, 0.5, 4.5, 0, 4095) 41 42 logging.info(f"{test_prefix}: throttle={throttle}, brake={brake}") 43 mka.assert_eqf(brake, adc_exp, BRAKE_TOL, f"{test_prefix}: brake ({brake}) should be approximately {adc_exp}") 44 mka.assert_eqf(throttle, adc_exp, THROTTLE_TOL, f"{test_prefix}: throttle ({throttle}) should be approximately {adc_exp}") 45 46 # logging.info(f"{test_prefix}: throttle={throttle}, brake={brake}, throttle_right={throttle_right}, brake_right={brake_right}") 47 48 # adc_exp = V_TO_ADC_INV(v) 49 # adc_exp_inv = V_TO_ADC(v) 50 # mka.assert_eqf(brake, adc_exp, BRAKE_TOL, f"{test_prefix}: brake ({brake}) should be approximately {adc_exp}") 51 # mka.assert_eqf(brake_right, adc_exp, BRAKE_TOL, f"{test_prefix}: brake_right ({brake_right}) should be approximately {adc_exp}") 52 # mka.assert_eqf(throttle, adc_exp, THROTTLE_TOL, f"{test_prefix}: throttle ({throttle}) should be approximately {adc_exp}") 53 # mka.assert_eqf(throttle_right, adc_exp_inv, THROTTLE_TOL, f"{test_prefix}: throttle_right ({throttle_right}) should be approximately {adc_exp_inv}") 54 55 56def t_4_2_5_test(h: hil2.Hil2): 57 """ 58 - Questionable setup: 59 - Brake pedal 1 is 0.5v (0% pressed) (via 5v -> 0.5v voltage divider) 60 - Brake pedal 2 is 0.5v (0% pressed) (via 5v -> 0.5v voltage divider) 61 - Throttle pedal 1 is varied (DAC/AO) 62 - Throttle pedal 2 is 2.5v (50% pressed) (via 5v -> 2.5v voltage divider) 63 64 Ideally: 65 - sens1 and sens2 similar, check motor on, sdc not triggered 66 - sens1 and sens2 slightly different, check motor on, sdc not triggered 67 - sens1 and sens2 10% different, check motor on, sdc not triggered 68 - sens1 and sens2 slightly different, check motor on, sdc not triggered 69 - sens1 and sens2 10% different, check motor on, sdc not triggered 70 - sens1 and sens2 still 10% different (~100 msec later), check motor off, sdc not triggered 71 - sens1 and sens2 similar, check motor on, sdc not triggered 72 Note: Check for motor off: throttle can message is 0 73 74 Right now: 75 - Setup everything 76 - Just check that the message is correct (throttle 50%, brake 0%) 77 """ 78 79 # brake1 = h.do("HIL2", "DO1") 80 # brake2 = h.do("HIL2", "DO5") 81 # throttle1 = h.ao("HIL2", "DAC3") 82 # throttle2 = h.do("HIL2", "DO9") 83 84 vcan = h.can("HIL2", "VCAN") 85 dac = h.ao("HIL2", "DAC1") 86 87 # brake1.set(True) 88 # brake2.set(True) 89 # throttle1.set(2.5) 90 # throttle2.set(True) 91 92 dac.set(2.5) 93 94 input("Setup (brakes 0%, throttle 50%), press Enter to continue...") 95 96 check_msg(vcan.get_last(MSG_NAME), 2.5, "Initial") 97 98 # vcan.clear() 99 # time.sleep(0.02) 100 # msg = vcan.get_last(MSG_NAME) 101 # check_msg(msg, 50.0, "Initial") 102 103 # all_msgs = vcan.get_all() 104 # for msg in all_msgs: 105 # print(f"Received CAN message: ID={msg.signal}, Data={msg.data}") 106 # # msg_ids = set([m. for m in all_msgs]) 107 # # msg_ids = msg_ids - {MSG_NAME} 108 # # print(f"Other CAN message IDs received on VCAN: {msg_ids}") 109 110 for i in range(5, 46, 1): 111 vcan.clear() 112 v = i / 10.0 113 dac.set(v) 114 time.sleep(0.02) 115 116 msg = vcan.get_last(MSG_NAME) 117 check_msg(msg, v, f"Throttle set to {v}V") 118 119 time.sleep(0.2) 120 121 122# Main --------------------------------------------------------------------------------# 123def main(): 124 logging.basicConfig(level=logging.INFO) 125 126 with hil2.Hil2( 127 "./tests/dashboard/config.json", 128 "device_configs", 129 None, 130 "./tests/dashboard/dbc" 131 ) as h: 132 133 mka.add_test(t_4_2_5_test, h) 134 mka.run_tests() 135 136 137if __name__ == "__main__": 138 main()
MSG_NAME =
144705536
BRAKE_PERCENT =
0.0
BRAKE_TOL =
10
THROTTLE_TOL =
10
def
SCALE(x, in_min, in_max, out_min, out_max):
28SCALE = lambda x, in_min, in_max, out_min, out_max: (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
31def check_msg(msg: Optional[can_helper.CanMessage], v: float, test_prefix: str): 32 mka.assert_true(msg is not None, f"{test_prefix}: VCAN message received") 33 if msg is None: 34 return 35 36 brake = msg.data["brake"] 37 # brake_right = msg.data["brake_right"] 38 throttle = msg.data["throttle"] 39 # throttle_right = msg.data["throttle_right"] 40 41 adc_exp = SCALE(v, 0.5, 4.5, 0, 4095) 42 43 logging.info(f"{test_prefix}: throttle={throttle}, brake={brake}") 44 mka.assert_eqf(brake, adc_exp, BRAKE_TOL, f"{test_prefix}: brake ({brake}) should be approximately {adc_exp}") 45 mka.assert_eqf(throttle, adc_exp, THROTTLE_TOL, f"{test_prefix}: throttle ({throttle}) should be approximately {adc_exp}") 46 47 # logging.info(f"{test_prefix}: throttle={throttle}, brake={brake}, throttle_right={throttle_right}, brake_right={brake_right}") 48 49 # adc_exp = V_TO_ADC_INV(v) 50 # adc_exp_inv = V_TO_ADC(v) 51 # mka.assert_eqf(brake, adc_exp, BRAKE_TOL, f"{test_prefix}: brake ({brake}) should be approximately {adc_exp}") 52 # mka.assert_eqf(brake_right, adc_exp, BRAKE_TOL, f"{test_prefix}: brake_right ({brake_right}) should be approximately {adc_exp}") 53 # mka.assert_eqf(throttle, adc_exp, THROTTLE_TOL, f"{test_prefix}: throttle ({throttle}) should be approximately {adc_exp}") 54 # mka.assert_eqf(throttle_right, adc_exp_inv, THROTTLE_TOL, f"{test_prefix}: throttle_right ({throttle_right}) should be approximately {adc_exp_inv}")
57def t_4_2_5_test(h: hil2.Hil2): 58 """ 59 - Questionable setup: 60 - Brake pedal 1 is 0.5v (0% pressed) (via 5v -> 0.5v voltage divider) 61 - Brake pedal 2 is 0.5v (0% pressed) (via 5v -> 0.5v voltage divider) 62 - Throttle pedal 1 is varied (DAC/AO) 63 - Throttle pedal 2 is 2.5v (50% pressed) (via 5v -> 2.5v voltage divider) 64 65 Ideally: 66 - sens1 and sens2 similar, check motor on, sdc not triggered 67 - sens1 and sens2 slightly different, check motor on, sdc not triggered 68 - sens1 and sens2 10% different, check motor on, sdc not triggered 69 - sens1 and sens2 slightly different, check motor on, sdc not triggered 70 - sens1 and sens2 10% different, check motor on, sdc not triggered 71 - sens1 and sens2 still 10% different (~100 msec later), check motor off, sdc not triggered 72 - sens1 and sens2 similar, check motor on, sdc not triggered 73 Note: Check for motor off: throttle can message is 0 74 75 Right now: 76 - Setup everything 77 - Just check that the message is correct (throttle 50%, brake 0%) 78 """ 79 80 # brake1 = h.do("HIL2", "DO1") 81 # brake2 = h.do("HIL2", "DO5") 82 # throttle1 = h.ao("HIL2", "DAC3") 83 # throttle2 = h.do("HIL2", "DO9") 84 85 vcan = h.can("HIL2", "VCAN") 86 dac = h.ao("HIL2", "DAC1") 87 88 # brake1.set(True) 89 # brake2.set(True) 90 # throttle1.set(2.5) 91 # throttle2.set(True) 92 93 dac.set(2.5) 94 95 input("Setup (brakes 0%, throttle 50%), press Enter to continue...") 96 97 check_msg(vcan.get_last(MSG_NAME), 2.5, "Initial") 98 99 # vcan.clear() 100 # time.sleep(0.02) 101 # msg = vcan.get_last(MSG_NAME) 102 # check_msg(msg, 50.0, "Initial") 103 104 # all_msgs = vcan.get_all() 105 # for msg in all_msgs: 106 # print(f"Received CAN message: ID={msg.signal}, Data={msg.data}") 107 # # msg_ids = set([m. for m in all_msgs]) 108 # # msg_ids = msg_ids - {MSG_NAME} 109 # # print(f"Other CAN message IDs received on VCAN: {msg_ids}") 110 111 for i in range(5, 46, 1): 112 vcan.clear() 113 v = i / 10.0 114 dac.set(v) 115 time.sleep(0.02) 116 117 msg = vcan.get_last(MSG_NAME) 118 check_msg(msg, v, f"Throttle set to {v}V") 119 120 time.sleep(0.2)
- Questionable setup:
- Brake pedal 1 is 0.5v (0% pressed) (via 5v -> 0.5v voltage divider)
- Brake pedal 2 is 0.5v (0% pressed) (via 5v -> 0.5v voltage divider)
- Throttle pedal 1 is varied (DAC/AO)
- Throttle pedal 2 is 2.5v (50% pressed) (via 5v -> 2.5v voltage divider)
Ideally:
- sens1 and sens2 similar, check motor on, sdc not triggered
- sens1 and sens2 slightly different, check motor on, sdc not triggered
- sens1 and sens2 10% different, check motor on, sdc not triggered
- sens1 and sens2 slightly different, check motor on, sdc not triggered
- sens1 and sens2 10% different, check motor on, sdc not triggered
- sens1 and sens2 still 10% different (~100 msec later), check motor off, sdc not triggered
- sens1 and sens2 similar, check motor on, sdc not triggered Note: Check for motor off: throttle can message is 0
Right now:
- Setup everything
- Just check that the message is correct (throttle 50%, brake 0%)
def
main():