diff --git a/main.py b/main.py index f442efa..aa3026f 100644 --- a/main.py +++ b/main.py @@ -60,7 +60,7 @@ pinmap.set_pin_function("P21", "I2C1_SDA") # pinmap.set_pin_function("A15", "I2C5_SCL") # pinmap.set_pin_function("A27", "I2C5_SDA")#ota升级要修改的 # ADC 触发阈值(用于检测扳机/激光触发) -ADC_TRIGGER_THRESHOLD = 3000 +ADC_TRIGGER_THRESHOLD = 2450 ADC_LASER_THRESHOLD = 3000 # 显示参数:激光十字线样式 @@ -565,6 +565,18 @@ def compute_laser_position(circle_center, laser_point, radius, method): dy = ly - cy return dx / (circle_r / 100.0), -dy / (circle_r / 100.0) +def compute_laser_position_v2(circle_center, laser_point): + print(f"circle_center : {circle_center}") + cx, cy = circle_center + lx, ly = 320,230 + # lx, ly = laser_point + dx = lx - cx + dy = ly - cy + r = 22.16 * 5 + target_x = dx/r*100 + target_y = dy/r*100 + print(f"lx:{lx} ly: {ly} cx: {cx} cy: {cy} dx: {dx} dy: {dy} result_x: {target_x} result_y: {-target_y}") + return (target_x, -target_y) # ==================== TCP 通信线程 ==================== @@ -790,7 +802,10 @@ def cmd_str(): while not app.need_exit(): current_time = time.ticks_ms() # print("压力传感器数值: ", adc_obj.read()) - if adc_obj.read() > ADC_TRIGGER_THRESHOLD: + adc_val = adc_obj.read() + if adc_val > 2450: + print(f"adc_val: {adc_val}") + if adc_val > ADC_TRIGGER_THRESHOLD: diff_ms = current_time-last_adc_trigger if diff_ms<3000: continue @@ -809,7 +824,22 @@ def cmd_str(): disp.show(result_img) # 计算偏移与距离 - dx, dy = compute_laser_position(center, (x, y), radius, method) + # dx, dy = compute_laser_position(center, (x, y), radius, method) + if center is None: + print(f"程序异常或者离谱脱靶") + # 构造上报数据 + inner_data = { + "x": 200.0, + "y": 200.0, + "r": 90.0, + "d": 0.0, # 距离(厘米) + "m": 'error', + 'adc': adc_val, + } + report_data = {"cmd": 1, "data": inner_data} + tcp_send_raw(make_packet(2, report_data)) + continue + dx, dy = compute_laser_position_v2(center, (x, y)) distance_m = estimate_distance(best_radius1) # 读取电量 @@ -817,12 +847,12 @@ def cmd_str(): battery_percent = voltage_to_percent(voltage) # 保存图像(带标注) - try: - jpg_count = len([f for f in os.listdir(photo_dir) if f.endswith('.jpg')]) - filename = f"{photo_dir}/{int(x)}_{int(y)}_{round((distance_m or 0.0) * 100)}_{method}_{jpg_count:04d}.jpg" - result_img.save(filename, quality=70) - except Exception as e: - print(f"❌ 保存失败: {e}") + # try: + # jpg_count = len([f for f in os.listdir(photo_dir) if f.endswith('.jpg')]) + # filename = f"{photo_dir}/{int(x)}_{int(y)}_{round((distance_m or 0.0) * 100)}_{method}_{jpg_count:04d}.jpg" + # result_img.save(filename, quality=70) + # except Exception as e: + # print(f"❌ 保存失败: {e}") # 构造上报数据 inner_data = { @@ -830,10 +860,12 @@ def cmd_str(): "y": float(dy) if dy is not None else 200.0, "r": 90.0, "d": round((distance_m or 0.0) * 100), # 距离(厘米) - "m": method + "m": method, + 'adc': adc_val, } report_data = {"cmd": 1, "data": inner_data} - safe_enqueue(report_data) + # safe_enqueue(report_data) + tcp_send_raw(make_packet(2, report_data)) print("📤 射箭事件已加入发送队列") time.sleep_ms(100)