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:
1 parent
9de485e4ef
commit
4b75e4ec6e
115 files changed
+1200
-818
No files matched your search
@@ -0,0 +1,296 @@
|
||||
#include "chassis.h"
|
||||
#include "hlj_pc.h"
|
||||
#include <math.h>
|
||||
#include "pid.h"
|
||||
#include "filter.h"
|
||||
#include "attitude.h"
|
||||
#include "can.h"
|
||||
#include "robot_param.h"
|
||||
#include "c_comm.h"
|
||||
|
||||
#define CHASSIS_ECD_HALF_TURN 4096//编码器半圈tick数
|
||||
#define CHASSIS_ECD_FULL_TURN 8192//编码器一圈tick数
|
||||
|
||||
dji_motor_t motor_wheel_rf;//0x201右前轮
|
||||
dji_motor_t motor_wheel_lf;//0x202右后轮(变量名沿用legacy)
|
||||
dji_motor_t motor_wheel_lr;//0x203左前轮(变量名沿用legacy)
|
||||
dji_motor_t motor_wheel_rr;//0x204左后轮(变量名沿用legacy)
|
||||
dji_motor_t motor_yaw_follow;//Yaw GM6020(CAN2 0x205,相对角零漂移)
|
||||
|
||||
static PID_PositionTypeDef wheel_speed_pid[4];//四轮转速PID(按CAN槽位0~3)
|
||||
static PID_PositionTypeDef follow_pid;//底盘跟随角PID
|
||||
|
||||
volatile float dbg_follow_err_deg = 0.0f;//跟随角误差(°)
|
||||
volatile float dbg_follow_out = 0.0f;//跟随环输出
|
||||
volatile float dbg_follow_sign = 0.0f;//综合后的跟随符号(0=未定)
|
||||
|
||||
static uint8_t follow_reset_request = 0;//重置跟随PID积分请求标志
|
||||
|
||||
/*
|
||||
*函数简介:判定单颗轮电机是否掉线
|
||||
*参数说明:电机实例指针
|
||||
*参数说明:当前系统tick(HAL_GetTick)
|
||||
*返回类型:1-掉线,0-在线
|
||||
*备注:无
|
||||
*/
|
||||
static uint8_t chassis_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;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:计算Yaw编码器相对机械零位的最短有符号差值
|
||||
*参数说明:无
|
||||
*返回类型:编码器差值(-4096~+4096)
|
||||
*备注:无
|
||||
*/
|
||||
static int16_t chassis_yaw_ecd_diff(void)
|
||||
{
|
||||
int16_t diff = (int16_t)motor_yaw_follow.fdb.raw_angle - (int16_t)YAW_GM6020_ENCODER_ZERO;
|
||||
if (diff > CHASSIS_ECD_HALF_TURN)
|
||||
diff -= CHASSIS_ECD_FULL_TURN;
|
||||
else if (diff < -CHASSIS_ECD_HALF_TURN)
|
||||
diff += CHASSIS_ECD_FULL_TURN;
|
||||
return diff;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化底盘四轮/跟随电机实例与PID控制器
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:必须在can_start()之后调用一次
|
||||
*/
|
||||
void chassis_motor_init(void)
|
||||
{
|
||||
dji_motor_init(&motor_wheel_rf, &hcan1, 0x201, DJI_MOTOR_3508);
|
||||
dji_motor_init(&motor_wheel_lf, &hcan1, 0x202, DJI_MOTOR_3508);
|
||||
dji_motor_init(&motor_wheel_lr, &hcan1, 0x203, DJI_MOTOR_3508);
|
||||
dji_motor_init(&motor_wheel_rr, &hcan1, 0x204, DJI_MOTOR_3508);
|
||||
dji_motor_init(&motor_yaw_follow, &hcan2, 0x205, DJI_MOTOR_6020);
|
||||
|
||||
for (uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
PID_PositionStructureInit(&wheel_speed_pid[i], 0);
|
||||
PID_PositionSetParameter(
|
||||
&wheel_speed_pid[i], MECANUM_WHEEL_SPEED_KP, MECANUM_WHEEL_SPEED_KI,
|
||||
MECANUM_WHEEL_SPEED_KD);
|
||||
PID_PositionSetOUTRange(
|
||||
&wheel_speed_pid[i], -MECANUM_WHEEL_SPEED_OUT_LIMIT, MECANUM_WHEEL_SPEED_OUT_LIMIT);
|
||||
}
|
||||
|
||||
PID_PositionStructureInit(&follow_pid, 0);
|
||||
PID_PositionSetParameter(&follow_pid, CHASSIS_FOLLOW_KP, CHASSIS_FOLLOW_KI, CHASSIS_FOLLOW_KD);
|
||||
PID_PositionSetOUTRange(&follow_pid, -CHASSIS_FOLLOW_OUT_LIMIT, CHASSIS_FOLLOW_OUT_LIMIT);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:判定底盘四轮电机是否有任意一颗掉线
|
||||
*参数说明:无
|
||||
*返回类型:1-有电机掉线,0-全部在线
|
||||
*备注:无
|
||||
*/
|
||||
uint8_t chassis_motor_offline(void)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
return (chassis_one_offline(&motor_wheel_rf, now) ||
|
||||
chassis_one_offline(&motor_wheel_lf, now) ||
|
||||
chassis_one_offline(&motor_wheel_lr, now) ||
|
||||
chassis_one_offline(&motor_wheel_rr, now));
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘麦轮逆运动学解算与四轮电流闭环下发
|
||||
*参数说明:前后平移速度(m/s,+vx朝1/3号轮一侧)
|
||||
*参数说明:左右平移速度(m/s,+vy朝3/4号轮一侧)
|
||||
*参数说明:自转角速度(rad/s)
|
||||
*参数说明:功率限制缩放系数(0~1)
|
||||
*返回类型:无
|
||||
*备注:固定2ms调用一次,经CAN1 0x200发送四轮电流
|
||||
*/
|
||||
void chassis_control(float vx, float vy, float w, float power_scale)
|
||||
{
|
||||
const float rear = w * (MECANUM_RX + MECANUM_RY) / 100.0f;
|
||||
const float factor = MECANUM_SPEED_FACTOR / MECANUM_WHEEL_RADIUS;
|
||||
const float target_rpm[4] = {
|
||||
(vx - vy - rear) * factor,//ID1右前
|
||||
(vx + vy - rear) * factor,//ID2右后
|
||||
(-vx - vy - rear) * factor,//ID3左前
|
||||
(-vx + vy - rear) * factor,//ID4左后
|
||||
};
|
||||
const dji_motor_t * wheels[4] = {
|
||||
&motor_wheel_rf, &motor_wheel_lf, &motor_wheel_lr, &motor_wheel_rr
|
||||
};
|
||||
float wheel_rpm[4];
|
||||
int16_t out[4];
|
||||
|
||||
for (uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
wheel_rpm[i] = (float)wheels[i]->fdb.rotor_speed;
|
||||
wheel_speed_pid[i].Need_Value = target_rpm[i];
|
||||
PID_PositionCalc(&wheel_speed_pid[i], wheel_rpm[i]);
|
||||
out[i] = (int16_t)(wheel_speed_pid[i].OUT * power_scale);
|
||||
}
|
||||
|
||||
#if HLJ_PC_ENABLE
|
||||
hlj_pc_limit_motors(wheel_rpm, out);
|
||||
#else
|
||||
(void)wheel_rpm;
|
||||
#endif
|
||||
dji_motor_send_current_low(&hcan1, out[0], out[1], out[2], out[3]);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:请求重置底盘跟随基准并清积分
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:无
|
||||
*/
|
||||
void chassis_follow_reset(void)
|
||||
{
|
||||
follow_reset_request = 1;
|
||||
PID_PositionClean(&follow_pid);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘自检跟随旋转符号
|
||||
*参数说明:无
|
||||
*返回类型:1-自检成功,0-电机离线/姿态未就绪/车体未转动
|
||||
*备注:施加小w脉冲并用底盘INS yaw判断旋转方向,阻塞约200ms
|
||||
*/
|
||||
uint8_t chassis_detect_follow_sign(void)
|
||||
{
|
||||
float yaw_before;
|
||||
float dyaw;
|
||||
|
||||
if (chassis_motor_offline() || !att_ready)
|
||||
return 0;
|
||||
|
||||
chassis_output_zero();
|
||||
HAL_Delay(50);//静默期
|
||||
|
||||
yaw_before = att_deg_yaw;
|
||||
chassis_control(0.0f, 0.0f, CHASSIS_DETECT_W, 1.0f);
|
||||
for (uint16_t i = 0; i < CHASSIS_DETECT_TIME_MS; i++)
|
||||
HAL_Delay(1);
|
||||
chassis_output_zero();
|
||||
|
||||
dyaw = theta_format(att_deg_yaw - yaw_before);
|
||||
HAL_Delay(80);//回稳
|
||||
|
||||
if (dyaw > CHASSIS_DETECT_YAW_MIN)
|
||||
{
|
||||
dbg_follow_sign = 1.0f;//w+->底盘CCW
|
||||
return 1;
|
||||
}
|
||||
if (dyaw < -CHASSIS_DETECT_YAW_MIN)
|
||||
{
|
||||
dbg_follow_sign = -1.0f;//w+->底盘CW
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:获取综合后的底盘跟随符号
|
||||
*参数说明:无
|
||||
*返回类型:+1.0或-1.0
|
||||
*备注:自检未完成时回退到CHASSIS_FOLLOW_SIGN
|
||||
*/
|
||||
float chassis_follow_sign(void)
|
||||
{
|
||||
if (dbg_follow_sign == 0.0f || ctoc_encoder_ccw == 0)
|
||||
return CHASSIS_FOLLOW_SIGN;
|
||||
return dbg_follow_sign * (float)ctoc_encoder_ccw;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:计算FOLLOW模式下的底盘跟随角速度
|
||||
*参数说明:无
|
||||
*返回类型:跟随环输出角速度w
|
||||
*备注:以Yaw GM6020编码器对齐机械零位为目标,固定2ms调用
|
||||
*/
|
||||
float chassis_follow_w(void)
|
||||
{
|
||||
int16_t tick_diff;
|
||||
float err_deg;
|
||||
|
||||
if (motor_yaw_follow.fdb.last_online_tick == 0U)
|
||||
return 0.0f;
|
||||
|
||||
if (follow_reset_request)
|
||||
{
|
||||
follow_reset_request = 0;
|
||||
PID_PositionClean(&follow_pid);
|
||||
}
|
||||
|
||||
tick_diff = chassis_yaw_ecd_diff();
|
||||
err_deg = (float)tick_diff * (360.0f / 8192.0f);
|
||||
if (ctoc_encoder_ccw < 0)
|
||||
err_deg = -err_deg;
|
||||
|
||||
if (err_deg > -CHASSIS_FOLLOW_DEADBAND && err_deg < CHASSIS_FOLLOW_DEADBAND)
|
||||
err_deg = 0.0f;
|
||||
|
||||
follow_pid.Need_Value = 0.0f;
|
||||
PID_PositionCalc(&follow_pid, chassis_follow_sign() * -err_deg);
|
||||
|
||||
dbg_follow_err_deg = err_deg;
|
||||
dbg_follow_out = follow_pid.OUT;
|
||||
return follow_pid.OUT;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘四轮CAN电流输出清零
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:无
|
||||
*/
|
||||
void chassis_output_zero(void)
|
||||
{
|
||||
dji_motor_send_current_low(&hcan1, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:将云台坐标系平移速度投影到底盘自身坐标系
|
||||
*参数说明:云台系前后速度(vx_g)
|
||||
*参数说明:云台系左右速度(vy_g)
|
||||
*参数说明:底盘当前旋转角速度(w,用于前馈补偿通信及响应滞后)
|
||||
*参数说明:输出底盘系前后速度指针
|
||||
*参数说明:输出底盘系左右速度指针
|
||||
*返回类型:无
|
||||
*备注:若0x205编码器掉线则直通输出
|
||||
*/
|
||||
void chassis_vector_to_body(float vx_g, float vy_g, float w, float * vx_c, float * vy_c)
|
||||
{
|
||||
int16_t tick_diff;
|
||||
float alpha_rad;
|
||||
int8_t enc_sign;
|
||||
float alpha_comp;
|
||||
float cos_a;
|
||||
float sin_a;
|
||||
|
||||
if (vx_c == NULL || vy_c == NULL)
|
||||
return;
|
||||
|
||||
if (motor_yaw_follow.fdb.last_online_tick == 0U)
|
||||
{
|
||||
*vx_c = vx_g;
|
||||
*vy_c = vy_g;
|
||||
return;
|
||||
}
|
||||
|
||||
tick_diff = chassis_yaw_ecd_diff();
|
||||
alpha_rad = (float)tick_diff * (2.0f * PI / 8192.0f);
|
||||
enc_sign = (ctoc_encoder_ccw != 0) ? ctoc_encoder_ccw : 1;
|
||||
if (enc_sign < 0)
|
||||
alpha_rad = -alpha_rad;
|
||||
|
||||
alpha_comp = alpha_rad - (CHASSIS_SPIN_FEEDFORWARD_K * w);
|
||||
cos_a = cosf(alpha_comp);
|
||||
sin_a = sinf(alpha_comp);
|
||||
|
||||
*vx_c = vx_g * cos_a + vy_g * sin_a;
|
||||
*vy_c = -vx_g * sin_a + vy_g * cos_a;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
#ifndef CHASSIS_H
|
||||
#define CHASSIS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "dji_motor.h"
|
||||
|
||||
extern dji_motor_t motor_wheel_rf;//0x201右前轮(CAN1)
|
||||
extern dji_motor_t motor_wheel_lf;//0x202右后轮(CAN1,变量名沿用legacy)
|
||||
extern dji_motor_t motor_wheel_lr;//0x203左前轮(CAN1,变量名沿用legacy)
|
||||
extern dji_motor_t motor_wheel_rr;//0x204左后轮(CAN1,变量名沿用legacy)
|
||||
extern dji_motor_t motor_yaw_follow;//Yaw GM6020(CAN2 0x205,底盘跟随反馈)
|
||||
|
||||
extern volatile float dbg_follow_err_deg;//跟随误差诊断(°)
|
||||
extern volatile float dbg_follow_out;//跟随环输出诊断(w)
|
||||
extern volatile float dbg_follow_sign;//综合后的跟随符号(0=未定)
|
||||
|
||||
void chassis_motor_init(void);//初始化底盘四轮/跟随电机实例与PID
|
||||
uint8_t chassis_motor_offline(void);//判定底盘任一轮电机是否掉线(1-掉线,0-正常)
|
||||
void chassis_control(float vx, float vy, float w, float power_scale);//麦轮逆解与四轮电流下发(2ms调用)
|
||||
uint8_t chassis_detect_follow_sign(void);//上电自检底盘跟随旋转符号(1-成功,0-失败)
|
||||
float chassis_follow_sign(void);//获取综合后的跟随符号(+1/-1)
|
||||
float chassis_follow_w(void);//计算FOLLOW模式下的底盘跟随角速度(2ms调用)
|
||||
void chassis_follow_reset(void);//请求下个周期重置跟随基准并清积分
|
||||
void chassis_output_zero(void);//底盘四轮电流输出清零
|
||||
void chassis_vector_to_body(float vx_g, float vy_g, float w, float * vx_c, float * vy_c);//将云台系平移速度投影到底盘自身坐标系
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
*文件名称:chassis_task.c
|
||||
*功能描述:底盘运动控制任务(2ms),包含等待/运行/跟随符号自检状态机与简易功率限制
|
||||
*备注:SHIFT为临时速度增益,后续接入超级电容驱动后替换
|
||||
*/
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include <math.h>
|
||||
#include "robot_typedef.h"
|
||||
#include "chassis.h"
|
||||
#include "attitude.h"
|
||||
#include "bmi088.h"
|
||||
#include "referee.h"
|
||||
#include "c_comm.h"
|
||||
#include "supercap.h"
|
||||
#include "hlj_pc.h"
|
||||
#include "robot_param.h"
|
||||
|
||||
/* 校准手势参数:左摇杆推最左+右摇杆推最右(与云台侧一致,两板同时自检) */
|
||||
#define CALIB_GESTURE_L_RL 400 //左摇杆最左阈值
|
||||
#define CALIB_GESTURE_R_RL 1600 //右摇杆最右阈值
|
||||
#define CALIB_GESTURE_MS 500 //手势保持时长(ms)
|
||||
#define CALIB_GESTURE_TICKS (CALIB_GESTURE_MS / 2) //2ms任务节拍数
|
||||
|
||||
/* 快陀螺变速自旋参数:w = MECANUM_SPIN_FAST_W + A*sin(wt+phi) */
|
||||
#define SPIN_WAVE_A 1.0f //正弦幅值(与w同量纲)
|
||||
#define SPIN_WAVE_OMEGA 3.1415927f //角频率(rad/s,周期2s)
|
||||
#define SPIN_WAVE_PHASE 0.0f //初相(rad)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
F_WAIT = 0,//等待底盘姿态就绪与四轮在线
|
||||
F_RUN, //正常模式控制(跟随/小陀螺/停车)
|
||||
F_DETECT, //跟随符号自检(仅手势显式触发)
|
||||
} follow_state_e;
|
||||
|
||||
/*
|
||||
*函数简介:检测操作手校准手势并做500ms防抖
|
||||
*参数说明:cnt:防抖计数器指针
|
||||
*返回类型:uint8_t:1=触发自检手势,0=未触发
|
||||
*备注:摇杆值来自0x149板间转发,仅遥控在线时允许计数
|
||||
*/
|
||||
static uint8_t chassis_check_gesture(uint16_t * cnt)
|
||||
{
|
||||
if (cnt == NULL)
|
||||
return 0U;
|
||||
|
||||
if (cmd_robot.online &&
|
||||
ctoc_remote.rc_l_rl < CALIB_GESTURE_L_RL && ctoc_remote.rc_r_rl > CALIB_GESTURE_R_RL)
|
||||
{
|
||||
if (*cnt < CALIB_GESTURE_TICKS)
|
||||
(*cnt)++;
|
||||
}
|
||||
else
|
||||
{
|
||||
*cnt = 0U;
|
||||
}
|
||||
|
||||
if (*cnt >= CALIB_GESTURE_TICKS)
|
||||
{
|
||||
*cnt = 0U;
|
||||
return 1U;
|
||||
}
|
||||
return 0U;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:根据底盘当前模式计算目标自旋角速度w
|
||||
*参数说明:无
|
||||
*返回类型:float:目标角速度w
|
||||
*备注:快陀螺模式叠加正弦变速分量
|
||||
*/
|
||||
static float chassis_calc_w(void)
|
||||
{
|
||||
switch (cmd_robot.chassis_mode)
|
||||
{
|
||||
case CHASSIS_MODE_SPIN_SLOW:
|
||||
return MECANUM_SPIN_SLOW_W;
|
||||
|
||||
case CHASSIS_MODE_SPIN_FAST:
|
||||
{
|
||||
const float t = (float)HAL_GetTick() * 0.001f;
|
||||
return MECANUM_SPIN_FAST_W + SPIN_WAVE_A * sinf(SPIN_WAVE_OMEGA * t + SPIN_WAVE_PHASE);
|
||||
}
|
||||
|
||||
case CHASSIS_MODE_FOLLOW:
|
||||
default:
|
||||
return chassis_follow_w();//编码器相对角,零漂移
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:执行HLJ_PC功率预算更新、超电通信与速度最优规划
|
||||
*参数说明:vx_body:底盘系期望前后速度,vy_body:底盘系期望左右速度,w_in:期望角速度
|
||||
*参数说明:is_spin:1=小陀螺模式,0=非小陀螺模式
|
||||
*参数说明:vx_out:规划后vx,vy_out:规划后vy,w_out:规划后w
|
||||
*返回类型:无
|
||||
*备注:内部函数,串联HLJ_PC预算层与速度规划层
|
||||
*/
|
||||
static void chassis_hlj_pc_step(float vx_body, float vy_body, float w_in, uint8_t is_spin,
|
||||
float * vx_out, float * vy_out, float * w_out)
|
||||
{
|
||||
const supercap_fdb_t * cap = supercap_get_fdb();
|
||||
const uint8_t cap_online = supercap_is_online();
|
||||
const float ref_lim = (referee.link_status && referee.chassis_power_limit > 0U)
|
||||
? (float)referee.chassis_power_limit
|
||||
: HLJ_PC_DEFAULT_POWER_W;
|
||||
|
||||
/* 100Hz向超电发送使能与当前裁判功率上限 */
|
||||
supercap_update_tx((uint8_t)(cmd_robot.online && !chassis_motor_offline()),
|
||||
(uint8_t)ref_lim);
|
||||
|
||||
#if HLJ_PC_ENABLE
|
||||
hlj_pc_input_t in = {
|
||||
.ref_online = referee.link_status,
|
||||
.ref_power_limit = ref_lim,
|
||||
.buffer_energy = referee.link_status ? referee.buffer_energy : 0.0f,
|
||||
.cap_online = cap_online,
|
||||
.cap_ready = cap->ready,
|
||||
.cap_enable = (uint8_t)(ctoc_remote.shift_push ? 1U : 0U),
|
||||
.cap_energy = cap->energy,
|
||||
};
|
||||
(void)hlj_pc_update_budget(&in);
|
||||
|
||||
const float wheel_rpm[4] = {
|
||||
(float)motor_wheel_rf.fdb.rotor_speed,
|
||||
(float)motor_wheel_lf.fdb.rotor_speed,
|
||||
(float)motor_wheel_lr.fdb.rotor_speed,
|
||||
(float)motor_wheel_rr.fdb.rotor_speed,
|
||||
};
|
||||
hlj_pc_plan_velocity(vx_body, vy_body, w_in, is_spin, wheel_rpm,
|
||||
vx_out, vy_out, w_out);
|
||||
#else
|
||||
(void)cap;
|
||||
(void)cap_online;
|
||||
(void)is_spin;
|
||||
*vx_out = vx_body;
|
||||
*vy_out = vy_body;
|
||||
*w_out = w_in;
|
||||
#endif
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘运动控制任务(2ms)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:门控失效时重置跟随基准并清零四轮输出
|
||||
*/
|
||||
void chassis_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
TickType_t last_wake = xTaskGetTickCount();//绝对延时基准
|
||||
follow_state_e state = F_WAIT;//当前状态机状态
|
||||
uint8_t detect_retry = 0U;//自检重试次数
|
||||
uint16_t gesture_cnt = 0U;//手势防抖计数
|
||||
|
||||
while (!app_ready)
|
||||
osDelay(2);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
const uint8_t gesture = chassis_check_gesture(&gesture_cnt);//自检手势触发标志
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case F_WAIT:
|
||||
chassis_output_zero();
|
||||
if (!chassis_motor_offline() && att_ready)
|
||||
state = F_RUN;//自身就绪即可进入运行(符号先用参数回退值)
|
||||
break;
|
||||
|
||||
case F_DETECT:
|
||||
if (chassis_detect_follow_sign())
|
||||
{
|
||||
chassis_follow_reset();//检测期间车被动转过,重锁基准
|
||||
state = F_RUN;
|
||||
}
|
||||
else if (++detect_retry >= 3U)
|
||||
{
|
||||
detect_retry = 0U;
|
||||
chassis_follow_reset();
|
||||
state = F_RUN;//失败3次回退参数符号
|
||||
}
|
||||
else
|
||||
{
|
||||
chassis_output_zero();
|
||||
}
|
||||
break;
|
||||
|
||||
case F_RUN:
|
||||
default:
|
||||
if (gesture && !chassis_motor_offline() && att_ready && ctoc_encoder_ccw != 0)
|
||||
{
|
||||
detect_retry = 0U;
|
||||
chassis_output_zero();
|
||||
chassis_follow_reset();
|
||||
state = F_DETECT;//操作手主动请求自检
|
||||
}
|
||||
else if (cmd_robot.online && !chassis_motor_offline())
|
||||
{
|
||||
const float w = chassis_calc_w();
|
||||
const float boost = ctoc_remote.shift_push ? MECANUM_SHIFT_BOOST_RATE : 1.0f;//SHIFT临时加速倍率
|
||||
const float vx = cmd_robot.vx * boost;
|
||||
const float vy = cmd_robot.vy * boost;
|
||||
|
||||
/* 坐标系旋转投影:将云台系平移速度转换到底盘系并叠加kw前馈 */
|
||||
float chassis_vx = vx;
|
||||
float chassis_vy = vy;
|
||||
const uint8_t is_spin = (uint8_t)(cmd_robot.chassis_mode == CHASSIS_MODE_SPIN_SLOW ||
|
||||
cmd_robot.chassis_mode == CHASSIS_MODE_SPIN_FAST);
|
||||
const float w_feedforward = is_spin ? bmi088_gyro[2] : 0.0f;//跟随无持续自旋前馈为0
|
||||
chassis_vector_to_body(vx, vy, w_feedforward, &chassis_vx, &chassis_vy);
|
||||
|
||||
float vx_opt = chassis_vx;
|
||||
float vy_opt = chassis_vy;
|
||||
float w_opt = w;
|
||||
chassis_hlj_pc_step(chassis_vx, chassis_vy, w, is_spin,
|
||||
&vx_opt, &vy_opt, &w_opt);
|
||||
|
||||
chassis_control(vx_opt, vy_opt, w_opt, 1.0f);
|
||||
}
|
||||
else
|
||||
{
|
||||
supercap_update_tx(0U, (uint8_t)HLJ_PC_DEFAULT_POWER_W);
|
||||
chassis_follow_reset();
|
||||
chassis_output_zero();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
monitor_heartbeat(HB_CHASSIS);
|
||||
vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,365 @@
|
||||
/*
|
||||
*文件名称:hlj_pc.c
|
||||
*功能描述:HLJ_PC底盘功率控制系统(功率预算+运动学速度规划+四轮功率闭环分配)
|
||||
*备注:复用module_test已验证的M3508功率估计模型,适配90W裁判上限/缓冲能量/超级电容
|
||||
*/
|
||||
#include "hlj_pc.h"
|
||||
#include "pid.h"
|
||||
#include "robot_param.h"
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
#define HLJ_PC_CMD_LIMIT_CNT 16000.0f//单电机电流软限幅(counts)
|
||||
#define HLJ_PC_BUFFER_TARGET_J 30.0f //缓冲能量PID目标值(J)
|
||||
#define HLJ_PC_BUFFER_KP 1.0f //缓冲能量PID Kp
|
||||
#define HLJ_PC_BUFFER_KI 0.0f //缓冲能量PID Ki
|
||||
#define HLJ_PC_BUFFER_KD 0.0f //缓冲能量PID Kd
|
||||
#define HLJ_PC_BUFFER_OUT_LIM 20.0f //缓冲能量对基准功率最大修正量(W)
|
||||
|
||||
hlj_pc_model_param_t g_hlj_pc_param = {
|
||||
.k_torque = 1.99688994e-6f,
|
||||
.k_a = 1.63e-07f,
|
||||
.k_speed_sq = 1.06e-07f,
|
||||
.k_constant = 2.0f,
|
||||
};
|
||||
|
||||
hlj_pc_info_t g_hlj_pc_info;//全局诊断状态
|
||||
|
||||
static PID_PositionTypeDef hlj_buffer_pid;//缓冲能量闭环PID
|
||||
static uint8_t hlj_pc_inited = 0U; //初始化标志
|
||||
|
||||
/*
|
||||
*函数简介:将浮点值限幅至[lo, hi]区间
|
||||
*参数说明:val:输入值,lo:下限,hi:上限
|
||||
*返回类型:float:限幅结果
|
||||
*备注:内部辅助函数
|
||||
*/
|
||||
static float hlj_clampf(float val, float lo, float hi)
|
||||
{
|
||||
if (val < lo)
|
||||
return lo;
|
||||
if (val > hi)
|
||||
return hi;
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化HLJ_PC功率控制系统(缓冲能量PID与默认预算)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:在CAN_Task初始化阶段调用一次
|
||||
*/
|
||||
void hlj_pc_init(void)
|
||||
{
|
||||
memset(&g_hlj_pc_info, 0, sizeof(g_hlj_pc_info));
|
||||
g_hlj_pc_info.ref_limit_w = HLJ_PC_DEFAULT_POWER_W;
|
||||
g_hlj_pc_info.total_budget_w = HLJ_PC_DEFAULT_POWER_W;
|
||||
g_hlj_pc_info.kv_scale = 1.0f;
|
||||
g_hlj_pc_info.kw_scale = 1.0f;
|
||||
g_hlj_pc_info.final_power_scale = 1.0f;
|
||||
|
||||
PID_PositionStructureInit(&hlj_buffer_pid, HLJ_PC_BUFFER_TARGET_J);
|
||||
PID_PositionSetParameter(&hlj_buffer_pid, HLJ_PC_BUFFER_KP, HLJ_PC_BUFFER_KI, HLJ_PC_BUFFER_KD);
|
||||
PID_PositionSetOUTRange(&hlj_buffer_pid, -HLJ_PC_BUFFER_OUT_LIM, HLJ_PC_BUFFER_OUT_LIM);
|
||||
hlj_pc_inited = 1U;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:计算单颗M3508电机的估计输入功率(W)
|
||||
*参数说明:speed_rpm:转子转速(RPM)
|
||||
*参数说明:cmd_current:电流控制指令(counts)
|
||||
*参数说明:p_mech:输出机械功率指针(可为NULL)
|
||||
*参数说明:p_loss:输出损耗功率指针(可为NULL)
|
||||
*返回类型:float:估计输入总功率(W,带符号)
|
||||
*备注:无
|
||||
*/
|
||||
float hlj_pc_calc_single_power(float speed_rpm, float cmd_current,
|
||||
float * p_mech, float * p_loss)
|
||||
{
|
||||
const float mech = g_hlj_pc_param.k_torque * speed_rpm * cmd_current;
|
||||
const float loss = g_hlj_pc_param.k_a * cmd_current * cmd_current +
|
||||
g_hlj_pc_param.k_speed_sq * speed_rpm * speed_rpm +
|
||||
g_hlj_pc_param.k_constant;
|
||||
|
||||
if (p_mech != NULL)
|
||||
*p_mech = mech;
|
||||
if (p_loss != NULL)
|
||||
*p_loss = loss;
|
||||
|
||||
return mech + loss;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:根据目标功率逆解单颗电机允许的最大电流指令(counts)
|
||||
*参数说明:speed_rpm:转子转速(RPM)
|
||||
*参数说明:raw_cmd:原始期望电流指令(counts)
|
||||
*参数说明:target_power:目标消耗功率(W)
|
||||
*返回类型:int16_t:限幅后的电流指令(±16000以内)
|
||||
*备注:不可达时取二次曲线最低功耗点(-b/(2a))并限幅
|
||||
*/
|
||||
int16_t hlj_pc_solve_single_cmd(float speed_rpm, int16_t raw_cmd, float target_power)
|
||||
{
|
||||
const float a = g_hlj_pc_param.k_a;
|
||||
const float b = g_hlj_pc_param.k_torque * speed_rpm;
|
||||
const float c = g_hlj_pc_param.k_speed_sq * speed_rpm * speed_rpm +
|
||||
g_hlj_pc_param.k_constant - target_power;
|
||||
float root = 0.0f;
|
||||
|
||||
if (a <= 0.0f || target_power < 0.0f)
|
||||
return raw_cmd;
|
||||
|
||||
const float inside = b * b - 4.0f * a * c;
|
||||
if (inside < 0.0f)
|
||||
{
|
||||
/* 高速工况下目标功率不可达,回退至最小功耗电流点 */
|
||||
root = -b / (2.0f * a);
|
||||
}
|
||||
else if (raw_cmd >= 0)
|
||||
{
|
||||
root = (-b + sqrtf(inside)) / (2.0f * a);
|
||||
}
|
||||
else
|
||||
{
|
||||
root = (-b - sqrtf(inside)) / (2.0f * a);
|
||||
}
|
||||
|
||||
root = hlj_clampf(root, -HLJ_PC_CMD_LIMIT_CNT, HLJ_PC_CMD_LIMIT_CNT);
|
||||
return (int16_t)root;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:更新本周期底盘总功率预算(裁判上限+缓冲能量+超电剩余)
|
||||
*参数说明:in:输入状态结构体指针
|
||||
*返回类型:float:本周期底盘允许消耗的总功率预算(W)
|
||||
*备注:缓冲能量不存在或超电离线时自动退回90W基础上限
|
||||
*/
|
||||
float hlj_pc_update_budget(const hlj_pc_input_t * in)
|
||||
{
|
||||
if (!hlj_pc_inited)
|
||||
hlj_pc_init();
|
||||
|
||||
float ref_limit = HLJ_PC_DEFAULT_POWER_W;
|
||||
float buf_delta = 0.0f;
|
||||
float cap_extra = 0.0f;
|
||||
|
||||
if (in != NULL)
|
||||
{
|
||||
if (in->ref_online && in->ref_power_limit > 0.0f)
|
||||
ref_limit = in->ref_power_limit;
|
||||
|
||||
if (in->ref_online && in->buffer_energy > 0.0f)
|
||||
{
|
||||
PID_PositionCalc(&hlj_buffer_pid, in->buffer_energy);
|
||||
buf_delta = -hlj_buffer_pid.OUT;
|
||||
}
|
||||
|
||||
if (in->cap_online && in->cap_ready && in->cap_enable &&
|
||||
in->cap_energy > HLJ_PC_CAP_MIN_ENERGY)
|
||||
{
|
||||
float ratio = ((float)in->cap_energy - (float)HLJ_PC_CAP_MIN_ENERGY) /
|
||||
(float)(HLJ_PC_CAP_FULL_ENERGY - HLJ_PC_CAP_MIN_ENERGY);
|
||||
ratio = hlj_clampf(ratio, 0.0f, 1.0f);
|
||||
cap_extra = HLJ_PC_CAP_EXTRA_MAX_W * ratio;
|
||||
}
|
||||
}
|
||||
|
||||
float budget = ref_limit + buf_delta + cap_extra;
|
||||
budget = hlj_clampf(budget, HLJ_PC_MIN_POWER_W, ref_limit + HLJ_PC_CAP_EXTRA_MAX_W);
|
||||
|
||||
g_hlj_pc_info.ref_limit_w = ref_limit;
|
||||
g_hlj_pc_info.buf_delta_w = buf_delta;
|
||||
g_hlj_pc_info.cap_extra_w = cap_extra;
|
||||
g_hlj_pc_info.total_budget_w = budget;
|
||||
return budget;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:依据麦轮逆解与轮速环比例估算给定(vx,vy,w)对应的四轮预计总正功率
|
||||
*参数说明:vx:底盘前后速度(m/s),vy:底盘左右速度(m/s),w:角速度(rad/s)
|
||||
*参数说明:wheel_rpm:四轮实测转速(RPM)
|
||||
*返回类型:float:四轮预计正向总功率(W)
|
||||
*备注:内部函数,用于速度规划层快速评估
|
||||
*/
|
||||
static float hlj_pc_estimate_motion_power(float vx, float vy, float w, const float wheel_rpm[4])
|
||||
{
|
||||
const float rear = w * (MECANUM_RX + MECANUM_RY) / 100.0f;
|
||||
const float factor = MECANUM_SPEED_FACTOR / MECANUM_WHEEL_RADIUS;
|
||||
const float target_rpm[4] = {
|
||||
(vx - vy - rear) * factor,
|
||||
(vx + vy - rear) * factor,
|
||||
(-vx - vy - rear) * factor,
|
||||
(-vx + vy - rear) * factor,
|
||||
};
|
||||
float total = 0.0f;
|
||||
|
||||
for (uint8_t i = 0U; i < 4U; i++)
|
||||
{
|
||||
float cmd_est = MECANUM_WHEEL_SPEED_KP * (target_rpm[i] - wheel_rpm[i]);
|
||||
cmd_est = hlj_clampf(cmd_est, -MECANUM_WHEEL_SPEED_OUT_LIMIT, MECANUM_WHEEL_SPEED_OUT_LIMIT);
|
||||
float p = hlj_pc_calc_single_power(wheel_rpm[i], cmd_est, NULL, NULL);
|
||||
if (p > 0.0f)
|
||||
total += p;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:固定某一轴缩放比例,二分求解另一轴满足功率预算的最大缩放因子
|
||||
*参数说明:vx:期望vx,vy:期望vy,w:期望w,wheel_rpm:四轮转速
|
||||
*参数说明:scale_v_mode:1=固定kw二分kv,0=固定kv二分kw
|
||||
*参数说明:fixed_scale:固定轴缩放比例
|
||||
*参数说明:lo:二分下界,hi:二分上界,budget:目标功率预算(W)
|
||||
*返回类型:float:最优缩放因子
|
||||
*备注:6次二分迭代精度约1.5%,耗时极低
|
||||
*/
|
||||
static float hlj_pc_bisect_scale(float vx, float vy, float w, const float wheel_rpm[4],
|
||||
uint8_t scale_v_mode, float fixed_scale,
|
||||
float lo, float hi, float budget)
|
||||
{
|
||||
for (uint8_t iter = 0U; iter < 6U; iter++)
|
||||
{
|
||||
float mid = 0.5f * (lo + hi);
|
||||
float kv = scale_v_mode ? mid : fixed_scale;
|
||||
float kw = scale_v_mode ? fixed_scale : mid;
|
||||
float p = hlj_pc_estimate_motion_power(vx * kv, vy * kv, w * kw, wheel_rpm);
|
||||
|
||||
if (p <= budget)
|
||||
lo = mid;
|
||||
else
|
||||
hi = mid;
|
||||
}
|
||||
|
||||
return lo;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:在当前功率预算下对底盘平移与小陀螺速度进行最优规划
|
||||
*参数说明:vx_in:期望底盘系前后速度(m/s)
|
||||
*参数说明:vy_in:期望底盘系左右速度(m/s)
|
||||
*参数说明:w_in:期望自旋角速度(rad/s)
|
||||
*参数说明:is_spin:1=小陀螺模式,0=非小陀螺模式
|
||||
*参数说明:wheel_rpm:四轮当前实测转子转速(RPM)
|
||||
*参数说明:vx_out:规划后前后速度输出指针
|
||||
*参数说明:vy_out:规划后左右速度输出指针
|
||||
*参数说明:w_out:规划后自旋角速度输出指针
|
||||
*返回类型:无
|
||||
*备注:边小陀螺边前进时先在[HLJ_PC_MIN_SPIN_RATIO,1]降陀螺保平移,再等比降平移保方向
|
||||
*/
|
||||
void hlj_pc_plan_velocity(float vx_in, float vy_in, float w_in,
|
||||
uint8_t is_spin, const float wheel_rpm[4],
|
||||
float * vx_out, float * vy_out, float * w_out)
|
||||
{
|
||||
if (vx_out == NULL || vy_out == NULL || w_out == NULL)
|
||||
return;
|
||||
|
||||
if (wheel_rpm == NULL)
|
||||
{
|
||||
*vx_out = vx_in;
|
||||
*vy_out = vy_in;
|
||||
*w_out = w_in;
|
||||
return;
|
||||
}
|
||||
|
||||
const float budget = g_hlj_pc_info.total_budget_w;
|
||||
const float p_full = hlj_pc_estimate_motion_power(vx_in, vy_in, w_in, wheel_rpm);
|
||||
float kv = 1.0f;
|
||||
float kw = 1.0f;
|
||||
uint8_t limited = 0U;
|
||||
|
||||
if (p_full > budget)
|
||||
{
|
||||
limited = 1U;
|
||||
const uint8_t has_trans = (uint8_t)(fabsf(vx_in) > 1e-4f || fabsf(vy_in) > 1e-4f);
|
||||
|
||||
if (is_spin && has_trans)
|
||||
{
|
||||
/* 边小陀螺边前进:先尝试在保留平移的前提下降低小陀螺转速 */
|
||||
float p_spin_min = hlj_pc_estimate_motion_power(vx_in, vy_in,
|
||||
w_in * HLJ_PC_MIN_SPIN_RATIO,
|
||||
wheel_rpm);
|
||||
if (p_spin_min <= budget)
|
||||
{
|
||||
kw = hlj_pc_bisect_scale(vx_in, vy_in, w_in, wheel_rpm,
|
||||
0U, 1.0f, HLJ_PC_MIN_SPIN_RATIO, 1.0f, budget);
|
||||
}
|
||||
else
|
||||
{
|
||||
kw = HLJ_PC_MIN_SPIN_RATIO;
|
||||
kv = hlj_pc_bisect_scale(vx_in, vy_in, w_in, wheel_rpm,
|
||||
1U, kw, 0.0f, 1.0f, budget);
|
||||
}
|
||||
}
|
||||
else if (is_spin)
|
||||
{
|
||||
/* 纯小陀螺(含变速小陀螺):直接缩放角速度避免无效铜损 */
|
||||
kw = hlj_pc_bisect_scale(vx_in, vy_in, w_in, wheel_rpm,
|
||||
0U, 1.0f, 0.25f, 1.0f, budget);
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 非小陀螺模式:等比缩放平移速度保持方向不变 */
|
||||
kv = hlj_pc_bisect_scale(vx_in, vy_in, w_in, wheel_rpm,
|
||||
1U, 1.0f, 0.0f, 1.0f, budget);
|
||||
}
|
||||
}
|
||||
|
||||
*vx_out = vx_in * kv;
|
||||
*vy_out = vy_in * kv;
|
||||
*w_out = w_in * kw;
|
||||
|
||||
g_hlj_pc_info.kv_scale = kv;
|
||||
g_hlj_pc_info.kw_scale = kw;
|
||||
g_hlj_pc_info.velocity_limited = limited;
|
||||
g_hlj_pc_info.planned_est_power_w = limited
|
||||
? hlj_pc_estimate_motion_power(*vx_out, *vy_out, *w_out, wheel_rpm)
|
||||
: p_full;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:四轮最终电流指令功率闭环限幅(兜底分配)
|
||||
*参数说明:wheel_rpm:四轮当前实测转子转速(RPM)
|
||||
*参数说明:motor_cmds:四轮PID原始指令数组(原地写回限幅后指令)
|
||||
*返回类型:无
|
||||
*备注:保证四轮正功率之和不超过total_budget_w
|
||||
*/
|
||||
void hlj_pc_limit_motors(const float wheel_rpm[4], int16_t motor_cmds[4])
|
||||
{
|
||||
if (wheel_rpm == NULL || motor_cmds == NULL)
|
||||
return;
|
||||
|
||||
if (!hlj_pc_inited)
|
||||
hlj_pc_init();
|
||||
|
||||
const float budget = g_hlj_pc_info.total_budget_w;
|
||||
float initial_p[4] = {0.0f};
|
||||
float total_p = 0.0f;
|
||||
|
||||
for (uint8_t i = 0U; i < 4U; i++)
|
||||
{
|
||||
initial_p[i] = hlj_pc_calc_single_power(wheel_rpm[i], (float)motor_cmds[i], NULL, NULL);
|
||||
if (initial_p[i] > 0.0f)
|
||||
total_p += initial_p[i];
|
||||
}
|
||||
|
||||
g_hlj_pc_info.initial_total_power = total_p;
|
||||
g_hlj_pc_info.final_power_scale = 1.0f;
|
||||
g_hlj_pc_info.current_limited = 0U;
|
||||
|
||||
if (total_p > budget && total_p > 0.0f)
|
||||
{
|
||||
const float scale = (budget > 0.0f) ? (budget / total_p) : 0.0f;
|
||||
g_hlj_pc_info.final_power_scale = scale;
|
||||
g_hlj_pc_info.current_limited = 1U;
|
||||
|
||||
for (uint8_t i = 0U; i < 4U; i++)
|
||||
{
|
||||
if (initial_p[i] <= 0.0f)
|
||||
continue;
|
||||
motor_cmds[i] = hlj_pc_solve_single_cmd(wheel_rpm[i], motor_cmds[i],
|
||||
initial_p[i] * scale);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
#ifndef HLJ_PC_H
|
||||
#define HLJ_PC_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "robot_param.h"
|
||||
|
||||
/* 电机功率模型参数结构体(默认针对M3508+C620) */
|
||||
typedef struct
|
||||
{
|
||||
float k_torque; //机械功率转矩系数
|
||||
float k_a; //铜损系数
|
||||
float k_speed_sq; //铁损/风阻系数
|
||||
float k_constant; //静态偏置功耗(W)
|
||||
} hlj_pc_model_param_t;
|
||||
|
||||
/* 功率预算输入参数结构体 */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t ref_online; //裁判系统是否在线
|
||||
float ref_power_limit; //裁判系统功率上限(W)
|
||||
float buffer_energy; //裁判系统缓冲能量(J,无缓冲传0)
|
||||
uint8_t cap_online; //超电是否在线
|
||||
uint8_t cap_ready; //超电是否就绪
|
||||
uint8_t cap_enable; //是否请求使用超电放电
|
||||
uint8_t cap_energy; //超电剩余能量百分比(0~100)
|
||||
} hlj_pc_input_t;
|
||||
|
||||
/* HLJ_PC系统诊断与状态信息结构体 */
|
||||
typedef struct
|
||||
{
|
||||
float ref_limit_w; //本周期采用的基础功率上限(W)
|
||||
float buf_delta_w; //缓冲能量闭环修正量(W)
|
||||
float cap_extra_w; //超电额外释放功率(W)
|
||||
float total_budget_w; //本周期总可用功率预算(W)
|
||||
float planned_est_power_w;//速度规划后的预计底盘功率(W)
|
||||
float kv_scale; //平移速度缩放因子[0,1]
|
||||
float kw_scale; //小陀螺角速度缩放因子[0,1]
|
||||
float initial_total_power;//四轮PID原始预计总正功率(W)
|
||||
float final_power_scale; //四轮电流级功率缩放因子(<=1.0)
|
||||
uint8_t velocity_limited; //速度规划层是否触发限幅
|
||||
uint8_t current_limited; //电流分配层是否触发限幅
|
||||
} hlj_pc_info_t;
|
||||
|
||||
extern hlj_pc_model_param_t g_hlj_pc_param;//全局功率模型参数
|
||||
extern hlj_pc_info_t g_hlj_pc_info; //全局HLJ_PC诊断状态
|
||||
|
||||
/*
|
||||
*函数简介:初始化HLJ_PC功率控制系统(缓冲能量PID与默认预算)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:在CAN_Task初始化阶段调用一次
|
||||
*/
|
||||
extern void hlj_pc_init(void);
|
||||
|
||||
/*
|
||||
*函数简介:计算单颗M3508电机的估计输入功率(W)
|
||||
*参数说明:speed_rpm:转子转速(RPM)
|
||||
*参数说明:cmd_current:电流控制指令(counts)
|
||||
*参数说明:p_mech:输出机械功率指针(可为NULL)
|
||||
*参数说明:p_loss:输出损耗功率指针(可为NULL)
|
||||
*返回类型:float:估计输入总功率(W,带符号)
|
||||
*备注:基于module_test已验证的功率估计模型
|
||||
*/
|
||||
extern float hlj_pc_calc_single_power(float speed_rpm, float cmd_current,
|
||||
float * p_mech, float * p_loss);
|
||||
|
||||
/*
|
||||
*函数简介:根据目标功率逆解单颗电机允许的最大电流指令(counts)
|
||||
*参数说明:speed_rpm:转子转速(RPM)
|
||||
*参数说明:raw_cmd:原始期望电流指令(counts)
|
||||
*参数说明:target_power:目标消耗功率(W)
|
||||
*返回类型:int16_t:限幅后的电流指令(±16000以内)
|
||||
*备注:无解时取二次函数最小功耗顶点并限幅,避免失控放行大电流
|
||||
*/
|
||||
extern int16_t hlj_pc_solve_single_cmd(float speed_rpm, int16_t raw_cmd, float target_power);
|
||||
|
||||
/*
|
||||
*函数简介:更新本周期底盘总功率预算(裁判上限+缓冲能量+超电剩余)
|
||||
*参数说明:in:输入状态结构体指针
|
||||
*返回类型:float:本周期底盘允许消耗的总功率预算(W)
|
||||
*备注:每2ms由chassis_task调用一次
|
||||
*/
|
||||
extern float hlj_pc_update_budget(const hlj_pc_input_t * in);
|
||||
|
||||
/*
|
||||
*函数简介:在当前功率预算下对底盘平移与小陀螺速度进行最优规划
|
||||
*参数说明:vx_in:期望底盘系前后速度(m/s)
|
||||
*参数说明:vy_in:期望底盘系左右速度(m/s)
|
||||
*参数说明:w_in:期望自旋角速度(rad/s)
|
||||
*参数说明:is_spin:1=小陀螺模式,0=非小陀螺模式
|
||||
*参数说明:wheel_rpm:四轮当前实测转子转速(RPM)
|
||||
*参数说明:vx_out:规划后前后速度输出指针
|
||||
*参数说明:vy_out:规划后左右速度输出指针
|
||||
*参数说明:w_out:规划后自旋角速度输出指针
|
||||
*返回类型:无
|
||||
*备注:边小陀螺边前进时优先保证平移方向不偏,依次压缩小陀螺转速与等比平移量
|
||||
*/
|
||||
extern void hlj_pc_plan_velocity(float vx_in, float vy_in, float w_in,
|
||||
uint8_t is_spin, const float wheel_rpm[4],
|
||||
float * vx_out, float * vy_out, float * w_out);
|
||||
|
||||
/*
|
||||
*函数简介:四轮最终电流指令功率闭环限幅(兜底分配)
|
||||
*参数说明:wheel_rpm:四轮当前实测转子转速(RPM)
|
||||
*参数说明:motor_cmds:四轮PID原始指令数组(原地写回限幅后指令)
|
||||
*返回类型:无
|
||||
*备注:由chassis_control()在CAN发送前调用
|
||||
*/
|
||||
extern void hlj_pc_limit_motors(const float wheel_rpm[4], int16_t motor_cmds[4]);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
*文件名称:supercap.c
|
||||
*功能描述:超级电容控制板CAN1通讯驱动(TX 0x001 / RX 0x100)
|
||||
*备注:周期发送输入功率上限与使能位,解析超电就绪状态与剩余能量百分比
|
||||
*/
|
||||
#include "supercap.h"
|
||||
#include "bsp_can.h"
|
||||
#include "can.h"
|
||||
#include "main.h"
|
||||
#include <string.h>
|
||||
|
||||
static supercap_fdb_t supercap_fdb;//超电反馈与通讯诊断状态
|
||||
static uint32_t last_tx_tick = 0U; //上次发送时间戳(ms)
|
||||
|
||||
/*
|
||||
*函数简介:CAN1 0x100超级电容反馈帧接收回调
|
||||
*参数说明:std_id:CAN标准帧ID
|
||||
*参数说明:rx_data:8字节接收数据
|
||||
*参数说明:dlc:有效数据长度
|
||||
*返回类型:无
|
||||
*备注:在CAN接收中断上下文调用
|
||||
*/
|
||||
static void supercap_rx_callback(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)std_id;
|
||||
if (rx_data == NULL)
|
||||
return;
|
||||
|
||||
supercap_fdb.last_tick = HAL_GetTick();
|
||||
supercap_fdb.rx_cnt++;
|
||||
|
||||
if (dlc >= 3U)
|
||||
{
|
||||
supercap_fdb.ready = rx_data[0];
|
||||
supercap_fdb.cap_state = rx_data[1];
|
||||
supercap_fdb.energy = rx_data[2];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化超级电容模块并注册CAN1 0x100接收回调
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:必须在can_start()之后由CAN_Task调用
|
||||
*/
|
||||
void supercap_init(void)
|
||||
{
|
||||
memset(&supercap_fdb, 0, sizeof(supercap_fdb));
|
||||
last_tx_tick = 0U;
|
||||
can_rx_register(&hcan1, SUPERCAP_RX_ID, supercap_rx_callback);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:按100Hz周期向超级电容发送使能与输入功率上限指令(0x001)
|
||||
*参数说明:enable:1=使能超电,0=关闭超电
|
||||
*参数说明:power_limit_w:当前裁判系统允许输入功率上限(W)
|
||||
*返回类型:无
|
||||
*备注:内部按SUPERCAP_TX_PERIOD_MS分频发送
|
||||
*/
|
||||
void supercap_update_tx(uint8_t enable, uint8_t power_limit_w)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - last_tx_tick < SUPERCAP_TX_PERIOD_MS)
|
||||
return;
|
||||
last_tx_tick = now;
|
||||
|
||||
CanCtrlData_s pkt = {0};
|
||||
pkt.hcan = &hcan1;
|
||||
pkt.tx_header.StdId = SUPERCAP_TX_ID;
|
||||
pkt.tx_header.IDE = CAN_ID_STD;
|
||||
pkt.tx_header.RTR = CAN_RTR_DATA;
|
||||
pkt.tx_header.DLC = SUPERCAP_DLC;
|
||||
pkt.tx_data[0] = enable ? 1U : 0U;
|
||||
pkt.tx_data[2] = power_limit_w;
|
||||
|
||||
if (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) > 0U)
|
||||
{
|
||||
CAN_SendTxMessage(&pkt);
|
||||
supercap_fdb.tx_cnt++;
|
||||
}
|
||||
else
|
||||
{
|
||||
supercap_fdb.tx_err++;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:查询超级电容是否在线
|
||||
*参数说明:无
|
||||
*返回类型:uint8_t:1=在线,0=离线或超时
|
||||
*备注:无
|
||||
*/
|
||||
uint8_t supercap_is_online(void)
|
||||
{
|
||||
uint32_t tick = supercap_fdb.last_tick;
|
||||
if (tick == 0U)
|
||||
return 0U;
|
||||
return (uint8_t)((HAL_GetTick() - tick) < SUPERCAP_OFFLINE_TIME_MS);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:获取超级电容最新反馈状态只读指针
|
||||
*参数说明:无
|
||||
*返回类型:const supercap_fdb_t *:反馈结构体指针
|
||||
*备注:无
|
||||
*/
|
||||
const supercap_fdb_t * supercap_get_fdb(void)
|
||||
{
|
||||
return &supercap_fdb;
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
#ifndef SUPERCAP_H
|
||||
#define SUPERCAP_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define SUPERCAP_TX_ID 0x001U//C板->超电控制帧标准ID
|
||||
#define SUPERCAP_RX_ID 0x100U//超电->C板反馈帧标准ID
|
||||
#define SUPERCAP_DLC 8U //CAN帧数据长度
|
||||
#define SUPERCAP_TX_PERIOD_MS 10U //发送周期(10ms=100Hz)
|
||||
#define SUPERCAP_OFFLINE_TIME_MS 200U //超电反馈超时掉线判定(ms)
|
||||
|
||||
/* 超级电容反馈状态结构体 */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t ready; //就绪标志(1=超电板就绪可放电)
|
||||
uint8_t cap_state; //超电工作状态码
|
||||
uint8_t energy; //电容剩余能量百分比(0~100)
|
||||
uint32_t last_tick; //最近一次收到反馈的时间戳(ms)
|
||||
uint32_t rx_cnt; //反馈接收计数
|
||||
uint32_t tx_cnt; //控制帧发送成功计数
|
||||
uint32_t tx_err; //控制帧发送失败计数
|
||||
} supercap_fdb_t;
|
||||
|
||||
/*
|
||||
*函数简介:初始化超级电容模块并注册CAN1 0x100接收回调
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:必须在can_start()之后由CAN_Task调用
|
||||
*/
|
||||
extern void supercap_init(void);
|
||||
|
||||
/*
|
||||
*函数简介:按100Hz周期向超级电容发送使能与输入功率上限指令(0x001)
|
||||
*参数说明:enable:1=使能超电,0=关闭超电
|
||||
*参数说明:power_limit_w:当前裁判系统允许输入功率上限(W)
|
||||
*返回类型:无
|
||||
*备注:内部按SUPERCAP_TX_PERIOD_MS分频发送
|
||||
*/
|
||||
extern void supercap_update_tx(uint8_t enable, uint8_t power_limit_w);
|
||||
|
||||
/*
|
||||
*函数简介:查询超级电容是否在线
|
||||
*参数说明:无
|
||||
*返回类型:uint8_t:1=在线,0=离线或超时
|
||||
*备注:无
|
||||
*/
|
||||
extern uint8_t supercap_is_online(void);
|
||||
|
||||
/*
|
||||
*函数简介:获取超级电容最新反馈状态只读指针
|
||||
*参数说明:无
|
||||
*返回类型:const supercap_fdb_t *:反馈结构体指针
|
||||
*备注:无
|
||||
*/
|
||||
extern const supercap_fdb_t * supercap_get_fdb(void);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in new issue
Block a user