def test_send_recv(serial_sender=None, serial_reciever=None): p_send = Panda(serial_sender) p_recv = Panda(serial_reciever) p_send.set_safety_mode(Panda.SAFETY_ALLOUTPUT) p_send.set_can_loopback(False) p_recv.set_can_loopback(False) assert not p_send.legacy assert not p_recv.legacy p_send.can_send_many([(0x1ba, 0, "message", 0)] * 2) time.sleep(0.05) p_recv.can_recv() p_send.can_recv() busses = [0, 1, 2] for bus in busses: for speed in [100, 250, 500, 750, 1000]: p_send.set_can_speed_kbps(bus, speed) p_recv.set_can_speed_kbps(bus, speed) time.sleep(0.05) comp_kbps = time_many_sends(p_send, bus, p_recv, two_pandas=True) saturation_pct = (comp_kbps / speed) * 100.0 assert_greater(saturation_pct, 80) assert_less(saturation_pct, 100) print( "two pandas bus {}, 100 messages at speed {:4d}, comp speed is {:7.2f}, percent {:6.2f}" .format(bus, speed, comp_kbps, saturation_pct))
def send_thread(serial): panda = Panda(serial) panda.set_safety_mode(Panda.SAFETY_ALLOUTPUT) panda.set_can_loopback(False) can_sock = messaging.sub_sock(service_list['can'].port) while True: # Send messages one bus 0 and 1 tsc = messaging.recv_one(can_sock) snd = can_capnp_to_can_list(tsc.can) snd = filter(lambda x: x[-1] <= 2, snd) panda.can_send_many(snd) # Drain panda message buffer panda.can_recv()
def send_thread(serial): try: panda = Panda(serial) panda.set_safety_mode(Panda.SAFETY_ALLOUTPUT) panda.set_can_loopback(False) can_sock = messaging.sub_sock('can') while True: # Send messages one bus 0 and 1 tsc = messaging.recv_one(can_sock) snd = can_capnp_to_can_list(tsc.can) snd = list(filter(lambda x: x[-1] <= 2, snd)) panda.can_send_many(snd) # Drain panda message buffer panda.can_recv() except Exception: traceback.print_exc()
class Prius(object): def __init__(self): panda_list = Panda.list() # choose panda serial prot if len(panda_list) > 1: for i, s in enumerate(panda_list, 1): print('{}) {}'.format(i, s)) serial = panda_list[input('Please input 1, 2,.... or 10 number: ') - 1] else: serial = panda_list[0] # Connect to panda if serial in panda_list: self.panda = Panda(serial) self.panda.set_safety_mode(Panda.SAFETY_ALLOUTPUT) self.panda.can_clear(0) self.frame = 0 print('Connect Panda [Send]') else: print('Not Panda connect') exit() def send_speed(self, speed=0): can_send = [] # speedometer can_send.append(create_speedometer(self.frame, 0xB4, speed, 0, True)) can_send.append(create_speedometer2(self.frame, 0xB1, speed, 0, True)) can_send.append(create_speedometer3(self.frame, 0xB3, speed, 0, True)) can_send.append(create_speedometer4(self.frame, 0x3ca, speed, 0)) self.frame += 1 self.panda.can_send_many(can_send) def send_door_lock(self, lock=True): can_send = [] if lock: msg = struct.pack('!BBB', 0xe4, 0x81, 0x00) can_send.append([0x5B6, 0, msg, 0]) self.panda.can_send_many(can_send) else: msg = struct.pack('!BBB', 0xe4, 0x00, 0x00) can_send.append([0x5B6, 0, msg, 0]) self.panda.can_send_many(can_send) def recv(self): can_msgs = self.panda.can_recv() can_msgs_bytes = [] for address, _, dat, src in can_msgs: can_msgs_bytes.append((address, 0, bytes(dat), src)) if address == 0xb4: print("Address: {}\t Data: {}\t src: {}".format( address, binary_show(dat), src))
# print chime, alert_id, hud_alert fcw_display, steer_required, acc_alert = (4, 1, 1) mph_shown_screen = 69 hud = HUDData(int(100), int(round(mph_shown_screen * 1.6)), 0x01, hud_car, 0xc1, 0x41, hud_lanes + steer_required, int(0), 0x48, (0 << 5) + fcw_display, acc_alert) # idx refers to the COUNTER field we see # ui_cmd = create_ui_commands(pcm_speed, hud, CS.CP.carFingerprint, idx) ui_cmd = create_ui_commands(0, hud, 0) print ui_cmd # Looks like: # [[780, 0, '\x00\x00\x00\x00\x01\xc0\xc1\x0f', 0], # [829, 0, 'A\x00\x00H\x04', 0], # [862, 0, '\x00\x00\x00\x00\x00\x00\x00\x02', 0], # [927, 0, '\x00\x00\x00\x00\xff\x7f\x00\t', 0]] print("Press Enter to send ui commands") idx_counter = 1 total_cmds_sent = 0 try: while raw_input() == '': print("Sending: " + str(ui_cmd) + " (#" + str(total_cmds_sent) + ")") panda.can_send_many(ui_cmd) idx_counter += 1 idx_counter %= 4 total_cmds_sent += 1 except KeyboardInterrupt: panda.close()
scale = 1.0 if len(sys.argv) == 4: scale = float(sys.argv[2]) # prepare msgs print("Publishing...") accum_to_send = to_sendeable_accum_original_bus(accum) total_msgs = len(accum_to_send) try: for idx, block_msg in enumerate(accum_to_send): if idx < len(accum_to_send): ini_t = timestamps[idx] next_t = timestamps[idx + 1] sleep_time = next_t - ini_t # print(block_msg) p.can_send_many(block_msg) # print("sleeping for: " + str(sleep_time)) time.sleep(sleep_time * scale) else: p.can_send_many(block_msg) # Give some feedback percent_msgs = int((float(idx) / float(total_msgs)) * 100.0) if percent_msgs % 5 == 0 and percent_msgs > 0: print("Replayed " + str(percent_msgs) + "% of messages...") print("(" + str(idx) + " / " + str(total_msgs) + " blocks of up to 256 msgs)") except KeyboardInterrupt: p.close() print("Done")
for row in csv_reader: try: (timestamp, address, bus, data) = row except: pass if rownumber >= 0 and float(timestamp) > 1.0 and int(bus) in ( 0, ): #1,2,128,129,130): if fingerprint.has_key(int(address)): datBytes = fingerprint[int(address)] else: datBytes = 8 if int(bus) > 127: bus = int(bus) - 128 if (len(out_cans) > 5 and sleepTime > 0) or len(out_cans) > 20: p.can_send_many(out_cans) print("total %d skipped %d sent %d lag %f" % (rownumber, skipped, len(out_cans), out_cans[len(out_cans) - 1][1] - out_cans[0][1])) out_cans = [] sleepTime = startTime + float(timestamp) - time.time() if sleepTime > 0: #print(sleepTime) time.sleep(sleepTime) sleepTime = startTime + float(timestamp) - time.time() out_cans.append([ int(address), float(timestamp), str(data).decode("hex")[:datBytes], int(bus) ])
def test_latency(serial_sender=None, serial_reciever=None): p_send = Panda(serial_sender) p_recv = Panda(serial_reciever) p_send.set_safety_mode(Panda.SAFETY_ALLOUTPUT) p_send.set_can_loopback(False) p_recv.set_can_loopback(False) assert not p_send.legacy assert not p_recv.legacy p_send.set_can_speed_kbps(0, 100) p_recv.set_can_speed_kbps(0, 100) time.sleep(0.05) p_send.can_send_many([(0x1ba, 0, "testmsg", 0)] * 10) time.sleep(0.05) p_recv.can_recv() p_send.can_recv() busses = [0, 1, 2] for bus in busses: for speed in [100, 250, 500, 750, 1000]: p_send.set_can_speed_kbps(bus, speed) p_recv.set_can_speed_kbps(bus, speed) time.sleep(0.1) #clear can buffers r = [1] while len(r) > 0: r = p_send.can_recv() r = [1] while len(r) > 0: r = p_recv.can_recv() time.sleep(0.05) latencies = [] comp_kbps_list = [] saturation_pcts = [] num_messages = 100 for i in range(num_messages): st = time.time() p_send.can_send(0x1ab, "message", bus) r = [] while len(r) < 1 and (time.time() - st) < 5: r = p_recv.can_recv() et = time.time() r_echo = [] while len(r_echo) < 1 and (time.time() - st) < 10: r_echo = p_send.can_recv() if len(r) == 0 or len(r_echo) == 0: print("r: {}, r_echo: {}".format(r, r_echo)) assert_equal(len(r), 1) assert_equal(len(r_echo), 1) et = (et - st) * 1000.0 comp_kbps = (1 + 11 + 1 + 1 + 1 + 4 + 8 * 8 + 15 + 1 + 1 + 1 + 7) / et latency = et - ( (1 + 11 + 1 + 1 + 1 + 4 + 8 * 8 + 15 + 1 + 1 + 1 + 7) / speed) assert_less(latency, 5.0) saturation_pct = (comp_kbps / speed) * 100.0 latencies.append(latency) comp_kbps_list.append(comp_kbps) saturation_pcts.append(saturation_pct) average_latency = sum(latencies) / num_messages assert_less(average_latency, 1.0) average_comp_kbps = sum(comp_kbps_list) / num_messages average_saturation_pct = sum(saturation_pcts) / num_messages print("two pandas bus {}, {} message average at speed {:4d}, latency is {:5.3f}ms, comp speed is {:7.2f}, percent {:6.2f}"\ .format(bus, num_messages, speed, average_latency, average_comp_kbps, average_saturation_pct))
class Prius(object): def __init__(self, dbc=False): panda_list = Panda.list() # choose panda serial prot if len(panda_list) > 1: for i, s in enumerate(panda_list, 1): print('{}) {}'.format(i, s)) serial = panda_list[input('Please input 1, 2,.... or 10 number: ') - 1] else: serial = panda_list[0] # Connect to panda if serial in panda_list: self.panda = Panda(serial) self.panda.set_safety_mode(Panda.SAFETY_ALLOUTPUT) self.panda.can_clear(0) self.frame = 0 print('Connect Panda [Send]') else: print('Not Panda connect') exit() # add dbc decoder if dbc: self.can_msg_parser = load_dbc_file(dbc) print(self.can_msg_parser.messages) self.upload_data = OrderedDict( (('wheel_angle', 0), ('SPEED', 0), ('gear_1', 35), ('gear_2', 96), ('brake_LI', 0), ('brake_H', 0), ('handcrat', 0), ('LIGHT_SMALL', 0), ('LIGHT_BIG', 0), ('LIGHT_FLASH', 0), ('door_lock', 128))) def send_speed(self, speed=0): can_send = [ create_speedometer_B4(self.frame, speed, 0, True), create_speedometer_B1(speed, 0, True), create_speedometer_B3(speed, 0, True), create_speedometer_3CA(speed, 0) ] # speedometer self.frame += 1 self.panda.can_send_many(can_send) def send_door_lock(self, lock=True): can_send = [] if lock: msg = struct.pack('!BBB', 0xe4, 0x81, 0x00) can_send.append([0x5B6, 0, msg, 0]) self.panda.can_send_many(can_send) else: msg = struct.pack('!BBB', 0xe4, 0x00, 0x00) can_send.append([0x5B6, 0, msg, 0]) self.panda.can_send_many(can_send) def recv_print(self, mode='all'): can_msgs = self.panda.can_recv() if mode == 'dbc': for msg in can_msgs: if msg[0] in fingerprint.keys(): try: print(msg[0]) print( self.can_msg_parser.decode_message(msg[0], msg[2])) except: pass else: can_msgs_bytes = [] for address, _, dat, src in can_msgs: can_msgs_bytes.append((address, 0, bytes(dat), src)) if mode == 'all': print("Address: {}\t Data: {}\t src: {}".format( address, binary_show(dat), src)) elif address == 0xb4: print("Address: {}\t Data: {}\t src: {}".format( address, binary_show(dat), src)) def recv(self): can_msgs = self.panda.can_recv() for msg in can_msgs: try: if msg[0] in fingerprint.keys(): data_dict = self.can_msg_parser.decode_message( msg[0], msg[2]) for i in fingerprint[msg[0]]: self.upload_data[i] = data_dict[i] except: pass return self.upload_data