logo

OpenMV RT运行300行代码后手动关闭时出现硬件故障

2025-10-07 15:18
V
vgsuznhmb1h3

OpenMV RT运行300行代码后手动关闭时出现硬件故障

用户在使用OpenMV RT时遇到了硬件故障问题。

  • 故障类型:硬件故障
  • 产品硬件:OpenMV RT
  • 故障现象:运行大约300行代码后手动关闭时出现问题,需要拔掉接口才能再次启动
  • 故障代码:已提供代码图片
  • 参考文章:无
  • 产品照片:已提供正面和反面照片
故障代码图片
产品照片
2025-10-07 15:25
V
vgsuznhmb1h3

代码短的就没这个问题,代码一长就有

2025-10-09 17:38
Z
星小瞳xs

您这个模组是OpenMV H7,并不是RT; 此外,这边复现问题需要您提供问题代码

2025-10-09 20:09
V
vgsuznhmb1h3
import sensor
import image
import time
from pyb import UART

# 初始化串口
uart = UART(3, 9600)
if not uart:
    print("警告:串口初始化失败!")

# 摄像头初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)
sensor.set_gainceiling(16)
sensor.set_auto_whitebal(False)

# 曝光设置
try:
    sensor.set_auto_exposure(True, exposure_us=30000)
    brightness_method = "曝光时间30ms"
except AttributeError:
    brightness_method = "默认增益模式"
except Exception as e:
    brightness_method = f"自动亮度 ({str(e)[:8]})"

clock = time.clock()

# 颜色阈值定义
color_thresholds = {
    1: (10, 81, 6, 127, 4, 127),    # 红色
    2: (30, 100, -64, -8, -32, 32), # 绿色
    3: (34, 84, -55, 127, -128, -18)# 蓝色
}
WHITE_THRESHOLD = (89, 100, -13, 10, -14, 16)

# 检测区域ROI
PHASE1_ROI = (98, 68, 139, 135)
MIDDLE_ROI = (95, 38, 138, 148)
SHAPE_ROI = (92, 18, 155, 218)

# 检测参数
PHASE1_MIN_AREA = 9000
PHASE1_MAX_AREA = 10000
MIN_CIRCLE_RADIUS = 16
MAX_CIRCLE_RADIUS = 20
CIRCLE_THRESHOLD = 2000
SHAPE_MIN_AREA = 1500
SHAPE_STAT_COUNT = 50

# 全局统计变量
shape_stats = {
    "solidity_sum": 0, "density_sum": 0, "roundness_sum": 0,
    "count": 0, "avg_solidity": 0, "avg_density": 0, "avg_roundness": 0
}

# 工具函数
def send_command(char):
    if uart:
        print(f"发送指令: {char}")
        uart.write(char)
    else:
        print("串口未初始化,无法发送")

def get_color_name(color_id):
    color_map = {1: "红色", 2: "绿色", 3: "蓝色"}
    return color_map.get(color_id, "未知")

def get_shape_name(shape_id):
    shape_map = {1: "圆柱形", 2: "圆锥形", 3: "腰鼓形"}
    return shape_map.get(shape_id, "未知")

def find_max_blob(blobs):
    if not blobs:
        return None
    max_blob = blobs[0]
    max_area = max_blob.area()
    for blob in blobs:
        if blob.area() > max_area:
            max_area = blob.area()
            max_blob = blob
    return max_blob if max_area > SHAPE_MIN_AREA else None

# 检测函数
def detect_explosive(img, color_id):
    if color_id not in color_thresholds:
        img.draw_string(10, 60, "无效颜色ID", color=(255,0,0), scale=1)
        return False
        
    img.draw_rectangle(PHASE1_ROI, color=(0,255,0), thickness=1)
    blobs = img.find_blobs(
        [color_thresholds[color_id]],
        roi=PHASE1_ROI,
        pixels_threshold=10,
        area_threshold=PHASE1_MIN_AREA,
        merge=True,
        margin=5
    )
    
    max_blob = find_max_blob(blobs)
    if not max_blob:
        img.draw_string(10, 45, "未找到排爆物", color=(255,0,0), scale=1)
        return False
        
    blob_area = max_blob.area()
    if not (PHASE1_MIN_AREA < blob_area < PHASE1_MAX_AREA):
        img.draw_string(10, 45, f"面积超限: {blob_area}", color=(255,0,0), scale=1)
        return False
        
    img.draw_rectangle(max_blob.rect(), color=(255,0,0))
    img.draw_cross(max_blob.cx(), max_blob.cy(), color=(255,0,0), size=5)
    img.draw_string(max_blob.x(), max_blob.y()+max_blob.h(), 
                   f"爆: {get_color_name(color_id)}", color=(255,255,255), scale=1)
    return True

def detect_anti_terror_target(img, color_id):
    if color_id not in color_thresholds:
        img.draw_string(10, 60, "无效靶心颜色", color=(255,0,0), scale=1)
        return False
        
    img.draw_rectangle(MIDDLE_ROI, color=(0,255,0), thickness=1)
    target_color = get_color_name(color_id)
    img.draw_string(10, 30, f"靶心: {target_color}", color=(255,255,0), scale=1)
    
    circles = img.find_circles(
        roi=MIDDLE_ROI,
        threshold=CIRCLE_THRESHOLD,
        x_margin=10,
        y_margin=10,
        r_margin=10,
        r_min=MIN_CIRCLE_RADIUS,
        r_max=MAX_CIRCLE_RADIUS,
        r_step=4
    )
    
    if not circles:
        img.draw_string(10, 45, "未检测到靶心", color=(255,0,0), scale=1)
        return False
        
    for circle in circles:
        circle_roi = (
            circle.x() - circle.r(),
            circle.y() - circle.r(),
            2 * circle.r(),
            2 * circle.r()
        )
        stats = img.get_statistics(roi=circle_roi)
        l, a, b = stats.l_mode(), stats.a_mode(), stats.b_mode()
        l_min, l_max, a_min, a_max, b_min, b_max = color_thresholds[color_id]
        
        if (l_min < l < l_max and a_min < a < a_max and b_min < b < b_max):
            img.draw_circle(circle.x(), circle.y(), circle.r(), color=(255,0,0), thickness=2)
            img.draw_cross(circle.x(), circle.y(), color=(255,0,0), size=5)
            img.draw_string(circle.x()-circle.r(), circle.y()-circle.r()-15,
                           f"靶: R={circle.r()}", color=(0,255,0), scale=1)
            return True
            
    img.draw_string(10, 45, "靶心颜色不匹配", color=(255,0,0), scale=1)
    return False

def detect_rescue_shape(img, shape_id):
    global shape_stats
    
    def update_stats(blob):
        shape_stats["solidity_sum"] += blob.solidity()
        shape_stats["density_sum"] += blob.density()
        shape_stats["roundness_sum"] += blob.roundness()
        shape_stats["count"] += 1
        
        if shape_stats["count"] >= SHAPE_STAT_COUNT:
            shape_stats["avg_solidity"] = shape_stats["solidity_sum"] / shape_stats["count"]
            shape_stats["avg_density"] = shape_stats["density_sum"] / shape_stats["count"]
            shape_stats["avg_roundness"] = shape_stats["roundness_sum"] / shape_stats["count"]
            shape_stats["solidity_sum"] = shape_stats["density_sum"] = shape_stats["roundness_sum"] = 0
            shape_stats["count"] = 0
            return True
        return False
    
    img.draw_rectangle(SHAPE_ROI, color=(0,255,0), thickness=1)
    blobs = img.find_blobs(
        [WHITE_THRESHOLD],
        roi=SHAPE_ROI,
        pixels_threshold=50,
        area_threshold=SHAPE_MIN_AREA,
        merge=True
    )
    
    max_blob = find_max_blob(blobs)
    if not max_blob:
        img.draw_string(10, 45, "未找到救援目标", color=(255,0,0), scale=1)
        img.draw_string(10, 60, f"目标: {get_shape_name(shape_id)}", color=(255,255,0), scale=1)
        return False
    
    img.draw_rectangle(max_blob.rect(), color=(0,0,255), thickness=2)
    img.draw_cross(max_blob.cx(), max_blob.cy(), color=(0,0,255), size=5)
    img.draw_string(10, 30, f"目标: {get_shape_name(shape_id)}", color=(255,255,0), scale=1)
    img.draw_string(10, 45, f"统计: {shape_stats['count']}/{SHAPE_STAT_COUNT}", color=(255,0,255), scale=1)
    
    if update_stats(max_blob):
        img.draw_string(10, 60, f"紧: {shape_stats['avg_solidity']:.2f}", color=(255,255,255), scale=1)
        img.draw_string(10, 75, f"密: {shape_stats['avg_density']:.2f}", color=(255,255,255), scale=1)
        img.draw_string(10, 90, f"圆: {shape_stats['avg_roundness']:.2f}", color=(255,255,255), scale=1)
        
        shape_match = False
        if shape_id == 1:  # 圆柱形
            if (shape_stats["avg_solidity"] > 0.90 and
                shape_stats["avg_density"] > 0.85 and
                0.40 < shape_stats["avg_roundness"] < 0.60):
                shape_match = True
        elif shape_id == 2:  # 圆锥形
            if (shape_stats["avg_solidity"] > 0.80 and
                shape_stats["avg_density"] > 0.75 and
                0.25 < shape_stats["avg_roundness"] < 0.40):
                shape_match = True
        elif shape_id == 3:  # 腰鼓形
            if (shape_stats["avg_solidity"] > 0.70 and
                shape_stats["avg_density"] > 0.65 and
                0.60 < shape_stats["avg_roundness"] < 0.80):
                shape_match = True
                
        if shape_match:
            img.draw_string(10, 105, f"匹配: {get_shape_name(shape_id)}", color=(0,255,0), scale=1)
            return True
        else:
            img.draw_string(10, 105, "形状不匹配", color=(255,0,0), scale=1)
            return False
    return False

# 主循环
current_phase = 0  # 0-二维码 1-排爆物 2-等待1 3-反恐靶 4-等待2 5-救援目标 6-完成
target_info = []
wait_start = 0
detection_cooldown = 0

while True:
    try:
        clock.tick()
        img = sensor.snapshot()
        
        # 镜头校正
        if current_phase in [1, 3, 5]:
            try:
                img.lens_corr(1.8)
            except:
                img.draw_string(10, 100, "校正错误", color=(255,0,0), scale=1)
        
        # 显示阶段信息
        phase_text = {
            0: "阶段: 二维码识别",
            1: "阶段: 排爆物检测",
            2: "等待1 (3秒)",
            3: "阶段: 反恐靶检测",
            4: "等待2 (3秒)",
            5: "阶段: 救援目标识别",
            6: "全部完成"
        }.get(current_phase, "未知")
        img.draw_string(10, 70, phase_text, color=(0,255,255), scale=1)
        img.draw_string(10, 90, f"亮度: {brightness_method}", color=(255,255,0), scale=1)
        
        # 检测冷却
        if detection_cooldown > 0:
            detection_cooldown -= 1
        else:
            detection_cooldown = 2
            
            # 阶段0: 二维码识别
            if current_phase == 0:
                try:
                    qrcodes = img.find_qrcodes()
                    for code in qrcodes:
                        img.draw_rectangle(code.rect(), color=(255,0,0))
                        payload = code.payload()
                        
                        if isinstance(payload, str) and len(payload) == 3 and payload.isdigit():
                            c1, c2, s = map(int, payload)
                            if 1<=c1<=3 and 1<=c2<=3 and 1<=s<=3:
                                target_info = [c1, c2, s]
                                current_phase = 1
                                print(f"解析成功: 排爆物{get_color_name(c1)}, 反恐靶{get_color_name(c2)}, 救援{get_shape_name(s)}")
                except Exception as e:
                    print(f"二维码错误: {e}")
                img.draw_string(10, 10, "寻找二维码...", color=(0,255,0), scale=1)
            
            # 阶段1: 排爆物检测
            elif current_phase == 1:
                if target_info:
                    color_id = target_info[0]
                    img.draw_string(10, 10, f"找{get_color_name(color_id)}排爆物...", color=(0,0,255), scale=1)
                    if detect_explosive(img, color_id):
                        send_command('a')
                        current_phase = 2
                        wait_start = time.ticks_ms()
                else:
                    img.draw_string(10, 10, "未获取排爆物信息", color=(255,0,0), scale=1)
            
            # 阶段3: 反恐靶检测
            elif current_phase == 3:
                if target_info and len(target_info)>=2:
                    color_id = target_info[1]
                    img.draw_string(10, 10, f"找{get_color_name(color_id)}反恐靶...", color=(0,0,255), scale=1)
                    if detect_anti_terror_target(img, color_id):
                        send_command('b')
                        current_phase = 4
                        wait_start = time.ticks_ms()
                else:
                    img.draw_string(10, 10, "未获取靶心信息", color=(255,0,0), scale=1)
            
            # 阶段5: 救援目标识别
            elif current_phase == 5:
                if target_info and len(target_info)>=3:
                    shape_id = target_info[2]
                    img.draw_string(10, 10, f"找{get_shape_name(shape_id)}...", color=(0,0,255), scale=1)
                    if detect_rescue_shape(img, shape_id):
                        send_command('c')
                        current_phase = 6
                else:
                    img.draw_string(10, 10, "未获取形状信息", color=(255,0,0), scale=1)
        
        # 等待阶段处理 - 修复了这里的语法错误
        if current_phase in [2, 4]:
            current_time = time.ticks_ms()
            elapsed_time = time.ticks_diff(current_time, wait_start)
            remaining_time = 3000 - elapsed_time  # 3秒等待
            
            if remaining_time > 0:
                img.draw_string(10, 10, f"等待: {remaining_time//1000}.{(remaining_time%1000)//100}秒", 
                              color=(255,255,0), scale=1)
            else:
                if current_phase == 2:
                    current_phase = 3
                elif current_phase == 4:
                    current_phase = 5
        
        # 显示FPS
        img.draw_string(10, 110, f"FPS: {clock.fps():.1f}", color=(255,255,255), scale=1)
        
    except Exception as e:
        print(f"主循环错误: {e}")
        time.sleep(100)
2025-10-10 17:35
Z
星小瞳xs

这边使用H7测试并没有出现您所描述的问题 这边观察到您的固件版本为4.5 您可以尝试更新到最新固件后尝试