@shuc 在 求问如何将这种坐标值来传输进32单片机里面进行通信?不使用字典 中说:
这里面也要咋去修改呀,换成列表list吗
@shuc 在 求问如何将这种坐标值来传输进32单片机里面进行通信?不使用字典 中说:
这里面也要咋去修改呀,换成列表list吗
from machine import Pin
from pyb import UART
import sensor, image, time,math,pyb
#import seekfree, pyb
import pyb
import json
import ustruct
laser_light=Pin("P9", Pin.OUT)
laser_light.value(1)
#lcd = seekfree.LCD180(3)
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 设置图像色彩格式为RGB565格式
sensor.set_framesize(sensor.QQVGA) # 设置图像大小为160*120
sensor.set_auto_whitebal(True) # 设置自动白平衡
sensor.set_brightness(3000) # 设置亮度为3000
sensor.skip_frames(time = 20) # 跳过帧
sensor.set_auto_gain(False)
clock = time.clock()
corner = 0
uart = UART(3,115200)
uart.init(115200, bits=8, parity=None, stop=1)
def sending_data(corner1_str,corner2_str,corner3_str,corner4_str):
global uart;
data = ustruct.pack("<bbhhhhb",
0x2C,
0x12,
str(corner1_str),
str(corner2_str),
str(corner3_str),
str(corner4_str),
0x5B)
uart.write(data);
while(True):
clock.tick()
img = sensor.snapshot()
# 在图像中寻找矩形
for r in img.find_rects(threshold = 10000):
# 判断矩形边长是否符合要求
if r.w() > 20 and r.h() > 20:
# 在屏幕上框出矩形
img.draw_rectangle(r.rect(), color = (255, 0, 0), scale = 4)
# 获取矩形角点位置
corner = r.corners()
# 在屏幕上圈出矩形角点
img.draw_circle(corner[0][0], corner[0][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(corner[1][0], corner[1][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(corner[2][0], corner[2][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(corner[3][0], corner[3][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
corner4_str = f"corner4 = ({corner[0][0]},{corner[0][1]})"
corner3_str = f"corner3 = ({corner[1][0]},{corner[1][1]})"
corner2_str = f"corner2 = ({corner[2][0]},{corner[2][1]})"
corner1_str = f"corner1 = ({corner[3][0]},{corner[3][1]})"
data = bytearray([0x2C,0x12,corner1_str,corner2_str,corner3_str,corner4_str,0x5B])
uart.write(data)
print(corner1_str + "\n" + corner2_str + "\n" + corner3_str + "\n" + corner4_str)
# 设置激光颜色阈值
red_td = [(56, 100, 45, 127, -128, 127)]
# 根据阈值找到色块
for b in img.find_blobs(red_td,pixels_threshold=2, area_threshold=15, merge=True,invert = 0):
# 在屏幕上画出色块
img.draw_rectangle(b.rect(), color = (0, 255, 0), scale = 2, thickness = 2)
print(f"rect = {b.x() + b.w()/2},{b.y() + b.h()/2}")
import sensor, image, time
from pid import PID
from pyb import Servo
pan_servo=Servo(1)
tilt_servo=Servo(2)
pan_servo.calibration(500,2500,500)
tilt_servo.calibration(500,2500,500)
red_threshold = (13, 49, 18, 61, 6, 47)
pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
#tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
sensor.reset() # Initialize the camera sensor.
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) # 关闭增益(色块识别时必须要关)
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().
img = sensor.snapshot() # Take a picture and return the image.
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 #表示没有找到
import sensor, image, time,math,pyb
from pid import PID
from pyb import Servo
from pyb import UART,LED
import json
import ustruct
pan_servo=Servo(1) #定义两个舵机
tilt_servo=Servo(2)
pan_servo.calibration(500,2500,500)
tilt_servo.calibration(500,2500,500)
red_threshold_01 = ((14, 88, 24, 116, -18, 106));
bule_threshold_01 = ((16, 65, -20, 35, -64, -21));
yello_threshold_01 = ((53, 71, -14, 0, -7, 22));
pan_pid = PID(p=0.07, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
tilt_pid = PID(p=0.05, i=0, imax=90) #脱机运行或者禁用图像传输,使用这个PID
#pan_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
#tilt_pid = PID(p=0.1, i=0, imax=90)#在线调试使用这个PID
sensor.reset() # Initialize the camera sensor.
sensor.set_pixformat(sensor.RGB565) # use RGB565.
sensor.set_framesize(sensor.QQVGA) # use QQVGA for speed.
sensor.skip_frames(10) # Let new settings take affect.
sensor.set_auto_gain(False) # 关闭自动增益
sensor.set_auto_whitebal(False) # turn this off.关闭白平衡
#sensor.set_vflip(True)#垂直方向翻转
clock = time.clock() # Tracks FPS.
uart = UART(3,115200)
uart.init(115200, bits=8, parity=None, stop=1)
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
def sending_data(flag):
global uart;
data = ustruct.pack("<bbhb",
0x2C,
0x12,
int(flag),
0x5B)
uart.write(data);
while(True):
flag = None
clock.tick()
img = sensor.snapshot()
blobs = img.find_blobs([red_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10); #红色物块
blobs1 = img.find_blobs([bule_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10); #绿色物块
blobs2 = img.find_blobs([yello_threshold_01], pixels_threshold=100, area_threshold=100, merge=True, margin=10); #黄色物块
if blobs:
max_blob = find_max(blobs)#判断最大的色块为哪一个
#如果找到了目标颜色
print("red")
flag = 1
for b in blobs:
#迭代找到的目标颜色区域
# Draw a rect around the blob.
img.draw_rectangle(max_blob.rect())#img.draw_rectangle(b[0:4]) # rect
#用矩形标记出目标颜色区域
img.draw_cross(max_blob.cx(), max_blob.cy())#img.draw_cross(b[5], b[6]) # cx, cy
#在目标颜色区域的中心画十字形标记
data = bytearray([0x2C,0x12,flag,0x5B])
uart.write(data)
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)
elif blobs1:
max_blob = find_max(blobs)#判断最大的色块为哪一个
#如果找到了目标颜色
print("bule")
flag = 2
for b in blobs1:
#迭代找到的目标颜色区域
# Draw a rect around the blob.
img.draw_rectangle(max_blob.rect())#img.draw_rectangle(b[0:4]) # rect
#用矩形标记出目标颜色区域
img.draw_cross(max_blob.cx(), max_blob.cy())#img.draw_cross(b[5], b[6]) # cx, cy
#在目标颜色区域的中心画十字形标记
data = bytearray([0x2C,0x12,flag,0x5B])
uart.write(data)
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)
elif blobs2:
max_blob = find_max(blobs)#判断最大的色块为哪一个
#如果找到了目标颜色
print("yello")
flag = 1
for b in blobs2:
#迭代找到的目标颜色区域
# Draw a rect around the blob.
img.draw_rectangle(max_blob.rect())#img.draw_rectangle(b[0:4]) # rect
#用矩形标记出目标颜色区域
img.draw_cross(max_blob.cx(), max_blob.cy())#img.draw_cross(b[5], b[6]) # cx, cy
#在目标颜色区域的中心画十字形标记
data = bytearray([0x2C,0x12,flag,0x5B])
uart.write(data)
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)
import sensor, image, time,math,pyb
from pyb import UART,LED
import json
import ustruct
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time = 2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
red_threshold_01=(10, 100, 127, 32, -43, 67)
clock = time.clock()
uart = UART(3,115200)
uart.init(115200, bits=8, parity=None, stop=1)
def find_max(blobs):
max_size=0
for blob in blobs:
if blob.pixels() > max_size:
max_blob=blob
max_size = blob.pixels()
return max_blob
def sending_data(flag):
global uart;
data = ustruct.pack("<bbhb",
0x2C,
0x12,
str(flag),
0x5B)
uart.write(data);
while(True):
flag = 0
clock.tick()
img = sensor.snapshot()
blobs = img.find_blobs([red_threshold_01])
cx=0;cy=0;
if blobs:
max_b = find_max(blobs)
cx=max_b[5]
cy=max_b[6]
cw=max_b[2]
ch=max_b[3]
flag = "red"
img.draw_rectangle(max_b[0:4])
img.draw_cross(max_b[5], max_b[6])
FH = bytearray([0x2C,0x12,str(flag),0x5B])
uart.write(FH)
print(flag)