使用openMV3与stm32进行通讯
想用openMV与stm32通讯,在网上找了一大圈,最后决定使用串口
可以找到openMV的引脚资源图如下:
这里使用P4(USART3_TX)
与P5(USART3_RX)
脚与stm32的USART1交叉联接。
openMV的代码如下,功能是进行色块识别并把中心点传给stm32,需要注意的是对数据的打包格式,用到了ustruct.pack这个函数:
import sensor, image, time, math
from pyb import UART
import json
import ustruct
#white_threshold_01 = ((95, 100, -18, 3, -8, 4)); #白色阈值
red_threshold_01 = ((35, 100, 41, 77, 24, 59));
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False) # must be turned off for color tracking
sensor.set_auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
uart = UART(3,115200) #定义串口3变量
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
def find_max(blobs): #定义寻找色块面积最大的函数
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
def sending_data(cx,cy):
global uart;
#frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
#data = bytearray(frame)
data = ustruct.pack("<bbhhb", #格式为俩个字符俩个短整型(2字节)
0x2C, #帧头1
0x12, #帧头2
int(cx), # up s