Files
Gimbal/modules/master_machine/master_machine.h
T
SkyLiCoder 9b8e204ff7 feat: add DEBUG mode with host-side PID tuning, motor enable/disable protection
- Add DEBUG mode (parallel to AUTO/MANUAL) for real-time PID parameter tuning
- New 0xA8 debug frame: 12 floats for vision/motor angle PID tuning params
- DMMotorSetMode: send zero-velocity before disable, clear PID to prevent jumps
- motor_enable topic: pub from robot_cmd, sub in gimbal, controls motor on/off
- motor_pid topic: pub from robot_cmd, sub in gimbal, updates motor angle PID
- Rename DM motor enums (DM_CMD_ENABLE_MODE/DISABLE_MODE, DM_Motor_Ctrl_Mode_e)
- SwitchToAuto/Manual: re-enable motors when switching back from DEBUG mode
- Static-allocate enable_msg/pid_tune in GimbalTask to avoid stack churn
- Add docs/host_comm_guide.md with Python Tkinter tuning GUI example
- Remove ARCHITECTURE.md
2026-05-31 03:49:41 +08:00

94 lines
2.8 KiB
C

#ifndef MASTER_MACHINE_H
#define MASTER_MACHINE_H
#include "bsp_usart.h"
#include "bsp_dwt.h"
#include "bsp_log.h"
#include "usart.h"
#include "daemon.h"
#include <string.h>
/*
* 上位机串口协议 — 四种帧类型, 通过帧头区分
*
* ---- 自动/视觉追踪数据帧 0xA5 (10 字节) ----
* [SOF:0xA5][error_x:4][error_y:4][checksum:1]
* error_x/y: float, 归一化误差, 视觉PID外环反馈值
*
* ---- 手动控制数据帧 0xA6 (11 字节) ----
* [SOF:0xA6][zero_force:1][yaw_ref:4][pitch_ref:4][checksum:1]
* zero_force: 0=正常角度控制, 1=零力/保持当前位置
* yaw_ref/pitch_ref: float, 角度参考值 (degrees)
*
* ---- 命令帧 0xA7 (3 字节) ----
* [SOF:0xA7][cmd:1][checksum:1]
* cmd: 0x01=自动模式, 0x02=手动模式, 0x03=激光开, 0x04=激光关, 0x05=调试模式
*
* ---- 调试调参帧 0xA8 (50 字节) ----
* [SOF:0xA8][vision_yaw_Kp:4][vision_yaw_Ki:4][vision_yaw_Kd:4]
* [vision_pitch_Kp:4][vision_pitch_Ki:4][vision_pitch_Kd:4]
* [motor_yaw_Kp:4][motor_yaw_Ki:4][motor_yaw_Kd:4]
* [motor_pitch_Kp:4][motor_pitch_Ki:4][motor_pitch_Kd:4][checksum:1]
* 全部 float, 依次为视觉环 yaw/pitch 和电机角度环 yaw/pitch 的 PID 参数
*
* checksum: 帧头到数据末尾前一字节累加和取低8位
*/
#define VISION_SOF_AUTO 0xA5
#define VISION_SOF_MANUAL 0xA6
#define VISION_SOF_CMD 0xA7
#define VISION_SOF_DEBUG 0xA8
#define VISION_AUTO_LEN 10
#define VISION_MANUAL_LEN 11
#define VISION_CMD_LEN 3
#define VISION_DEBUG_LEN 50
/* 命令码 */
#define VISION_CMD_SWITCH_AUTO 0x01
#define VISION_CMD_SWITCH_MANUAL 0x02
#define VISION_CMD_LASER_ON 0x03
#define VISION_CMD_LASER_OFF 0x04
#define VISION_CMD_SWITCH_DEBUG 0x05
/* 工作模式 */
typedef enum
{
VISION_MODE_AUTO = 0, // 自动/视觉追踪
VISION_MODE_MANUAL = 1, // 手动
VISION_MODE_DEBUG = 2, // 调试
} Vision_Mode_e;
typedef struct
{
/* 自动模式数据 */
float error_x;
float error_y;
uint8_t auto_updated; // 收到新的自动数据帧标志
/* 手动模式数据 */
uint8_t zero_force;
float yaw_ref;
float pitch_ref;
uint8_t manual_updated; // 收到新的手动数据帧标志
/* 调试模式数据 */
float vision_yaw_Kp, vision_yaw_Ki, vision_yaw_Kd;
float vision_pitch_Kp, vision_pitch_Ki, vision_pitch_Kd;
float motor_yaw_Kp, motor_yaw_Ki, motor_yaw_Kd;
float motor_pitch_Kp, motor_pitch_Ki, motor_pitch_Kd;
uint8_t debug_updated;
/* 命令 */
uint8_t cmd; // 最新命令码
uint8_t cmd_pending; // 有待处理命令标志
float frequency; // 接收频率 (Hz)
} VisionData_t;
extern VisionData_t vision_data;
void Vision_Uart_Init(UART_HandleTypeDef *_handle);
#endif // MASTER_MACHINE_H