This chapter mainly explains how to use OpenMV tracking and the communication between OpenMV and STM32 serial port
foreword
Software: STM32CubeMx, Keil5 MDK, serial debugging assistant XCOM, OpenMV_IDE
Hardware: OpenMV, STM32F103C8T6 core board, downloader ST_LINK, USB to TTL or J-LINK, a car
Introduction to OpenMV
OpenMV is an open source, powerful machine vision module.
It takes STM32F427CPU as the core and integrates OV7725 camera chip. On the compact hardware module, it implements the core machine vision algorithm efficiently in C language and provides Python programming interface. Its machine vision algorithms include finding color patches, face detection, eye tracking, edge detection, sign tracking, and more.
Tracking method
The effect of running is shown in the figure
This method is similar to the principle of the general tracking module.
Use the red frame to segment the image. These frames are used as the basis for us to follow the line. When the color that appears in the frame meets the set threshold, it is equivalent to detecting the track line.
The convenience of using OpenMV to track is that lines of various colors can be found. You only need to modify the corresponding threshold value, and OpenMV can also complete the task of recognizing numbers or objects at the same time. For details, please refer to the star Hitomi Technology official website .
Implementation
Next we will show how to implement OpenMV tracking.
First, let’s set the size and position of the frame captured by our camera on the screen
for rec in roi1:
img.draw_rectangle(rec, color=(255,0,0))#绘制出roi区域 就画出框
Draw 5 detection areas, corresponding to five-way tracking
roi1 = [(0, 40, 20, 40), # L1 x y w h 每个对应画的框的位置,最后两个是框的大小
(35, 40, 20, 40), # L2
(70,40,10,10), # M0
(105,40,20,40), # R1
(140,40,20,40)] # R2
Set the threshold value of the color to be recognized. Below is the threshold code of the black line. If you want to follow lines of other colors, you need to modify it to the threshold value of the corresponding color.
GROUND_THRESHOLD=(21, 100, -124, 32, -26, 22)#黑线 设置要寻的线的阈值。在颜色空间中,阈值被用于定义需要跟踪的颜色
main function part
Returns 1 if the line is detected, otherwise returns 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_THRESHOLD1], roi=roi1[0]) #left
blob2 = img.find_blobs([GROUND_THRESHOLD1], roi=roi1[1]) #middle
blob3 = img.find_blobs([GROUND_THRESHOLD1], roi=roi1[2])
blob4 = img.find_blobs([GROUND_THRESHOLD1], roi=roi1[3])
blob5 = img.find_blobs([GROUND_THRESHOLD1], 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 #右边检测到黑线
Serial communication part
It is often not enough to use OpenMV to control the track of the car. We generally use OpenMV to process the track image, send the processed data to the STM32 through the serial port, and use the STM32 to control the car.
Its purpose is to tell the data observed by OpenMV to STM32, so that the car can respond accordingly.
code show as below:
Initialize serial port 3, baud rate 115200, 8 bit, no parity, 1 stop bit, overflow time 1000ms
uart = UART(3,115200,bits=8, parity=None, stop=1, timeout_char = 1000) #初始化串口
Process the data, convert the binary number of the array into a hexadecimal number, package and send
data=0
for i in (0,1,2,3,4): # 0 1 2 3 4 把从最左边到最右边,检测到的记1,然后转化成16进制
data|=(flag[i]<<(4-i))
Send in a certain protocol format
def sending_data(data): #A5 A6 data
global uart;
data = ustruct.pack("<bbb", #格式为俩个字符俩个短整型(2字节)
0xA5, #帧头1
0xA6,
data
) #数组大小为7,其中2,3,4,5为有效数据,0,1,6为帧头帧尾
uart.write(data); #必须要传入一个字节数组
STM32 serial port analysis
Use the interrupt callback function to analyze the data. For the specific configuration of the serial port, please refer to the previous tutorial STM32CubeMx tutorial (3) - serial communication, serial protocol
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
UNUSED(huart);
if(huart->Instance == USART1)
{
rec_dat[rec_pointer++] = rec_buf;
if(rec_pointer == 3)//接收完成
{
rec_pointer = 0;
if(rec_dat[0] == 0xA5 && rec_dat[1] == 0xA6)//判断帧头
sensor_val = rec_dat[2];
else
memset(rec_dat,'\0',sizeof(rec_dat));
}
HAL_UART_Receive_IT(&huart1,(uint8_t *)&rec_buf,1);
}
}
Connect STM32 with OpenMV
OpenMV_RX P5 connects to STM32_TX1 PA9
OpenMV_TX P4 connects to STM32_RX1 PA10
OpenMV_GND connects to STM32_GND
Enter Debug to observe the change of sensor_val, according to the change of sensor_val, drive the speed of left and right wheels to track along the track line. For the specific tracking part, please refer to previous tutorials.