logo

追踪色块时,小车转角过大,可能是什么原因造成的?

2019-04-19 19:59
V
bsx3
# Blob Detection Example
#
# This example shows off how to use the find_blobs function to find color
# blobs in the image. This example in particular looks for dark red objects.

import sensor, image, time#导入感光元件,图像,时钟模块
import car
from pid import PID

# You may need to tweak the above settings for tracking red things...
# Select an area in the Framebuffer to copy the color settings.

#常规设置
sensor.reset() # Initialize the camera sensor.重置图像分辨率
sensor.set_pixformat(sensor.RGB565) # use RGB565.设置图像为RGB565,彩色
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.跳过几帧,使以上设置生效
sensor.set_auto_whitebal(False) # turn this off.设置白平衡关闭;颜色识别时,要关闭白平衡,避免影响识别
clock = time.clock() # Tracks FPS.跟踪快照之间经过的毫秒数



# For color tracking to work really well you should ideally be in a very, very,
# very, controlled enviroment where the lighting is constant...

#设置图像阈值,根据需要修改
blue_threshold   = (52, 13, 34, -3, -26, -51)

'''
标准参数;
作用是如果摄像头检测到的小球的色块的像素点数>2000的话,说明我们的小车距离我们的小球很近
>2000,就使小车后退一点,和小球保持一段距离
<2000,就使小车追踪这个小车

'''
size_threshold = 2000   #自定义


#x_pid是方向pid,控制电机的方向
#如果小车转动的角度过大,可以调整'p'参数小一点
#???只传入p, i参数,imax代表???
x_pid = PID(p=0.2, i=1, imax=100)


#h_pid是距离pid,控制小车的速度
#如果小车的速度过快,可以调整'p'参数小一点

h_pid = PID(p=0.02, i=0.1, imax=50)


#find_max函数找到视野中面积最大的小球
#在视野中出现不止一个小球,找到面积最大,即最近的小球;
def find_max(blobs):
    max_size=0
    for blob in blobs:
        if blob[2]*blob[3] > max_size:
            max_blob=blob
            max_size = blob[2]*blob[3]
    return max_blob

while(True):
    clock.tick() # Track elapsed milliseconds between snapshots().跟踪快照之间经过的毫秒数

    #从sensor中截取一段图像
    img = sensor.snapshot() # Take a picture and return the image.

    #调用颜色识别的函数
    blobs = img.find_blobs([blue_threshold])
    if blobs:   #如果找到色块
        max_blob = find_max(blobs)#找到最大的色块去追踪
        #pid的差值,用于计算后面pid的参数
        x_error = max_blob[5]-img.width()/2
        h_error = max_blob[2]*max_blob[3]-size_threshold
        print("x error: ", x_error)

        '''
        for b in blobs:
            # Draw a rect around the blob.
            img.draw_rectangle(b[0:4]) # rect
            img.draw_cross(b[5], b[6]) # cx, cy
        '''

        img.draw_rectangle(max_blob[0:4]) # rect 在找到的色块上划矩形
        img.draw_cross(max_blob[5], max_blob[6]) # cx, cy 在找到的色块上划十字

        #通过传参给pid函数,得到结果,放回控制小车的参数
        x_output=x_pid.get_pid(x_error,1)#传入误差值和积分参数
        h_output=h_pid.get_pid(h_error,1)

        print("h_output",h_output)

        #小车的距离pid一致,方向pid相反
        car.run(-h_output-x_output,-h_output+x_output)

    #未找到色块则慢速原地旋转,寻找四周的色块
    else:
        car.run(0,0)

代码主要是教程里的,改了几个参数而已。

在追踪色块时,我固定好蓝色色块,让小车靠近,然后PID控制的时候有点奇怪,小车会偏向右边(或左边)直到固定的色块不在镜头范围内。

我的电机是12v620转的。

2019-04-20 09:42
I
网红小智智 星瞳科技

如果不是标准的小车,没有硬件我没办法判断。

2019-04-20 11:51
V
bsx3

硬件:电机25GA370直流减速电机,tb6612驱动芯片,电源模块,电池,openmv

e0ed8390-cd46-457a-84b0-0f37698dabc6.jpg
2019-04-20 21:54
I
网红小智智 星瞳科技

没有硬件我没办法判断。