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
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
#include "dr16.h"
|
||||
#include <stddef.h>
|
||||
|
||||
dr16_t dr16;//全局遥控器数据实例
|
||||
uint8_t dr16_status = 0;//遥控器收帧状态(1-曾收到帧,0-未连接)
|
||||
uint8_t dr16_start_flag = 1;//启动标志(1-未连接,2-已收到首帧)
|
||||
uint32_t dr16_last_tick = 0;//最近收帧系统tick(HAL_GetTick)
|
||||
|
||||
/*
|
||||
*函数简介:按键上升沿翻转锁存辅助函数
|
||||
*参数说明:上一帧按键状态
|
||||
*参数说明:当前帧按键状态
|
||||
*参数说明:锁存变量指针
|
||||
*返回类型:无
|
||||
*备注:仅在0->1跳变瞬间翻转*latch
|
||||
*/
|
||||
static void dr16_toggle_on_rising(uint8_t prev, uint8_t curr, uint8_t * latch)
|
||||
{
|
||||
if (prev == 0U && curr == 1U)
|
||||
*latch = !(*latch);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:解析一帧18字节DT7遥控器原始数据
|
||||
*参数说明:18字节原始报文指针
|
||||
*返回类型:无
|
||||
*备注:通常在USART3空闲中断回调中调用,内部更新在线时间戳与按键翻转边沿
|
||||
*/
|
||||
void dr16_parse(const uint8_t * data)
|
||||
{
|
||||
uint8_t last_key_q = dr16.key_q;
|
||||
uint8_t last_key_e = dr16.key_e;
|
||||
uint8_t last_key_shift = dr16.key_shift;
|
||||
uint8_t last_key_ctrl = dr16.key_ctrl;
|
||||
uint8_t last_mouse_r = dr16.mouse_key_r;
|
||||
|
||||
if (data == NULL)
|
||||
return;
|
||||
|
||||
dr16.rc_r_RL = (((uint16_t)data[1] << 8) | data[0]) & 0x07FF;//B[0:10]
|
||||
dr16.rc_r_UD = (((uint16_t)data[2] << 5) | (data[1] >> 3)) & 0x07FF;//B[11:21]
|
||||
dr16.rc_l_RL = (((uint16_t)data[4] << 10) | (((uint16_t)data[3] << 2) | (data[2] >> 6))) & 0x07FF;//B[22:32]
|
||||
dr16.rc_l_UD = (((uint16_t)data[5] << 7) | (data[4] >> 1)) & 0x07FF;//B[33:43]
|
||||
|
||||
dr16.s2 = (data[5] >> 4) & 0x03;//B[44:45]
|
||||
dr16.s1 = (data[5] >> 6) & 0x03;//B[46:47]
|
||||
|
||||
dr16.mouse_rl = (int16_t)(((uint16_t)data[7] << 8) | data[6]);//B[48:63]
|
||||
dr16.mouse_du = (int16_t)(((uint16_t)data[9] << 8) | data[8]);//B[64:79]
|
||||
dr16.mouse_wheel = (int16_t)(((uint16_t)data[11] << 8) | data[10]);//B[80:95]
|
||||
dr16.mouse_key_l = data[12];
|
||||
dr16.mouse_key_r = data[13];
|
||||
|
||||
dr16.key_w = data[14] & 0x01;
|
||||
dr16.key_s = (data[14] >> 1) & 0x01;
|
||||
dr16.key_a = (data[14] >> 2) & 0x01;
|
||||
dr16.key_d = (data[14] >> 3) & 0x01;
|
||||
dr16.key_shift = (data[14] >> 4) & 0x01;
|
||||
dr16.key_ctrl = (data[14] >> 5) & 0x01;
|
||||
dr16.key_q = (data[14] >> 6) & 0x01;
|
||||
dr16.key_e = (data[14] >> 7) & 0x01;
|
||||
dr16.key_alpha1 = (data[14] >> 9) & 0x01;
|
||||
dr16.key_alpha2 = (data[14] >> 10) & 0x01;
|
||||
|
||||
dr16.thumb_wheel = (int16_t)(((uint16_t)data[17] << 8) | data[16]);//B[120:135]
|
||||
|
||||
dr16_toggle_on_rising(last_key_q, dr16.key_q, &dr16.key_push_q);
|
||||
dr16_toggle_on_rising(last_key_e, dr16.key_e, &dr16.key_push_e);
|
||||
dr16_toggle_on_rising(last_key_shift, dr16.key_shift, &dr16.key_push_shift);
|
||||
dr16_toggle_on_rising(last_key_ctrl, dr16.key_ctrl, &dr16.key_push_ctrl);
|
||||
dr16.mouse_key_push_r = (last_mouse_r == 0U && dr16.mouse_key_r == 1U) ? 1U : 0U;
|
||||
|
||||
dr16_last_tick = HAL_GetTick();
|
||||
dr16_status = 1;
|
||||
|
||||
if (dr16_start_flag == 1U)
|
||||
dr16_start_flag = 2;//首次收到数据后置启动标志
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:判定遥控器是否掉线
|
||||
*参数说明:无
|
||||
*返回类型:1-掉线,0-在线
|
||||
*备注:超过DR16_OFFLINE_TIME_MS(25ms)未收到帧即判为掉线
|
||||
*/
|
||||
uint8_t dr16_offline(void)
|
||||
{
|
||||
return (dr16_status == 0U || (HAL_GetTick() - dr16_last_tick) > DR16_OFFLINE_TIME_MS) ? 1U : 0U;
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
#ifndef DR16_H
|
||||
#define DR16_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
#define DR16_OFFLINE_TIME_MS 25//遥控器掉线判定时间(ms)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t rc_r_RL;//通道0-右摇杆左右(364~1684,中值1024)
|
||||
uint16_t rc_r_UD;//通道1-右摇杆上下(364~1684,中值1024)
|
||||
uint16_t rc_l_RL;//通道2-左摇杆左右(364~1684,中值1024)
|
||||
uint16_t rc_l_UD;//通道3-左摇杆上下(364~1684,中值1024)
|
||||
|
||||
uint8_t s1;//左侧三档拨杆(1-上,2-下,3-中)
|
||||
uint8_t s2;//右侧三档拨杆(1-上,2-下,3-中)
|
||||
|
||||
int16_t mouse_rl;//鼠标X轴速度
|
||||
int16_t mouse_du;//鼠标Y轴速度
|
||||
int16_t mouse_wheel;//鼠标滚轮
|
||||
uint8_t mouse_key_l;//鼠标左键
|
||||
uint8_t mouse_key_r;//鼠标右键
|
||||
|
||||
uint8_t key_w;//W键
|
||||
uint8_t key_s;//S键
|
||||
uint8_t key_a;//A键
|
||||
uint8_t key_d;//D键
|
||||
uint8_t key_shift;//Shift键
|
||||
uint8_t key_ctrl;//Ctrl键
|
||||
uint8_t key_q;//Q键
|
||||
uint8_t key_e;//E键
|
||||
uint8_t key_alpha1;//图传扩展键1
|
||||
uint8_t key_alpha2;//图传扩展键2
|
||||
|
||||
int16_t thumb_wheel;//遥控器左上角拨轮
|
||||
|
||||
uint8_t key_push_q;//Q键按下翻转锁存
|
||||
uint8_t key_push_e;//E键按下翻转锁存
|
||||
uint8_t key_push_shift;//Shift键按下翻转锁存
|
||||
uint8_t key_push_ctrl;//Ctrl键按下翻转锁存
|
||||
uint8_t mouse_key_push_r;//鼠标右键按下瞬间标志
|
||||
} dr16_t;//DT7遥控器与键鼠数据结构体
|
||||
|
||||
extern dr16_t dr16;//全局遥控器数据实例
|
||||
extern uint8_t dr16_status;//遥控器收帧状态(1-曾收到帧,0-未连接)
|
||||
extern uint8_t dr16_start_flag;//启动标志(1-准备启动,2-已收到首帧)
|
||||
extern uint32_t dr16_last_tick;//最近收帧系统tick(HAL_GetTick)
|
||||
|
||||
void dr16_parse(const uint8_t * data);//解析一帧18字节DR16原始数据
|
||||
uint8_t dr16_offline(void);//判定遥控器是否掉线(1-掉线,0-在线)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,242 @@
|
||||
/*
|
||||
*文件名称:robot_cmd_task.c
|
||||
*功能描述:机器人控制指令汇总任务(2ms),发布全局cmd_robot控制结构体
|
||||
*备注:云台板汇总DR16与0x0304图传键鼠,底盘板汇总CToC虚拟遥控器
|
||||
*/
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "dr16.h"
|
||||
#include "c_comm.h"
|
||||
#include "robot_typedef.h"
|
||||
#include "robot_param.h"
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
#include "robot_cmd_task.h"
|
||||
#include "referee.h"
|
||||
#include "gimbal.h"
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
#include "referee.h"
|
||||
#endif
|
||||
|
||||
volatile cmd_robot_t cmd_robot;//全局统一控制指令结构体
|
||||
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
|
||||
keyboard_data_t keyboard_data;//0x0304图传键鼠原始数据
|
||||
static uint8_t keyboard_online = 0U;//0x0304图传键鼠在线标志
|
||||
|
||||
/*
|
||||
*函数简介:USART3 DR16遥控器整帧接收回调
|
||||
*参数说明:frame:接收帧数据指针
|
||||
*参数说明:len:接收帧字节长度
|
||||
*返回类型:无
|
||||
*备注:仅处理18字节标准DR16帧
|
||||
*/
|
||||
void dr16_frame_callback(const uint8_t * frame, uint16_t len)
|
||||
{
|
||||
if (frame != NULL && len == 18U)
|
||||
dr16_parse(frame);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:裁判系统0x0304图传键鼠数据解析钩子
|
||||
*参数说明:data:12字节图传键鼠负载
|
||||
*返回类型:无
|
||||
*备注:在接收中断上下文调用,完成Q/E/Shift/Ctrl按下沿翻转
|
||||
*/
|
||||
static void referee_keyboard_hook(const uint8_t data[12])
|
||||
{
|
||||
if (data == NULL)
|
||||
return;
|
||||
|
||||
keyboard_data.mouse_rl = (int16_t)(((uint16_t)data[1] << 8) | data[0]);
|
||||
keyboard_data.mouse_du = (int16_t)(((uint16_t)data[3] << 8) | data[2]);
|
||||
keyboard_data.mouse_wheel = (int16_t)(((uint16_t)data[5] << 8) | data[4]);
|
||||
keyboard_data.mouse_key_l = data[6];
|
||||
keyboard_data.mouse_key_r = data[7];
|
||||
|
||||
keyboard_data.key_w = data[8] & 0x01U;
|
||||
keyboard_data.key_s = (data[8] >> 1) & 0x01U;
|
||||
keyboard_data.key_a = (data[8] >> 2) & 0x01U;
|
||||
keyboard_data.key_d = (data[8] >> 3) & 0x01U;
|
||||
keyboard_data.key_shift = (data[8] >> 4) & 0x01U;
|
||||
keyboard_data.key_ctrl = (data[8] >> 5) & 0x01U;
|
||||
keyboard_data.key_q = (data[8] >> 6) & 0x01U;
|
||||
keyboard_data.key_e = (data[8] >> 7) & 0x01U;
|
||||
|
||||
if (keyboard_data.key_last_q == 0U && keyboard_data.key_q == 1U)
|
||||
keyboard_data.key_push_q = (uint8_t)!keyboard_data.key_push_q;
|
||||
if (keyboard_data.key_last_e == 0U && keyboard_data.key_e == 1U)
|
||||
keyboard_data.key_push_e = (uint8_t)!keyboard_data.key_push_e;
|
||||
if (keyboard_data.key_last_shift == 0U && keyboard_data.key_shift == 1U)
|
||||
keyboard_data.key_push_shift = (uint8_t)!keyboard_data.key_push_shift;
|
||||
if (keyboard_data.key_last_ctrl == 0U && keyboard_data.key_ctrl == 1U)
|
||||
keyboard_data.key_push_ctrl = (uint8_t)!keyboard_data.key_push_ctrl;
|
||||
|
||||
keyboard_data.key_last_q = keyboard_data.key_q;
|
||||
keyboard_data.key_last_e = keyboard_data.key_e;
|
||||
keyboard_data.key_last_shift = keyboard_data.key_shift;
|
||||
keyboard_data.key_last_ctrl = keyboard_data.key_ctrl;
|
||||
keyboard_data.last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:云台板控制源汇总任务(2ms)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:无
|
||||
*/
|
||||
void robot_cmd_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
static uint8_t spin_flag = 0U;//小陀螺保持标志
|
||||
static uint8_t last_ctrl = 0U;//上一拍Ctrl状态
|
||||
|
||||
referee_set_keyboard_hook(referee_keyboard_hook);
|
||||
|
||||
while (!app_ready)
|
||||
osDelay(2);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
keyboard_online = (uint8_t)(keyboard_data.last_tick != 0U &&
|
||||
(HAL_GetTick() - keyboard_data.last_tick < DEVICE_OFFLINE_TIME_MS * 10U));
|
||||
|
||||
cmd_robot.online = (uint8_t)(dr16_status == 1 && !dr16_offline());
|
||||
cmd_robot.source = keyboard_online ? 1U : 0U;
|
||||
|
||||
if (cmd_robot.online)
|
||||
{
|
||||
float yaw_delta = 0.0f;
|
||||
float pitch_delta = 0.0f;
|
||||
uint16_t pos = motor_pitch_gimbal.fdb.raw_angle;
|
||||
|
||||
if (!keyboard_online)
|
||||
{
|
||||
if (dr16.rc_l_UD > 1050 && pos > PITCH_GM6020_POSITION_UPPER_LIMIT)
|
||||
pitch_delta -= GIMBAL_PITCH_LEVER_RATE / 8192.0f * 360.0f *
|
||||
((float)dr16.rc_l_UD - 1024.0f) / 660.0f;
|
||||
else if (dr16.rc_l_UD < 1000 && pos < PITCH_GM6020_POSITION_LOWER_LIMIT)
|
||||
pitch_delta += GIMBAL_PITCH_LEVER_RATE / 8192.0f * 360.0f *
|
||||
(1024.0f - (float)dr16.rc_l_UD) / 660.0f;
|
||||
|
||||
if (dr16.rc_l_RL > 1050 || dr16.rc_l_RL < 1000)
|
||||
yaw_delta -= GIMBAL_LEVER_SPEED_MAP_RATE * GIMBAL_YAW_PITCH_SPEED_RATE *
|
||||
GIMBAL_YAW_PITCH_SPEED_RATE * 0.0439453125f *
|
||||
((float)dr16.rc_l_RL - 1024.0f) / 660.0f;
|
||||
|
||||
cmd_robot.friction_on = (uint8_t)(dr16.s1 == 1U);
|
||||
cmd_robot.fire = (uint8_t)(dr16.thumb_wheel < 1000);
|
||||
cmd_robot.eject = (uint8_t)(dr16.thumb_wheel > 1050);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (keyboard_data.mouse_du > 8 && pos > PITCH_GM6020_POSITION_UPPER_LIMIT)
|
||||
pitch_delta -= GIMBAL_PITCH_LEVER_RATE / 8192.0f * 360.0f *
|
||||
((float)keyboard_data.mouse_du * PC_MOUSE_DU_SENSITIVITY) / 660.0f;
|
||||
else if (keyboard_data.mouse_du < -8 && pos < PITCH_GM6020_POSITION_LOWER_LIMIT)
|
||||
pitch_delta += GIMBAL_PITCH_LEVER_RATE / 8192.0f * 360.0f *
|
||||
(-(float)keyboard_data.mouse_du * PC_MOUSE_DU_SENSITIVITY) / 660.0f;
|
||||
|
||||
if (keyboard_data.mouse_rl != 0)
|
||||
yaw_delta -= GIMBAL_LEVER_SPEED_MAP_RATE * GIMBAL_YAW_PITCH_SPEED_RATE *
|
||||
GIMBAL_YAW_PITCH_SPEED_RATE * 0.0439453125f *
|
||||
((float)keyboard_data.mouse_rl * PC_MOUSE_RL_SENSITIVITY / 660.0f * 2.0f);
|
||||
|
||||
cmd_robot.friction_on = keyboard_data.key_push_q;
|
||||
cmd_robot.fire = keyboard_data.mouse_key_l;
|
||||
cmd_robot.eject = keyboard_data.key_push_e;
|
||||
}
|
||||
|
||||
cmd_robot.gimbal_yaw_delta = yaw_delta;
|
||||
cmd_robot.gimbal_pitch_delta = pitch_delta;
|
||||
cmd_robot.thumb_wheel = dr16.thumb_wheel;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd_robot.gimbal_yaw_delta = 0.0f;
|
||||
cmd_robot.gimbal_pitch_delta = 0.0f;
|
||||
cmd_robot.friction_on = 0U;
|
||||
cmd_robot.fire = 0U;
|
||||
cmd_robot.eject = 0U;
|
||||
}
|
||||
|
||||
uint8_t ctrl_now = (!keyboard_online) ? dr16.key_push_ctrl : 0U;
|
||||
if (last_ctrl == 0U && ctrl_now == 1U)
|
||||
spin_flag = (uint8_t)!spin_flag;
|
||||
last_ctrl = ctrl_now;
|
||||
cmd_robot.spin_flag = spin_flag;
|
||||
|
||||
cmd_robot.gimbal_reset = 0U;
|
||||
if (dr16_start_flag == 2U)
|
||||
{
|
||||
dr16_start_flag = 0U;
|
||||
cmd_robot.gimbal_reset = 1U;
|
||||
cmd_robot.spin_flag = 0U;
|
||||
}
|
||||
|
||||
monitor_heartbeat(HB_CMD);
|
||||
osDelay(2);
|
||||
}
|
||||
}
|
||||
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
|
||||
/*
|
||||
*函数简介:底盘板控制源汇总任务(2ms)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:从CToC转发的虚拟遥控器(0x149/0x189)生成底盘控制指令
|
||||
*/
|
||||
void robot_cmd_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
while (!app_ready)
|
||||
osDelay(2);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
uint8_t ctoc_alive = (uint8_t)(ctoc_remote.status == 1U &&
|
||||
ctoc_remote.last_tick != 0U &&
|
||||
(HAL_GetTick() - ctoc_remote.last_tick < CTOC_OFFLINE_TIME_MS) &&
|
||||
ctoc_remote.gimbal_fault == 0U);
|
||||
cmd_robot.online = ctoc_alive;
|
||||
cmd_robot.source = 0U;
|
||||
|
||||
if (cmd_robot.online)
|
||||
{
|
||||
if (ctoc_remote.spin_flag)
|
||||
cmd_robot.chassis_mode = CHASSIS_MODE_SPIN_FAST;
|
||||
else if (ctoc_remote.s2 == 1U)
|
||||
cmd_robot.chassis_mode = CHASSIS_MODE_SPIN_SLOW;
|
||||
else if (ctoc_remote.s2 == 2U)
|
||||
cmd_robot.chassis_mode = CHASSIS_MODE_SPIN_FAST;
|
||||
else
|
||||
cmd_robot.chassis_mode = CHASSIS_MODE_FOLLOW;
|
||||
|
||||
cmd_robot.vx = (1024.0f - (float)ctoc_remote.rc_r_ud) * MECANUM_CHASSIS_SPEED_SCALE;
|
||||
cmd_robot.vy = (1024.0f - (float)ctoc_remote.rc_r_rl) * MECANUM_CHASSIS_SPEED_SCALE;
|
||||
cmd_robot.wz_manual = 0.0f;//左摇杆仅控云台Yaw,底盘由跟随/小陀螺接管
|
||||
|
||||
cmd_robot.gimbal_reset = 0U;
|
||||
if (ctoc_remote.start_flag == 2U)
|
||||
{
|
||||
ctoc_remote.start_flag = 0U;
|
||||
cmd_robot.gimbal_reset = 1U;//复用字段:跟随朝向重新锁定
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd_robot.chassis_mode = CHASSIS_MODE_OFF;
|
||||
cmd_robot.vx = 0.0f;
|
||||
cmd_robot.vy = 0.0f;
|
||||
cmd_robot.wz_manual = 0.0f;
|
||||
}
|
||||
|
||||
monitor_heartbeat(HB_CMD);
|
||||
osDelay(2);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,25 @@
|
||||
#ifndef ROBOT_CMD_TASK_H
|
||||
#define ROBOT_CMD_TASK_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "robot_typedef.h"
|
||||
|
||||
/* 图传键鼠数据结构体(0x0304解析结果) */
|
||||
typedef struct
|
||||
{
|
||||
int16_t mouse_rl; //鼠标水平增量
|
||||
int16_t mouse_du; //鼠标垂直增量
|
||||
int16_t mouse_wheel; //鼠标滚轮增量
|
||||
uint8_t mouse_key_l; //鼠标左键状态
|
||||
uint8_t mouse_key_r; //鼠标右键状态
|
||||
uint8_t key_w, key_s, key_a, key_d; //方向键状态
|
||||
uint8_t key_shift, key_ctrl, key_q, key_e; //功能键状态
|
||||
uint8_t key_alpha1, key_alpha2; //数字键状态
|
||||
uint8_t key_last_q, key_last_e, key_last_shift, key_last_ctrl;//上一拍按键状态
|
||||
uint8_t key_push_q, key_push_e, key_push_shift, key_push_ctrl;//按下翻转锁存状态
|
||||
uint32_t last_tick; //最近更新时间戳(ms)
|
||||
} keyboard_data_t;
|
||||
|
||||
extern keyboard_data_t keyboard_data;//0x0304图传键鼠原始数据
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,342 @@
|
||||
#include "c_comm.h"
|
||||
#include "robot_param.h"
|
||||
#include "referee.h"
|
||||
#include <string.h>
|
||||
|
||||
volatile ctoc_remote_t ctoc_remote;//底盘虚拟遥控器数据
|
||||
volatile uint8_t ctoc_shooter_power = 1;//发射机构上电状态(默认1便于台架单板调试)
|
||||
volatile uint32_t ctoc_reply_last_tick = 0;//最近收到0x019的tick
|
||||
volatile uint8_t ctoc_remote_fault = 0;//底盘电机故障标志
|
||||
volatile float ctoc_gimbal_yaw = 0.0f;//云台世界系偏航角(°)
|
||||
volatile uint8_t ctoc_att_ready = 0;//云台姿态就绪标志
|
||||
static volatile uint32_t ctoc_att_last_tick = 0;//最近收到0x1A9的tick
|
||||
volatile int8_t ctoc_encoder_ccw = 0;//云台编码器增向(+1=CCW,-1=CW,0=未检测)
|
||||
|
||||
volatile float ctoc_ui_pitch_deg = 0.0f;//UI用云台Pitch角(°)
|
||||
volatile float ctoc_ui_fric_l_rpm = 0.0f;//UI用左摩擦轮转速(RPM)
|
||||
volatile float ctoc_ui_fric_r_rpm = 0.0f;//UI用右摩擦轮转速(RPM)
|
||||
volatile uint8_t ctoc_ui_spin_flag = 0;//UI用小陀螺标志
|
||||
volatile uint32_t ctoc_ui_last_tick = 0;//最近收到0x1B9的tick
|
||||
|
||||
/*
|
||||
*函数简介:判定0x1A9云台姿态帧是否新鲜
|
||||
*参数说明:无
|
||||
*返回类型:1-新鲜,0-超时或未收到
|
||||
*备注:底盘跟随时用其门控,云台侧留作诊断
|
||||
*/
|
||||
uint8_t ctoc_att_fresh(void)
|
||||
{
|
||||
return (ctoc_att_last_tick != 0U &&
|
||||
(HAL_GetTick() - ctoc_att_last_tick) < CTOC_OFFLINE_TIME_MS) ? 1U : 0U;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:将4个uint16大端打包为8字节CAN数据
|
||||
*参数说明:输出8字节数组
|
||||
*参数说明:输入4个uint16数组
|
||||
*返回类型:无
|
||||
*备注:无
|
||||
*/
|
||||
static void c_comm_pack_uint16(uint8_t data[8], const uint16_t val[4])
|
||||
{
|
||||
if (data == NULL || val == NULL)
|
||||
return;
|
||||
|
||||
for (uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
data[2 * i] = (uint8_t)(val[i] >> 8);
|
||||
data[2 * i + 1] = (uint8_t)(val[i] & 0xFFU);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:发送一帧8字节标准CAN报文
|
||||
*参数说明:目标CAN句柄指针
|
||||
*参数说明:标准帧ID
|
||||
*参数说明:8字节发送数据
|
||||
*返回类型:无
|
||||
*备注:内部使用零初始化CanCtrlData_s并经CAN_SendTxMessage发送
|
||||
*/
|
||||
static void c_comm_send_frame(hcan_t * hcan, uint32_t std_id, const uint8_t data[8])
|
||||
{
|
||||
CanCtrlData_s msg = {0};
|
||||
|
||||
if (hcan == NULL || data == NULL)
|
||||
return;
|
||||
|
||||
msg.hcan = hcan;
|
||||
msg.tx_header.StdId = std_id;
|
||||
msg.tx_header.IDE = CAN_ID_STD;//标准帧
|
||||
msg.tx_header.RTR = CAN_RTR_DATA;//数据帧
|
||||
msg.tx_header.DLC = 8;//8字节
|
||||
msg.tx_header.TransmitGlobalTime = DISABLE;
|
||||
memcpy(msg.tx_data, data, 8);
|
||||
CAN_SendTxMessage(&msg);
|
||||
}
|
||||
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
|
||||
/*
|
||||
*函数简介:云台侧0x019底盘回传帧接收回调
|
||||
*参数说明:标准帧ID
|
||||
*参数说明:8字节接收数据
|
||||
*参数说明:数据长度
|
||||
*返回类型:无
|
||||
*备注:ISR上下文执行,解析发射机构上电状态与底盘故障位
|
||||
*/
|
||||
static void c_comm_rx_0x019(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)std_id;
|
||||
(void)dlc;
|
||||
ctoc_shooter_power = rx_data[0];//发射机构上电状态
|
||||
ctoc_remote_fault = rx_data[1] & 0x01U;//底盘电机故障位
|
||||
ctoc_reply_last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化板间通讯(云台主机)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:在can_start()之后调用,注册CAN2 0x019接收回调
|
||||
*/
|
||||
void c_comm_init(void)
|
||||
{
|
||||
can_rx_register(&hcan2, CTOC_MASTER_ID1, c_comm_rx_0x019);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:云台板综合判定底盘是否故障
|
||||
*参数说明:无
|
||||
*返回类型:1-故障或超时,0-正常或底盘不在场
|
||||
*备注:从未收到过0x019视为底盘不在场(方便云台单独台架调试)
|
||||
*/
|
||||
uint8_t ctoc_chassis_fault(void)
|
||||
{
|
||||
if (ctoc_reply_last_tick == 0U)
|
||||
return 0;
|
||||
if ((HAL_GetTick() - ctoc_reply_last_tick) > CTOC_OFFLINE_TIME_MS)
|
||||
return 1;
|
||||
return ctoc_remote_fault;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:云台板发送0x1A9世界系姿态帧
|
||||
*参数说明:云台世界系偏航角(°)
|
||||
*参数说明:状态标志位(bit0=att_ready,bit1=编码器增向,bit2=自检完成)
|
||||
*返回类型:无
|
||||
*备注:float按小端序拷贝至Data[0..3]
|
||||
*/
|
||||
void c_comm_master_send_attitude(float yaw_deg, uint8_t flags)
|
||||
{
|
||||
uint8_t data[8] = {0};
|
||||
|
||||
memcpy(data, &yaw_deg, sizeof(yaw_deg));
|
||||
data[4] = flags;
|
||||
c_comm_send_frame(&hcan2, CTOC_SLAVE_ID3, data);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:云台板发送摇杆帧(0x149)与控制状态帧(0x189)
|
||||
*参数说明:键鼠是否在线(1-键鼠合成摇杆量,0-转发DR16摇杆)
|
||||
*参数说明:键鼠12字节报文缓冲区
|
||||
*参数说明:云台电机掉线故障标志
|
||||
*参数说明:小陀螺使能标志
|
||||
*返回类型:无
|
||||
*备注:Data[0]实时使用dr16_offline()判定在线状态,防止掉线后保持旧值
|
||||
*/
|
||||
void c_comm_master_send(
|
||||
uint8_t keyboard_online, const uint8_t kb[12], uint8_t motor_fault, uint8_t spin_enable)
|
||||
{
|
||||
uint8_t data[8] = {0};
|
||||
|
||||
if (keyboard_online && kb == NULL)
|
||||
keyboard_online = 0;
|
||||
|
||||
if (!keyboard_online)
|
||||
{
|
||||
const uint16_t sticks[4] = {dr16.rc_r_RL, dr16.rc_r_UD, dr16.rc_l_RL, dr16.rc_l_UD};
|
||||
c_comm_pack_uint16(data, sticks);
|
||||
}
|
||||
else
|
||||
{
|
||||
uint8_t key_go = kb[8] & 0x01U;
|
||||
uint8_t key_back = (kb[8] >> 1) & 0x01U;
|
||||
uint8_t key_left = (kb[8] >> 2) & 0x01U;
|
||||
uint8_t key_right = (kb[8] >> 3) & 0x01U;
|
||||
int16_t mouse_rl = (int16_t)(((uint16_t)kb[1] << 8) | kb[0]);
|
||||
int16_t mouse_du = (int16_t)(((uint16_t)kb[3] << 8) | kb[2]);
|
||||
|
||||
const uint16_t sticks[4] = {
|
||||
(uint16_t)(1024 + (key_right - key_left) * 660),//AD->右摇杆RL
|
||||
(uint16_t)(1024 + (key_go - key_back) * 660),//WS->右摇杆UD
|
||||
(uint16_t)(1024 + mouse_rl * PC_MOUSE_RL_SENSITIVITY),//鼠标->左摇杆RL
|
||||
(uint16_t)(1024 + mouse_du * PC_MOUSE_DU_SENSITIVITY),//鼠标->左摇杆UD
|
||||
};
|
||||
c_comm_pack_uint16(data, sticks);
|
||||
}
|
||||
c_comm_send_frame(&hcan2, CTOC_SLAVE_ID1, data);
|
||||
|
||||
data[0] = (dr16_status && !dr16_offline()) ? 1U : 0U;//实时在线判定
|
||||
data[1] = dr16.s2;
|
||||
data[2] = dr16.key_push_ctrl;
|
||||
data[3] = dr16.key_push_shift;
|
||||
data[4] = dr16_start_flag;
|
||||
data[5] = dr16.s1;
|
||||
data[6] = (motor_fault ? 0x80U : 0U) | (spin_enable ? 0x04U : 0U) |
|
||||
(dr16.key_alpha1 ? 0x01U : 0U) | (dr16.key_alpha2 ? 0x02U : 0U);//bit7=故障,bit2=陀螺使能
|
||||
data[7] = 0;
|
||||
c_comm_send_frame(&hcan2, CTOC_SLAVE_ID2, data);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:云台板发送0x1B9自定义UI专用数据帧
|
||||
*参数说明:云台Pitch角(°)
|
||||
*参数说明:左摩擦轮转速(RPM)
|
||||
*参数说明:右摩擦轮转速(RPM)
|
||||
*参数说明:小陀螺使能标志
|
||||
*返回类型:无
|
||||
*备注:[0..1]pitch(0.01°),[2..3]左摩擦轮(rpm/100),[4..5]右摩擦轮(rpm/100),[6]bit0=小陀螺
|
||||
*/
|
||||
void c_comm_master_send_ui(float pitch_deg, float fric_l_rpm, float fric_r_rpm, uint8_t spin_on)
|
||||
{
|
||||
uint8_t data[8] = {0};
|
||||
int16_t p = (int16_t)(pitch_deg * 100.0f);
|
||||
int16_t fl = (int16_t)(fric_l_rpm / 100.0f);
|
||||
int16_t fr = (int16_t)(fric_r_rpm / 100.0f);
|
||||
|
||||
data[0] = (uint8_t)(p & 0xFF);
|
||||
data[1] = (uint8_t)((uint16_t)p >> 8);
|
||||
data[2] = (uint8_t)(fl & 0xFF);
|
||||
data[3] = (uint8_t)((uint16_t)fl >> 8);
|
||||
data[4] = (uint8_t)(fr & 0xFF);
|
||||
data[5] = (uint8_t)((uint16_t)fr >> 8);
|
||||
data[6] = spin_on ? 0x01U : 0U;
|
||||
c_comm_send_frame(&hcan2, CTOC_SLAVE_ID4, data);
|
||||
}
|
||||
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
|
||||
/*
|
||||
*函数简介:从2字节小端缓冲解包int16
|
||||
*参数说明:指向2字节小端数据的指针
|
||||
*返回类型:解包后的int16_t值
|
||||
*备注:无
|
||||
*/
|
||||
static int16_t c_comm_unpack_int16_le(const uint8_t * buf)
|
||||
{
|
||||
return (int16_t)(((uint16_t)buf[1] << 8) | buf[0]);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘侧0x149摇杆帧接收回调
|
||||
*参数说明:标准帧ID
|
||||
*参数说明:8字节接收数据
|
||||
*参数说明:数据长度
|
||||
*返回类型:无
|
||||
*备注:ISR上下文执行
|
||||
*/
|
||||
static void c_comm_rx_0x149(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)std_id;
|
||||
(void)dlc;
|
||||
ctoc_remote.rc_r_rl = ((uint16_t)rx_data[0] << 8) | rx_data[1];
|
||||
ctoc_remote.rc_r_ud = ((uint16_t)rx_data[2] << 8) | rx_data[3];
|
||||
ctoc_remote.rc_l_rl = ((uint16_t)rx_data[4] << 8) | rx_data[5];
|
||||
ctoc_remote.rc_l_ud = ((uint16_t)rx_data[6] << 8) | rx_data[7];
|
||||
ctoc_remote.last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘侧0x189控制状态帧接收回调
|
||||
*参数说明:标准帧ID
|
||||
*参数说明:8字节接收数据
|
||||
*参数说明:数据长度
|
||||
*返回类型:无
|
||||
*备注:ISR上下文执行
|
||||
*/
|
||||
static void c_comm_rx_0x189(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)std_id;
|
||||
(void)dlc;
|
||||
ctoc_remote.status = rx_data[0];
|
||||
ctoc_remote.s2 = rx_data[1];
|
||||
ctoc_remote.ctrl_push = rx_data[2];
|
||||
ctoc_remote.shift_push = rx_data[3];
|
||||
ctoc_remote.start_flag = rx_data[4];
|
||||
ctoc_remote.s1 = rx_data[5];
|
||||
ctoc_remote.spin_flag = (rx_data[6] >> 2) & 0x01U;//小陀螺使能位
|
||||
ctoc_remote.gimbal_fault = (rx_data[6] & 0x80U) ? 1U : 0U;//云台电机故障位
|
||||
ctoc_remote.last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘侧0x1A9云台姿态帧接收回调
|
||||
*参数说明:标准帧ID
|
||||
*参数说明:8字节接收数据
|
||||
*参数说明:数据长度
|
||||
*返回类型:无
|
||||
*备注:ISR上下文执行
|
||||
*/
|
||||
static void c_comm_rx_0x1a9(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
float yaw;
|
||||
|
||||
(void)std_id;
|
||||
(void)dlc;
|
||||
memcpy(&yaw, rx_data, sizeof(yaw));
|
||||
ctoc_gimbal_yaw = yaw;
|
||||
ctoc_att_ready = rx_data[4] & 0x01U;
|
||||
if (rx_data[4] & 0x04U)//云台自检完成位
|
||||
ctoc_encoder_ccw = (rx_data[4] & 0x02U) ? 1 : -1;//tick增方向
|
||||
ctoc_att_last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘侧0x1B9自定义UI数据帧接收回调
|
||||
*参数说明:标准帧ID
|
||||
*参数说明:8字节接收数据
|
||||
*参数说明:数据长度
|
||||
*返回类型:无
|
||||
*备注:[0..1]pitch(0.01°),[2..3]左摩擦轮(rpm/100),[4..5]右摩擦轮(rpm/100),[6]bit0=小陀螺
|
||||
*/
|
||||
static void c_comm_rx_0x1b9(uint32_t std_id, const uint8_t rx_data[8], uint32_t dlc)
|
||||
{
|
||||
(void)std_id;
|
||||
(void)dlc;
|
||||
ctoc_ui_pitch_deg = (float)c_comm_unpack_int16_le(&rx_data[0]) * 0.01f;
|
||||
ctoc_ui_fric_l_rpm = (float)c_comm_unpack_int16_le(&rx_data[2]) * 100.0f;
|
||||
ctoc_ui_fric_r_rpm = (float)c_comm_unpack_int16_le(&rx_data[4]) * 100.0f;
|
||||
ctoc_ui_spin_flag = rx_data[6] & 0x01U;
|
||||
ctoc_ui_last_tick = HAL_GetTick();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化板间通讯(底盘从机)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:在can_start()之后调用,注册CAN2 0x149/0x189/0x1A9/0x1B9接收回调
|
||||
*/
|
||||
void c_comm_init(void)
|
||||
{
|
||||
can_rx_register(&hcan2, CTOC_SLAVE_ID1, c_comm_rx_0x149);
|
||||
can_rx_register(&hcan2, CTOC_SLAVE_ID2, c_comm_rx_0x189);
|
||||
can_rx_register(&hcan2, CTOC_SLAVE_ID3, c_comm_rx_0x1a9);
|
||||
can_rx_register(&hcan2, CTOC_SLAVE_ID4, c_comm_rx_0x1b9);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘板发送0x019回传状态帧
|
||||
*参数说明:底盘电机故障标志
|
||||
*返回类型:无
|
||||
*备注:回传发射机构上电状态与底盘电机故障位
|
||||
*/
|
||||
void c_comm_slave_send(uint8_t motor_fault)
|
||||
{
|
||||
uint8_t data[8] = {0};
|
||||
|
||||
data[0] = referee.shooter_power_on;//发射机构上电状态
|
||||
data[1] = motor_fault ? 0x01U : 0U;//底盘电机故障位
|
||||
c_comm_send_frame(&hcan2, CTOC_MASTER_ID1, data);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,49 @@
|
||||
#ifndef C_COMM_H
|
||||
#define C_COMM_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "bsp_can.h"
|
||||
#include "dr16.h"
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint16_t rc_r_rl;//右摇杆左右(0x149 Data[0..1])
|
||||
uint16_t rc_r_ud;//右摇杆上下(0x149 Data[2..3])
|
||||
uint16_t rc_l_rl;//左摇杆左右(0x149 Data[4..5])
|
||||
uint16_t rc_l_ud;//左摇杆上下(0x149 Data[6..7])
|
||||
uint8_t s1;//左侧拨杆(0x189 Data[5])
|
||||
uint8_t s2;//右侧拨杆(0x189 Data[1])
|
||||
uint8_t ctrl_push;//保留字段(0x189 Data[2])
|
||||
uint8_t shift_push;//Shift翻转态(0x189 Data[3])
|
||||
uint8_t start_flag;//启动标志(0x189 Data[4])
|
||||
uint8_t status;//云台侧遥控器在线状态(0x189 Data[0])
|
||||
uint8_t spin_flag;//小陀螺使能(0x189 Data[6] bit2)
|
||||
uint8_t gimbal_fault;//云台电机故障标志(0x189 Data[6] bit7)
|
||||
uint32_t last_tick;//最近收帧tick(掉线检测)
|
||||
} ctoc_remote_t;//底盘虚拟遥控器数据结构体(由0x149/0x189解析)
|
||||
|
||||
extern volatile ctoc_remote_t ctoc_remote;//底盘接收到的虚拟遥控器数据
|
||||
|
||||
extern volatile uint8_t ctoc_shooter_power;//底盘回传发射机构上电状态(0x019 Data[0])
|
||||
extern volatile uint32_t ctoc_reply_last_tick;//云台最近收到0x019的系统tick
|
||||
extern volatile uint8_t ctoc_remote_fault;//底盘电机故障标志(0x019 Data[1] bit0)
|
||||
|
||||
extern volatile float ctoc_gimbal_yaw;//云台世界系偏航角(0x1A9 Data[0..3],LE float)
|
||||
extern volatile uint8_t ctoc_att_ready;//云台姿态就绪标志(0x1A9 Data[4] bit0)
|
||||
extern volatile int8_t ctoc_encoder_ccw;//云台编码器方向(+1=CCW,-1=CW,0=未检测)
|
||||
|
||||
extern volatile float ctoc_ui_pitch_deg;//UI用云台Pitch角(°)
|
||||
extern volatile float ctoc_ui_fric_l_rpm;//UI用左摩擦轮转速(RPM)
|
||||
extern volatile float ctoc_ui_fric_r_rpm;//UI用右摩擦轮转速(RPM)
|
||||
extern volatile uint8_t ctoc_ui_spin_flag;//UI用小陀螺标志
|
||||
extern volatile uint32_t ctoc_ui_last_tick;//最近收到0x1B9的系统tick
|
||||
|
||||
void c_comm_init(void);//注册板间通讯CAN2接收回调(can_start()之后调用)
|
||||
uint8_t ctoc_chassis_fault(void);//云台板综合判定底盘是否故障(1-故障,0-正常)
|
||||
uint8_t ctoc_att_fresh(void);//判定0x1A9云台姿态帧是否新鲜(1-新鲜,0-超时)
|
||||
void c_comm_master_send_attitude(float yaw_deg, uint8_t flags);//云台板发送0x1A9姿态帧
|
||||
void c_comm_master_send_ui(float pitch_deg, float fric_l_rpm, float fric_r_rpm, uint8_t spin_on);//云台板发送0x1B9自定义UI数据帧
|
||||
void c_comm_master_send(uint8_t keyboard_online, const uint8_t kb[12], uint8_t motor_fault, uint8_t spin_enable);//云台板发送0x149摇杆帧与0x189状态帧
|
||||
void c_comm_slave_send(uint8_t motor_fault);//底盘板发送0x019发射机构状态与故障帧
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
*文件名称:c_comm_task.c
|
||||
*功能描述:板间通讯周期发送任务(2ms),云台板转发摇杆/控制状态与姿态,底盘板回传发射机构上电状态
|
||||
*备注:接收方向在中断内由c_comm模块回调完成,本任务仅负责周期发送
|
||||
*/
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "c_comm.h"
|
||||
#include "robot_typedef.h"
|
||||
#include "robot_param.h"
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
#include "robot_cmd_task.h"
|
||||
#include "gimbal.h"
|
||||
#include "attitude.h"
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
#include "chassis.h"
|
||||
#endif
|
||||
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
/*
|
||||
*函数简介:将键鼠输入数据打包为12字节板间通讯负载
|
||||
*参数说明:kb:输出缓冲区指针(至少12字节)
|
||||
*返回类型:无
|
||||
*备注:内部函数,由c_comm_task在云台板分支调用
|
||||
*/
|
||||
static void c_comm_pack_kb(uint8_t kb[12])
|
||||
{
|
||||
if (kb == NULL)
|
||||
return;
|
||||
|
||||
kb[0] = (uint8_t)(keyboard_data.mouse_rl & 0xFF);
|
||||
kb[1] = (uint8_t)((uint16_t)keyboard_data.mouse_rl >> 8);
|
||||
kb[2] = (uint8_t)(keyboard_data.mouse_du & 0xFF);
|
||||
kb[3] = (uint8_t)((uint16_t)keyboard_data.mouse_du >> 8);
|
||||
kb[4] = (uint8_t)(keyboard_data.mouse_wheel & 0xFF);
|
||||
kb[5] = (uint8_t)((uint16_t)keyboard_data.mouse_wheel >> 8);
|
||||
kb[6] = keyboard_data.mouse_key_l;
|
||||
kb[7] = keyboard_data.mouse_key_r;
|
||||
kb[8] = (uint8_t)(keyboard_data.key_w | (keyboard_data.key_s << 1) |
|
||||
(keyboard_data.key_a << 2) | (keyboard_data.key_d << 3) |
|
||||
(keyboard_data.key_shift << 4) | (keyboard_data.key_ctrl << 5) |
|
||||
(keyboard_data.key_q << 6) | (keyboard_data.key_e << 7));
|
||||
kb[9] = (uint8_t)(keyboard_data.key_alpha1 | (keyboard_data.key_alpha2 << 1));
|
||||
kb[10] = 0;
|
||||
kb[11] = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
*函数简介:板间通讯周期发送任务(2ms)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:云台板发送控制状态与0x1A9姿态帧;底盘板回传发射机构上电状态与电机故障位
|
||||
*/
|
||||
void c_comm_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
TickType_t last_wake = xTaskGetTickCount();//绝对延时基准
|
||||
|
||||
while (!app_ready)
|
||||
osDelay(2);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
uint8_t kb_online = (uint8_t)(keyboard_data.last_tick != 0U &&
|
||||
(HAL_GetTick() - keyboard_data.last_tick < DEVICE_OFFLINE_TIME_MS * 10U));//键鼠在线标志
|
||||
uint8_t kb[12] = {0};//键鼠打包缓冲区
|
||||
|
||||
c_comm_pack_kb(kb);
|
||||
c_comm_master_send(kb_online, kb, gimbal_motor_offline(),
|
||||
cmd_robot.spin_flag);//故障位+小陀螺使能位
|
||||
c_comm_master_send_attitude(att_deg_yaw, att_ready);//0x1A9姿态帧(双INS跟随数据源)
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
c_comm_slave_send(chassis_motor_offline());//携带底盘电机故障位
|
||||
#endif
|
||||
|
||||
monitor_heartbeat(HB_CCOMM);
|
||||
vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,322 @@
|
||||
/*
|
||||
*文件名称:referee.c
|
||||
*功能描述:裁判系统串口协议收发模块(0x0201机器人状态/0x0202实时功率/0x0304图传键鼠/0x0301交互发送)
|
||||
*备注:采用CRC8(0x31,0xFF)与CRC16(0x1021,0xFFFF)位运算校验,解析在中断上下文完成
|
||||
*/
|
||||
#include "referee.h"
|
||||
#include "bsp_usart.h"
|
||||
#include "main.h"
|
||||
|
||||
#define REF_FRAME_MAX_LEN 160U//裁判系统单帧最大长度(字节)
|
||||
|
||||
/*
|
||||
*函数简介:单字节CRC8迭代计算(多项式0x31)
|
||||
*参数说明:crc:当前CRC8值
|
||||
*参数说明:data:输入字节
|
||||
*返回类型:uint8_t:更新后的CRC8值
|
||||
*备注:内部函数
|
||||
*/
|
||||
static uint8_t crc8_update(uint8_t crc, uint8_t data)
|
||||
{
|
||||
crc ^= data;
|
||||
for (uint8_t i = 0U; i < 8U; i++)
|
||||
crc = (crc & 0x80U) ? (uint8_t)((crc << 1) ^ 0x31U) : (uint8_t)(crc << 1);
|
||||
return crc;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:单字节CRC16迭代计算(多项式0x1021)
|
||||
*参数说明:crc:当前CRC16值
|
||||
*参数说明:data:输入字节
|
||||
*返回类型:uint16_t:更新后的CRC16值
|
||||
*备注:内部函数
|
||||
*/
|
||||
static uint16_t crc16_update(uint16_t crc, uint8_t data)
|
||||
{
|
||||
crc ^= (uint16_t)data << 8;
|
||||
for (uint8_t i = 0U; i < 8U; i++)
|
||||
crc = (crc & 0x8000U) ? (uint16_t)((crc << 1) ^ 0x1021U) : (uint16_t)(crc << 1);
|
||||
return crc;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:校验帧头CRC8
|
||||
*参数说明:data:帧头缓冲区指针
|
||||
*参数说明:len:含CRC8在内的总字节数
|
||||
*返回类型:uint8_t:1=校验通过,0=失败
|
||||
*备注:内部函数
|
||||
*/
|
||||
static uint8_t verify_crc8(const uint8_t * data, uint16_t len)
|
||||
{
|
||||
if (data == NULL || len < 1U)
|
||||
return 0U;
|
||||
|
||||
uint8_t crc = 0xFFU;
|
||||
for (uint16_t i = 0U; i < (uint16_t)(len - 1U); i++)
|
||||
crc = crc8_update(crc, data[i]);
|
||||
return (uint8_t)(crc == data[len - 1U]);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:校验整帧CRC16
|
||||
*参数说明:data:整帧缓冲区指针
|
||||
*参数说明:len:含CRC16在内的整帧字节数
|
||||
*返回类型:uint8_t:1=校验通过,0=失败
|
||||
*备注:内部函数
|
||||
*/
|
||||
static uint8_t verify_crc16(const uint8_t * data, uint16_t len)
|
||||
{
|
||||
if (data == NULL || len < 2U)
|
||||
return 0U;
|
||||
|
||||
uint16_t crc = 0xFFFFU;
|
||||
for (uint16_t i = 0U; i < (uint16_t)(len - 2U); i++)
|
||||
crc = crc16_update(crc, data[i]);
|
||||
return (uint8_t)((uint8_t)(crc & 0xFFU) == data[len - 2U] &&
|
||||
(uint8_t)(crc >> 8) == data[len - 1U]);
|
||||
}
|
||||
|
||||
referee_data_t referee = {
|
||||
.robot_id = 0U,
|
||||
.shooter_power_on = 1U,
|
||||
.chassis_power_limit = 60U,
|
||||
.chassis_power = 0.0f,
|
||||
.buffer_energy = 60.0f,
|
||||
.link_status = 0U,
|
||||
.last_update_tick = 0U,
|
||||
};
|
||||
|
||||
static referee_keyboard_hook_f keyboard_hook = NULL;//0x0304键鼠数据回调函数
|
||||
|
||||
/*
|
||||
*函数简介:注册0x0304图传键鼠数据解析回调函数
|
||||
*参数说明:hook:回调函数指针
|
||||
*返回类型:无
|
||||
*备注:仅云台板需要注册
|
||||
*/
|
||||
void referee_set_keyboard_hook(referee_keyboard_hook_f hook)
|
||||
{
|
||||
keyboard_hook = hook;
|
||||
}
|
||||
|
||||
typedef enum
|
||||
{
|
||||
REF_STATE_SOF = 0, //等待帧头0xA5
|
||||
REF_STATE_LEN_LOW, //接收数据长度低字节
|
||||
REF_STATE_LEN_HIGH, //接收数据长度高字节
|
||||
REF_STATE_SEQ, //接收包序号
|
||||
REF_STATE_HEADER_CRC8, //接收帧头CRC8
|
||||
REF_STATE_CMD_LOW, //接收命令码低字节
|
||||
REF_STATE_CMD_HIGH, //接收命令码高字节
|
||||
REF_STATE_DATA, //接收数据负载
|
||||
REF_STATE_CRC16_LOW, //接收帧尾CRC16低字节
|
||||
REF_STATE_CRC16_HIGH, //接收帧尾CRC16高字节
|
||||
} ref_state_e;
|
||||
|
||||
static ref_state_e ref_state = REF_STATE_SOF;//当前解析状态机状态
|
||||
static uint16_t ref_data_len = 0U;//当前帧负载长度
|
||||
static uint16_t ref_cmd_id = 0U;//当前帧命令码
|
||||
static uint16_t ref_data_index = 0U;//当前已接收负载字节计数
|
||||
static uint8_t ref_frame_buf[REF_FRAME_MAX_LEN];//整帧接收拼装缓冲
|
||||
static uint16_t ref_frame_index = 0U;//整帧缓冲写入索引
|
||||
static uint8_t ref_tx_seq = 0U;//发送包自增序号
|
||||
|
||||
/*
|
||||
*函数简介:根据命令码分发并解析校验通过的裁判系统帧
|
||||
*参数说明:cmd_id:命令码
|
||||
*参数说明:data:负载数据指针
|
||||
*参数说明:len:负载字节长度
|
||||
*返回类型:无
|
||||
*备注:内部函数,由CRC16校验通过后调用
|
||||
*/
|
||||
static void referee_dispatch(uint16_t cmd_id, const uint8_t * data, uint16_t len)
|
||||
{
|
||||
if (data == NULL)
|
||||
return;
|
||||
|
||||
switch (cmd_id)
|
||||
{
|
||||
case 0x0201://机器人状态
|
||||
if (len >= 13U)
|
||||
{
|
||||
referee.robot_id = data[0];
|
||||
referee.chassis_power_limit = (uint16_t)(data[10] | ((uint16_t)data[11] << 8));
|
||||
referee.shooter_power_on = (uint8_t)((data[12] & 0x04U) >> 2);
|
||||
referee.last_update_tick = HAL_GetTick();
|
||||
referee.link_status = 1U;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x0202://实时功率与缓冲能量
|
||||
if (len >= 10U)
|
||||
{
|
||||
float power = 0.0f;
|
||||
memcpy(&power, &data[4], 4U);
|
||||
referee.chassis_power = power;
|
||||
referee.buffer_energy = (float)((uint16_t)(data[8] | ((uint16_t)data[9] << 8)));
|
||||
referee.last_update_tick = HAL_GetTick();
|
||||
referee.link_status = 1U;
|
||||
}
|
||||
break;
|
||||
|
||||
case 0x0304://图传链路转发自定义控制器键鼠
|
||||
if (len >= 12U && keyboard_hook != NULL)
|
||||
keyboard_hook(data);
|
||||
referee.last_update_tick = HAL_GetTick();
|
||||
referee.link_status = 1U;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:流式解析单字节裁判系统数据
|
||||
*参数说明:byte:串口接收字节
|
||||
*返回类型:无
|
||||
*备注:在接收中断上下文调用
|
||||
*/
|
||||
void referee_parse_byte(uint8_t byte)
|
||||
{
|
||||
if (ref_state != REF_STATE_SOF && ref_frame_index >= REF_FRAME_MAX_LEN)
|
||||
{
|
||||
ref_state = REF_STATE_SOF;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (ref_state)
|
||||
{
|
||||
case REF_STATE_SOF:
|
||||
if (byte == 0xA5U)
|
||||
{
|
||||
ref_frame_index = 0U;
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
ref_state = REF_STATE_LEN_LOW;
|
||||
}
|
||||
break;
|
||||
|
||||
case REF_STATE_LEN_LOW:
|
||||
ref_data_len = byte;
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
ref_state = REF_STATE_LEN_HIGH;
|
||||
break;
|
||||
|
||||
case REF_STATE_LEN_HIGH:
|
||||
ref_data_len |= (uint16_t)byte << 8;
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
if (ref_data_len > (uint16_t)(REF_FRAME_MAX_LEN - 9U))
|
||||
ref_state = REF_STATE_SOF;//帧过长,重新找帧头
|
||||
else
|
||||
ref_state = REF_STATE_SEQ;
|
||||
break;
|
||||
|
||||
case REF_STATE_SEQ:
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
ref_state = REF_STATE_HEADER_CRC8;
|
||||
break;
|
||||
|
||||
case REF_STATE_HEADER_CRC8:
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
if (verify_crc8(ref_frame_buf, 5U))
|
||||
ref_state = REF_STATE_CMD_LOW;
|
||||
else
|
||||
ref_state = REF_STATE_SOF;
|
||||
break;
|
||||
|
||||
case REF_STATE_CMD_LOW:
|
||||
ref_cmd_id = byte;
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
ref_state = REF_STATE_CMD_HIGH;
|
||||
break;
|
||||
|
||||
case REF_STATE_CMD_HIGH:
|
||||
ref_cmd_id |= (uint16_t)byte << 8;
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
ref_data_index = 0U;
|
||||
if (ref_data_len == 0U)
|
||||
ref_state = REF_STATE_CRC16_LOW;
|
||||
else
|
||||
ref_state = REF_STATE_DATA;
|
||||
break;
|
||||
|
||||
case REF_STATE_DATA:
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
if (++ref_data_index >= ref_data_len)
|
||||
ref_state = REF_STATE_CRC16_LOW;
|
||||
break;
|
||||
|
||||
case REF_STATE_CRC16_LOW:
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
ref_state = REF_STATE_CRC16_HIGH;
|
||||
break;
|
||||
|
||||
case REF_STATE_CRC16_HIGH:
|
||||
ref_frame_buf[ref_frame_index++] = byte;
|
||||
if (verify_crc16(ref_frame_buf, ref_frame_index))
|
||||
referee_dispatch(ref_cmd_id, &ref_frame_buf[7], ref_data_len);
|
||||
ref_state = REF_STATE_SOF;
|
||||
break;
|
||||
|
||||
default:
|
||||
ref_state = REF_STATE_SOF;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:批量喂入裁判系统接收字节流
|
||||
*参数说明:data:接收数据缓冲区指针
|
||||
*参数说明:len:接收字节长度
|
||||
*返回类型:无
|
||||
*备注:在USART6整帧回调内调用
|
||||
*/
|
||||
void referee_parse_bytes(const uint8_t * data, uint16_t len)
|
||||
{
|
||||
if (data == NULL || len == 0U)
|
||||
return;
|
||||
|
||||
while (len--)
|
||||
referee_parse_byte(*data++);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:组包并通过USART6 DMA发送裁判系统交互帧
|
||||
*参数说明:cmd_id:命令码(如0x0301)
|
||||
*参数说明:data:负载数据指针(不含cmd_id)
|
||||
*参数说明:len:负载字节长度
|
||||
*返回类型:无
|
||||
*备注:内部使用静态缓冲+DMA立即返回,勿高频调用
|
||||
*/
|
||||
void referee_send_frame(uint16_t cmd_id, const uint8_t * data, uint16_t len)
|
||||
{
|
||||
static uint8_t frame[REF_FRAME_MAX_LEN];
|
||||
uint16_t i = 0U;
|
||||
|
||||
if (len > (uint16_t)(REF_FRAME_MAX_LEN - 9U) || data == NULL)
|
||||
return;
|
||||
|
||||
frame[i++] = 0xA5U;
|
||||
frame[i++] = (uint8_t)(len & 0xFFU);
|
||||
frame[i++] = (uint8_t)(len >> 8);
|
||||
frame[i++] = ref_tx_seq++;
|
||||
|
||||
uint8_t crc8 = 0xFFU;
|
||||
for (uint16_t k = 0U; k < 4U; k++)
|
||||
crc8 = crc8_update(crc8, frame[k]);
|
||||
frame[i++] = crc8;
|
||||
|
||||
frame[i++] = (uint8_t)(cmd_id & 0xFFU);
|
||||
frame[i++] = (uint8_t)(cmd_id >> 8);
|
||||
|
||||
for (uint16_t k = 0U; k < len; k++)
|
||||
frame[i++] = data[k];
|
||||
|
||||
uint16_t crc16 = 0xFFFFU;
|
||||
for (uint16_t k = 0U; k < i; k++)
|
||||
crc16 = crc16_update(crc16, frame[k]);
|
||||
frame[i++] = (uint8_t)(crc16 & 0xFFU);
|
||||
frame[i++] = (uint8_t)(crc16 >> 8);
|
||||
|
||||
usart6_tx_dma_enable(frame, i);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#ifndef REFEREE_H
|
||||
#define REFEREE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
/* 裁判系统解析数据结构体(0x0201机器人状态/0x0202实时功率/0x0304自定义控制器) */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t robot_id; //机器人ID(无裁判系统时为0,4号步兵为4/104)
|
||||
uint8_t shooter_power_on; //发射机构上电状态(0x0201)
|
||||
uint16_t chassis_power_limit;//底盘功率上限(W)
|
||||
float chassis_power; //底盘实时功率(W)
|
||||
float buffer_energy; //底盘缓冲能量(J)
|
||||
uint8_t link_status; //链路在线标志(1=收到有效帧)
|
||||
uint32_t last_update_tick; //最近有效帧时间戳(ms)
|
||||
} referee_data_t;
|
||||
|
||||
extern referee_data_t referee;//全局裁判系统数据实例
|
||||
|
||||
/*
|
||||
*函数简介:流式解析单字节裁判系统数据
|
||||
*参数说明:byte:串口接收字节
|
||||
*返回类型:无
|
||||
*备注:在接收中断上下文调用
|
||||
*/
|
||||
extern void referee_parse_byte(uint8_t byte);
|
||||
|
||||
/*
|
||||
*函数简介:批量喂入裁判系统接收字节流
|
||||
*参数说明:data:接收数据缓冲区指针
|
||||
*参数说明:len:接收字节长度
|
||||
*返回类型:无
|
||||
*备注:在USART6整帧回调内调用
|
||||
*/
|
||||
extern void referee_parse_bytes(const uint8_t * data, uint16_t len);
|
||||
|
||||
/* 0x0304图传键鼠数据回调类型(中断上下文执行) */
|
||||
typedef void (*referee_keyboard_hook_f)(const uint8_t data[12]);
|
||||
|
||||
/*
|
||||
*函数简介:注册0x0304图传键鼠数据解析回调函数
|
||||
*参数说明:hook:回调函数指针
|
||||
*返回类型:无
|
||||
*备注:仅云台板需要注册
|
||||
*/
|
||||
extern void referee_set_keyboard_hook(referee_keyboard_hook_f hook);
|
||||
|
||||
/*
|
||||
*函数简介:组包并通过USART6 DMA发送裁判系统交互帧
|
||||
*参数说明:cmd_id:命令码(如0x0301)
|
||||
*参数说明:data:负载数据指针(不含cmd_id)
|
||||
*参数说明:len:负载字节长度
|
||||
*返回类型:无
|
||||
*备注:用于自定义UI发送,内部使用静态缓冲+DMA立即返回,勿高频调用
|
||||
*/
|
||||
extern void referee_send_frame(uint16_t cmd_id, const uint8_t * data, uint16_t len);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
*文件名称:referee_task.c
|
||||
*功能描述:裁判系统链路监测与自定义UI刷新任务(100ms)
|
||||
*备注:数据解析在USART6空闲中断内完成,本任务负责超时监测、心跳上报与UI周期发送
|
||||
*/
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "bsp_usart.h"
|
||||
#include "referee.h"
|
||||
#include "referee_ui.h"
|
||||
#include "c_comm.h"
|
||||
#include "attitude.h"
|
||||
#include "robot_param.h"
|
||||
|
||||
/*
|
||||
*函数简介:按UI_SEND_HZ分频组装并发送选手端自定义UI数据
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:内部函数,由referee_task每100ms调用一次
|
||||
*/
|
||||
static void referee_ui_step(void)
|
||||
{
|
||||
static uint8_t ui_div = 0U;
|
||||
|
||||
if (++ui_div < (uint8_t)(10U / UI_SEND_HZ))
|
||||
return;
|
||||
ui_div = 0U;
|
||||
|
||||
ui_data_t ui = {0};
|
||||
ui.pitch_deg = ctoc_ui_pitch_deg;
|
||||
ui.yaw_deg = ctoc_gimbal_yaw;
|
||||
ui.hdg_deg = att_deg_yaw;
|
||||
ui.pwr_w = referee.chassis_power;
|
||||
ui.pwr_limit_w = (float)referee.chassis_power_limit;
|
||||
ui.buf_j = referee.buffer_energy;
|
||||
ui.cap_pct = (uint8_t)(referee.buffer_energy >= 60.0f
|
||||
? 100U
|
||||
: (referee.buffer_energy * 100.0f / 60.0f));
|
||||
ui.fric_l_rpm = ctoc_ui_fric_l_rpm;
|
||||
ui.fric_r_rpm = ctoc_ui_fric_r_rpm;
|
||||
ui.spin_on = ctoc_remote.spin_flag;
|
||||
ui.ref_online = referee.link_status;
|
||||
|
||||
referee_ui_update(&ui);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:USART6裁判系统整帧接收回调
|
||||
*参数说明:frame:接收帧数据指针
|
||||
*参数说明:len:接收帧字节长度
|
||||
*返回类型:无
|
||||
*备注:将接收字节流喂入裁判系统解析状态机
|
||||
*/
|
||||
void referee_frame_callback(const uint8_t * frame, uint16_t len)
|
||||
{
|
||||
if (frame == NULL || len == 0U)
|
||||
return;
|
||||
|
||||
referee_parse_bytes(frame, len);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:裁判系统链路超时监测与UI发送任务(100ms)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:无
|
||||
*/
|
||||
void referee_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
while (!app_ready)
|
||||
osDelay(2);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
if (referee.link_status &&
|
||||
(HAL_GetTick() - referee.last_update_tick > DEVICE_OFFLINE_TIME_MS * 10U))
|
||||
{
|
||||
referee.link_status = 0U;//链路超时置断线
|
||||
}
|
||||
|
||||
referee_ui_step();
|
||||
|
||||
monitor_heartbeat(HB_REFEREE);
|
||||
osDelay(100);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#ifndef REFEREE_TASK_H
|
||||
#define REFEREE_TASK_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/*
|
||||
*函数简介:USART6裁判系统整帧接收回调
|
||||
*参数说明:frame:接收帧数据指针
|
||||
*参数说明:len:接收帧字节长度
|
||||
*返回类型:无
|
||||
*备注:由CAN_Task注册到BSP串口回调
|
||||
*/
|
||||
extern void referee_frame_callback(const uint8_t * frame, uint16_t len);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,331 @@
|
||||
/*
|
||||
*文件名称:referee_ui.c
|
||||
*功能描述:裁判系统自定义UI组包与发送模块
|
||||
*备注:每包包含6字节交互头与1/2/5/7个15字节图形数据,首轮ADD之后一律MODIFY
|
||||
*/
|
||||
#include "referee_ui.h"
|
||||
#include "referee.h"
|
||||
#include "robot_param.h"
|
||||
#include <string.h>
|
||||
|
||||
_Static_assert(sizeof(ui_head_t) == 11, "ui_head_t 必须是 11 字节");
|
||||
_Static_assert(sizeof(ui_draw_t) == 15, "ui_draw_t 必须是 15 字节");
|
||||
_Static_assert(sizeof(ui_num_t) == 15, "ui_num_t 必须是 15 字节");
|
||||
_Static_assert(sizeof(ui_graphic_t) == 15, "ui_graphic_t 必须是 15 字节");
|
||||
|
||||
static uint16_t ui_name_seq = 0U;//图形名称自增序号(base26)
|
||||
|
||||
#define UI_PAYLOAD_MAX (6U + UI_MAX_PER_PACKET * sizeof(ui_graphic_t))
|
||||
static uint8_t ui_pkt[UI_PAYLOAD_MAX];//单包发送拼装缓冲
|
||||
static uint8_t ui_pkt_n = 0U;//当前包内已装填图形数
|
||||
static uint8_t ui_op = UI_OP_ADD;//当前图形操作码(首轮ADD,之后MOD)
|
||||
static uint8_t ui_ready = 0U;//首轮ADD发送完成标志
|
||||
|
||||
/*
|
||||
*函数简介:生成唯一三字符图形名称(base26编码)
|
||||
*参数说明:name:3字节名称输出缓冲区
|
||||
*返回类型:无
|
||||
*备注:内部函数
|
||||
*/
|
||||
static void ui_next_name(uint8_t name[3])
|
||||
{
|
||||
uint16_t v = ui_name_seq++;
|
||||
name[0] = (uint8_t)('A' + (v / 676U) % 26U);
|
||||
name[1] = (uint8_t)('A' + (v / 26U) % 26U);
|
||||
name[2] = (uint8_t)('A' + v % 26U);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:将当前缓冲内的图形打包通过裁判系统0x0301发出
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:协议仅支持1/2/5/7个图形每包
|
||||
*/
|
||||
static void ui_flush(void)
|
||||
{
|
||||
if (ui_pkt_n == 0U)
|
||||
return;
|
||||
|
||||
uint16_t cmd = 0U;
|
||||
switch (ui_pkt_n)
|
||||
{
|
||||
case 1U: cmd = UI_CMD_1; break;
|
||||
case 2U: cmd = UI_CMD_2; break;
|
||||
case 5U: cmd = UI_CMD_5; break;
|
||||
case 7U: cmd = UI_CMD_7; break;
|
||||
default: return;
|
||||
}
|
||||
|
||||
ui_pkt[0] = (uint8_t)(cmd & 0xFFU);
|
||||
ui_pkt[1] = (uint8_t)(cmd >> 8);
|
||||
uint16_t sid = (uint16_t)referee.robot_id;
|
||||
ui_pkt[2] = (uint8_t)(sid & 0xFFU);
|
||||
ui_pkt[3] = (uint8_t)(sid >> 8);
|
||||
ui_pkt[4] = (uint8_t)(UI_RECEIVER_ID & 0xFFU);
|
||||
ui_pkt[5] = (uint8_t)(UI_RECEIVER_ID >> 8);
|
||||
|
||||
referee_send_frame(UI_CMD_BASE, ui_pkt, (uint16_t)(6U + ui_pkt_n * sizeof(ui_graphic_t)));
|
||||
ui_pkt_n = 0U;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:在发送缓冲中分配并初始化一个新的图形槽位
|
||||
*参数说明:type:图形类型(UI_T_*)
|
||||
*参数说明:layer:图层编号(0~9)
|
||||
*参数说明:color:颜色编号(UI_C_*)
|
||||
*返回类型:ui_graphic_t *:新图形联合体指针
|
||||
*备注:缓冲满7个时自动触发ui_flush发送
|
||||
*/
|
||||
static ui_graphic_t * ui_new(uint8_t type, uint8_t layer, uint8_t color)
|
||||
{
|
||||
if (ui_pkt_n >= UI_MAX_PER_PACKET)
|
||||
ui_flush();
|
||||
|
||||
ui_graphic_t * g = (ui_graphic_t *)&ui_pkt[6U + ui_pkt_n * sizeof(ui_graphic_t)];
|
||||
memset(g, 0, sizeof(*g));
|
||||
ui_next_name(g->d.h.name);
|
||||
g->d.h.type = type;
|
||||
g->d.h.layer = layer;
|
||||
g->d.h.color = color;
|
||||
g->d.h.operate = ui_op;
|
||||
ui_pkt_n++;
|
||||
return g;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:绘制直线图元
|
||||
*参数说明:layer:图层,color:颜色,x0/y0:起点,x1/y1:终点,w:线宽
|
||||
*返回类型:无
|
||||
*备注:内部图元封装
|
||||
*/
|
||||
static void ui_line(uint8_t layer, uint8_t color, int x0, int y0, int x1, int y1, int w)
|
||||
{
|
||||
ui_graphic_t * g = ui_new(UI_T_LINE, layer, color);
|
||||
g->d.h.x0 = (uint32_t)x0;
|
||||
g->d.h.y0 = (uint32_t)y0;
|
||||
g->d.x1 = (uint32_t)x1;
|
||||
g->d.y1 = (uint32_t)y1;
|
||||
g->d.h.width = (uint32_t)w;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:绘制矩形图元
|
||||
*参数说明:layer:图层,color:颜色,x0/y0:左上角,x1/y1:右下角
|
||||
*返回类型:无
|
||||
*备注:内部图元封装
|
||||
*/
|
||||
static void ui_rect(uint8_t layer, uint8_t color, int x0, int y0, int x1, int y1)
|
||||
{
|
||||
ui_graphic_t * g = ui_new(UI_T_RECT, layer, color);
|
||||
g->d.h.x0 = (uint32_t)x0;
|
||||
g->d.h.y0 = (uint32_t)y0;
|
||||
g->d.x1 = (uint32_t)x1;
|
||||
g->d.y1 = (uint32_t)y1;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:绘制浮点数值图元
|
||||
*参数说明:layer:图层,color:颜色,x/y:起点坐标,v:浮点值,digits:小数位数
|
||||
*返回类型:无
|
||||
*备注:内部图元封装
|
||||
*/
|
||||
static void ui_fval(uint8_t layer, uint8_t color, int x, int y, float v, int digits)
|
||||
{
|
||||
ui_graphic_t * g = ui_new(UI_T_FLOAT, layer, color);
|
||||
g->n.h.x0 = (uint32_t)x;
|
||||
g->n.h.y0 = (uint32_t)y;
|
||||
g->n.h.width = (uint32_t)digits;
|
||||
g->n.val.f = v;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:绘制整型数值图元
|
||||
*参数说明:layer:图层,color:颜色,x/y:起点坐标,v:整型值
|
||||
*返回类型:无
|
||||
*备注:内部图元封装
|
||||
*/
|
||||
static void ui_ival(uint8_t layer, uint8_t color, int x, int y, int32_t v)
|
||||
{
|
||||
ui_graphic_t * g = ui_new(UI_T_INT, layer, color);
|
||||
g->n.h.x0 = (uint32_t)x;
|
||||
g->n.h.y0 = (uint32_t)y;
|
||||
g->n.val.i = v;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:绘制短字符图元(最多4字节ASCII)
|
||||
*参数说明:layer:图层,color:颜色,x/y:起点坐标,s:字符串指针
|
||||
*返回类型:无
|
||||
*备注:内部图元封装,遇到'\0'停止拷贝避免短字符串越界读取
|
||||
*/
|
||||
static void ui_char(uint8_t layer, uint8_t color, int x, int y, const char * s)
|
||||
{
|
||||
if (s == NULL)
|
||||
return;
|
||||
|
||||
ui_graphic_t * g = ui_new(UI_T_CHAR, layer, color);
|
||||
g->n.h.x0 = (uint32_t)x;
|
||||
g->n.h.y0 = (uint32_t)y;
|
||||
g->n.h.width = (uint32_t)strlen(s);
|
||||
for (int i = 0; i < 4 && s[i] != '\0'; i++)
|
||||
g->n.val.s[i] = s[i];
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:将像素长度限制在[0, max_val]区间内
|
||||
*参数说明:val:原始像素值
|
||||
*参数说明:max_val:最大允许像素值
|
||||
*返回类型:int:限幅后的像素值
|
||||
*备注:内部函数
|
||||
*/
|
||||
static int ui_clamp_px(int val, int max_val)
|
||||
{
|
||||
if (val < 0)
|
||||
return 0;
|
||||
if (val > max_val)
|
||||
return max_val;
|
||||
return val;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:将角度归一化到[0, 360)区间
|
||||
*参数说明:a:输入角度(°)
|
||||
*返回类型:float:归一化角度(°)
|
||||
*备注:内部函数
|
||||
*/
|
||||
static float ui_norm360(float a)
|
||||
{
|
||||
while (a < 0.0f) a += 360.0f;
|
||||
while (a >= 360.0f) a -= 360.0f;
|
||||
return a;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:组包并发送选手端自定义UI图形
|
||||
*参数说明:d:UI显示数据结构体指针
|
||||
*返回类型:无
|
||||
*备注:裁判系统离线时发送一轮删除图层并重置状态为ADD
|
||||
*/
|
||||
void referee_ui_update(const ui_data_t * d)
|
||||
{
|
||||
if (d == NULL)
|
||||
return;
|
||||
|
||||
if (!d->ref_online)
|
||||
{
|
||||
if (ui_ready)
|
||||
{
|
||||
ui_pkt_n = 0U;
|
||||
ui_graphic_t * g = ui_new(UI_T_RECT, UI_L_FRAME, UI_C_BLACK);
|
||||
g->d.h.operate = UI_OP_DEL;
|
||||
g->d.h.x0 = 0U;
|
||||
g->d.h.y0 = 0U;
|
||||
g->d.x1 = 0U;
|
||||
g->d.y1 = 0U;
|
||||
ui_flush();
|
||||
ui_ready = 0U;
|
||||
ui_name_seq = 0U;
|
||||
ui_op = UI_OP_ADD;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* 左上:预估底盘功率 */
|
||||
uint8_t pw_col = (d->pwr_w > d->pwr_limit_w) ? UI_C_RED
|
||||
: (d->pwr_w > d->pwr_limit_w * 0.8f) ? UI_C_ORANGE : UI_C_GREEN;
|
||||
ui_rect(UI_L_FRAME, UI_C_CYAN, UI_PWR_X0, UI_PWR_Y0, UI_PWR_X1, UI_PWR_Y0 + 4);
|
||||
ui_char(UI_L_TEXT, UI_C_CYAN, UI_PWR_X0 + 20, UI_PWR_Y0 + 30, "PWR");
|
||||
ui_fval(UI_L_TEXT, UI_C_WHITE, UI_PWR_X0 + 20, UI_PWR_Y0 + 64, d->pwr_w, 1);
|
||||
ui_char(UI_L_TEXT, UI_C_WHITE, UI_PWR_X0 + 210, UI_PWR_Y0 + 84, "W");
|
||||
const int pw_bar_max = UI_PWR_X1 - UI_PWR_X0 - 40;
|
||||
int pw_w_px = ui_clamp_px((int)(pw_bar_max * (d->pwr_w / (d->pwr_limit_w > 1.0f ? d->pwr_limit_w : 1.0f))),
|
||||
pw_bar_max);
|
||||
ui_rect(UI_L_GAUGE, pw_col, UI_PWR_X0 + 20, UI_PWR_Y0 + 120, UI_PWR_X0 + 20 + pw_w_px, UI_PWR_Y0 + 142);
|
||||
ui_line(UI_L_GAUGE, UI_C_WHITE, UI_PWR_X0 + 20 + pw_bar_max - 3, UI_PWR_Y0 + 116,
|
||||
UI_PWR_X0 + 20 + pw_bar_max - 3, UI_PWR_Y0 + 146, 2);
|
||||
|
||||
/* 左中:超级电容 */
|
||||
uint8_t cap_col = (d->cap_pct < 15U) ? UI_C_RED : (d->cap_pct < 40U) ? UI_C_ORANGE : UI_C_CYAN;
|
||||
ui_rect(UI_L_FRAME, cap_col, UI_CAP_X0, UI_CAP_Y0, UI_CAP_X1, UI_CAP_Y0 + 4);
|
||||
ui_char(UI_L_TEXT, cap_col, UI_CAP_X0 + 20, UI_CAP_Y0 + 30, "CAP");
|
||||
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_CAP_X0 + 20, UI_CAP_Y0 + 64, (int32_t)d->cap_pct);
|
||||
ui_char(UI_L_TEXT, UI_C_WHITE, UI_CAP_X0 + 130, UI_CAP_Y0 + 84, "%");
|
||||
int cap_px = (int)((UI_CAP_X1 - UI_CAP_X0 - 40) * (d->cap_pct / 100.0f));
|
||||
ui_rect(UI_L_GAUGE, cap_col, UI_CAP_X0 + 20, UI_CAP_Y0 + 118, UI_CAP_X0 + 20 + cap_px, UI_CAP_Y0 + 140);
|
||||
|
||||
/* 顶部:罗盘刻度带(底盘正方向朝向) */
|
||||
{
|
||||
float h = ui_norm360(d->hdg_deg);
|
||||
ui_rect(UI_L_FRAME, UI_C_CYAN, UI_HDG_X0, UI_HDG_Y0, UI_HDG_X1, UI_HDG_Y0 + 4);
|
||||
ui_line(UI_L_FRAME, UI_C_CYAN, UI_HDG_X0 + 20, UI_HDG_Y0 + 62, UI_HDG_X1 - 20, UI_HDG_Y0 + 62, 2);
|
||||
int cx = (UI_HDG_X0 + UI_HDG_X1) / 2;
|
||||
for (int k = -1; k <= 1; k++)
|
||||
{
|
||||
int x = cx + k * (UI_HDG_TAPE_HALF / 2);
|
||||
ui_line(UI_L_FRAME, UI_C_CYAN, x, UI_HDG_Y0 + 40, x, UI_HDG_Y0 + 62, (k == 0) ? 3 : 2);
|
||||
}
|
||||
ui_ival(UI_L_TEXT, UI_C_CYAN, cx - UI_HDG_TAPE_HALF / 2 - 24, UI_HDG_Y0 + 18,
|
||||
(int32_t)ui_norm360(h - 30.0f));
|
||||
ui_ival(UI_L_TEXT, UI_C_CYAN, cx + UI_HDG_TAPE_HALF / 2 - 24, UI_HDG_Y0 + 18,
|
||||
(int32_t)ui_norm360(h + 30.0f));
|
||||
ui_line(UI_L_FRAME, UI_C_WHITE, cx, UI_HDG_Y0 + 62, cx - 8, UI_HDG_Y0 + 50, 2);
|
||||
ui_line(UI_L_FRAME, UI_C_WHITE, cx, UI_HDG_Y0 + 62, cx + 8, UI_HDG_Y0 + 50, 2);
|
||||
ui_char(UI_L_TEXT, UI_C_CYAN, UI_HDG_X0 + 24, UI_HDG_Y0 + 70, "HDG");
|
||||
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_HDG_X0 + 90, UI_HDG_Y0 + 66, (int32_t)h);
|
||||
ui_char(UI_L_TEXT, UI_C_CYAN, UI_HDG_X1 - 170, UI_HDG_Y0 + 70, "YAW");
|
||||
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_HDG_X1 - 104, UI_HDG_Y0 + 66, (int32_t)ui_norm360(d->yaw_deg));
|
||||
}
|
||||
|
||||
/* 中央:俯仰梯 */
|
||||
ui_line(UI_L_FRAME, UI_C_WHITE, UI_LAD_CX - 44, UI_LAD_CY, UI_LAD_CX - 14, UI_LAD_CY, 4);
|
||||
ui_line(UI_L_FRAME, UI_C_WHITE, UI_LAD_CX + 14, UI_LAD_CY, UI_LAD_CX + 44, UI_LAD_CY, 4);
|
||||
for (int i = -2; i <= 2; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
continue;
|
||||
int v = i * 5;
|
||||
int yy = UI_LAD_CY - v * UI_LAD_PX_PER_DEG;
|
||||
int half = (i % 2 == 0) ? UI_LAD_HALF_LONG : UI_LAD_HALF_SHORT;
|
||||
ui_line(UI_L_GAUGE, UI_C_CYAN, UI_LAD_CX - half, yy, UI_LAD_CX - 18, yy, 2);
|
||||
ui_line(UI_L_GAUGE, UI_C_CYAN, UI_LAD_CX + 18, yy, UI_LAD_CX + half, yy, 2);
|
||||
}
|
||||
ui_ival(UI_L_TEXT, UI_C_CYAN, UI_LAD_CX - UI_LAD_HALF_SHORT - 40, UI_LAD_CY - 10 * UI_LAD_PX_PER_DEG - 10, 10);
|
||||
ui_ival(UI_L_TEXT, UI_C_CYAN, UI_LAD_CX + UI_LAD_HALF_SHORT + 12, UI_LAD_CY - 10 * UI_LAD_PX_PER_DEG - 10, 10);
|
||||
{
|
||||
int py = UI_LAD_CY - (int)(d->pitch_deg * UI_LAD_PX_PER_DEG);
|
||||
ui_line(UI_L_GAUGE, UI_C_ORANGE, UI_LAD_CX - UI_LAD_HALF_LONG - 20, py, UI_LAD_CX - UI_LAD_HALF_LONG - 4, py, 4);
|
||||
ui_char(UI_L_TEXT, UI_C_ORANGE, UI_LAD_CX - UI_LAD_HALF_LONG - 132, py - 10, "PIT");
|
||||
ui_fval(UI_L_TEXT, UI_C_WHITE, UI_LAD_CX - UI_LAD_HALF_LONG - 76, py - 14, d->pitch_deg, 1);
|
||||
}
|
||||
|
||||
/* 右上:摩擦轮速度 */
|
||||
{
|
||||
uint8_t f_col = (d->fric_l_rpm > 1000.0f && d->fric_r_rpm > 1000.0f) ? UI_C_GREEN : UI_C_ORANGE;
|
||||
ui_rect(UI_L_FRAME, f_col, UI_FRIC_X0, UI_FRIC_Y0, UI_FRIC_X1, UI_FRIC_Y0 + 4);
|
||||
ui_char(UI_L_TEXT, UI_C_CYAN, UI_FRIC_X0 + 24, UI_FRIC_Y0 + 34, "FRIC");
|
||||
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_FRIC_X0 + 24, UI_FRIC_Y0 + 74, (int32_t)d->fric_l_rpm);
|
||||
ui_ival(UI_L_TEXT, UI_C_WHITE, UI_FRIC_X0 + 250, UI_FRIC_Y0 + 74, (int32_t)d->fric_r_rpm);
|
||||
ui_char(UI_L_TEXT, f_col, UI_FRIC_X0 + 24, UI_FRIC_Y1 - 60,
|
||||
(d->fric_l_rpm > 1000.0f && d->fric_r_rpm > 1000.0f) ? "ARMED" : "IDLE");
|
||||
const int fric_bar_max = UI_FRIC_Y1 - UI_FRIC_Y0 - 140;
|
||||
int bl = ui_clamp_px((int)(fric_bar_max * (d->fric_l_rpm / 7000.0f)), fric_bar_max);
|
||||
int br = ui_clamp_px((int)(fric_bar_max * (d->fric_r_rpm / 7000.0f)), fric_bar_max);
|
||||
ui_rect(UI_L_GAUGE, UI_C_GREEN, UI_FRIC_X0 + 30, UI_FRIC_Y1 - 40 - bl, UI_FRIC_X0 + 74, UI_FRIC_Y1 - 40);
|
||||
ui_rect(UI_L_GAUGE, UI_C_GREEN, UI_FRIC_X0 + 256, UI_FRIC_Y1 - 40 - br, UI_FRIC_X0 + 300, UI_FRIC_Y1 - 40);
|
||||
}
|
||||
|
||||
/* 底部:小陀螺状态 */
|
||||
ui_rect(UI_L_FRAME, d->spin_on ? UI_C_GREEN : UI_C_CYAN, UI_SPIN_X0, UI_SPIN_Y0, UI_SPIN_X1, UI_SPIN_Y0 + 4);
|
||||
ui_char(UI_L_TEXT, UI_C_CYAN, UI_SPIN_X0 + 24, UI_SPIN_Y0 + 34, "SPIN");
|
||||
if (d->spin_on)
|
||||
ui_rect(UI_L_GAUGE, UI_C_GREEN, UI_SPIN_X0 + 160, UI_SPIN_Y0 + 26, UI_SPIN_X0 + 268, UI_SPIN_Y0 + 66);
|
||||
ui_char(UI_L_TEXT, d->spin_on ? UI_C_BLACK : UI_C_WHITE, UI_SPIN_X0 + 184, UI_SPIN_Y0 + 36,
|
||||
d->spin_on ? "ON" : "OFF");
|
||||
|
||||
ui_flush();
|
||||
if (!ui_ready)
|
||||
{
|
||||
ui_ready = 1U;
|
||||
ui_op = UI_OP_MOD;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
#ifndef REFEREE_UI_H
|
||||
#define REFEREE_UI_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#pragma pack(push, 1)
|
||||
/* 自定义UI图形共用头部(11字节) */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t name[3]; //图形名称(3字节ASCII)
|
||||
uint32_t operate : 3; //操作类型(UI_OP_*)
|
||||
uint32_t type : 3; //图形类型(UI_T_*)
|
||||
uint32_t layer : 4; //图层编号(0~9)
|
||||
uint32_t color : 4; //图形颜色(UI_C_*)
|
||||
uint32_t sa : 9; //起始角(弧/椭圆)
|
||||
uint32_t ea : 9; //结束角(弧/椭圆)
|
||||
uint32_t width : 10; //线宽/小数位数/字符长度
|
||||
uint32_t x0 : 11; //起点X坐标
|
||||
uint32_t y0 : 11; //起点Y坐标
|
||||
} ui_head_t;
|
||||
|
||||
/* 绘制类图形结构体(15字节) */
|
||||
typedef struct
|
||||
{
|
||||
ui_head_t h; //11字节共用头部
|
||||
uint32_t radius : 10; //圆半径
|
||||
uint32_t x1 : 11; //终点X坐标
|
||||
uint32_t y1 : 11; //终点Y坐标
|
||||
} ui_draw_t;
|
||||
|
||||
/* 数值/字符类图形结构体(15字节) */
|
||||
typedef struct
|
||||
{
|
||||
ui_head_t h; //11字节共用头部
|
||||
union
|
||||
{
|
||||
float f; //浮点数值
|
||||
int32_t i; //整型数值
|
||||
char s[4]; //最多4字节ASCII字符
|
||||
} val;
|
||||
} ui_num_t;
|
||||
|
||||
/* 自定义UI图形联合体(15字节) */
|
||||
typedef union
|
||||
{
|
||||
ui_draw_t d; //绘制类视图
|
||||
ui_num_t n; //数值类视图
|
||||
uint8_t raw[15]; //原始字节视图
|
||||
} ui_graphic_t;
|
||||
#pragma pack(pop)
|
||||
|
||||
/* 命令码与单包上限定义 */
|
||||
#define UI_CMD_BASE 0x0301//机器人交互数据命令码
|
||||
#define UI_CMD_1 0x0101//单图形子命令
|
||||
#define UI_CMD_2 0x0102//双图形子命令
|
||||
#define UI_CMD_5 0x0103//五图形子命令
|
||||
#define UI_CMD_7 0x0104//七图形子命令
|
||||
#define UI_MAX_PER_PACKET 7 //单包最大图形数
|
||||
|
||||
/* 图形操作码 */
|
||||
#define UI_OP_NOP 0 //空操作
|
||||
#define UI_OP_ADD 1 //新增图形(首轮使用)
|
||||
#define UI_OP_MOD 2 //修改图形(稳态刷新使用)
|
||||
#define UI_OP_DEL 3 //删除图形
|
||||
|
||||
/* 图形类型 */
|
||||
#define UI_T_LINE 0 //直线
|
||||
#define UI_T_RECT 1 //矩形
|
||||
#define UI_T_CIRCLE 2 //正圆
|
||||
#define UI_T_ELLIPSE 3 //椭圆
|
||||
#define UI_T_ARC 4 //圆弧
|
||||
#define UI_T_FLOAT 5 //浮点数
|
||||
#define UI_T_INT 6 //整型数
|
||||
#define UI_T_CHAR 7 //字符
|
||||
|
||||
/* 官方颜色编号(0~8) */
|
||||
#define UI_C_RED 0 //红/蓝主色
|
||||
#define UI_C_YELLOW 1 //黄色
|
||||
#define UI_C_GREEN 2 //绿色
|
||||
#define UI_C_ORANGE 3 //橙色
|
||||
#define UI_C_PURPLE 4 //紫红色
|
||||
#define UI_C_PINK 5 //粉色
|
||||
#define UI_C_CYAN 6 //青色
|
||||
#define UI_C_BLACK 7 //黑色
|
||||
#define UI_C_WHITE 8 //白色
|
||||
|
||||
/* 图层分配(0~9) */
|
||||
#define UI_L_FRAME 0 //卡片边框/刻度
|
||||
#define UI_L_GAUGE 1 //能量条/进度条
|
||||
#define UI_L_TEXT 2 //数值/字符
|
||||
|
||||
#define UI_SEND_HZ 5 //UI刷新频率(Hz)
|
||||
#define UI_RECEIVER_ID 0x8080//选手端默认接收ID
|
||||
|
||||
#define UI_SCREEN_W 1920 //画布宽度(像素)
|
||||
#define UI_SCREEN_H 1080 //画布高度(像素)
|
||||
|
||||
/* 版式坐标定义(避开官方保留区) */
|
||||
#define UI_PWR_X0 56 //左上功率框左边界
|
||||
#define UI_PWR_Y0 176 //左上功率框上边界
|
||||
#define UI_PWR_X1 520 //左上功率框右边界
|
||||
#define UI_PWR_Y1 386 //左上功率框下边界
|
||||
|
||||
#define UI_CAP_X0 56 //左中超电框左边界
|
||||
#define UI_CAP_Y0 404 //左中超电框上边界
|
||||
#define UI_CAP_X1 520 //左中超电框右边界
|
||||
#define UI_CAP_Y1 566 //左中超电框下边界
|
||||
|
||||
#define UI_HDG_X0 626 //顶部罗盘左边界
|
||||
#define UI_HDG_Y0 186 //顶部罗盘上边界
|
||||
#define UI_HDG_X1 1258 //顶部罗盘右边界
|
||||
#define UI_HDG_Y1 270 //顶部罗盘下边界
|
||||
#define UI_HDG_TAPE_HALF 216 //罗盘刻度带半宽(±30°)
|
||||
|
||||
#define UI_LAD_CX 960 //中央俯仰梯中心X
|
||||
#define UI_LAD_CY 540 //中央俯仰梯中心Y
|
||||
#define UI_LAD_PX_PER_DEG 11 //俯仰梯每度像素数
|
||||
#define UI_LAD_HALF_LONG 96 //长刻度线半宽
|
||||
#define UI_LAD_HALF_SHORT 62 //短刻度线半宽
|
||||
|
||||
#define UI_FRIC_X0 1420 //右上摩擦轮框左边界
|
||||
#define UI_FRIC_Y0 176 //右上摩擦轮框上边界
|
||||
#define UI_FRIC_X1 1864 //右上摩擦轮框右边界
|
||||
#define UI_FRIC_Y1 520 //右上摩擦轮框下边界
|
||||
|
||||
#define UI_SPIN_X0 700 //底部小陀螺框左边界
|
||||
#define UI_SPIN_Y0 874 //底部小陀螺框上边界
|
||||
#define UI_SPIN_X1 1220 //底部小陀螺框右边界
|
||||
#define UI_SPIN_Y1 1000 //底部小陀螺框下边界
|
||||
|
||||
/* 自定义UI输入数据结构体 */
|
||||
typedef struct
|
||||
{
|
||||
float pitch_deg; //云台Pitch角(来自CToC)
|
||||
float yaw_deg; //云台Yaw角(来自CToC姿态帧)
|
||||
float hdg_deg; //底盘正方向朝向(本板att_deg_yaw)
|
||||
float pwr_w; //底盘实时功率(W)
|
||||
float pwr_limit_w; //功率上限(W)
|
||||
float buf_j; //缓冲能量(J)
|
||||
uint8_t cap_pct; //电容百分比(0~100)
|
||||
float fric_l_rpm; //左摩擦轮转速(RPM)
|
||||
float fric_r_rpm; //右摩擦轮转速(RPM)
|
||||
uint8_t spin_on; //小陀螺使能标志
|
||||
uint8_t ref_online; //裁判系统在线标志(离线时删全部图层)
|
||||
} ui_data_t;
|
||||
|
||||
/*
|
||||
*函数简介:组包并发送选手端自定义UI图形
|
||||
*参数说明:d:UI显示数据结构体指针
|
||||
*返回类型:无
|
||||
*备注:建议以UI_SEND_HZ(5Hz)周期调用;裁判系统离线时发送一次删除图层
|
||||
*/
|
||||
extern void referee_ui_update(const ui_data_t * d);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
*文件名称:robot_param.h
|
||||
*功能描述:底盘板全局参数配置头文件(整车唯一调参入口)
|
||||
*备注:控制周期2ms,修改周期时需同步重调各环PID参数
|
||||
*/
|
||||
#ifndef ROBOT_PARAM_CHASSIS_H
|
||||
#define ROBOT_PARAM_CHASSIS_H
|
||||
|
||||
/* ===== 公共参数段(底盘/云台保持一致,由check_param_common.py校验) ===== */
|
||||
#define CTOC_MASTER_ID1 0x019 //底盘->云台:发射机构状态+底盘故障位
|
||||
#define CTOC_SLAVE_ID1 0x149 //云台->底盘:摇杆数据
|
||||
#define CTOC_SLAVE_ID2 0x189 //云台->底盘:控制状态+故障/小陀螺使能位
|
||||
#define CTOC_SLAVE_ID3 0x1A9 //云台->底盘:云台世界系yaw(float)+att_ready
|
||||
#define CTOC_SLAVE_ID4 0x1B9 //云台->底盘:自定义UI专用(pitch+摩擦轮L/R转速)
|
||||
#define DEBUG_BUZZER_MELODY 0 //1=CAN掉线报警音替换为旋律,0=正常蜂鸣报警
|
||||
#define BUZZER_MELODY_SPEED_DIV 1 //旋律降速分频(1=每拍100ms)
|
||||
#define DEVICE_OFFLINE_TIME_MS 100 //CAN设备/裁判系统掉线判定时间(ms)
|
||||
#define CTOC_OFFLINE_TIME_MS 100 //板间通讯掉线判定时间(ms)
|
||||
#define IMU_TEMPERATURE_TARGET 40.0f //BMI088恒温目标(℃)
|
||||
#define IMU_TEMPERATURE_LIMIT 50.0f //温度报警阈值(℃)
|
||||
#define IMU_TEMP_KP 20.0f //IMU恒温PID Kp
|
||||
#define IMU_TEMP_KI 0.0003f //IMU恒温PID Ki
|
||||
#define IMU_TEMP_KD 0.0f //IMU恒温PID Kd
|
||||
|
||||
/* 底盘跟随与云台解耦共用参数 */
|
||||
#define YAW_GM6020_ENCODER_ZERO 7704 //Yaw轴编码器机械正前方零位(0~8191)
|
||||
#define CHASSIS_SPIN_FEEDFORWARD_K 0.008f //小陀螺投影解耦前馈时间系数(s)
|
||||
/* ===== 公共参数段结束 ===== */
|
||||
|
||||
/* ============================ 底盘 ============================ */
|
||||
#define MECANUM_WHEEL_RADIUS 7.0f //麦轮半径(cm)
|
||||
#define MECANUM_RX 18.0f //底盘中心到轮心的x轴分量(cm)
|
||||
#define MECANUM_RY 18.75f //底盘中心到轮心的y轴分量(cm)
|
||||
#define MECANUM_SPEED_FACTOR 18143.663512f//线速度换算RPM系数:19*60*100/(2*pi*R_cm)
|
||||
|
||||
#define MECANUM_CHASSIS_SPEED_SCALE 0.004f //摇杆->线速度映射比例
|
||||
#define MECANUM_KEYBOARD_SPEED 2.4f //键盘WASD平移速度(m/s映射值)
|
||||
#define MECANUM_SPIN_SLOW_W -4.0f //慢速小陀螺角速度(rad/s映射值)
|
||||
#define MECANUM_SPIN_FAST_W -6.0f //快速小陀螺基准角速度(rad/s映射值)
|
||||
#define MECANUM_SHIFT_BOOST_RATE 1.3f //SHIFT加速增益(超电接入前临时方案)
|
||||
|
||||
/* 底盘轮速环PID(2ms控制周期) */
|
||||
#define MECANUM_WHEEL_SPEED_KP 0.8f //轮速环Kp
|
||||
#define MECANUM_WHEEL_SPEED_KI 0.0f //轮速环Ki
|
||||
#define MECANUM_WHEEL_SPEED_KD 0.0f //轮速环Kd
|
||||
#define MECANUM_WHEEL_SPEED_OUT_LIMIT 15000.0f //轮速环输出限幅(counts)
|
||||
|
||||
/* 跟随环PID(2ms,双INS世界系方案) */
|
||||
#define CHASSIS_FOLLOW_KP 0.4678f //跟随环Kp(w量纲/°)
|
||||
#define CHASSIS_FOLLOW_KI 0.0f //跟随环Ki
|
||||
#define CHASSIS_FOLLOW_KD 24.452f //跟随环Kd
|
||||
#define CHASSIS_FOLLOW_OUT_LIMIT 5.5f //跟随输出限幅
|
||||
#define CHASSIS_FOLLOW_DEADBAND 1.0f //跟随误差死区(°)
|
||||
|
||||
/* 跟随符号综合自检参数 */
|
||||
#define CHASSIS_FOLLOW_SIGN (1.0f) //跟随符号回退值
|
||||
#define CHASSIS_DETECT_W 1.2f //自检w脉冲幅值
|
||||
#define CHASSIS_DETECT_TIME_MS 80 //自检脉冲时长(ms)
|
||||
#define CHASSIS_DETECT_YAW_MIN 0.5f //判定转动的INS yaw阈值(°)
|
||||
|
||||
/* HLJ_PC功率控制系统参数 */
|
||||
#define HLJ_PC_ENABLE 1 //1=开启HLJ_PC功率控制模型,0=关闭功率控制(直通输出)
|
||||
#define HLJ_PC_DEFAULT_POWER_W 90.0f //默认裁判功率上限(W,无裁判时使用)
|
||||
#define HLJ_PC_MIN_POWER_W 20.0f //底盘最低保底功率预算(W)
|
||||
#define HLJ_PC_CAP_EXTRA_MAX_W 60.0f //超电满能量时最大额外释放功率(W)
|
||||
#define HLJ_PC_CAP_MIN_ENERGY 10U //超电允许放电的最低能量阈值(%)
|
||||
#define HLJ_PC_CAP_FULL_ENERGY 30U //超电满额释放额外功率的能量阈值(%)
|
||||
#define HLJ_PC_MIN_SPIN_RATIO 0.5f //边陀螺边平移时小陀螺最低保留比例
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef ROBOT_TYPEDEF_H
|
||||
#define ROBOT_TYPEDEF_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#if !defined(ROBOT_TYPE_GIMBAL) && !defined(ROBOT_TYPE_CHASSIS)
|
||||
#error "必须定义 ROBOT_TYPE_GIMBAL 或 ROBOT_TYPE_CHASSIS"
|
||||
#endif
|
||||
|
||||
/* 底盘运行模式枚举 */
|
||||
typedef enum
|
||||
{
|
||||
CHASSIS_MODE_OFF = 0, //停车
|
||||
CHASSIS_MODE_MANUAL, //手动平移+手动旋转
|
||||
CHASSIS_MODE_FOLLOW, //跟随云台(叠加手动平移)
|
||||
CHASSIS_MODE_SPIN_SLOW, //慢速小陀螺(叠加手动平移)
|
||||
CHASSIS_MODE_SPIN_FAST, //快速小陀螺(叠加手动平移)
|
||||
} chassis_mode_e;
|
||||
|
||||
/* 机器人全局控制指令结构体(robot_cmd_task发布,各控制任务只读) */
|
||||
typedef struct
|
||||
{
|
||||
uint8_t online; //控制源在线标志(遥控器或键鼠链路)
|
||||
float vx; //前进方向速度指令(m/s映射值)
|
||||
float vy; //左右方向速度指令(m/s映射值)
|
||||
float wz_manual; //手动旋转角速度(已废弃,恒为0)
|
||||
chassis_mode_e chassis_mode; //底盘控制模式
|
||||
uint8_t spin_flag; //小陀螺使能标志(Ctrl沿翻转,云台下发)
|
||||
float gimbal_yaw_delta; //本周期云台Yaw角度增量(°)
|
||||
float gimbal_pitch_delta; //本周期云台Pitch角度增量(°)
|
||||
uint8_t gimbal_reset; //云台角度复位请求(遥控器重连首帧置位)
|
||||
uint8_t friction_on; //摩擦轮开关(保持型)
|
||||
uint8_t fire; //发射指令
|
||||
uint8_t eject; //退弹指令
|
||||
int16_t thumb_wheel; //遥控器拨轮原始值
|
||||
uint8_t source; //控制源标志(0=遥控器,1=键鼠)
|
||||
} cmd_robot_t;
|
||||
|
||||
extern volatile cmd_robot_t cmd_robot;//全局统一控制指令实例
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef APP_TASKS_H
|
||||
#define APP_TASKS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef enum
|
||||
{
|
||||
HB_IMU = 0,//IMU姿态解算任务心跳
|
||||
HB_CMD,//遥控指令综合任务心跳
|
||||
HB_GIMBAL,//云台控制任务心跳
|
||||
HB_SHOOT,//发射机构控制任务心跳
|
||||
HB_CHASSIS,//底盘控制任务心跳
|
||||
HB_CCOMM,//板间通讯发送任务心跳
|
||||
HB_REFEREE,//裁判系统解析任务心跳
|
||||
HB_COUNT,//心跳枚举总数
|
||||
} heartbeat_id_e;//任务心跳编号枚举
|
||||
|
||||
extern volatile uint8_t app_ready;//CAN_Task完成硬件初始化后置1
|
||||
|
||||
void monitor_heartbeat(heartbeat_id_e id);//各任务周期调用向监控任务报活
|
||||
|
||||
void dr16_frame_callback(const uint8_t * frame, uint16_t len);//USART3整帧接收回调(云台板生效)
|
||||
void referee_frame_callback(const uint8_t * frame, uint16_t len);//USART6整帧接收回调
|
||||
|
||||
void CAN_Task(void const * argument);//硬件初始化任务(freertos弱函数强定义)
|
||||
void LED_Task(void const * argument);//心跳监控与看门狗任务(freertos弱函数强定义)
|
||||
void IMU_Task(void const * argument);//姿态解算任务(freertos弱函数强定义)
|
||||
|
||||
void robot_cmd_task(void const * argument);//遥控与键鼠指令综合任务
|
||||
void gimbal_task(void const * argument);//云台双轴控制任务(仅云台板)
|
||||
void shoot_task(void const * argument);//发射机构控制任务(仅云台板)
|
||||
void chassis_task(void const * argument);//底盘运动控制任务(仅底盘板)
|
||||
void c_comm_task(void const * argument);//板间CAN通讯发送任务
|
||||
void referee_task(void const * argument);//裁判系统解析与UI任务
|
||||
void debug_task(void const * argument);//USB CDC串口诊断输出任务
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,65 @@
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "bsp_can.h"
|
||||
#include "bsp_usart.h"
|
||||
#include "usb_device.h"
|
||||
#include "c_comm.h"
|
||||
#include "led.h"
|
||||
#include "buzzer.h"
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
#include "gimbal.h"
|
||||
#include "shoot.h"
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
#include "chassis.h"
|
||||
#include "supercap.h"
|
||||
#include "hlj_pc.h"
|
||||
#endif
|
||||
|
||||
volatile uint8_t app_ready = 0;//全部硬件初始化完成标志
|
||||
volatile uint32_t dbg_can_loop = 0;//CAN_Task循环计数(诊断用)
|
||||
|
||||
static uint8_t dr16_rx_buf[36];//DR16单帧18字节,双倍缓冲防粘连
|
||||
static uint8_t referee_rx_buf[128];//裁判系统变长帧接收缓冲
|
||||
|
||||
/*
|
||||
*函数简介:硬件层统一初始化任务
|
||||
*参数说明:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:依次初始化LED/蜂鸣器/USB/CAN/电机/板间通讯/串口,完成后置app_ready=1
|
||||
*/
|
||||
void CAN_Task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
led_init();
|
||||
buzzer_init();
|
||||
led_set_blue(1);//初始化期间蓝灯常亮
|
||||
|
||||
MX_USB_DEVICE_Init();
|
||||
can_start();//CAN过滤器/启动/中断
|
||||
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
gimbal_motor_init();
|
||||
shoot_init();
|
||||
c_comm_init();
|
||||
|
||||
usart3_init(dr16_rx_buf, 36, dr16_frame_callback);//DR16遥控器
|
||||
usart6_init(referee_rx_buf, 128, referee_frame_callback);//图传链路(0x0304)
|
||||
usart1_tx_dma_init();
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
chassis_motor_init();
|
||||
supercap_init();
|
||||
hlj_pc_init();
|
||||
c_comm_init();
|
||||
|
||||
usart6_init(referee_rx_buf, 128, referee_frame_callback);//裁判系统(0x0201/0x0202)
|
||||
#endif
|
||||
|
||||
app_ready = 1;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
dbg_can_loop++;
|
||||
osDelay(1000);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/*
|
||||
*文件名称:debug_task.c
|
||||
*功能描述:USB虚拟串口(CDC)姿态与控制量遥测输出任务(底盘板)
|
||||
*备注:每DEBUG_OUT_PERIOD_MS输出一行欧拉角、姿态健康诊断与底盘控制模式
|
||||
*/
|
||||
#include "debug_task.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "usbd_cdc_if.h"
|
||||
#include "attitude.h"
|
||||
#include "ins.h"
|
||||
#include "bmi088.h"
|
||||
#include "robot_typedef.h"
|
||||
#include "c_comm.h"
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#define DEBUG_ATT_HEALTH 1 //1=输出健康位(cvg/rdy/imu/an/gn),0=仅欧拉角
|
||||
#define DEBUG_ATT_DIAG 1 //1=附加原始陀螺/零偏/采样计数,0=关闭
|
||||
#define DEBUG_CMD_OUT 1 //1=追加底盘控制量/模式,0=不追加
|
||||
#define DEBUG_OUT_PERIOD_MS 100//遥测输出周期(ms)
|
||||
|
||||
#if DEBUG_CMD_OUT
|
||||
/*
|
||||
*函数简介:将底盘控制模式枚举转换为可读字符串
|
||||
*参数说明:m:底盘控制模式枚举
|
||||
*返回类型:const char *:模式名称字符串
|
||||
*备注:仅用于遥测日志格式化
|
||||
*/
|
||||
static const char * cmd_mode_name(chassis_mode_e m)
|
||||
{
|
||||
switch (m)
|
||||
{
|
||||
case CHASSIS_MODE_OFF: return "OFF";
|
||||
case CHASSIS_MODE_MANUAL: return "MANUAL";
|
||||
case CHASSIS_MODE_FOLLOW: return "FOLLOW";
|
||||
case CHASSIS_MODE_SPIN_SLOW: return "SPIN_SLOW";
|
||||
case CHASSIS_MODE_SPIN_FAST: return "SPIN_FAST";
|
||||
default: return "?";
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
*函数简介:USB虚拟串口遥测输出任务
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:无
|
||||
*/
|
||||
void debug_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
char line[448];
|
||||
char tail[112];
|
||||
|
||||
for (;;)
|
||||
{
|
||||
const float an = sqrtf(bmi088_accel[0] * bmi088_accel[0] +
|
||||
bmi088_accel[1] * bmi088_accel[1] +
|
||||
bmi088_accel[2] * bmi088_accel[2]);
|
||||
const float gn = sqrtf(bmi088_gyro[0] * bmi088_gyro[0] +
|
||||
bmi088_gyro[1] * bmi088_gyro[1] +
|
||||
bmi088_gyro[2] * bmi088_gyro[2]);
|
||||
|
||||
tail[0] = '\0';
|
||||
#if DEBUG_ATT_HEALTH && DEBUG_ATT_DIAG
|
||||
snprintf(tail, sizeof(tail),
|
||||
" gx=%.4f gy=%.4f gz=%.4f zo=%.4f ck=%u ca=%lu cg=%lu se=%lu st=%u",
|
||||
bmi088_gyro[0], bmi088_gyro[1], bmi088_gyro[2],
|
||||
ins_gyro_z_offset, (unsigned)ins_z_cal_ok,
|
||||
(unsigned long)bmi088_accel_cnt, (unsigned long)bmi088_gyro_cnt,
|
||||
(unsigned long)bmi088_spi_errors, (unsigned)bmi088_spi_state());
|
||||
#endif
|
||||
|
||||
#if DEBUG_CMD_OUT
|
||||
char ctl[192];
|
||||
snprintf(ctl, sizeof(ctl),
|
||||
" mode=%s online=%u vx=%.3f vy=%.3f wz=%.3f spin=%u s2=%u reset=%u",
|
||||
cmd_mode_name(cmd_robot.chassis_mode),
|
||||
(unsigned)cmd_robot.online,
|
||||
cmd_robot.vx, cmd_robot.vy, cmd_robot.wz_manual,
|
||||
(unsigned)ctoc_remote.spin_flag,
|
||||
(unsigned)ctoc_remote.s2,
|
||||
(unsigned)cmd_robot.gimbal_reset);
|
||||
#else
|
||||
const char * ctl = "";
|
||||
#endif
|
||||
|
||||
#if DEBUG_ATT_HEALTH
|
||||
int len = snprintf(line, sizeof(line),
|
||||
"yaw=%.2f pitch=%.2f roll=%.2f cvg=%u rdy=%u imu=%u an=%.2f gn=%.3f%s%s\r\n",
|
||||
att_deg_yaw, att_deg_pitch, att_deg_roll,
|
||||
(unsigned)INS.ConvergeFlag, (unsigned)att_ready,
|
||||
(unsigned)bmi088_online, an, gn, tail, ctl);
|
||||
#else
|
||||
int len = snprintf(line, sizeof(line), "yaw=%.2f pitch=%.2f roll=%.2f%s\r\n",
|
||||
att_deg_yaw, att_deg_pitch, att_deg_roll, ctl);
|
||||
#endif
|
||||
|
||||
if (len > 0)
|
||||
{
|
||||
uint16_t tx_len = (len < (int)sizeof(line)) ? (uint16_t)len : (uint16_t)(sizeof(line) - 1U);
|
||||
CDC_Transmit_FS((uint8_t *)line, tx_len);
|
||||
}
|
||||
|
||||
osDelay(DEBUG_OUT_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#ifndef DEBUG_TASK_H
|
||||
#define DEBUG_TASK_H
|
||||
|
||||
/*
|
||||
*函数简介:USB虚拟串口(CDC)姿态与控制量遥测输出任务
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:具体输出字段与开关配置见debug_task.c
|
||||
*/
|
||||
extern void debug_task(void const * argument);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
*文件名称:imu_task.c
|
||||
*功能描述:IMU姿态解算与恒温控制任务(两板通用,1kHz,最高优先级)
|
||||
*备注:包含TIM10_CH1加热PWM与PID恒温控制,过温或BMI088离线时强制关闭加热
|
||||
*/
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "attitude.h"
|
||||
#include "bmi088.h"
|
||||
#include "ist8310.h"
|
||||
#include "pid.h"
|
||||
#include "tim.h"
|
||||
#include "robot_param.h"
|
||||
|
||||
extern TIM_HandleTypeDef htim10;//TIM10句柄(加热PWM)
|
||||
|
||||
volatile uint32_t dbg_imu_loop = 0;//诊断:IMU任务循环计数
|
||||
|
||||
static PID_PositionTypeDef imu_temp_pid;//IMU恒温位置式PID实例
|
||||
|
||||
/*
|
||||
*函数简介:初始化IMU恒温PID与TIM10_CH1加热PWM(1kHz/千分位)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:内部函数,由IMU_Task启动阶段调用
|
||||
*/
|
||||
static void imu_temp_ctrl_init(void)
|
||||
{
|
||||
PID_PositionStructureInit(&imu_temp_pid, IMU_TEMPERATURE_TARGET);
|
||||
PID_PositionSetParameter(&imu_temp_pid, IMU_TEMP_KP, IMU_TEMP_KI, IMU_TEMP_KD);
|
||||
PID_PositionSetEkRange(&imu_temp_pid, -1, 1);
|
||||
PID_PositionSetOUTRange(&imu_temp_pid, 0, 100);
|
||||
|
||||
__HAL_TIM_SET_PRESCALER(&htim10, 168 - 1);
|
||||
__HAL_TIM_SET_AUTORELOAD(&htim10, 1000 - 1);
|
||||
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, 0);
|
||||
HAL_TIM_PWM_Start(&htim10, TIM_CHANNEL_1);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:IMU姿态解算与恒温控制任务(1kHz)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:任务入口先上报一次心跳,防止传感器初始化校准期间看门狗误判
|
||||
*/
|
||||
void IMU_Task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
monitor_heartbeat(HB_IMU);//初始化期间先行报活
|
||||
imu_temp_ctrl_init();
|
||||
attitude_init();//含阻塞式传感器初始化与零偏校准
|
||||
|
||||
TickType_t last_wake = xTaskGetTickCount();//绝对延时基准
|
||||
for (;;)
|
||||
{
|
||||
attitude_update();
|
||||
ist8310_poll();//磁力计200Hz轮询(硬件I2C3,内部5ms分频)
|
||||
|
||||
/* 恒温控制:BMI088离线或过温时禁止加热,防止全功率失控 */
|
||||
if (bmi088_online && bmi088_temperature <= IMU_TEMPERATURE_LIMIT)
|
||||
{
|
||||
PID_PositionCalc(&imu_temp_pid, bmi088_temperature);
|
||||
__HAL_TIM_SET_COMPARE(&htim10, TIM_CHANNEL_1, (uint32_t)(imu_temp_pid.OUT * 10.0f));
|
||||
}
|
||||
else
|
||||
{
|
||||
imu_heater_off();
|
||||
}
|
||||
|
||||
monitor_heartbeat(HB_IMU);
|
||||
dbg_imu_loop++;
|
||||
vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(1));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
/*
|
||||
*文件名称:monitor_task.c
|
||||
*功能描述:系统监控任务(LED_Task强定义),负责任务心跳检测、LED/蜂鸣器报警与IWDG看门狗
|
||||
*备注:上电前5s宽限期无条件喂狗,之后仅当全部任务心跳正常时喂狗
|
||||
*/
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "stm32f4xx_hal.h"
|
||||
#include "led.h"
|
||||
#include "buzzer.h"
|
||||
#include "dr16.h"
|
||||
#include "referee.h"
|
||||
#include "c_comm.h"
|
||||
#include "robot_param.h"
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
#include "gimbal.h"
|
||||
#include "bmi088.h"
|
||||
#include "ist8310.h"
|
||||
#include "attitude.h"
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
#include "chassis.h"
|
||||
#endif
|
||||
|
||||
static volatile uint32_t heartbeat_tick[HB_COUNT];//各任务最近一次心跳时间戳(ms)
|
||||
|
||||
/*
|
||||
*函数简介:上报指定任务的心跳时间戳
|
||||
*参数说明:id:任务心跳枚举ID
|
||||
*返回类型:无
|
||||
*备注:由各周期任务在每次循环末尾调用
|
||||
*/
|
||||
void monitor_heartbeat(heartbeat_id_e id)
|
||||
{
|
||||
if ((uint32_t)id < (uint32_t)HB_COUNT)
|
||||
heartbeat_tick[id] = HAL_GetTick();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:检查本板所有受监控任务的心跳是否均在500ms内活跃
|
||||
*参数说明:无
|
||||
*返回类型:uint8_t:1=全部正常,0=存在超时或未启动任务
|
||||
*备注:无
|
||||
*/
|
||||
static uint8_t all_heartbeats_alive(void)
|
||||
{
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
static const heartbeat_id_e expected[] = {HB_IMU, HB_CMD, HB_GIMBAL, HB_SHOOT, HB_CCOMM, HB_REFEREE};
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
static const heartbeat_id_e expected[] = {HB_IMU, HB_CMD, HB_CHASSIS, HB_CCOMM, HB_REFEREE};
|
||||
#endif
|
||||
uint32_t now = HAL_GetTick();
|
||||
|
||||
for (uint8_t i = 0U; i < (uint8_t)(sizeof(expected) / sizeof(expected[0])); i++)
|
||||
{
|
||||
if (heartbeat_tick[expected[i]] == 0U || (now - heartbeat_tick[expected[i]] > 500U))
|
||||
return 0U;
|
||||
}
|
||||
return 1U;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:判断当前是否满足IWDG喂狗条件
|
||||
*参数说明:无
|
||||
*返回类型:uint8_t:1=允许喂狗,0=禁止喂狗
|
||||
*备注:启动后5s宽限期内无条件喂狗,之后要求全部任务心跳正常
|
||||
*/
|
||||
static uint8_t iwdg_should_feed(void)
|
||||
{
|
||||
if (HAL_GetTick() < 5000U)
|
||||
return 1U;
|
||||
return all_heartbeats_alive();
|
||||
}
|
||||
|
||||
#define LINK_OFFLINE(tick) ((tick) == 0U || (now - (tick) > DEVICE_OFFLINE_TIME_MS))
|
||||
/*
|
||||
*函数简介:查找首个掉线CAN设备的序号
|
||||
*参数说明:无
|
||||
*返回类型:int8_t:设备序号(0起),全部在线返回-1
|
||||
*备注:序号+1对应蜂鸣器报警响声次数
|
||||
*/
|
||||
static int8_t linkcheck_first_offline(void)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
if (LINK_OFFLINE(motor_friction_l.fdb.last_online_tick))
|
||||
return 0;
|
||||
if (LINK_OFFLINE(motor_friction_r.fdb.last_online_tick))
|
||||
return 1;
|
||||
if (LINK_OFFLINE(motor_pitch_gimbal.fdb.last_online_tick))
|
||||
return 2;
|
||||
if (LINK_OFFLINE(motor_yaw_gimbal.fdb.last_online_tick))
|
||||
return 3;
|
||||
#if GIMBAL_RAMMER_INSTALLED
|
||||
if (LINK_OFFLINE(motor_rammer.fdb.last_online_tick))
|
||||
return 4;
|
||||
#endif
|
||||
if (LINK_OFFLINE(ctoc_reply_last_tick))
|
||||
return 5;
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
if (LINK_OFFLINE(motor_wheel_rf.fdb.last_online_tick))
|
||||
return 0;
|
||||
if (LINK_OFFLINE(motor_wheel_lf.fdb.last_online_tick))
|
||||
return 1;
|
||||
if (LINK_OFFLINE(motor_wheel_lr.fdb.last_online_tick))
|
||||
return 2;
|
||||
if (LINK_OFFLINE(motor_wheel_rr.fdb.last_online_tick))
|
||||
return 3;
|
||||
if (ctoc_remote.last_tick == 0U || (now - ctoc_remote.last_tick > CTOC_OFFLINE_TIME_MS))
|
||||
return 4;
|
||||
#endif
|
||||
return -1;
|
||||
}
|
||||
#undef LINK_OFFLINE
|
||||
|
||||
/*
|
||||
*函数简介:直接写寄存器启动独立看门狗IWDG(约2.5s溢出)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:PR=4对应64分频(500Hz),RLR=1250对应约2.5s
|
||||
*/
|
||||
static void iwdg_init(void)
|
||||
{
|
||||
IWDG->KR = 0x5555U;//解除写保护
|
||||
IWDG->PR = 4U; //64分频
|
||||
IWDG->RLR = 1250U; //重装载值
|
||||
IWDG->KR = 0xAAAAU;//重装载
|
||||
IWDG->KR = 0xCCCCU;//启动看门狗
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:更新CAN掉线报警与遥控器上线提示音
|
||||
*参数说明:can_alarm:CAN设备掉线标志(1=存在掉线)
|
||||
*参数说明:counter:100ms节拍计数器(1~100)
|
||||
*返回类型:无
|
||||
*备注:内部函数,由LED_Task周期调用
|
||||
*/
|
||||
static void monitor_update_can_alarm(uint8_t can_alarm, uint8_t counter)
|
||||
{
|
||||
if (can_alarm)
|
||||
{
|
||||
led_set_red(1);
|
||||
int8_t first_offline = linkcheck_first_offline();
|
||||
|
||||
#if DEBUG_BUZZER_MELODY
|
||||
static uint16_t melody_step = 0U;
|
||||
(void)first_offline;
|
||||
buzzer_play_melody(melody_step++);
|
||||
#else
|
||||
if (first_offline >= 0)
|
||||
{
|
||||
uint8_t n = (uint8_t)(first_offline + 1);
|
||||
uint8_t w = (uint8_t)(counter % 20U);//2s周期窗口
|
||||
if ((w / 2U) < n && (w % 2U) == 0U)
|
||||
buzzer_on(BUZZER_L1);
|
||||
else
|
||||
buzzer_off();
|
||||
}
|
||||
else
|
||||
{
|
||||
buzzer_off();
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
static uint8_t prev_remote_online = 0U;
|
||||
static uint8_t chime_step = 0U;
|
||||
uint8_t remote_online = (uint8_t)!dr16_offline();
|
||||
|
||||
if (remote_online && !prev_remote_online)
|
||||
chime_step = 4U;//遥控器上线瞬间触发两声上扬提示音
|
||||
prev_remote_online = remote_online;
|
||||
|
||||
if (chime_step > 0U)
|
||||
{
|
||||
if (chime_step == 4U)
|
||||
buzzer_on(BUZZER_M3);
|
||||
else if (chime_step == 2U)
|
||||
buzzer_on(BUZZER_H3);
|
||||
else
|
||||
buzzer_off();
|
||||
chime_step--;
|
||||
}
|
||||
else
|
||||
{
|
||||
buzzer_off();
|
||||
}
|
||||
#else
|
||||
(void)counter;
|
||||
buzzer_off();
|
||||
led_set_red(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
/*
|
||||
*函数简介:更新云台板红绿报警灯语(遥控器掉线/图传超时/IMU过温与传感器故障)
|
||||
*参数说明:counter:100ms节拍计数器(1~100)
|
||||
*参数说明:can_alarm:CAN设备掉线标志
|
||||
*参数说明:imu_overheat:IMU过温锁存标志指针
|
||||
*返回类型:无
|
||||
*备注:内部函数,仅云台板编译
|
||||
*/
|
||||
static void monitor_update_gimbal_leds(uint8_t counter, uint8_t can_alarm, uint8_t * imu_overheat)
|
||||
{
|
||||
uint8_t phase = (uint8_t)(counter % 20U);
|
||||
|
||||
/* 遥控器掉线:1s内红灯连闪两下 */
|
||||
if (dr16_offline())
|
||||
{
|
||||
if (phase == 1U || phase == 13U)
|
||||
led_set_red(1);
|
||||
else if (phase == 5U || phase == 17U)
|
||||
led_set_red(0);
|
||||
}
|
||||
else if (!can_alarm)
|
||||
{
|
||||
led_set_red(0);
|
||||
}
|
||||
|
||||
/* 图传链路超时:绿灯1s周期闪烁 */
|
||||
if (referee.link_status &&
|
||||
(HAL_GetTick() - referee.last_update_tick > DEVICE_OFFLINE_TIME_MS * 10U))
|
||||
{
|
||||
led_set_green(phase < 10U ? 1U : 0U);
|
||||
}
|
||||
|
||||
/* IMU过温:绿灯常亮并关闭加热 */
|
||||
if (bmi088_temperature > IMU_TEMPERATURE_LIMIT)
|
||||
{
|
||||
if (imu_overheat != NULL)
|
||||
*imu_overheat = 1U;
|
||||
imu_heater_off();
|
||||
}
|
||||
if (imu_overheat != NULL && *imu_overheat)
|
||||
led_set_green(1);
|
||||
|
||||
/* 传感器检测失败灯语 */
|
||||
if (!bmi088_online && !ist8310_online)
|
||||
{
|
||||
led_set_green(1);
|
||||
}
|
||||
else if (!bmi088_online)
|
||||
{
|
||||
if (phase == 1U || phase == 13U)
|
||||
led_set_green(1);
|
||||
else if (phase == 5U || phase == 17U)
|
||||
led_set_green(0);
|
||||
}
|
||||
else if (!ist8310_online)
|
||||
{
|
||||
led_set_green(phase < 10U ? 1U : 0U);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
*函数简介:系统监控任务(100ms,LED_Task强定义)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:负责看门狗喂狗、设备掉线报警与健康心跳灯语
|
||||
*/
|
||||
void LED_Task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
uint8_t counter = 0U;//100ms周期计数(1~100)
|
||||
uint8_t imu_overheat = 0U;//IMU过温锁存标志
|
||||
uint8_t can_alarm = 0U;//CAN掉线报警标志
|
||||
uint8_t first_ready = 1U;//初始化完成后首拍清灯标志
|
||||
|
||||
iwdg_init();//入口立即启动看门狗
|
||||
|
||||
while (!app_ready)
|
||||
{
|
||||
IWDG->KR = 0xAAAAU;//初始化阶段保持喂狗
|
||||
osDelay(10);
|
||||
}
|
||||
|
||||
for (;;)
|
||||
{
|
||||
counter++;
|
||||
if (counter > 100U)
|
||||
counter = 1U;
|
||||
|
||||
if (first_ready)
|
||||
{
|
||||
first_ready = 0U;
|
||||
led_set_all_off();//熄灭初始化蓝灯,交由报警逻辑接管
|
||||
}
|
||||
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
can_alarm = gimbal_motor_offline();
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
can_alarm = chassis_motor_offline();
|
||||
#endif
|
||||
|
||||
monitor_update_can_alarm(can_alarm, counter);
|
||||
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
monitor_update_gimbal_leds(counter, can_alarm, &imu_overheat);
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
led_set_green(0);
|
||||
#endif
|
||||
|
||||
/* 健康心跳:无报警时蓝灯每2s闪100ms */
|
||||
uint8_t healthy = (uint8_t)(!can_alarm && !imu_overheat);
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
healthy = (uint8_t)(healthy && bmi088_online && ist8310_online);
|
||||
#endif
|
||||
if (healthy)
|
||||
{
|
||||
if (counter % 20U == 10U)
|
||||
led_set_blue(1);
|
||||
else if (counter % 20U == 11U)
|
||||
led_set_blue(0);
|
||||
}
|
||||
|
||||
if (iwdg_should_feed())
|
||||
IWDG->KR = 0xAAAAU;
|
||||
|
||||
osDelay(100);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user