refactor: 哨兵分支整理目录结构,按功能归类(只搬家,程序不变)

基于战队 main 9de485e。两块板统一改为三层目录,Keil 工程分组与文件夹一一对应:
- App/   本板业务,按功能分文件夹(system/ cmd/ comm/ referee/,
         云台另有 gimbal/ shoot/ vision/,底盘另有 chassis/,含 hlj_pc、supercap)
- Lib/   与业务无关的通用模块(algorithm/ motor/ imu/ sensor/ dev/),两板逐字节相同
- Bsp/   bsp_can、bsp_usart,不再分 Inc/Src

具体改动:
- 107 个文件全部 git mv(纯重命名,内容不变),文件名不变,#include 不用改
- .uvprojx:Application/Task、Modules、BSP 三个分组换成新分组,登记文件集合不变;
  头文件路径换成新目录,CubeMX/第三方目录不动
- .uvoptx 分组同步,文件序号重新连续编号
- 根 README 和两块板 README 的路径、代码地图、模块清单同步更新(补 hlj_pc、supercap)
- 新增 doc/目录整理说明.md(新旧路径对照、验证方法、以后如何跟进步兵更新)

验证(整理前后同一编译器):
- 两块板 Program Size 完全相同
  底盘 Code=111184 RO=1956 RW=432 ZI=62720
  云台 Code=109876 RO=1960 RW=420 ZI=62764
- .map 符号表(名字/类型/大小/所在 .o)底盘 1155 个、云台 1156 个,逐条一致

未改动:Core/、Drivers/、Middlewares/、USB_DEVICE/、CubeMX 分组名与任务名、module_test/

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tmr.textandClaude Opus 5.5 committed 2026-11-30 11:38:19 +08:00
1 parent 9de485e4ef
commit 4b75e4ec6e
115 files changed
+1200 -818

No files matched your search

+327
View File
@@ -0,0 +1,327 @@
#include "gimbal.h"
#include "pid.h"
#include "filter.h"
#include "attitude.h"
#include "bmi088.h"
#include "ins.h"
#include "can.h"
#include "robot_param.h"
#include "robot_typedef.h"
#include "dr16.h"
#include <math.h>
#ifndef ANGLE_TO_RAD
#define ANGLE_TO_RAD 0.01745329251994329576923690768489f//角度转弧度系数(PI/180)
#endif
#define GIMBAL_ECD_HALF_TURN 4096//编码器半圈tick数
#define GIMBAL_ECD_FULL_TURN 8192//编码器一圈tick数
dji_motor_t motor_yaw_gimbal;//Yaw GM6020(CAN2 0x205)
dji_motor_t motor_pitch_gimbal;//Pitch GM6020(CAN1 0x206)
dji_motor_t motor_friction_l;//左摩擦轮M3508(CAN1 0x201)
dji_motor_t motor_friction_r;//右摩擦轮M3508(CAN1 0x202)
dji_motor_t motor_rammer;//拨弹盘M2006(CAN2 0x201)
static PID_PositionTypeDef yaw_angle_pid, yaw_speed_pid;//Yaw位置环与速度环PID
static PID_PositionTypeDef pitch_angle_pid, pitch_speed_pid;//Pitch位置环与速度环PID
static float yaw_target_deg;//世界系Yaw目标角(°)
static float pitch_target_deg;//Pitch目标角(°)
static float yaw_target_prev_deg;//上周期Yaw目标角(前馈微分用)
static float yaw_rate_ff;//低通滤波后目标角速度前馈(°/s)
static float yaw_current;//Yaw最终电流指令(±16384)
float pitch_pid = 0.0f;//Pitch速度环输出
float pitch_g = 0.0f;//Pitch重力前馈
float pitch_current = 0.0f;//Pitch最终电流指令
static float yaw_sign_f = 0.0f;//实测Yaw装配符号(0=未检,±1=已测)
static uint8_t yaw_sign_valid = 0;//Yaw装配符号有效标志
/*
*函数简介:计算编码器相对零位的最短有符号差值
*参数说明:当前编码器原始值(0~8191)
*参数说明:机械零位编码器值(0~8191)
*返回类型:最短差值(-4096~+4096)
*备注:无
*/
static int16_t gimbal_ecd_shortest_diff(uint16_t raw, uint16_t zero)
{
int16_t diff = (int16_t)raw - (int16_t)zero;
if (diff > GIMBAL_ECD_HALF_TURN)
diff -= GIMBAL_ECD_FULL_TURN;
else if (diff < -GIMBAL_ECD_HALF_TURN)
diff += GIMBAL_ECD_FULL_TURN;
return diff;
}
/*
*函数简介:判定单颗电机是否掉线
*参数说明:电机实例指针
*参数说明:当前系统tick(HAL_GetTick)
*返回类型:1-掉线,0-在线
*备注:无
*/
static uint8_t gimbal_one_offline(const dji_motor_t * motor, uint32_t now)
{
return (motor->fdb.last_online_tick == 0U ||
(now - motor->fdb.last_online_tick) > DEVICE_OFFLINE_TIME_MS) ? 1U : 0U;
}
/*
*函数简介:初始化云台与发射机构电机实例及双轴PID控制器
*参数说明:无
*返回类型:无
*备注:必须在can_start()之后调用一次
*/
void gimbal_motor_init(void)
{
dji_motor_init(&motor_yaw_gimbal, &hcan2, 0x205, DJI_MOTOR_6020);
dji_motor_init(&motor_pitch_gimbal, &hcan1, 0x206, DJI_MOTOR_6020);
dji_motor_init(&motor_friction_l, &hcan1, 0x201, DJI_MOTOR_3508);
dji_motor_init(&motor_friction_r, &hcan1, 0x202, DJI_MOTOR_3508);
dji_motor_init(&motor_rammer, &hcan2, 0x201, DJI_MOTOR_2006);
PID_PositionStructureInit(&yaw_angle_pid, 0);//Yaw位置环:°->°/s
PID_PositionSetParameter(&yaw_angle_pid, GIMBAL_YAW_ANGLE_KP, GIMBAL_YAW_ANGLE_KI, GIMBAL_YAW_ANGLE_KD);
PID_PositionSetEkRange(&yaw_angle_pid, -GIMBAL_YAW_ANGLE_EK_DEADBAND, GIMBAL_YAW_ANGLE_EK_DEADBAND);
PID_PositionSetOUTRange(&yaw_angle_pid, -GIMBAL_YAW_ANGLE_OUT_LIMIT, GIMBAL_YAW_ANGLE_OUT_LIMIT);
PID_PositionStructureInit(&yaw_speed_pid, 0);//Yaw速度环:°/s->电流
PID_PositionSetParameter(&yaw_speed_pid, GIMBAL_YAW_SPEED_KP, GIMBAL_YAW_SPEED_KI, GIMBAL_YAW_SPEED_KD);
PID_PositionSetOUTRange(&yaw_speed_pid, -GIMBAL_YAW_SPEED_OUT_LIMIT, GIMBAL_YAW_SPEED_OUT_LIMIT);
PID_PositionStructureInit(&pitch_angle_pid, 0);//Pitch位置环:°->RPM
PID_PositionSetParameter(&pitch_angle_pid, GIMBAL_PITCH_ANGLE_KP, GIMBAL_PITCH_ANGLE_KI, GIMBAL_PITCH_ANGLE_KD);
PID_PositionSetEkRange(&pitch_angle_pid, -GIMBAL_PITCH_ANGLE_EK_DEADBAND, GIMBAL_PITCH_ANGLE_EK_DEADBAND);
PID_PositionSetOUTRange(&pitch_angle_pid, -200.0f, 200.0f);
PID_PositionStructureInit(&pitch_speed_pid, 300.0f);//Pitch速度环:RPM->电流
PID_PositionSetParameter(&pitch_speed_pid, GIMBAL_PITCH_SPEED_KP, GIMBAL_PITCH_SPEED_KI, GIMBAL_PITCH_SPEED_KD);
PID_PositionSetEkRange(&pitch_speed_pid, 0.0f, 0.0f);
PID_PositionSetOUTRange(&pitch_speed_pid, -16384.0f, 16384.0f);
}
/*
*函数简介:上电自检Yaw装配符号
*参数说明:无
*返回类型:1-自检成功,0-电机离线/被按住/无响应
*备注:给Yaw电机施加60ms正向脉冲并测量陀螺z轴响应,阻塞约190ms
*/
uint8_t gimbal_detect_yaw_sign(void)
{
float sum = 0.0f;
float rate_dps;
if (gimbal_motor_offline())
return 0;
dji_motor_send_gm6020_current_low(&hcan2, 0, 0, 0, 0);//静默等待稳定
HAL_Delay(50);
dji_motor_send_gm6020_current_low(&hcan2, GIMBAL_YAW_DETECT_CURRENT, 0, 0, 0);
for (uint16_t i = 0; i < 60; i++)
{
sum += bmi088_gyro[2];//rad/s(z轴向上为正)
HAL_Delay(1);
}
dji_motor_send_gm6020_current_low(&hcan2, 0, 0, 0, 0);
HAL_Delay(80);//回稳
rate_dps = sum / 60.0f * RAD_TO_ANGLE;
if (rate_dps > GIMBAL_YAW_DETECT_RATE_MIN)
{
yaw_sign_f = 1.0f;//正电流->陀螺正转
yaw_sign_valid = 1;
return 1;
}
if (rate_dps < -GIMBAL_YAW_DETECT_RATE_MIN)
{
yaw_sign_f = -1.0f;//反向装配
yaw_sign_valid = 1;
return 1;
}
return 0;
}
/*
*函数简介:获取Yaw装配符号
*参数说明:无
*返回类型:+1.0或-1.0
*备注:未自检时回退到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)
{
if (!yaw_sign_valid)
return 0;
return (yaw_sign_f > 0.0f) ? 1 : -1;
}
/*
*函数简介:获取Yaw速度环PID原始输出
*参数说明:无
*返回类型:电流码(±16384,不含小陀螺前馈)
*备注:仅用于标定GIMBAL_YAW_SPIN_FEEDFORWARD_K
*/
float gimbal_yaw_speed_out(void)
{
return yaw_speed_pid.OUT;
}
/*
*函数简介:云台双轴控制主循环
*参数说明:本周期Yaw目标角增量(°)
*参数说明:本周期Pitch目标角增量(°)
*参数说明:复位标志(非0时重锁零位并清PID)
*返回类型:无
*备注:固定2ms调用一次,内部直接经CAN1/CAN2发送GM6020电流指令
*/
void gimbal_control(float yaw_delta, float pitch_delta, uint8_t reset_flag)
{
static uint8_t first_run = 1;
const float sign = gimbal_yaw_sign();
const float yaw_meas = sign * att_deg_yaw;//°
const float yaw_rate_meas = sign * ((bmi088_gyro[2] - ins_gyro_z_offset) * RAD_TO_ANGLE);//°/s
float yaw_rate_cmd;
float yaw_rate_ref;
float spin_feedforward = 0.0f;
float a_deg;
float a_rad;
if (first_run || reset_flag)
{
int16_t ecd_diff;
int16_t pitch_ecd_diff;
first_run = 0;
ecd_diff = gimbal_ecd_shortest_diff(motor_yaw_gimbal.fdb.raw_angle, YAW_GM6020_ENCODER_ZERO);
yaw_target_deg = yaw_meas - (float)ecd_diff * (360.0f / 8192.0f);
pitch_ecd_diff = (int16_t)motor_pitch_gimbal.fdb.raw_angle - (int16_t)PITCH_GM6020_ENCODER_ZERO;
pitch_target_deg = -att_deg_roll - (float)pitch_ecd_diff * (360.0f / 8192.0f);
yaw_target_prev_deg = yaw_target_deg;
yaw_rate_ff = 0.0f;
if (reset_flag)
{
PID_PositionClean(&yaw_angle_pid);
PID_PositionClean(&yaw_speed_pid);
PID_PositionClean(&pitch_angle_pid);
PID_PositionClean(&pitch_speed_pid);
}
}
yaw_target_deg += yaw_delta;
pitch_target_deg += pitch_delta;
//目标角速度前馈(2ms周期*500折算为°/s,一阶低通抑制视觉阶跃噪声)
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_angle_pid.Need_Value = yaw_target_deg;
PID_PositionCalc(&yaw_angle_pid, yaw_meas);
yaw_rate_ref = fp32_constrain(
yaw_angle_pid.OUT + GIMBAL_YAW_FF_GAIN * yaw_rate_ff,
-GIMBAL_YAW_ANGLE_OUT_LIMIT, GIMBAL_YAW_ANGLE_OUT_LIMIT);
yaw_speed_pid.Need_Value = yaw_rate_ref;
PID_PositionCalc(&yaw_speed_pid, yaw_rate_meas);
if (cmd_robot.spin_flag || dr16.s2 == 1 || dr16.s2 == 2)
spin_feedforward = GIMBAL_YAW_SPIN_FEEDFORWARD_K * (float)motor_yaw_gimbal.fdb.rotor_speed;
yaw_current = fp32_constrain(
yaw_speed_pid.OUT + spin_feedforward,
-GIMBAL_YAW_SPEED_OUT_LIMIT, GIMBAL_YAW_SPEED_OUT_LIMIT);
//Pitch串级闭环(位置环反馈为AHRS Roll)
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_deg = ((float)motor_pitch_gimbal.fdb.raw_angle - GIMBAL_PITCH_GRAVITY_ZERO_ECD) * (360.0f / 8192.0f);
a_rad = a_deg * ANGLE_TO_RAD;
pitch_g = GIMBAL_PITCH_GRAVITY_K_COS * cosf(a_rad) +
GIMBAL_PITCH_GRAVITY_K_SIN * sinf(a_rad) +
GIMBAL_PITCH_GRAVITY_K_CONST;
pitch_current = fp32_constrain(pitch_pid + pitch_g, -16384.0f, 16384.0f);
dji_motor_send_gm6020_current_low(&hcan2, (int16_t)yaw_current, 0, 0, 0);//Yaw->CAN2 0x1FE槽1
dji_motor_send_gm6020_current_low(&hcan1, 0, (int16_t)pitch_current, 0, 0);//Pitch->CAN1 0x1FE槽2
}
/*
*函数简介:直接设定云台绝对目标角(视觉自瞄用)
*参数说明:世界系Yaw目标角(°)
*参数说明:Pitch目标角(°)
*返回类型:无
*备注:Yaw自动取距当前目标最近等效角,Pitch按编码器剩余行程做软限位
*/
void gimbal_set_target_deg(float yaw_deg, float pitch_deg)
{
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;
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_target_deg = fp32_constrain(pitch_deg, p_min, p_max);
}
/*
*函数简介:检查云台与发射机构电机是否掉线
*参数说明:无
*返回类型:1-有电机掉线,0-全部在线
*备注:拨弹电机是否参与判定受GIMBAL_RAMMER_INSTALLED宏控制
*/
uint8_t gimbal_motor_offline(void)
{
uint32_t now = HAL_GetTick();
uint8_t rammer_off = 0;
#if GIMBAL_RAMMER_INSTALLED
rammer_off = gimbal_one_offline(&motor_rammer, now);
#endif
return (gimbal_one_offline(&motor_yaw_gimbal, now) ||
gimbal_one_offline(&motor_pitch_gimbal, now) ||
gimbal_one_offline(&motor_friction_l, now) ||
gimbal_one_offline(&motor_friction_r, now) ||
rammer_off);
}
/*
*函数简介:云台与发射机构四路CAN电流输出清零
*参数说明:无
*返回类型:无
*备注:失能或异常保护时调用
*/
void gimbal_output_zero(void)
{
dji_motor_send_gm6020_current_low(&hcan2, 0, 0, 0, 0);
dji_motor_send_gm6020_current_low(&hcan1, 0, 0, 0, 0);
dji_motor_send_current_low(&hcan1, 0, 0, 0, 0);
dji_motor_send_current_low(&hcan2, 0, 0, 0, 0);
}