From f16cefc46cdd310dadb7f68604bc04cff3d5a3ac Mon Sep 17 00:00:00 2001 From: Michelle Rossouw Date: Fri, 5 Jan 2024 17:07:40 +1100 Subject: [PATCH] AP_Scripting: Add CAN_Logger example script Co-authored-by: Andrew Tridgell --- .../AP_Scripting/examples/CAN_logger.lua | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 libraries/AP_Scripting/examples/CAN_logger.lua diff --git a/libraries/AP_Scripting/examples/CAN_logger.lua b/libraries/AP_Scripting/examples/CAN_logger.lua new file mode 100644 index 0000000000000..aa3c5ff990a64 --- /dev/null +++ b/libraries/AP_Scripting/examples/CAN_logger.lua @@ -0,0 +1,52 @@ +--[[ + This script captures raw packets to a log file for later playback using Tools/scripts/CAN/CAN_playback.py onto a CAN bus. + Set CAN_D1_PROTOCOL to 10 for scripting. + Also need LOG_DISARMED set to 1 if running this while disarmed. +--]] + +local can_driver = CAN:get_device(25) + +if not can_driver then + gcs:send_text(0,"No scripting CAN interface found") + return +end + +local last_print_ms = millis() +local frame_count = 0 +local last_frame_count = 0 + +function update() + + local more_frames = true + for _ = 1, 25 do + local frame = can_driver:read_frame() + if not frame then + more_frames = false + break + end + local id = frame:id() + logger.write("CANF",'Id,DLC,FC,B0,B1,B2,B3,B4,B5,B6,B7','IBIBBBBBBBB', + id, + frame:dlc(), + frame_count, + frame:data(0), frame:data(1), frame:data(2), frame:data(3), + frame:data(4), frame:data(5), frame:data(6), frame:data(7)) + frame_count = frame_count + 1 + end + + local now = millis() + if now - last_print_ms >= 1000 then + local dt = (now - last_print_ms):tofloat()*0.001 + gcs:send_text(0, string.format("CAN: %.2f fps", (frame_count-last_frame_count)/dt)) + last_print_ms = now + last_frame_count = frame_count + end + + if more_frames then + -- sleep for min possible time to try not to lose frames + return update, 0 + end + return update, 2 +end + +return update()