logo

只打印第二分支帧率,不打印和aprilcode 的距离是什么原因?

2023-05-25 16:11
M
agci
6376443f-6901-4d27-8847-bf52e8b60495.png
2023-05-26 19:14
I
网红小智智 星瞳科技

如果涉及代码,需要报错提示与全部代码文本,请注意不要贴代码图片

2026-08-03 11:46
M
agci

This work is licensed under the MIT license.

Copyright (c) 2013-2023 OpenMV LLC. All rights reserved.

https://github.com/openmv/openmv/blob/master/LICENSE

MAVLink AprilTags Landing Target Script.

This script sends out AprilTag detections using the MAVLink protocol to

an ArduPilot/PixHawk controller for precision landing using your OpenMV Cam.

P4 = TXD

import math import sensor import struct import time import machine

UART_BAUDRATE = 115200 MAV_system_id = 1 MAV_component_id = 0x54 packet_sequence = 0

lens_mm = 2.8 # Standard Lens. lens_to_camera_mm = 22 # Standard Lens. sensor_w_mm = 4.592 # For OV5650 sensor sensor_h_mm = 3.423 # For OV5650 sensor

Only tags with a tag ID in the dictionary below will be accepted by this

code. You may add as many tag IDs to the below dictionary as you want...

For each tag ID you need to provide then length of the black tag border

in mm. Any side of the tag black border square will work.

valid_tag_ids = { 0: 150, # 8.5" x 11" tag black border size in mm 1: 300, # 8.5" x 11" tag black border size in mm 2: 500, # 8.5" x 11" tag black border size in mm }

Camera Setup

sensor.reset() sensor.set_pixformat(sensor.GRAYSCALE) sensor.set_framesize(sensor.QQVGA) #sensor.set_framesize(sensor.QVGA) sensor.skip_frames(time=2000)

x_res = 160 # QQVGA y_res = 120 # QQVGA #x_res = 320 # QVGA #y_res = 240 # QVGA f_x = (lens_mm / sensor_w_mm) x_res f_y = (lens_mm / sensor_h_mm) y_res c_x = x_res / 2 c_y = y_res / 2 h_fov = 2 math.atan((sensor_w_mm / 2) / lens_mm) v_fov = 2 math.atan((sensor_h_mm / 2) / lens_mm)

def translation_to_mm(translation, tag_size): # translation is in decimeters... return ((translation 100) tag_size) / 210

def tag_value(tag, name): value = getattr(tag, name) return value() if callable(value) else value

def tag_area(tag): return tag_value(tag, "w") * tag_value(tag, "h")

Link Setup

uart = machine.UART(3, UART_BAUDRATE, timeout_char=1000)

https://github.com/mavlink/c_library_v1/blob/master/checksum.h

def checksum(data, extra): output = 0xFFFF for i in range(len(data)): tmp = data[i] ^ (output & 0xFF) tmp = (tmp ^ (tmp << 4)) & 0xFF output = ((output >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4)) & 0xFFFF tmp = extra ^ (output & 0xFF) tmp = (tmp ^ (tmp << 4)) & 0xFF output = ((output >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4)) & 0xFFFF return output

MAV_LANDING_TARGET_message_id = 149 MAV_LANDING_TARGET_min_distance = 1 / 100 # in meters MAV_LANDING_TARGET_max_distance = 10000 / 100 # in meters MAV_LANDING_TARGET_frame = 8 # MAV_FRAME_BODY_NED MAV_LANDING_TARGET_extra_crc = 200

http://mavlink.org/messages/common#LANDING_TARGET

https://github.com/mavlink/c_library_v1/blob/master/common/mavlink_msg_landing_target.h

def send_landing_target_packet(tag, dist_mm, w, h): global packet_sequence temp = struct.pack( "<qfffffbb", 0, ((tag_value(tag, "cx") / w) - 0.5) h_fov, ((tag_value(tag, "cy") / h) - 0.5) v_fov, min( max(dist_mm * 0.001, MAV_LANDING_TARGET_min_distance), MAV_LANDING_TARGET_max_distance, ), 0.0, 0.0, 0, MAV_LANDING_TARGET_frame, ) temp = struct.pack( "<bbbbb30s", 30, packet_sequence & 0xFF, MAV_system_id, MAV_component_id, MAV_LANDING_TARGET_message_id, temp, ) temp = struct.pack( "<b35sh", 0xFE, temp, checksum(temp, MAV_LANDING_TARGET_extra_crc) ) packet_sequence += 1 uart.write(temp)

LED control

led_success = machine.LED("LED_GREEN") led_fail = machine.LED("LED_RED") led_counter = 0

def update_led(target_found): global led_counter

if target_found:
    led = led_success
    led_fail.off()
else:
    led = led_fail
    led_success.off()

if led_counter % 4 == 0:
    led.toggle()

led_counter += 1

Main Loop

clock = time.clock() while True: clock.tick() img = sensor.snapshot() tags = sorted( img.find_apriltags(fx=f_x, fy=f_y, cx=c_x, cy=c_y), key=tag_area, reverse=True, ) target_found = False if tags: tag_id = tag_value(tags[0], "id") print("Tag ID %d" % tag_id) if tags and (tag_id in valid_tag_ids): target_found = True tag_size = valid_tag_ids[tag_id] dist_mm = math.sqrt( translation_to_mm(tag_value(tags[0], "x_translation"), tag_size) 2 + translation_to_mm(tag_value(tags[0], "y_translation"), tag_size) 2 + translation_to_mm(tag_value(tags[0], "z_translation"), tag_size) ** 2 ) send_landing_target_packet(tags[0], dist_mm, img.width(), img.height()) img.draw_rectangle(tag_value(tags[0], "rect")) #img.draw_cross(tag_value(tags[0], "cx"), tag_value(tags[0], "cy")) print("Distance %f mm - FPS %f" % (dist_mm, clock.fps())) else: print("FPS %f" % clock.fps())

update_led(target_found)
M
agci

使用这个代码检测精准降落二维码的时候,程序会卡死

M
agci

具体:如果使用

x_res = 160 # QQVGA y_res = 120 # QQVGA不会卡死

M
agci

如果使用#x_res = 320 # QVGA #y_res = 240 # QVGA 会卡死

2026-08-03 12:02
Z
星小瞳xs

用QQVGA,不要用QVGA,QVGA太占内存了,容易爆内存

2026-08-03 14:22
M
agci

你们其他的板卡有支持QVGA的吗,就这个程序而言

2026-08-04 09:41
Z
星小瞳xs

所有版本的OpenMV都支持啊,但是长时间运行都容易爆内存,所以在apritag中更推荐使用QQVGA