潘虹辉 发表于 2019-9-8 10:15:15

openMV机器视觉模块与麦昆小车(2)巡线的麦昆

本帖最后由 潘虹辉 于 2019-9-8 10:15 编辑

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

1、结构
这次把openMV装高一点然后大约45度斜向下方,安装好了如下图

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代码如下:
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()
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()
    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())


mind+小车程序



请注意小车还是向后开的,所以左右和方向是反向的


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)


小车巡线视频如下:
http://player.youku.com/embed/XNDM1MzI0MjUyNA==



请叫我林辰 发表于 2019-9-8 18:50:26

我把麦昆小车看成了麦轮小车{:5_116:}

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

这个项目很赞!可以巡线的。
页: [1]
查看完整版本: openMV机器视觉模块与麦昆小车(2)巡线的麦昆