logo

串口通信输出问题,。

2019-05-06 22:29
R
_17826808312
# AprilTags Example
#
# This example shows the power of the OpenMV Cam to detect April Tags
# on the OpenMV Cam M7. The M4 versions cannot detect April Tags.

import sensor, image, time, math
from pyb import UART

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...
sensor.skip_frames(30)
sensor.set_auto_gain(False)  # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
clock = time.clock()

# 注意!与find_qrcodes不同,find_apriltags 不需要软件矫正畸变就可以工作。

# 注意,输出的姿态的单位是弧度,可以转换成角度,但是位置的单位是和你的大小有关,需要等比例换算

# 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中心位置

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

while(True):
    clock.tick()
    img = sensor.snapshot()
    for tag in img.find_apriltags(families=image.TAG25H9, 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))
        degress = tag.rotation()
        print_args = (tag.x_translation(), tag.y_translation(), tag.z_translation(), \
            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(tag.id(),degress)
        x = 0
        y = 0
        def tag_id(t):
            global x
            global y
            if (t == 0):
                x = 25
                y = 25
            elif (t == 1):
                x = 125
                y = 25
            elif (t == 2):
                x = 225
                y =25
        tag_id(tag.id())
        o = tag.x_translation()
        p = tag.y_translation()
        q = tag.z_translation()
        a = o*o + p*p + q*q
        k = 29.62962
        b = k*math.sqrt(a)
        c = b*b-10*10
        d = math.sqrt(c)
        #print(d)
        α = (180 * tag.rotation()) / math.pi
        if 0 < α < 90:
           m = x - d*math.sin(α)
           n = y + d*math.cos(α)
        if 270 < α <360:
           m = x + d*math.sin(360-α)
           n = y + d*math.cos(360-α)
        if 90 < α < 180:
           m = x - d*math.sin(180-α)
           n = y - d*math.cos(180-α)
        if 180 < α < 270:
           m = x + d*math.sin(α-180)
           n = y - d*math.cos(α-180)
        print(m, n)


uart = UART(3, 19200)

while(True):
    output_str="[%d,%d]" % (m,n)
    uart.write(output_str+'\r\n')
    time.sleep(1000)

我的目的现在是想要通过串口实时输出计算后的m,n的值,程序中到print(m,n)这一步为止都是可以正常输出的,但就是加了串口程序以后,发现串口输出不了m,n的值(单独用uart这一块程序时是可以实现串口通信输出的,这个已经尝试过也是成功的,就是和识别Apriltag的程序放一起就不行了),所以老师我这边是程序的问题吧?我不知道该怎么把上一部分的m,n的值给到下一部分串口程序然后输出?

2019-05-07 12:47
I
网红小智智 星瞳科技

你有两个死循环,第二个死循环永远执行不到。