Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/scripts/CAN/CAN_playback.py
Views: 1799
#!/usr/bin/env python31'''2playback a set of CAN frames3capture frames either using libraries/AP_Scripting/examples/CAN_logger.lua4or the CAN_Pn_OPTIONS bit to enable CAN logging5'''67import dronecan8import time9import sys10import threading11from pymavlink import mavutil12from dronecan.driver.common import CANFrame13import struct141516# get command line arguments17from argparse import ArgumentParser18parser = ArgumentParser(description='CAN playback')19parser.add_argument("logfile", default=None, type=str, help="logfile")20parser.add_argument("canport", default=None, type=str, help="CAN port")21parser.add_argument("--bus", default=0, type=int, help="CAN bus")2223args = parser.parse_args()2425print("Connecting to %s" % args.canport)26driver = dronecan.driver.make_driver(args.canport)2728print("Opening %s" % args.logfile)29mlog = mavutil.mavlink_connection(args.logfile)3031tstart = time.time()32first_tstamp = None3334def dlc_to_datalength(dlc):35# Data Length Code 9 10 11 12 13 14 1536# Number of data bytes 12 16 20 24 32 48 6437if (dlc <= 8):38return dlc39elif (dlc == 9):40return 1241elif (dlc == 10):42return 1643elif (dlc == 11):44return 2045elif (dlc == 12):46return 2447elif (dlc == 13):48return 3249elif (dlc == 14):50return 4851return 645253while True:54m = mlog.recv_match(type=['CANF','CAFD'])5556if m is None:57print("Rewinding")58mlog.rewind()59tstart = time.time()60first_tstamp = None61continue6263if getattr(m,'bus',0) != args.bus:64continue6566if first_tstamp is None:67first_tstamp = m.TimeUS68dt = time.time() - tstart69dt2 = (m.TimeUS - first_tstamp)*1.0e-670if dt2 > dt:71time.sleep(dt2 - dt)7273canfd = m.get_type() == 'CAFD'74if canfd:75data = struct.pack("<QQQQQQQQ", m.D0, m.D1, m.D2, m.D3, m.D4, m.D5, m.D6, m.D7)76data = data[:dlc_to_datalength(m.DLC)]77else:78data = struct.pack("<BBBBBBBB", m.B0, m.B1, m.B2, m.B3, m.B4, m.B5, m.B6, m.B7)79data = data[:m.DLC]8081fid = m.Id82is_extended = (fid & (1<<31)) != 083driver.send(fid, data, extended=is_extended, canfd=canfd)84print(m)858687