基于战队 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>
243 lines
8.8 KiB
C
243 lines
8.8 KiB
C
/*
|
|
*文件名称: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
|