OPENMV端代码
# main.py -- put your code here!
import pyb, sensor, image, math, time
from pyb import UART
import ustruct
from image import SEARCH_DS, SEARCH_EX
import time
import sensor, display
uart = UART(3, 115200, bits=8, parity=None, stop=1, timeout_char=1000)
roi1 = [(0, 80, 20, 10),
(20, 80, 35, 10),
(55, 80, 50, 10),
(105, 80, 35, 10),
(140, 80, 20, 10),]
led = pyb.LED(1)
led.on()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(True)
GROUND_THRESHOLD = ((0, 16, -62, 123, -19, 40))
#def send_five_uchar(c1, c2, c3, c4, c5):
# global uart;
# data = ustruct.pack("<BBBBBBBB",
# 0xa3,
# 0xb3,
# c1, c2, c3, c4, c5,
# 0xc3)
#uart.write(data);
#print(data)
while True:
data = 0
blob1 = None
blob2 = None
blob3 = None
blob4 = None
blob5 = None
flag = [0, 0, 0, 0, 0]
img = sensor.snapshot().lens_corr(strength=1.7, zoom=1.0)
blob1 = img.find_blobs([GROUND_THRESHOLD], roi=roi1[0])
blob2 = img.find_blobs([GROUND_THRESHOLD], roi=roi1[1])
blob3 = img.find_blobs([GROUND_THRESHOLD], roi=roi1[2])
blob4 = img.find_blobs([GROUND_THRESHOLD], roi=roi1[3])
blob5 = img.find_blobs([GROUND_THRESHOLD], roi=roi1[4])
if blob1:
flag[0] = 1
if blob2:
flag[1] = 1
if blob3:
flag[2] = 1
if blob4:
flag[3] = 1
if blob5:
flag[4] = 1
time.sleep_ms(10)
# send_five_uchar(flag[0], flag[1], flag[2], flag[3], flag[4])
data = bytearray([0xa3, 0xb3,flag[0],flag[1],flag[2],flag[3],flag[4],0xc3]) # 打包:帧头1 + 帧头2 + x + y + 帧尾
uart.write(data);
print(data)
for rec in roi1:
img.draw_rectangle(rec, color=(255, 0, 0))
M0端代码
#include "ti_msp_dl_config.h"
#include "oled.h"
#include "stdio.h"
int openmv_data[8]; //openmv发来的数据包
int a1=5, a2=5, a3=5,a4=5,a5=5;
int b =5;
int data_test(int data[]) //判断数据是否合理的函数
{
if(data[7]!=0xc3) return 0; //帧尾
//if(data[2]>150) return 0; //x坐标上限
//if(data[3]>110) return 0; //y坐标上限
return 1;
}
int main(void)
{
uint8_t str[64];
SYSCFG_DL_init();
NVIC_ClearPendingIRQ(UART_0_INST_INT_IRQN); //先清除一下中断,防止直接接入中断
NVIC_EnableIRQ(UART_0_INST_INT_IRQN); //开启串口的中断
OLED_Init();
OLED_Fill(0x00);
OLED_ShowStr(0,0,(unsigned char*)("MSPM0G3507"),1);
while (1)
{
sprintf((char *)str, "%d", a1);
OLED_ShowStr(0,4, str,2);
sprintf((char *)str, "%d", a2);
OLED_ShowStr(16,4, str,2);
sprintf((char *)str, "%d", a3);
OLED_ShowStr(32,4, str,2);
sprintf((char *)str, "%d", a4);
OLED_ShowStr(48,4, str,2);
sprintf((char *)str, "%d", a5);
OLED_ShowStr(64,4, str,2);
b=data_test(openmv_data);
sprintf((char *)str, "%d", b);
OLED_ShowStr(0,6, str,2);
}
}
void UART_0_INST_IRQHandler(void)
{
static int i=0;
switch (DL_UART_Main_getPendingInterrupt(UART_0_INST))
{
case DL_UART_MAIN_IIDX_RX:
DL_GPIO_togglePins(GPIO_LEDS_PORT,GPIO_LEDS_USER_LED_1_PIN | GPIO_LEDS_USER_TEST_PIN);
// gEchoData = DL_UART_Main_receiveData(UART_0_INST);
//DL_UART_Main_transmitData(UART_0_INST, gEchoData);
openmv_data[i++] = DL_UART_Main_receiveData(UART_0_INST); //接收数据
if(openmv_data[0]!=0xa3) i=0; //判断第一个帧头
if((i==2)&&(openmv_data[1]!=0xb3)) i=0; //判断第二个帧头
if(i==8) //代表一组数据传输完毕
{
i = 0;
if( data_test(openmv_data) ) //判断数据合理性
{
a1 = openmv_data[2];
a2 = openmv_data[3];
a3 = openmv_data[4];
a4 = openmv_data[5];
a5 = openmv_data[6];
}
}
break;
default:
break;
}
}
串口的配置:
串口0默认是通过数据线通信的,需要把串口0的跳线帽换一下位置,
用IO引脚的串口0在下一篇文章讲