基于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<b_err<40 and -30<t_err<30:
            rho_output = rho_pid.get_pid(rho_err,1)
            theta_output = theta_pid.get_pid(theta_err,1)
            output = rho_output+theta_output
            car.run(50+output, 50-output)
        else:
            car.run(0,0)
    else:
        car.run(50,-50)
        pass
    #print(clock.fps())

以下为星瞳科技的注解:

必须是元组列表。 [(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("<b",
                   data
                   )
    uart.write(data);
    print(data[0])
getp=0
GROUND_THRESHOLD1=(59, 25, 127, 19, -128, 89)
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_THRESHOLD1], roi=roi1[0])
    blob2 = img.find_blobs([GROUND_THRESHOLD1], roi=roi1[1])
    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[4] = 1
    if blob2:
        flag[3] = 1
    if blob3:
        flag[2] = 1
    if blob4:
        flag[1] = 1
    if blob5:
        flag[0] = 1
    print(flag[4],flag[3],flag[2],flag[1],flag[0])
    for i in (0,1,2,3,4):
        data|=(flag[i]<<(4-i))
    sending_data(data)
    for rec in roi1:
        img.draw_rectangle(rec, color=(255,0,0))

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

	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;

  • 4
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值