logo

这个代码是要实现云台追踪应该怎么修改?

2023-08-02 19:23
U
lw3f

import sensor, image, time, math

threshold=(60, 255, -20, 20, -20, 20) # 激光灯在纸上颜色的阈值,可以执行调节

from pyb import Pin

sensor.reset()

sensor.set_auto_gain(False)

sensor.set_pixformat(sensor.GRAYSCALE) # or sensor.RGB565

sensor.set_framesize(sensor. QVGA) # or sensor.QVGA (or others)

sensor.skip_frames(time=900) # Let new settings take affect.

sensor.set_auto_exposure(False, 1000)#在这里调节曝光度,调节完可以比较清晰地看清激光点

sensor.set_auto_whitebal(False) # turn this off.

sensor.set_auto_gain(False) # 关闭增益(色块识别时必须要关)

clock = time.clock() # Tracks FPS.

def color_blob(threshold):

blobs = img.find_blobs([threshold,x_stride=1, y_stride=1, area_threshold=0, pixels_threshold=0,merge=False,margin=1])

if len(blobs)>=1 :#有色块

# Draw a rect around the blob.

b = blobs[0]

img.draw_rectangle(b[0:4]) # rect

cx = b[5]

cy = b[6]

for i in range(len(blobs)-1):

img.draw_rectangle(b[0:4]) # rect

cx = blobs[i][5]+cx

cy = blobs[i][6]+cy

cx=int(cx/len(blobs))

cy=int(cy/len(blobs))

img.draw_cross(cx, cy) # cx, cy

print(cx,cy)

return int(cx), int(cy)

return -1, -1 #表示没有找到

while(True):

clock.tick() # Track elapsed milliseconds between snapshots().

img = sensor.snapshot() # Take a picture and return the image.

blobs = img.find_blobs([threshold])

if blobs:

max_blob = color_blob(threshold)

pan_error = max_blob.cx()-img.width()/2

tilt_error = max_blob.cy()-img.height()/2

print("pan_error: ", pan_error)

img.draw_rectangle(max_blob.rect()) # rect

img.draw_cross(max_blob.cx(), max_blob.cy()) # cx, cy

pan_output=pan_pid.get_pid(pan_error,1)/2

tilt_output=tilt_pid.get_pid(tilt_error,1)

print("pan_output",pan_output)

pan_servo.angle(pan_servo.angle()+pan_output)

tilt_servo.angle(tilt_servo.angle()-tilt_output)

2023-08-02 22:48
I
网红小智智 星瞳科技

https://singtown.com/learn/49603/