基于openmv的巡线方法

小编用的是串口6的接口,初始化就不展示了,直接上串口接收函数(代码在后面!!!)

首先看一下星瞳科技的巡线方案(附上其开源源码):

THRESHOLD = (5, 70, -23, 15, -57, 0) # Grayscale threshold for dark things...
import sensor, image, time
from pyb import LED
import car
from pid import PID
rho_pid = PID(p=0.4, i=0)
theta_pid = PID(p=0.001, i=0)
 
LED(1).on()
LED(2).on()
LED(3).on()
 
sensor.reset()
sensor.set_vflip(True)
sensor.set_hmirror(True)
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQQVGA) # 80x60 (4,800 pixels) - O(N^2) max = 2,3040,000.
#sensor.set_windowing([0,20,80,40])
sensor.skip_frames(time = 2000)     # WARNING: If you use QQVGA it may take seconds
clock = time.clock()                # to process a frame sometimes.
 
while(True):
    clock.tick()
    img = sensor.snapshot().binary([THRESHOLD])
    line = img.get_regression([(100,100)], robust = True)
    if (line):
        rho_err = abs(line.rho())-img.width()/2
        if line.theta()>90:
            theta_err = line.theta()-180
        else:
            theta_err = line.theta()
        img.draw_line(line.line(), color = 127)
        print(rho_err,line.magnitude(),rho_err)
        if line.magnitude()>8:
            #if -40

以下为星瞳科技的注解:

必须是元组列表。 [(lo, hi), (lo, hi), ..., (lo, hi)] 定义你想追踪的颜色范围。 对于灰度图像,每个元组需要包含两个值 - 最小灰度值和最大灰度值。 仅考虑落在这些阈值之间的像素区域。 对于RGB565图像,每个元组需要有六个值(l_lo,l_hi,a_lo,a_hi,b_lo,b_hi) - 分别是LAB L,A和B通道的最小值和最大值。 为方便使用,此功能将自动修复交换的最小值和最大值。 此外,如果元组大于六个值,则忽略其余值。相反,如果元组太短,则假定其余阈值处于最大范围。


星瞳科技利用这两个值的方式,感觉效果不太好:

附上我自己的代码:

import pyb, sensor, image, math, time
from pyb import UART
import ustruct
from image import SEARCH_EX, SEARCH_DS
sensor.set_contrast(1)
sensor.set_gainceiling(16)
clock = time.clock()
uart = UART(3,115200,bits=8, parity=None, stop=1, timeout_char = 1000)
roi1 =	 [(13, 40, 20, 50),
            (42,40, 15, 40),
            (65,40,16,16),
            (89,40,15,40),
            (111,40,20,50)]
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_whitebal(True)
sensor.set_auto_gain(False)
sensor.set_vflip(False)
sensor.set_hmirror(False)
clock = time.clock()
GROUND_THRESHOLD=(59, 25, 127, 19, -128, 89)
def sending_data(data):
    global uart;
    data = ustruct.pack("

当然,既然有了巡线的代码,肯定还要有接收函数呀:

	a = USART6->DR;
		R2 = ((u8)a & 0x10) >> 4;
		R1 = ((u8)a & 0x08) >> 3;
		M  = ((u8)a & 0x04) >> 2;
		L1 = ((u8)a & 0x02) >> 1;
		L2 = (u8)a & 0x01;

你可能感兴趣的:(stm32,单片机)