增加调试功能,以及yaw补偿
This commit is contained in:
1 parent
267a659242
commit
957c535bda
257 files changed
+239436
-692
No files matched your search
+79
-99
@@ -2,20 +2,19 @@
|
||||
* gimbal.c - 云台双轴控制(串级 PID)
|
||||
* Yaw(2026-09 重写):双陀螺环,见 gimbal.h 与 robot_param.h 说明
|
||||
* 位置环:err(°) = 目标角 - SIGN*att_deg_yaw -> 目标角速度(°/s,限幅)
|
||||
* 速度环:err(°/s) = 目标角速度 - SIGN*gyro_z -> 电流(限幅)
|
||||
* 速度环:给定 = 位置环输出 + 目标角速度前馈(低通),反馈 SIGN*gyro_z -> 电流
|
||||
* 稳定性条件:SIGN*G>0,G 为"电流->陀螺 z 角速度"的装配增益,
|
||||
* 由 gimbal_detect_yaw_sign() 上电实测,彻底消除双传感器符号打架
|
||||
* Pitch:位置环用 QEKF Roll(安装方向),速度环转子转速(legacy 案,无符号史问题)
|
||||
* 发送:CAN2 0x1FE 槽1=Yaw 电流;CAN1 0x1FE 槽2=Pitch 电流
|
||||
* ========================================================================== */
|
||||
#include "gimbal.h"
|
||||
#include "shoot.h"
|
||||
#include "pid.h"
|
||||
#include "attitude.h"
|
||||
#include "bmi088.h"
|
||||
#include "ins.h"
|
||||
#include "can.h"
|
||||
#include "robot_param.h"
|
||||
#include "filter.h"
|
||||
#include "robot_typedef.h"
|
||||
#include "dr16.h"
|
||||
#include <math.h>
|
||||
@@ -39,25 +38,19 @@ static PID_PositionTypeDef pitch_angle_pid, pitch_speed_pid;
|
||||
|
||||
static float yaw_target_deg; // 世界系目标角(°)
|
||||
static float pitch_target_deg; // 目标角(°)
|
||||
static float yaw_target_prev_deg; // 上周期目标角(目标角速度前馈用)
|
||||
static float yaw_rate_ff; // 低通滤波后的目标角速度前馈(°/s)
|
||||
static float yaw_current;
|
||||
float pitch_pid = 0.0f;
|
||||
float pitch_g = 0.0f;
|
||||
float pitch_current = 0.0f;
|
||||
|
||||
/* ===== 2026-10-15 台架调参临时量(已停用,保留备查) ===== */
|
||||
#if 0
|
||||
/* 台架调参用 Pitch 目标角附加偏置(°)。由 gimbal_task 的方波发生器写入,
|
||||
* 只叠加到位置环设定值上,不累加进 pitch_target_deg,默认 0 无副作用 */
|
||||
float gimbal_bench_pitch_offset = 0.0f;
|
||||
|
||||
/* 实际施加的偏置(经限位安全门控后),仅用于遥测显示真实设定值 */
|
||||
static float pitch_applied_offset = 0.0f;
|
||||
#endif
|
||||
|
||||
/* 装配符号:0=未检测,±1=已测定;未检测前 gimbal_control 不应被调用 */
|
||||
static float yaw_sign_f = 0.0f;
|
||||
static uint8_t yaw_sign_valid = 0;
|
||||
|
||||
/* 初始化云台/发射机构电机实例与四个 PID 控制器。必须在 can_start 之后调用一次。
|
||||
* 无参数、无返回值。 */
|
||||
void gimbal_motor_init(void)
|
||||
{
|
||||
dji_motor_init(&motor_yaw_gimbal, &hcan2, 0x205, DJI_MOTOR_6020);
|
||||
@@ -89,6 +82,10 @@ void gimbal_motor_init(void)
|
||||
PID_PositionSetOUTRange(&pitch_speed_pid, -16384, 16384);
|
||||
}
|
||||
|
||||
/* 上电自检 Yaw 装配符号:给 Yaw 电机一个正电流脉冲(单位:CAN 电流码),
|
||||
* 测陀螺 z 轴平均角速度(单位:°/s)判断"正电流是否对应陀螺正转"。
|
||||
* 阻塞约 190 ms,期间云台会轻转几度。
|
||||
* 返回 1=已测定(可读 gimbal_yaw_sign),0=电机离线/被按住/无响应 */
|
||||
uint8_t gimbal_detect_yaw_sign(void)
|
||||
{
|
||||
if (gimbal_motor_offline())
|
||||
@@ -126,11 +123,15 @@ uint8_t gimbal_detect_yaw_sign(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* 取 Yaw 装配符号:自检成功返回 +1/-1,未自检时退回参数 GIMBAL_YAW_SIGN_FALLBACK。
|
||||
* 无参数,返回无量纲符号值。 */
|
||||
float gimbal_yaw_sign(void)
|
||||
{
|
||||
return yaw_sign_valid ? yaw_sign_f : GIMBAL_YAW_SIGN_FALLBACK;
|
||||
}
|
||||
|
||||
/* 编码器计数增大方向对应的云台旋转方向:+1=逆时针(CCW),-1=顺时针(CW),
|
||||
* 0=尚未自检。随 0x1A9 下发底盘做跟随符号综合。无参数。 */
|
||||
int8_t gimbal_encoder_ccw(void)
|
||||
{
|
||||
/* 自检:电流正脉冲 -> 陀螺 z 正转(云台 CCW),而电流正方向即编码器
|
||||
@@ -140,12 +141,26 @@ int8_t gimbal_encoder_ccw(void)
|
||||
return (yaw_sign_f > 0.0f) ? 1 : -1;
|
||||
}
|
||||
|
||||
/* 取 Yaw 速度环 PID 输出(id 电流码,±16384,量程 ±3 A)。不含小陀螺前馈,
|
||||
* 供串口标定 GIMBAL_YAW_SPIN_FEEDFORWARD_K 时记录。无参数。 */
|
||||
float gimbal_yaw_speed_out(void)
|
||||
{
|
||||
return yaw_speed_pid.OUT;
|
||||
}
|
||||
|
||||
/* 云台双轴控制主循环,固定 2 ms 调用一次(500 Hz)。
|
||||
* yaw_delta : 本周期 Yaw 目标角增量(°)
|
||||
* pitch_delta: 本周期 Pitch 目标角增量(°)
|
||||
* reset_flag : 非 0 时重锁目标角并清空 PID 积分/历史
|
||||
* 无返回值;函数内部直接发送 CAN 电流(Yaw ±16384,Pitch ±16384) */
|
||||
void gimbal_control(float yaw_delta, float pitch_delta, uint8_t reset_flag)
|
||||
{
|
||||
/* 双环共用装配符号 */
|
||||
const float sign = gimbal_yaw_sign();
|
||||
const float yaw_meas = sign * att_deg_yaw; // °
|
||||
const float yaw_rate_meas = sign * (bmi088_gyro[2] * RAD_TO_ANGLE); // °/s
|
||||
const float yaw_meas = sign * att_deg_yaw; // °
|
||||
|
||||
/* 与 EKF 积分 att_deg_yaw 用同一份扣偏角速度 */
|
||||
const float yaw_rate_meas = sign * ((bmi088_gyro[2] - ins_gyro_z_offset) * RAD_TO_ANGLE); // °/s
|
||||
|
||||
/* 首次进入控制或复位:将目标角设定为设定的编码器零位朝向(类似 Pitch 上电归位) */
|
||||
static uint8_t first_run = 1;
|
||||
@@ -169,6 +184,10 @@ void gimbal_control(float yaw_delta, float pitch_delta, uint8_t reset_flag)
|
||||
const float pitch_ecd_err_deg = (float)pitch_ecd_diff * (360.0f / 8192.0f);
|
||||
pitch_target_deg = -att_deg_roll - pitch_ecd_err_deg;
|
||||
|
||||
/* 目标角被同步覆写,前馈状态清零,避免同步跳变被微分成速度冲击 */
|
||||
yaw_target_prev_deg = yaw_target_deg;
|
||||
yaw_rate_ff = 0.0f;
|
||||
|
||||
if (reset_flag)
|
||||
{
|
||||
PID_PositionClean(&yaw_angle_pid);
|
||||
@@ -181,15 +200,29 @@ void gimbal_control(float yaw_delta, float pitch_delta, uint8_t reset_flag)
|
||||
yaw_target_deg += yaw_delta;
|
||||
pitch_target_deg += pitch_delta;
|
||||
|
||||
/* 目标角速度前馈:目标角每周期增量折算 °/s(2ms 周期×500),手动增量
|
||||
* 与视觉绝对角共用同一测量。视觉帧是阶跃且带噪声,一阶低通(τ≈38ms)
|
||||
* 滤成平均速度;匀速跟踪时前馈承担全部速度给定,消除纯 P 位置环
|
||||
* v/Kp 的稳态滞后 */
|
||||
const float yaw_rate_cmd = (yaw_target_deg - yaw_target_prev_deg) * 500.0f;
|
||||
yaw_target_prev_deg = yaw_target_deg;
|
||||
yaw_rate_ff += GIMBAL_YAW_FF_ALPHA * (yaw_rate_cmd - yaw_rate_ff);
|
||||
|
||||
/* Yaw 位置环:° -> °/s(反馈为带符号姿态角) */
|
||||
yaw_angle_pid.Need_Value = yaw_target_deg;
|
||||
PID_PositionCalc(&yaw_angle_pid, yaw_meas);
|
||||
|
||||
/* Yaw 速度环:°/s -> 电流(反馈为带符号陀螺角速度,双环符号自洽) */
|
||||
yaw_speed_pid.Need_Value = yaw_angle_pid.OUT;
|
||||
/* Yaw 速度环:°/s -> 电流(反馈为带符号陀螺角速度,双环符号自洽)
|
||||
* 给定 = 位置环输出 + 目标角速度前馈,总限幅不超过位置环输出限幅 */
|
||||
float yaw_rate_ref = yaw_angle_pid.OUT + GIMBAL_YAW_FF_GAIN * yaw_rate_ff;
|
||||
if (yaw_rate_ref > GIMBAL_YAW_ANGLE_OUT_LIMIT)
|
||||
yaw_rate_ref = GIMBAL_YAW_ANGLE_OUT_LIMIT;
|
||||
else if (yaw_rate_ref < -GIMBAL_YAW_ANGLE_OUT_LIMIT)
|
||||
yaw_rate_ref = -GIMBAL_YAW_ANGLE_OUT_LIMIT;
|
||||
yaw_speed_pid.Need_Value = yaw_rate_ref;
|
||||
PID_PositionCalc(&yaw_speed_pid, yaw_rate_meas);
|
||||
|
||||
/* 小陀螺底盘自旋解耦前馈:根据底盘相对转速补偿动摩擦与反电动势拖拽,消除同向/反向手感不对称 */
|
||||
/* 小陀螺底盘自旋解耦前馈:根据底盘相对转速补偿动摩擦与反电动势拖拽*/
|
||||
float spin_feedforward = 0.0f;
|
||||
if (cmd_robot.spin_flag || dr16.s2 == 1 || dr16.s2 == 2)
|
||||
{
|
||||
@@ -202,32 +235,15 @@ void gimbal_control(float yaw_delta, float pitch_delta, uint8_t reset_flag)
|
||||
yaw_current = -GIMBAL_YAW_SPEED_OUT_LIMIT;
|
||||
|
||||
/* Pitch 串级闭环,位置环反馈为 AHRS Roll */
|
||||
#if 0
|
||||
/* ===== 2026-10-15 台架调参临时改动(已停用,保留备查) =====
|
||||
* 启用时:先算编码器角与上下软限位,做台架偏置的限位安全门控(轴距限位
|
||||
* 不足 GIMBAL_BENCH_PITCH_SAFE_MARGIN_DEG 时不施加阶跃),并把下面一行
|
||||
* 改为 pitch_angle_pid.Need_Value = pitch_target_deg + pitch_applied_offset; */
|
||||
const float pitch_ecd_deg = ((float)motor_pitch_gimbal.fdb.raw_angle -
|
||||
(float)PITCH_GM6020_ENCODER_ZERO) * (360.0f / 8192.0f);
|
||||
const float pitch_lim_up_deg = ((float)PITCH_GM6020_POSITION_UPPER_LIMIT -
|
||||
(float)PITCH_GM6020_ENCODER_ZERO) * (360.0f / 8192.0f);
|
||||
const float pitch_lim_dn_deg = ((float)PITCH_GM6020_POSITION_LOWER_LIMIT -
|
||||
(float)PITCH_GM6020_ENCODER_ZERO) * (360.0f / 8192.0f);
|
||||
pitch_applied_offset = gimbal_bench_pitch_offset;
|
||||
if (pitch_ecd_deg < pitch_lim_up_deg + GIMBAL_BENCH_PITCH_SAFE_MARGIN_DEG ||
|
||||
pitch_ecd_deg > pitch_lim_dn_deg - GIMBAL_BENCH_PITCH_SAFE_MARGIN_DEG)
|
||||
pitch_applied_offset = 0.0f;
|
||||
#endif
|
||||
pitch_angle_pid.Need_Value = pitch_target_deg;
|
||||
PID_PositionCalc(&pitch_angle_pid, -att_deg_roll);
|
||||
pitch_speed_pid.Need_Value = pitch_angle_pid.OUT;
|
||||
PID_PositionCalc(&pitch_speed_pid, motor_pitch_gimbal.fdb.rotor_speed);
|
||||
pitch_pid = pitch_speed_pid.OUT;
|
||||
|
||||
/* Pitch 重力拟合前馈补偿:
|
||||
* a 为当前物理编码值减去 3200*360/8192 的数值(折算为弧度送入三角函数)
|
||||
* I = 3105.88 * cos(a) + 3565.50 * sin(a) + 64.31
|
||||
* 最终电流输出 pitch_out = pitch_pid + pitch_g (速度环不代入 pitch_g) */
|
||||
/* Pitch 重力拟合前馈:按编码值算出的机械倾角 a(°) 查拟合式,
|
||||
* I = K_COS*cos(a) + K_SIN*sin(a) + K_CONST(单位:电流码),
|
||||
* 直接加在最终电流上,不进入速度环 */
|
||||
const float a_deg = ((float)motor_pitch_gimbal.fdb.raw_angle - GIMBAL_PITCH_GRAVITY_ZERO_ECD) * (360.0f / 8192.0f);
|
||||
const float a_rad = a_deg * ANGLE_TO_RAD;
|
||||
pitch_g = GIMBAL_PITCH_GRAVITY_K_COS * cosf(a_rad) +
|
||||
@@ -240,75 +256,38 @@ void gimbal_control(float yaw_delta, float pitch_delta, uint8_t reset_flag)
|
||||
else if (pitch_current < -16384.0f)
|
||||
pitch_current = -16384.0f;
|
||||
|
||||
#if 0
|
||||
/* ===== 2026-10-15 台架调参临时改动:Pitch 软限位/堵转保护(已停用,保留备查) =====
|
||||
* 背景:原设计的上下限位只作用于遥控输入路径(robot_cmd_task),控制环自身的
|
||||
* 目标角/前馈没有任何限位约束。台架烧录会失力矩十几秒~几分钟,轴会掉到限位,
|
||||
* 若不加保护,原逻辑会持续输出大电流顶着限位(GM6020 直驱堵转 = 持续过载发热)。
|
||||
* 做法:越限位时只允许"离开限位"方向的电流(正电流 = 抬头/编码器减小,故下限位
|
||||
* 只留正向、上限位只留负向),反方向清零;限位外连续 GIMBAL_PITCH_LIMIT_STALL_MS
|
||||
* 编码器不动则判堵转,电流降到涓流上限。轴回到软限位内后闭环自动恢复正常。 */
|
||||
if (pitch_ecd_deg >= pitch_lim_dn_deg || pitch_ecd_deg <= pitch_lim_up_deg)
|
||||
{
|
||||
/* 堵转计时:相对基准编码值移动超过 10 个刻度(0.44°)才算"在动"。
|
||||
* 阈值取 10 而不是 2:陀螺/结构微振会让编码器抖 2~3 个刻度,阈值太小会把
|
||||
* 静止误判成在动,保护永远不生效(实测踩过);慢速抬升 5°/s 在 300ms 内
|
||||
* 也能走 34 个刻度,不会被误判。 */
|
||||
static uint16_t lim_stall = 0;
|
||||
static uint16_t lim_ref_ecd = 0;
|
||||
const int16_t d_ecd = (int16_t)((int16_t)motor_pitch_gimbal.fdb.raw_angle -
|
||||
(int16_t)lim_ref_ecd);
|
||||
if (d_ecd > 10 || d_ecd < -10)
|
||||
{
|
||||
lim_ref_ecd = motor_pitch_gimbal.fdb.raw_angle;
|
||||
lim_stall = 0;
|
||||
}
|
||||
else if (lim_stall < 60000u)
|
||||
lim_stall++;
|
||||
|
||||
const float cap = (lim_stall * 2u >= (uint16_t)GIMBAL_PITCH_LIMIT_STALL_MS)
|
||||
? GIMBAL_PITCH_LIMIT_CURRENT
|
||||
: GIMBAL_PITCH_LIMIT_CURRENT_RECOVER;
|
||||
|
||||
if (pitch_ecd_deg >= pitch_lim_dn_deg) /* 轴过低:只许抬升 */
|
||||
{
|
||||
if (pitch_current < 0.0f)
|
||||
pitch_current = 0.0f;
|
||||
else if (pitch_current > cap)
|
||||
pitch_current = cap;
|
||||
}
|
||||
else /* 轴过高:只许下压 */
|
||||
{
|
||||
if (pitch_current > 0.0f)
|
||||
pitch_current = 0.0f;
|
||||
else if (pitch_current < -cap)
|
||||
pitch_current = -cap;
|
||||
}
|
||||
PID_PositionClean(&pitch_speed_pid);
|
||||
}
|
||||
#endif
|
||||
/* ===== 停用块结束 ===== */
|
||||
|
||||
/* 发送:CAN2 0x1FE 槽1 = Yaw 电流;CAN1 0x1FE 槽2 = Pitch 电流 */
|
||||
dji_motor_send_gm6020_current_low(&hcan2, (int16_t)yaw_current, 0, 0, 0);
|
||||
dji_motor_send_gm6020_current_low(&hcan1, 0, (int16_t)pitch_current, 0, 0);
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* ===== 2026-10-15 台架调参临时接口(已停用,保留备查) ===== */
|
||||
/* Pitch 位置环实际设定值(°) = 真实目标角 + 经限位门控后的台架偏置 */
|
||||
float gimbal_pitch_setpoint_deg(void)
|
||||
/* 直接设定云台绝对目标角(视觉自瞄用),坐标系与环反馈一致:
|
||||
* yaw_deg = 世界系偏航角(°),可取任意多圈值,内部自动取最近等效角
|
||||
* pitch_deg= 俯仰角(°),按当前编码器剩余行程做软限位
|
||||
* 无返回值;须在 gimbal_control 之后调用,下一周期闭环生效 */
|
||||
void gimbal_set_target_deg(float yaw_deg, float pitch_deg)
|
||||
{
|
||||
return pitch_target_deg + pitch_applied_offset;
|
||||
while (yaw_deg - yaw_target_deg > 180.0f)
|
||||
yaw_deg -= 360.0f;
|
||||
while (yaw_deg - yaw_target_deg < -180.0f)
|
||||
yaw_deg += 360.0f;
|
||||
yaw_target_deg = yaw_deg;
|
||||
|
||||
/* Pitch 软限位:目标角增量与编码器增量 1:1
|
||||
* 把当前编码值的剩余行程折算成目标角允许范围 */
|
||||
const float k = 360.0f / 8192.0f;
|
||||
const float ecd = (float)motor_pitch_gimbal.fdb.raw_angle;
|
||||
const float p_min = pitch_target_deg + ((float)PITCH_GM6020_POSITION_UPPER_LIMIT - ecd) * k;
|
||||
const float p_max = pitch_target_deg + ((float)PITCH_GM6020_POSITION_LOWER_LIMIT - ecd) * k;
|
||||
if (pitch_deg < p_min)
|
||||
pitch_deg = p_min;
|
||||
else if (pitch_deg > p_max)
|
||||
pitch_deg = p_max;
|
||||
pitch_target_deg = pitch_deg;
|
||||
}
|
||||
|
||||
/* Pitch 位置环输出 = 速度环目标(°/s),用于判断内环是否被限幅 */
|
||||
float gimbal_pitch_speed_ref(void)
|
||||
{
|
||||
return pitch_angle_pid.OUT;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* 检查云台/发射机构电机是否掉线:任一电机超过 DEVICE_OFFLINE_TIME_MS(ms)
|
||||
* 没收到反馈即视为离线。返回 1=有电机掉线,0=全部在线。 */
|
||||
uint8_t gimbal_motor_offline(void)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
@@ -324,6 +303,7 @@ uint8_t gimbal_motor_offline(void)
|
||||
rammer_off);
|
||||
}
|
||||
|
||||
/* 云台四路 CAN 电流全部写 0(等待/失能时调用)。无参数、无返回值。 */
|
||||
void gimbal_output_zero(void)
|
||||
{
|
||||
dji_motor_send_gm6020_current_low(&hcan2, 0, 0, 0, 0);
|
||||
|
||||
Reference in new issue
Block a user