2019-09-25 08:50:53 +08:00
|
|
|
#!/usr/bin/env python3
|
2019-09-25 13:33:46 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
import os
|
|
|
|
import time
|
|
|
|
import random
|
2017-07-15 03:30:34 +08:00
|
|
|
import argparse
|
2017-05-03 01:20:05 +08:00
|
|
|
from itertools import permutations
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2023-08-15 03:52:02 +08:00
|
|
|
from panda import Panda
|
2017-06-14 09:50:47 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
def get_test_string():
|
2020-06-01 16:49:26 +08:00
|
|
|
return b"test" + os.urandom(10)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-07-15 03:30:34 +08:00
|
|
|
def run_test(sleep_duration):
|
2017-04-07 09:11:36 +08:00
|
|
|
pandas = Panda.list()
|
2017-06-14 09:50:47 +08:00
|
|
|
print(pandas)
|
2017-04-18 22:34:56 +08:00
|
|
|
|
2021-12-07 06:39:28 +08:00
|
|
|
if len(pandas) < 2:
|
2023-08-07 05:59:22 +08:00
|
|
|
raise Exception("Minimum two pandas are needed for test")
|
2017-04-18 22:34:56 +08:00
|
|
|
|
2017-07-15 03:30:34 +08:00
|
|
|
run_test_w_pandas(pandas, sleep_duration)
|
2017-05-03 00:54:57 +08:00
|
|
|
|
2017-07-15 03:30:34 +08:00
|
|
|
def run_test_w_pandas(pandas, sleep_duration):
|
2023-08-15 03:52:02 +08:00
|
|
|
h = [Panda(x) for x in pandas]
|
2017-06-28 10:47:53 +08:00
|
|
|
print("H", h)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-05-03 01:20:05 +08:00
|
|
|
for hh in h:
|
2017-08-13 07:32:36 +08:00
|
|
|
hh.set_safety_mode(Panda.SAFETY_ALLOUTPUT)
|
2017-05-02 14:40:49 +08:00
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
# test both directions
|
2019-09-25 13:33:46 +08:00
|
|
|
for ho in permutations(list(range(len(h))), r=2):
|
2017-06-14 09:50:47 +08:00
|
|
|
print("***************** TESTING", ho)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-07-15 03:30:34 +08:00
|
|
|
panda0, panda1 = h[ho[0]], h[ho[1]]
|
|
|
|
|
2017-04-07 09:11:36 +08:00
|
|
|
# **** test health packet ****
|
2017-07-13 02:25:10 +08:00
|
|
|
print("health", ho[0], h[ho[0]].health())
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
# **** test can line loopback ****
|
2024-03-22 04:56:36 +08:00
|
|
|
for bus, obd in [(0, False), (1, False), (2, False), (1, True), (2, True)]:
|
2017-07-13 02:25:10 +08:00
|
|
|
print("\ntest can", bus)
|
2017-04-07 09:11:36 +08:00
|
|
|
# flush
|
2017-07-13 02:25:10 +08:00
|
|
|
cans_echo = panda0.can_recv()
|
|
|
|
cans_loop = panda1.can_recv()
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2024-03-22 04:56:36 +08:00
|
|
|
panda0.set_obd(None)
|
|
|
|
panda1.set_obd(None)
|
2017-08-13 08:17:22 +08:00
|
|
|
|
2024-03-22 04:56:36 +08:00
|
|
|
if obd is True:
|
|
|
|
panda0.set_obd(bus)
|
|
|
|
panda1.set_obd(bus)
|
2017-08-13 07:47:07 +08:00
|
|
|
bus = 3
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
# send the characters
|
2017-08-13 07:32:36 +08:00
|
|
|
at = random.randint(1, 2000)
|
2017-04-07 09:11:36 +08:00
|
|
|
st = get_test_string()[0:8]
|
2017-07-13 02:25:10 +08:00
|
|
|
panda0.can_send(at, st, bus)
|
2017-04-07 09:11:36 +08:00
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
# check for receive
|
2017-07-13 02:25:10 +08:00
|
|
|
cans_echo = panda0.can_recv()
|
|
|
|
cans_loop = panda1.can_recv()
|
2017-04-07 09:11:36 +08:00
|
|
|
|
2017-06-28 10:47:53 +08:00
|
|
|
print("Bus", bus, "echo", cans_echo, "loop", cans_loop)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
assert len(cans_echo) == 1
|
|
|
|
assert len(cans_loop) == 1
|
|
|
|
|
|
|
|
assert cans_echo[0][0] == at
|
|
|
|
assert cans_loop[0][0] == at
|
|
|
|
|
|
|
|
assert cans_echo[0][2] == st
|
|
|
|
assert cans_loop[0][2] == st
|
|
|
|
|
2017-07-15 03:30:34 +08:00
|
|
|
assert cans_echo[0][3] == 0x80 | bus
|
2017-06-10 05:11:37 +08:00
|
|
|
if cans_loop[0][3] != bus:
|
2017-06-14 09:50:47 +08:00
|
|
|
print("EXPECTED %d GOT %d" % (bus, cans_loop[0][3]))
|
2017-04-07 09:11:36 +08:00
|
|
|
assert cans_loop[0][3] == bus
|
|
|
|
|
2017-06-14 09:50:47 +08:00
|
|
|
print("CAN pass", bus, ho)
|
2017-07-15 03:30:34 +08:00
|
|
|
time.sleep(sleep_duration)
|
2017-04-07 09:11:36 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2017-07-15 03:30:34 +08:00
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-n", type=int, help="Number of test iterations to run")
|
|
|
|
parser.add_argument("-sleep", type=int, help="Sleep time between tests", default=0)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.n is None:
|
2017-06-14 09:50:47 +08:00
|
|
|
while True:
|
2017-07-15 03:30:34 +08:00
|
|
|
run_test(sleep_duration=args.sleep)
|
|
|
|
else:
|
2023-08-07 05:59:22 +08:00
|
|
|
for _ in range(args.n):
|
2017-07-15 03:30:34 +08:00
|
|
|
run_test(sleep_duration=args.sleep)
|