背景
线控调试中,实时显示can报文中的信号值,有助于分析执行器性能表现和控制器的工作策略。 相对于从log文件(asc或blf)中读取can报文实现信号可视化的离线操作,实时可视化的难点是如何减少频繁io操作带来的cpu负载升高。
技术要点
简单来说,就是不要收到一帧can报文就plot一个数据点,而要把一定时间内或一定数量的数据点缓存起来,再以相对低的帧率去更新plot中的一堆数据点。如果can设备支持硬件缓存,那读取can报文的io操作也可以同理降频。
至于具体降频多少,程序中缓存数据点的队列深度设置多大,plot的帧率设置多大。需要根据具体的报文周期,can设备硬件buffer大小,总线负载率,图像显示的流畅度要求来计算。
示例介绍
本例子中选用没有硬件缓存的Linux Socketcan作为can通道,python中使用python-can来获取socketcan通道来发送和接收(一个py脚本发送,另一个py脚本接收并可视化)can报文,使用cantools来对报文中的can信号值进行encode和decode,使用pyqtgraph来实时显示can信号物理值的变化。
代码实现
1. ubuntu中创建socketcan类型的虚拟can通道
sudo modprobe vcan
sudo ip link add dev vcan0 type vcan
sudo ip link set up vcan0
2. dbc描述:
BO_ 2552038048 Temperature: 8 Vector__XXX
SG_ temperature : 48|8@1+ (1,-40) [-40|215] "" Vector__XXX
3. python脚本模拟can发送
import can
import cantools
import time
from threading import Thread
run_flag = True
def send_can_loop():
van_tx_bus = can.interface.Bus(bustype='socketcan', channel='vcan0')
can_db = cantools.db.load_file("./test.dbc")
temp_value = 0
while run_flag:
temp_value = (temp_value + 1) % 256
phc_temp_value = temp_value - 40
data_raw = can_db.encode_message(frame_id_or_name=0x181d02a0, data={'temperature': phc_temp_value})
can_msg = can.Message(arbitration_id=0x181d02a0, data=data_raw)
van_tx_bus.send(can_msg)
time.sleep(0.01)
van_tx_bus.shutdown()
if __name__ == "__main__":
send_thread = Thread(target=send_can_loop, args=())
send_thread.start()
while run_flag:
cmd_input = input()
if cmd_input == "q":
run_flag = False
send_thread.join()
4. python脚本接收can报文并实时可视化信号值
import can
import cantools
from collections import deque
import pyqtgraph as pg
import threading
import time
win = pg.GraphicsLayoutWidget(show=True)
win.setWindowTitle('can data realtime plot')
p1_win = pg.PlotItem()
win.addItem(p1_win)
p1 = p1_win
p1.setTitle("can data plot")
# 显示最近10秒内的数据
show_seconds = -10
p1.setXRange(show_seconds, 0)
curve1 = p1.plot()
run_flag = True
receive_run_condition = threading.Condition()
class FixedSizeQueue:
def __init__(self, max_size):
self.queue = deque(maxlen=max_size)
def put(self, item):
self.queue.append(item)
def get(self):
return self.queue.popleft() if self.queue else None
# can报文周期是0.01s,plot的显示周期是0.2s,缓存队列的最小长度应该0.2\0.01 = 20, 保险起见,设置1.5倍的最小长度
receive_can_queue = FixedSizeQueue(35)
can_db = cantools.db.load_file("./test.dbc")
def receive_can_loop():
van_tx_bus = can.interface.Bus(interface='socketcan', channel='vcan0')
can_notifier = can.Notifier([van_tx_bus], [receive_can_queue.put])
with receive_run_condition:
receive_run_condition.wait()
can_notifier.stop()
van_tx_bus.shutdown()
timestamp_data_list = []
def real_time_plot():
global timestamp_data_list
msg = receive_can_queue.get()
while msg:
data = can_db.decode_message(frame_id_or_name=0x181d02a0, data=msg.data)
timestamp_data_list.append([msg.timestamp, data["temperature"]])
msg = receive_can_queue.get()
_timestamp_data_list = [list(t_d_i) for t_d_i in zip(*timestamp_data_list)]
if _timestamp_data_list:
now_timestamp = time.time()
_timestamp_data_list[0] = [timestamp_i - now_timestamp for timestamp_i in _timestamp_data_list[0]]
_timestamp_data_list = [[_timestamp_data_list[i][j] for j in range(len(_timestamp_data_list[i])) if
_timestamp_data_list[0][j] >= show_seconds] for
i in range(len(_timestamp_data_list))]
timestamp_data_list = [sublist for sublist in timestamp_data_list if
sublist[0] >= now_timestamp + show_seconds]
curve1.setData(_timestamp_data_list[0], _timestamp_data_list[1])
if __name__ == "__main__":
receive_thread = threading.Thread(target=receive_can_loop, args=())
receive_thread.start()
timer = pg.QtCore.QTimer()
timer.timeout.connect(real_time_plot)
# plot帧率是5Hz
timer.start(200)
pg.exec()
run_flag = False
with receive_run_condition:
receive_run_condition.notify_all()
receive_thread.join()
5. 结果显示
pyqt窗口显示
htop检查资源消耗: