stm32、openmv、电脑之间的通讯
stm32、openmv、电脑之间的通讯
效果展示
图片为传输的最后一次数据,B6对应182,82对应130。
python
下面是openmv中的代码,识别屏幕中间色块后追踪x,y坐标并发送至电脑
import sensor, image, time, math
from pyb import UART
import json
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变量
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
r = [(320//2)-(50//2), (240//2)-(50//2), 50, 50] # 50x50 center of QVGA.
for i in range(60):
img = sensor.snapshot()
img.draw_rectangle(r)
threshold = [50, 50, 0, 0, 0, 0] # Middle L, A, B values.
for i in range(60):
img = sensor.snapshot()
hist = img.get_histogram(roi=r)
lo = hist.get_percentile(0.01) # Get the CDF of the histogram at the 1% range (ADJUST AS NECESSARY)!
hi = hist.get_percentile(0.99) # Get the CDF of the histogram at the 99% range (ADJUST AS NECESSARY)!
# Average in percentile values.
threshold[0] = (threshold[0] + lo.l_value()) // 2
threshold[1] = (threshold[1] + hi.l_value()) // 2
threshold[2] = (threshold[2] + lo.a_value()) // 2
threshold[3] = (threshold[3] + hi.a_value()) // 2
threshold[4] = (threshold[4] + lo.b_value()) // 2
threshold[5] = (threshold[5] + hi.b_value()) // 2
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
img.draw_rectangle(r)
while(True):
clock.tick()
img = sensor.snapshot()
for blob in img.find_blobs([threshold], pixels_threshold=100, area_threshold=100, merge=True, margin=10):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
print(blob.cx(), blob.cy())
img_data = bytearray([0x2C,18,blob.cx(),blob.cy()])
uart.write(img_data)
c
下面是keil中主程序
#include <stm32f10x.h>
#include "delay.h"
#include "sys.h"
#include "lcd.h"
#include "usart.h"
#include "stdio.h"
static u8 openmv[18]; //存取数据
static u8 state = 0;
static u8 bit_number=0;
void Openmv_Receive_Data(int16_t data)//接收Openmv传过来的数据
{
if(state==0&&data==0x2C){
state=1;
openmv[bit_number++]=data;
}
else if(state==1&&data==18){
state=2;
openmv[bit_number++]=data;
}
else if(state==2){
openmv[bit_number++]=data;
if(bit_number>=17){
state=3;
}
}
else if(state==3) //检测是否接受到结束标志
{
if(data == 0x5B){
state = 0;
openmv[bit_number++]=data;
}
else if(data != 0x5B){
state = 0;
for(int i=0;i<18;i++){
openmv[i]=0x00;
}
}
}
else{
state = 0;
bit_number=0;
for(int i=0;i<18;i++){
openmv[i]=0x00;
}
}
}
static u8 com_data;
int flag=0;
void USART2_IRQHandler(void) //串口4全局中断服务函数
{
//接收中断
if( USART_GetITStatus(USART2,USART_IT_RXNE) ){
USART_ClearITPendingBit(USART2,USART_IT_RXNE);//清除中断标志
com_data = USART2->DR;
Openmv_Receive_Data(com_data);//openmv数据处理函数
}
}
int main(void){
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(115200); //串口初始化为115200
LCD_Init();
POINT_COLOR=RED;
while(1) {
/*LCD_ShowNum(0,40,X_black_data,10,24);
LCD_ShowNum(0,70,Y_black_data,10,24);
LCD_ShowNum(0,90,X_red_data,10,24);
LCD_ShowNum(0,110,Y_red_data,10,24);
LCD_ShowNum(0,130,X_yellow_data,10,24);
LCD_ShowNum(0,150,Y_yellow_data,10,24); */
LCD_ShowNum(0,20,openmv[2],10,24);
LCD_ShowNum(0,40,openmv[3],10,24);
USART_SendData(USART1, openmv[2]);//向串口1发送数据
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
USART_SendData(USART1, openmv[3]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);//等待发送结束
USART_RX_STA=0;
}
}
头函数使用正点原子库函数版本,只做了少量简单修改,不在此展示