实现完整串口指令协议:模式切换、手动控制、PID在线调参

- 新增 PIDClear() 仅清运行时状态,保留 Kp/Ki/Kd 等配置
- 命令帧 0xA7 (3B): 自动/手动/调试三模式切换,带模式状态追踪
- 手动控制帧 0xA6 (8B): 解析 motor_speed_base(int16) + angle_velocity(float)
- 调试调参帧 0xA8 (38B): 在线更新全部9个 Kp/Ki/Kd 参数
- Command 解析纳入 Task_50ms (20Hz) 定时执行
- 添加通信协议文档 docs/通信协议.md
This commit is contained in:
2026-06-07 16:23:10 +08:00
parent d120fa0d17
commit 022e3424da
7 changed files with 488 additions and 25 deletions
+7 -6
View File
@@ -7,7 +7,6 @@
#include "GrayscaleSensor.h"
#include "Motor.h"
#include "MotorEncoder.h"
#include "PID_Controller.h"
#include "Command.h"
@@ -20,9 +19,9 @@ volatile int16_t motor_speed_right_ref = 0;
volatile int16_t motor_speed_left_ref = 0;
/*角度环接管速度之后的基础直行速度*/
volatile int16_t motor_speed_base = 0;
/*角度环输入,开启位置环后由位置环接管*/
/*角度环参考值*/
volatile float yaw_ref = 0;
/*位置环的输出*/
/*角度速率控制,开启位置环后由位置环接管*/
volatile float angle_velocity = 0;
volatile PIDInstance pid_speed_right={0};
@@ -61,6 +60,7 @@ void Task_50ms(uint32 state, void *ptr)
// 50ms 执行一次的任务代码(20Hz)
key_scanner();
beep_actuator();
command_task();
}
void task_init(void)
{
@@ -111,12 +111,15 @@ void angle_control_init(float T)
.Kd = 0.0f,
.T = T,
.MaxOut = 25,
.Improve = PID_Yaw_Error_Corretion
.Improve = PID_Yaw_Error_Corretion | PID_Derivative_On_Measurement
};
PIDInit((PIDInstance *)&pid_yaw, &config_yaw);
}
void angle_control_task(void)
{
//角度速率控制
set_yaw_ref(yaw_ref + angle_velocity*pid_yaw.dt);
float speed_dif = PIDCalculate((PIDInstance *)&pid_yaw,imu_angle.yaw,yaw_ref);
motor_speed_left_ref = motor_speed_base - speed_dif;
motor_speed_right_ref = motor_speed_base + speed_dif;
@@ -139,8 +142,6 @@ void position_control_task(void)
//发送给云台,角度速率前馈
command_send_angle_velocity((float*)&angle_velocity);
set_yaw_ref(yaw_ref + angle_velocity*pid_position.dt);
}
void set_yaw_ref(float set_value)
{