这个代码的串口通信有问题吗?ttl口不接收?
-
请在这里import sensor, image, time from machine import UART import math # 颜色阈值 thr_write = (60, 100, -20, 20, -20, 20) # 白色阈值 thr_black = (0, 35, -20, 20, -20, 20) # 黑色阈值 # 初始化摄像头 sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.VGA) sensor.skip_frames(time=2000) sensor.set_auto_gain(False) # 关闭自动增益 sensor.set_auto_whitebal(False) # 关闭自动白平衡 clock = time.clock() # 初始化串口 (与main.py保持一致) uart = UART(3, 115200) uart.init(115200, bits=8, parity=None, stop=1) # 明确设置串口参数 # 图像中心坐标 img_center_x = sensor.width() // 2 img_center_y = sensor.height() // 2 # PID参数 Kp = 0.9 # 比例增益 Ki = 0 # 积分增益 Kd = 0 # 微分增益 # 发送数据相关变量 (与main.py保持一致) Servo_dx = 0 Servo_dy = 0 Servo_data = [0,0] # PID变量 last_error_x = 0 integral_x = 0 last_error_y = 0 integral_y = 0 # 通信协议定义 (改进版) HEADER = b'\xFF' # 包头 (字节类型) FOOTER = b'\xFE' # 包尾 (字节类型) # 目标参数 target_offset = [0, 0] # 目标补偿量 target = [0, 0] # 目标坐标 view_offset = [0, 0] # 视角偏移补偿 target_valid = False # 目标有效性标志 # 发送数据包函数 (修复版) def send_packet(data): try: # 将数据转换为字节 data_bytes = bytes(data) # 计算数据长度 (新增) length = len(data_bytes).to_bytes(1, 'big') # 按协议格式拼接数据包: 包头 + 长度 + 数据 + 包尾 packet = HEADER + length + data_bytes + FOOTER # 发送数据包 bytes_sent = uart.write(packet) # 验证是否所有字节都已发送 if bytes_sent != len(packet): print(f"Warning: Only sent {bytes_sent}/{len(packet)} bytes") return True except Exception as e: print(f"Error sending packet: {e}") return False def pid_control(error, last_error, integral, Kp, Ki, Kd): """PID控制器""" proportional = Kp * error integral += error integral_term = Ki * integral derivative = Kd * (error - last_error) output = proportional + integral_term + derivative return output, error, integral def output_to_servo(output_x, output_y): """将PID输出转换为舵机控制信号""" # 限制输出范围 output_x = max(-100, min(100, output_x)) output_y = max(-100, min(100, output_y)) # 转换为0-255范围 (128为中心) Servo_x = int(output_x * 1.28 + 128) Servo_y = int(output_y * 1.28 + 128) # 确保在0-255范围内 Servo_x = max(0, min(255, Servo_x)) Servo_y = max(0, min(255, Servo_y)) return Servo_x, Servo_y while True: clock.tick() img = sensor.snapshot() target_valid = False # 重置目标有效性标志 # 目标识别逻辑 black_blobs = img.find_blobs([thr_black], merge=False) if black_blobs: for blob in black_blobs: if blob.pixels() / (blob.w() * blob.h()) < 0.3: white_blobs = img.find_blobs([thr_write], area_threshold=2, roi=blob.rect(), merge=False) if white_blobs: largest_white = max(white_blobs, key=lambda b: b.area()) if (2 < largest_white.pixels() / blob.pixels() < 4 and abs(largest_white.cx() - blob.cx()) < 10 and abs(largest_white.cy() - blob.cy()) < 10): target = [largest_white.cx() + target_offset[0], largest_white.cy() + target_offset[1]] target_valid = True # 绘制识别结果 img.draw_rectangle(blob.rect(), color=(255,0,0), thickness=2) img.draw_rectangle(largest_white.rect(), color=(0,255,0), thickness=2) img.draw_cross(target[0], target[1], color=(0,0,255), size=10, thickness=2) img.draw_string(target[0]+10, target[1]+10, "(%d,%d)"%(target[0],target[1]), color=(255,255,255), scale=1.5) # 目标丢失处理 (与main.py保持一致的数据包格式) if not target_valid: send_packet([128, 128]) # 只发送两个舵机值 print('Target lost!') continue # 跳过本次循环剩余部分 # 计算偏差量 view_offset[0] += round((target[0] - img_center_x) * 0.5) view_offset[1] += round((target[1] - img_center_y) * 0.5) view_offset[0] = min(sensor.width(), max(0, view_offset[0])) view_offset[1] = min(sensor.height(), max(0, view_offset[1])) # 计算误差 error_x = target[0] + view_offset[0] - img_center_x error_y = target[1] + view_offset[1] - img_center_y # PID控制 output_x, last_error_x, integral_x = pid_control(error_x, last_error_x, integral_x, Kp, Ki, Kd) output_y, last_error_y, integral_y = pid_control(error_y, last_error_y, integral_y, Kp, Ki, Kd) # 转换为舵机信号并发送 (与main.py保持一致) Servo_data[0], Servo_data[1] = output_to_servo(output_x, output_y) send_packet(Servo_data) print(f"Sent: {Servo_data}") #print("FPS:", clock.fps()) 粘贴代码
-
@twph 和stm32通信控制舵机,根本没反应,ttl口也接收不到
-
检查IO和接线是否有问题,OpenMV的TX连接32的RX,OpenMV的RX连接32的TX:https://book.openmv.cc/MCU/pyb.html
-
应该是没问题,帮我看一下我的代码有问题吗?