上面的帖子,格式有点不对。这里重新整理了一下。请看下面: # 色块监测 例子 # # 这个例子展示了如何通过find_blobs()函数来查找图像中的色块 # 这个例子查找的颜色是深绿色 import sensor, image, time import time, omv from pyb import CAN import math from machine import LED # 颜色追踪的例子,一定要控制环境的光,保持光线是稳定的。 #green_threshold = ( 0, 80, -70, -10, -0, 30) #green_threshold = (24, 49, -43, 5, -29, -10) # blue #green_threshold = (0, 44, -82, -10, -64, 27) # dark green green_threshold = (95,100,-11,23,-7,40) # white #设置绿色的阈值,括号里面的数值分别是L A B 的最大值和最小值(minL, maxL, minA, # maxA, minB, maxB),LAB的值在图像左侧三个坐标图中选取。如果是灰度图,则只需 #设置(min, max)两个数字即可。 #---------------------------------------------------- # CAN recv callback function rxbuf = bytearray(8) rxlist = [0, 0, 0, 0, memoryview(rxbuf)] def canrx_callback(bus, reason): #print(bus) can.recv(0, rxlist, timeout=-1) can.send(txbuf, txid, timeout=-1) print(rxbuf[0]) if reason == 0: print('pending') if reason == 1: print('full') if reason == 2: print('overflow') #---------------------------------------------------- sensor.reset() # 初始化摄像头 sensor.set_pixformat(sensor.RGB565) # 格式为 RGB565. #sensor.set_framesize(sensor.QQVGA) # QQVGA:160*120 使用 QQVGA 速度快一些 #sensor.set_framesize(sensor.QVGA) # QVGA:320*240 sensor.set_framesize(sensor.VGA) # VGA:640*480 sensor.skip_frames(time = 2000) # 跳过2000s,使新设置生效,并自动调节白平衡 sensor.set_auto_gain(False) # 关闭自动自动增益。默认开启的,在颜色识别中,一定要关闭白平衡。 sensor.set_auto_whitebal(False)#关闭白平衡。白平衡是默认开启的,在颜色识别中,一定要关闭白平衡。 clock = time.clock() # 追踪帧率 led = LED("LED_BLUE") # 注意:在接收节点上设置为False。 TRANSMITTER = True can = CAN(2, CAN.NORMAL) # 设置不同的波特率(默认为125Kbps) # 注意:以下参数仅适用于H7。 # can.init(CAN.NORMAL, prescaler=32, sjw=1, bs1=8, bs2=3) # 125Kbps can.init(CAN.NORMAL, prescaler=16, sjw=1, bs1=8, bs2=3) # 250Kbps # can.init(CAN.NORMAL, prescaler=8, sjw=1, bs1=8, bs2=3) # 500Kbps # can.init(CAN.NORMAL, prescaler=4, sjw=1, bs1=8, bs2=3) # 1000Kbps # # Set a filter to receive messages with id=1 -> 4 # Filter index, mode (RANGE, DUAL or MASK), FIFO (0 or 1), params can.setfilter(0, CAN.RANGE, 0, (0x100, 0x101)) # Set a filter to receive messages with id=1, 2, 3 and 4 # Filter index, mode (LIST16, etc..), FIFO (0 or 1), params # can.setfilter(0, CAN.LIST16, 0, (0x100, 0x200)) # can.restart() # # Receive messages on FIFO 0 #can.recv(0, timeout=10000) txid = 0x100 txbuf = bytearray([1,2,3,4,5,6,7,8]) can.rxcallback(0, canrx_callback) b = None while(True): led.on() time.sleep_ms(30) led.off() time.sleep_ms(30) clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot()#.lens_corr(1.8) # 从感光芯片获得一张图像 blobs = img.find_blobs([green_threshold], merge=True,area_threshold=25) #find_blobs(thresholds, invert=False, roi=Auto),thresholds为颜色阈值, #是一个元组,需要用括号[ ]括起来。invert=1,反转颜色阈值,invert=False默认 #不反转。roi设置颜色识别的视野区域,roi是一个元组, roi = (x, y, w, h),代表 #从左上顶点(x,y)开始的宽为w高为h的矩形区域,roi不设置的话默认为整个图像视野。 #这个函数返回一个列表,[0]代表识别到的目标颜色区域左上顶点的x坐标,[1]代表 #左上顶点y坐标,[2]代表目标区域的宽,[3]代表目标区域的高,[4]代表目标 #区域像素点的个数,[5]代表目标区域的中心点x坐标,[6]代表目标区域中心点y坐标, #[7]代表目标颜色区域的旋转角度(是弧度值,浮点型,列表其他元素是整型), #[8]代表与此目标区域交叉的目标个数,[9]代表颜色的编号(它可以用来分辨这个 #区域是用哪个颜色阈值threshold识别出来的)。 if blobs: #如果找到了目标颜色 for b in blobs: #迭代找到的目标颜色区域 # Draw a rect around the blob. img.draw_rectangle(b[0:4]) # rect #用矩形标记出目标颜色区域 img.draw_cross(b[5], b[6],color=(255,0,0),thickness=1) # cx, cy #在目标颜色区域的中心画十字形标记 #print(b.rotation()*180/math.pi) if b.pixels() > 1000: print(b[5],b[6],b.pixels(),b.count(),b.area(),b.density()) #print(clock.fps()) # 注意: 你的OpenMV连到电脑后帧率大概为原来的一半 # CAN 数据发送 #Send message with id 1 if b is not None: txbuf[0] = b[5] txbuf[1] = b[5]>>8 txbuf[2] = b[6] txbuf[3] = b[6]>>8 #print(can.state()==CAN.ERROR_ACTIVE) #print(CAN.STOPPED) #can.send(txbuf, txid, timeout=1000) #print(can.state()) # 在以下调用中未分配堆内存 #can.recv(0, lst, timeout=-1) #print(lst[4][0]) 问题描述: 1、can.recv(0, timeout=1000),如果设置了超时时间,超时时间到,直接死机。 2、can.send(txbuf, txid, timeout=1000),如果设置了超时时间,时间到直接死机。 为了解决这个问题,我只能在回调函数中调用can.recv(...),例如使用can.rxcallback(0, canrx_callback),再定义函数如下: def canrx_callback(bus, reason): #print(bus) can.recv(0, rxlist, timeout=-1) can.send(txbuf, txid, timeout=-1) 意思就是,上位机发送一个can指令,openMV收到数据,就回复一帧数据。 然而,CAN总线的鲁棒性还是很差。 3、CAN总线的CANH,CANL两根线短时间短接,再恢复,直接死机。(鲁棒性测试) 4、CAN总线的其中一根线断开,再连接上,直接死机。(鲁棒性测试) 关于timeout=-1,我认为是无限等待,官方资料没有写。官方资料还是有点不详细的。 期待能够提高CAN的鲁棒性,如果有改进,希望能给我发送一个邮件通知我,30175224@qq.com