11156浏览
查看: 11156|回复: 7

[高级教程] openMV机器视觉模块与麦昆小车(2)巡线的麦昆

[复制链接]
本帖最后由 潘虹辉 于 2019-9-8 10:15 编辑

这是openMV机器视觉模块的第二篇
第一篇是openMV机器视觉模块与麦昆小车(1)追球的麦昆https://mc.dfrobot.com.cn/thread-297837-1-1.html

1、结构
这次把openMV装高一点然后大约45度斜向下方,安装好了如下图
openMV机器视觉模块与麦昆小车(2)巡线的麦昆图1
2、接线
小车                   openMV
GND                  GND
+                        VIN
p1(TX)                p5(RX)
p2(RX)                p4(TX)



3、编程
首先是巡线的原理看下面的这个网址的视频https://singtown.com/learn/50037/
简单的说是摄像头采集图像,通过图像处理将线的图像通过线性回归算法计算出一个直线line,计算出线的位置line.rho和角度line.theta。
通过计算位置与角度的误差值,然后算出相应的PID值,然后相加得出总的PID调整值,就是电机的速度左右调整值。
xunx.py代码如下:
[mw_shl_code=python,true]THRESHOLD = (64, 100, -31, 29, 5, 127) # Grayscale threshold for dark things...
import sensor, image, time
from pyb import UART
from pyb import LED
from pid import PID
rho_pid = PID(p=8, i=16)
theta_pid = PID(p=1.5, i=3)
uart = UART(3, 19200, timeout_char=1000)

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,0,0,0,0)], 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(),theta_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

            uart.write("e"+str(int(output))+"\r")
            time.sleep(60)
        else:

    else:
uart.write("null\r")
        time.sleep(60)
        pass
    #print(clock.fps())
[/mw_shl_code]

mind+小车程序

openMV机器视觉模块与麦昆小车(2)巡线的麦昆图2
openMV机器视觉模块与麦昆小车(2)巡线的麦昆图3
请注意小车还是向后开的,所以左右和方向是反向的


4、要调整根据实际情况调整的参数有这几处
      a、色块的阈值
在xunx.py的这句
THRESHOLD = (64, 100, -31, 29, 5, 127)
根据你的线的颜色调整,直到连线运行程序时看到的线是白色的,其它的基本是黑色的,方法看openMV的视频教程
     b、PID参数
rho_pid = PID(p=8, i=16)
theta_pid = PID(p=1.5, i=3)


小车巡线视频如下:




201909088187.png

请叫我林辰  见习技师

发表于 2019-9-8 18:50:26

我把麦昆小车看成了麦轮小车
回复

使用道具 举报

DFB1nkb8GUS  见习技师

发表于 2019-9-8 19:27:39

NICE
回复

使用道具 举报

gada888  版主

发表于 2019-9-13 09:48:43

还有续集呀
回复

使用道具 举报

chen1xuan  见习技师

发表于 2019-9-17 20:22:38

6666
回复

使用道具 举报

rzyzzxw  版主

发表于 2019-10-4 08:29:33

好帅
回复

使用道具 举报

zing  学徒

发表于 2022-5-6 10:01:26

你这个程序是不是少一块,openmv的PID控制那块代码没有,怎么去import
回复

使用道具 举报

赤星三春牛!  初级技神

发表于 2022-5-14 20:52:16

这个项目很赞!可以巡线的。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail