新手python也不怎么会...请问是哪里出了问题!谢谢!
import image, math, pyb, sensor, struct, time
from pyb import UART
# P4 = TXD
sensor.reset() #初始化相机传感器
sensor.set_pixformat(sensor.GRAYSCALE) #设置相机模块的像素模式
sensor.set_framesize(sensor.QQVGA) #设置相机模块的帧大小
sensor.skip_frames(time = 2000) #跳过2000毫秒的帧
uart = UART(3, 115200,timeout_char = 1000)
def send_data_packet():
global packet_sequence
temp = struct.pack("<bbffffff",
0xEB,
0x90,
dx,
dy,
dz,
degrees(tag.x_rotation(),
degrees(tag.y_rotation(),
degrees(tag.z_rotation())
uart.write(temp)
# f_x 是x的像素为单位的焦距。对于标准的OpenMV,应该等于2.8/3.984*656,这个值是用毫米为单位的焦距除以x方向的感光元件的长度,乘以x方向的感光元件的像素(OV7725)
# f_y 是y的像素为单位的焦距。对于标准的OpenMV,应该等于2.8/2.952*488,这个值是用毫米为单位的焦距除以y方向的感光元件的长度,乘以y方向的感光元件的像素(OV7725)
# c_x 是图像的x中心位置
# c_y 是图像的y中心位置
#kz=实际距离/Tz
#20cm处,kz=200mm/6.85=29.197
#tz=9.89
#实际=kz*tz=9.89*200/6.85=288.759mm
f_x = (2.8 / 3.984) * 160 # 默认值
f_y = (2.8 / 2.952) * 120 # 默认值
c_x = 160 * 0.5 # 默认值(image.w * 0.5)
c_y = 120 * 0.5 # 默认值(image.h * 0.5)
def degrees(radians):
return (180 * radians) / math.pi
clock = time.clock()
while(True):
clock.tick() #开始追踪运行时间
img = sensor.snapshot() #使用相机拍摄一张照片,并返回image对象
for tag in img.find_apriltags(fx=f_x, fy=f_y, cx=c_x, cy=c_y): # 默认为TAG36H11
img.draw_rectangle(tag.rect(),color = (255, 0, 0))
img.draw_cross(tag.cx(), tag.cy(),color = (0, 255, 0))
print_args=(tag.id())
print_args = (tag.x_translation(), tag.y_translation(), tag.z_translation(), \
degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
# 位置的单位是未知的,旋转的单位是角度
k1=18.867
k2=65.74
if tag.id()==1:
k=k1
else:
k=k2
dx=k*tag.x_translation()
dy=k*tag.y_translation()
dz=k*tag.z_translation()
print_output=(dx, dy, dz, \
degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
#print("Tx: %f, Ty %f, Tz %f, Rx %f, Ry %f, Rz %f" % print_args)
#print("Tx: %f, Ty %f, Tz %f, Rx %f, Ry %f, Rz %f" % print_args)
print('dx:%f,dy:%f,dz:%f,rx:%f,ry:%f,rz:%f'%print_output+'\n',tag.id())
#print(output_str+'\n',tag.id())
else:
print(0)
请在这里粘贴代码https://singtown.com/learn/50235/