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,94 @@
|
||||
#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)
|
||||
uint32_t dr16_frame_count = 0;//累计收到帧数
|
||||
|
||||
/*
|
||||
*函数简介:按键上升沿翻转锁存辅助函数
|
||||
*参数说明:上一帧按键状态
|
||||
*参数说明:当前帧按键状态
|
||||
*参数说明:锁存变量指针
|
||||
*返回类型:无
|
||||
*备注:仅在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.key_code_lo = data[14];//原始按键低字节
|
||||
dr16.key_code_hi = data[15];//原始按键高字节
|
||||
|
||||
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;
|
||||
dr16_frame_count++;
|
||||
|
||||
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,57 @@
|
||||
#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
|
||||
|
||||
uint8_t key_code_lo;//原始按键低字节(data[14])
|
||||
uint8_t key_code_hi;//原始按键高字节(data[15])
|
||||
|
||||
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)
|
||||
extern uint32_t dr16_frame_count;//累计收到帧数
|
||||
|
||||
void dr16_parse(const uint8_t * data);//解析一帧18字节DR16原始数据
|
||||
uint8_t dr16_offline(void);//判定遥控器是否掉线(1-掉线,0-在线)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,341 @@
|
||||
/*
|
||||
*文件名称:robot_cmd_task.c
|
||||
*功能描述:机器人控制指令汇总任务(2ms),发布全局cmd_robot控制结构体
|
||||
*备注:云台板汇总DR16与键鼠(0x0304优先+USART3帧内键鼠合并),底盘板汇总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)
|
||||
|
||||
#define KB_FROM_DBUS_FRAME 1//1=同时接受USART3帧内键鼠,0=仅0x0304图传键鼠
|
||||
|
||||
keyboard_data_t keyboard_data;//0x0304图传键鼠原始数据
|
||||
keyboard_data_t keyboard_merged;//合并后的键鼠共享视图
|
||||
uint8_t keyboard_merged_online = 0U;//键鼠在控标志
|
||||
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();
|
||||
}
|
||||
|
||||
#if KB_FROM_DBUS_FRAME
|
||||
/*
|
||||
*函数简介:根据USART3帧内键鼠动作与云台左摇杆动作更新键鼠模式状态
|
||||
*参数说明:kb_mode:当前键鼠模式标志
|
||||
*返回类型:uint8_t:更新后的键鼠模式标志
|
||||
*备注:左摇杆动作优先把控制权交还遥控器
|
||||
*/
|
||||
static uint8_t cmd_update_kb_mode(uint8_t kb_mode)
|
||||
{
|
||||
uint8_t kb_act = 0U;
|
||||
uint8_t rc_act = 0U;
|
||||
|
||||
if (dr16.mouse_rl > 1 || dr16.mouse_rl < -1) kb_act = 1U;
|
||||
if (dr16.mouse_du > 1 || dr16.mouse_du < -1) kb_act = 1U;
|
||||
if (dr16.mouse_wheel != 0) kb_act = 1U;
|
||||
if (dr16.mouse_key_l || dr16.mouse_key_r) kb_act = 1U;
|
||||
if (dr16.key_code_lo || dr16.key_code_hi) kb_act = 1U;
|
||||
|
||||
if (dr16.rc_l_UD > 1050 || dr16.rc_l_UD < 1000) rc_act = 1U;
|
||||
if (dr16.rc_l_RL > 1050 || dr16.rc_l_RL < 1000) rc_act = 1U;
|
||||
|
||||
if (kb_act)
|
||||
kb_mode = 1U;
|
||||
if (rc_act)
|
||||
kb_mode = 0U;
|
||||
|
||||
return kb_mode;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
*函数简介:将0x0304图传键鼠与USART3帧内键鼠合并至共享视图keyboard_merged
|
||||
*参数说明:kb_online:0x0304图传键鼠在线标志
|
||||
*返回类型:无
|
||||
*备注:0x0304在线时整份采用keyboard_data,否则拷贝DR16帧内键鼠字段
|
||||
*/
|
||||
static void cmd_merge_keyboard(uint8_t kb_online)
|
||||
{
|
||||
if (kb_online)
|
||||
{
|
||||
keyboard_merged = keyboard_data;
|
||||
return;
|
||||
}
|
||||
|
||||
keyboard_merged.mouse_rl = dr16.mouse_rl;
|
||||
keyboard_merged.mouse_du = dr16.mouse_du;
|
||||
keyboard_merged.mouse_wheel = dr16.mouse_wheel;
|
||||
keyboard_merged.mouse_key_l = dr16.mouse_key_l;
|
||||
keyboard_merged.mouse_key_r = dr16.mouse_key_r;
|
||||
keyboard_merged.key_w = dr16.key_w;
|
||||
keyboard_merged.key_s = dr16.key_s;
|
||||
keyboard_merged.key_a = dr16.key_a;
|
||||
keyboard_merged.key_d = dr16.key_d;
|
||||
keyboard_merged.key_shift = dr16.key_shift;
|
||||
keyboard_merged.key_ctrl = dr16.key_ctrl;
|
||||
keyboard_merged.key_q = dr16.key_q;
|
||||
keyboard_merged.key_e = dr16.key_e;
|
||||
keyboard_merged.key_alpha1 = dr16.key_alpha1;
|
||||
keyboard_merged.key_alpha2 = dr16.key_alpha2;
|
||||
keyboard_merged.key_push_q = dr16.key_push_q;
|
||||
keyboard_merged.key_push_e = dr16.key_push_e;
|
||||
keyboard_merged.key_push_shift = dr16.key_push_shift;
|
||||
keyboard_merged.key_push_ctrl = dr16.key_push_ctrl;
|
||||
keyboard_merged.last_tick = dr16_last_tick;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:根据当前控制源计算云台Yaw/Pitch增量并写入cmd_robot
|
||||
*参数说明:kb_active:键鼠在控标志(1=键鼠,0=遥控器)
|
||||
*返回类型:无
|
||||
*备注:含Pitch机械上下限位门控
|
||||
*/
|
||||
static void cmd_calc_gimbal_delta(uint8_t kb_active)
|
||||
{
|
||||
float yaw_delta = 0.0f;
|
||||
float pitch_delta = 0.0f;
|
||||
uint16_t pos = motor_pitch_gimbal.fdb.raw_angle;
|
||||
|
||||
if (!kb_active)
|
||||
{
|
||||
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;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (keyboard_merged.mouse_du > 8 && pos > PITCH_GM6020_POSITION_UPPER_LIMIT)
|
||||
pitch_delta -= GIMBAL_PITCH_LEVER_RATE / 8192.0f * 360.0f *
|
||||
((float)keyboard_merged.mouse_du * PC_MOUSE_DU_SENSITIVITY) / 660.0f;
|
||||
else if (keyboard_merged.mouse_du < -8 && pos < PITCH_GM6020_POSITION_LOWER_LIMIT)
|
||||
pitch_delta += GIMBAL_PITCH_LEVER_RATE / 8192.0f * 360.0f *
|
||||
(-(float)keyboard_merged.mouse_du * PC_MOUSE_DU_SENSITIVITY) / 660.0f;
|
||||
|
||||
if (keyboard_merged.mouse_rl != 0)
|
||||
yaw_delta -= GIMBAL_LEVER_SPEED_MAP_RATE * GIMBAL_YAW_PITCH_SPEED_RATE *
|
||||
GIMBAL_YAW_PITCH_SPEED_RATE * 0.0439453125f *
|
||||
((float)keyboard_merged.mouse_rl * PC_MOUSE_RL_SENSITIVITY / 660.0f * 2.0f);
|
||||
}
|
||||
|
||||
cmd_robot.gimbal_yaw_delta = yaw_delta;
|
||||
cmd_robot.gimbal_pitch_delta = pitch_delta;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:云台板控制源汇总任务(2ms)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:无
|
||||
*/
|
||||
void robot_cmd_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
static uint8_t kb_mode = 0U;//帧内键鼠模式标志
|
||||
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));
|
||||
|
||||
#if KB_FROM_DBUS_FRAME
|
||||
kb_mode = cmd_update_kb_mode(kb_mode);
|
||||
#endif
|
||||
|
||||
uint8_t kb_active = (keyboard_online || kb_mode) ? 1U : 0U;
|
||||
cmd_merge_keyboard(keyboard_online);
|
||||
keyboard_merged_online = kb_active;
|
||||
|
||||
cmd_robot.online = (uint8_t)(dr16_status == 1 && !dr16_offline());
|
||||
cmd_robot.source = kb_active ? 1U : 0U;
|
||||
|
||||
if (cmd_robot.online)
|
||||
{
|
||||
cmd_calc_gimbal_delta(kb_active);
|
||||
cmd_robot.aim = kb_active ? (uint8_t)(keyboard_merged.mouse_key_r != 0U) : (uint8_t)(dr16.s1 == 2U);
|
||||
|
||||
if (!kb_active)
|
||||
{
|
||||
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
|
||||
{
|
||||
cmd_robot.friction_on = keyboard_merged.key_push_q;
|
||||
cmd_robot.fire = keyboard_merged.mouse_key_l;
|
||||
cmd_robot.eject = keyboard_merged.key_push_e;
|
||||
}
|
||||
cmd_robot.thumb_wheel = dr16.thumb_wheel;
|
||||
}
|
||||
else
|
||||
{
|
||||
cmd_robot.gimbal_yaw_delta = 0.0f;
|
||||
cmd_robot.gimbal_pitch_delta = 0.0f;
|
||||
cmd_robot.aim = 0U;
|
||||
cmd_robot.friction_on = 0U;
|
||||
cmd_robot.fire = 0U;
|
||||
cmd_robot.eject = 0U;
|
||||
}
|
||||
|
||||
/* 小陀螺使能:Ctrl沿翻转(云台为数据源,防止底盘重连误触发) */
|
||||
uint8_t ctrl_now = keyboard_online ? keyboard_data.key_push_ctrl : dr16.key_push_ctrl;
|
||||
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,27 @@
|
||||
#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图传键鼠原始数据
|
||||
extern keyboard_data_t keyboard_merged; //合并后的键鼠共享视图(0x0304优先)
|
||||
extern uint8_t keyboard_merged_online; //1=键鼠在控(0x0304在线或帧内键鼠动作)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,305 @@
|
||||
#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=未检测)
|
||||
|
||||
/*
|
||||
*函数简介:判定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)
|
||||
|
||||
/*
|
||||
*函数简介:底盘侧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();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化板间通讯(底盘从机)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:在can_start()之后调用,注册CAN2 0x149/0x189/0x1A9接收回调
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:底盘板发送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,43 @@
|
||||
#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=未检测)
|
||||
|
||||
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,85 @@
|
||||
#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字节报文
|
||||
*参数说明:输出12字节键鼠缓冲区
|
||||
*返回类型:无
|
||||
*备注:直接使用robot_cmd_task生成的keyboard_merged,保证云台与底盘键鼠来源一致
|
||||
*/
|
||||
static void c_comm_pack_kb(uint8_t kb[12])
|
||||
{
|
||||
kb[0] = (uint8_t)(keyboard_merged.mouse_rl & 0xFF);
|
||||
kb[1] = (uint8_t)((uint16_t)keyboard_merged.mouse_rl >> 8);
|
||||
kb[2] = (uint8_t)(keyboard_merged.mouse_du & 0xFF);
|
||||
kb[3] = (uint8_t)((uint16_t)keyboard_merged.mouse_du >> 8);
|
||||
kb[4] = (uint8_t)(keyboard_merged.mouse_wheel & 0xFF);
|
||||
kb[5] = (uint8_t)((uint16_t)keyboard_merged.mouse_wheel >> 8);
|
||||
kb[6] = keyboard_merged.mouse_key_l;
|
||||
kb[7] = keyboard_merged.mouse_key_r;
|
||||
kb[8] = (uint8_t)(keyboard_merged.key_w | (keyboard_merged.key_s << 1) |
|
||||
(keyboard_merged.key_a << 2) | (keyboard_merged.key_d << 3) |
|
||||
(keyboard_merged.key_shift << 4) | (keyboard_merged.key_ctrl << 5) |
|
||||
(keyboard_merged.key_q << 6) | (keyboard_merged.key_e << 7));
|
||||
kb[9] = (uint8_t)(keyboard_merged.key_alpha1 | (keyboard_merged.key_alpha2 << 1));
|
||||
kb[10] = 0;
|
||||
kb[11] = 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
*函数简介:板间通讯周期发送任务(2ms)
|
||||
*参数说明:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:云台板发0x149/0x189/0x1B9/0x1A9,底盘板回传0x019
|
||||
*/
|
||||
void c_comm_task(void const * argument)
|
||||
{
|
||||
TickType_t last_wake = xTaskGetTickCount();
|
||||
|
||||
(void)argument;
|
||||
|
||||
while (!app_ready)
|
||||
osDelay(2);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
#if defined(ROBOT_TYPE_GIMBAL)
|
||||
uint8_t kb[12];
|
||||
const int8_t enc = gimbal_encoder_ccw();
|
||||
uint8_t flags = att_ready;
|
||||
|
||||
c_comm_pack_kb(kb);
|
||||
c_comm_master_send(keyboard_merged_online, kb, gimbal_motor_offline(), cmd_robot.spin_flag);
|
||||
|
||||
c_comm_master_send_ui(-att_deg_roll,
|
||||
(float)motor_friction_l.fdb.rotor_speed,
|
||||
(float)motor_friction_r.fdb.rotor_speed,
|
||||
cmd_robot.spin_flag);//发送0x1B9自定义UI数据
|
||||
|
||||
if (enc > 0)
|
||||
flags |= 0x02U;//bit1=编码器增向CCW
|
||||
if (enc != 0)
|
||||
flags |= 0x04U;//bit2=编码器方向有效
|
||||
c_comm_master_send_attitude(att_deg_yaw, flags);//发送0x1A9姿态帧
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
c_comm_slave_send(chassis_motor_offline());//回传0x019发射机构状态与故障位
|
||||
#endif
|
||||
|
||||
monitor_heartbeat(HB_CCOMM);
|
||||
vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,327 @@
|
||||
#include "gimbal.h"
|
||||
#include "pid.h"
|
||||
#include "filter.h"
|
||||
#include "attitude.h"
|
||||
#include "bmi088.h"
|
||||
#include "ins.h"
|
||||
#include "can.h"
|
||||
#include "robot_param.h"
|
||||
#include "robot_typedef.h"
|
||||
#include "dr16.h"
|
||||
#include <math.h>
|
||||
|
||||
#ifndef ANGLE_TO_RAD
|
||||
#define ANGLE_TO_RAD 0.01745329251994329576923690768489f//角度转弧度系数(PI/180)
|
||||
#endif
|
||||
|
||||
#define GIMBAL_ECD_HALF_TURN 4096//编码器半圈tick数
|
||||
#define GIMBAL_ECD_FULL_TURN 8192//编码器一圈tick数
|
||||
|
||||
dji_motor_t motor_yaw_gimbal;//Yaw GM6020(CAN2 0x205)
|
||||
dji_motor_t motor_pitch_gimbal;//Pitch GM6020(CAN1 0x206)
|
||||
dji_motor_t motor_friction_l;//左摩擦轮M3508(CAN1 0x201)
|
||||
dji_motor_t motor_friction_r;//右摩擦轮M3508(CAN1 0x202)
|
||||
dji_motor_t motor_rammer;//拨弹盘M2006(CAN2 0x201)
|
||||
|
||||
static PID_PositionTypeDef yaw_angle_pid, yaw_speed_pid;//Yaw位置环与速度环PID
|
||||
static PID_PositionTypeDef pitch_angle_pid, pitch_speed_pid;//Pitch位置环与速度环PID
|
||||
|
||||
static float yaw_target_deg;//世界系Yaw目标角(°)
|
||||
static float pitch_target_deg;//Pitch目标角(°)
|
||||
static float yaw_target_prev_deg;//上周期Yaw目标角(前馈微分用)
|
||||
static float yaw_rate_ff;//低通滤波后目标角速度前馈(°/s)
|
||||
static float yaw_current;//Yaw最终电流指令(±16384)
|
||||
float pitch_pid = 0.0f;//Pitch速度环输出
|
||||
float pitch_g = 0.0f;//Pitch重力前馈
|
||||
float pitch_current = 0.0f;//Pitch最终电流指令
|
||||
|
||||
static float yaw_sign_f = 0.0f;//实测Yaw装配符号(0=未检,±1=已测)
|
||||
static uint8_t yaw_sign_valid = 0;//Yaw装配符号有效标志
|
||||
|
||||
/*
|
||||
*函数简介:计算编码器相对零位的最短有符号差值
|
||||
*参数说明:当前编码器原始值(0~8191)
|
||||
*参数说明:机械零位编码器值(0~8191)
|
||||
*返回类型:最短差值(-4096~+4096)
|
||||
*备注:无
|
||||
*/
|
||||
static int16_t gimbal_ecd_shortest_diff(uint16_t raw, uint16_t zero)
|
||||
{
|
||||
int16_t diff = (int16_t)raw - (int16_t)zero;
|
||||
if (diff > GIMBAL_ECD_HALF_TURN)
|
||||
diff -= GIMBAL_ECD_FULL_TURN;
|
||||
else if (diff < -GIMBAL_ECD_HALF_TURN)
|
||||
diff += GIMBAL_ECD_FULL_TURN;
|
||||
return diff;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:判定单颗电机是否掉线
|
||||
*参数说明:电机实例指针
|
||||
*参数说明:当前系统tick(HAL_GetTick)
|
||||
*返回类型:1-掉线,0-在线
|
||||
*备注:无
|
||||
*/
|
||||
static uint8_t gimbal_one_offline(const dji_motor_t * motor, uint32_t now)
|
||||
{
|
||||
return (motor->fdb.last_online_tick == 0U ||
|
||||
(now - motor->fdb.last_online_tick) > DEVICE_OFFLINE_TIME_MS) ? 1U : 0U;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化云台与发射机构电机实例及双轴PID控制器
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:必须在can_start()之后调用一次
|
||||
*/
|
||||
void gimbal_motor_init(void)
|
||||
{
|
||||
dji_motor_init(&motor_yaw_gimbal, &hcan2, 0x205, DJI_MOTOR_6020);
|
||||
dji_motor_init(&motor_pitch_gimbal, &hcan1, 0x206, DJI_MOTOR_6020);
|
||||
dji_motor_init(&motor_friction_l, &hcan1, 0x201, DJI_MOTOR_3508);
|
||||
dji_motor_init(&motor_friction_r, &hcan1, 0x202, DJI_MOTOR_3508);
|
||||
dji_motor_init(&motor_rammer, &hcan2, 0x201, DJI_MOTOR_2006);
|
||||
|
||||
PID_PositionStructureInit(&yaw_angle_pid, 0);//Yaw位置环:°->°/s
|
||||
PID_PositionSetParameter(&yaw_angle_pid, GIMBAL_YAW_ANGLE_KP, GIMBAL_YAW_ANGLE_KI, GIMBAL_YAW_ANGLE_KD);
|
||||
PID_PositionSetEkRange(&yaw_angle_pid, -GIMBAL_YAW_ANGLE_EK_DEADBAND, GIMBAL_YAW_ANGLE_EK_DEADBAND);
|
||||
PID_PositionSetOUTRange(&yaw_angle_pid, -GIMBAL_YAW_ANGLE_OUT_LIMIT, GIMBAL_YAW_ANGLE_OUT_LIMIT);
|
||||
|
||||
PID_PositionStructureInit(&yaw_speed_pid, 0);//Yaw速度环:°/s->电流
|
||||
PID_PositionSetParameter(&yaw_speed_pid, GIMBAL_YAW_SPEED_KP, GIMBAL_YAW_SPEED_KI, GIMBAL_YAW_SPEED_KD);
|
||||
PID_PositionSetOUTRange(&yaw_speed_pid, -GIMBAL_YAW_SPEED_OUT_LIMIT, GIMBAL_YAW_SPEED_OUT_LIMIT);
|
||||
|
||||
PID_PositionStructureInit(&pitch_angle_pid, 0);//Pitch位置环:°->RPM
|
||||
PID_PositionSetParameter(&pitch_angle_pid, GIMBAL_PITCH_ANGLE_KP, GIMBAL_PITCH_ANGLE_KI, GIMBAL_PITCH_ANGLE_KD);
|
||||
PID_PositionSetEkRange(&pitch_angle_pid, -GIMBAL_PITCH_ANGLE_EK_DEADBAND, GIMBAL_PITCH_ANGLE_EK_DEADBAND);
|
||||
PID_PositionSetOUTRange(&pitch_angle_pid, -200.0f, 200.0f);
|
||||
|
||||
PID_PositionStructureInit(&pitch_speed_pid, 300.0f);//Pitch速度环:RPM->电流
|
||||
PID_PositionSetParameter(&pitch_speed_pid, GIMBAL_PITCH_SPEED_KP, GIMBAL_PITCH_SPEED_KI, GIMBAL_PITCH_SPEED_KD);
|
||||
PID_PositionSetEkRange(&pitch_speed_pid, 0.0f, 0.0f);
|
||||
PID_PositionSetOUTRange(&pitch_speed_pid, -16384.0f, 16384.0f);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:上电自检Yaw装配符号
|
||||
*参数说明:无
|
||||
*返回类型:1-自检成功,0-电机离线/被按住/无响应
|
||||
*备注:给Yaw电机施加60ms正向脉冲并测量陀螺z轴响应,阻塞约190ms
|
||||
*/
|
||||
uint8_t gimbal_detect_yaw_sign(void)
|
||||
{
|
||||
float sum = 0.0f;
|
||||
float rate_dps;
|
||||
|
||||
if (gimbal_motor_offline())
|
||||
return 0;
|
||||
|
||||
dji_motor_send_gm6020_current_low(&hcan2, 0, 0, 0, 0);//静默等待稳定
|
||||
HAL_Delay(50);
|
||||
|
||||
dji_motor_send_gm6020_current_low(&hcan2, GIMBAL_YAW_DETECT_CURRENT, 0, 0, 0);
|
||||
for (uint16_t i = 0; i < 60; i++)
|
||||
{
|
||||
sum += bmi088_gyro[2];//rad/s(z轴向上为正)
|
||||
HAL_Delay(1);
|
||||
}
|
||||
dji_motor_send_gm6020_current_low(&hcan2, 0, 0, 0, 0);
|
||||
HAL_Delay(80);//回稳
|
||||
|
||||
rate_dps = sum / 60.0f * RAD_TO_ANGLE;
|
||||
if (rate_dps > GIMBAL_YAW_DETECT_RATE_MIN)
|
||||
{
|
||||
yaw_sign_f = 1.0f;//正电流->陀螺正转
|
||||
yaw_sign_valid = 1;
|
||||
return 1;
|
||||
}
|
||||
if (rate_dps < -GIMBAL_YAW_DETECT_RATE_MIN)
|
||||
{
|
||||
yaw_sign_f = -1.0f;//反向装配
|
||||
yaw_sign_valid = 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:获取Yaw装配符号
|
||||
*参数说明:无
|
||||
*返回类型:+1.0或-1.0
|
||||
*备注:未自检时回退到GIMBAL_YAW_SIGN_FALLBACK
|
||||
*/
|
||||
float gimbal_yaw_sign(void)
|
||||
{
|
||||
return yaw_sign_valid ? yaw_sign_f : GIMBAL_YAW_SIGN_FALLBACK;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:获取编码器计数增向对应的云台旋转方向
|
||||
*参数说明:无
|
||||
*返回类型:+1=逆时针(CCW),-1=顺时针(CW),0=尚未自检
|
||||
*备注:随0x1A9报文发给底盘综合跟随符号
|
||||
*/
|
||||
int8_t gimbal_encoder_ccw(void)
|
||||
{
|
||||
if (!yaw_sign_valid)
|
||||
return 0;
|
||||
return (yaw_sign_f > 0.0f) ? 1 : -1;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:获取Yaw速度环PID原始输出
|
||||
*参数说明:无
|
||||
*返回类型:电流码(±16384,不含小陀螺前馈)
|
||||
*备注:仅用于标定GIMBAL_YAW_SPIN_FEEDFORWARD_K
|
||||
*/
|
||||
float gimbal_yaw_speed_out(void)
|
||||
{
|
||||
return yaw_speed_pid.OUT;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:云台双轴控制主循环
|
||||
*参数说明:本周期Yaw目标角增量(°)
|
||||
*参数说明:本周期Pitch目标角增量(°)
|
||||
*参数说明:复位标志(非0时重锁零位并清PID)
|
||||
*返回类型:无
|
||||
*备注:固定2ms调用一次,内部直接经CAN1/CAN2发送GM6020电流指令
|
||||
*/
|
||||
void gimbal_control(float yaw_delta, float pitch_delta, uint8_t reset_flag)
|
||||
{
|
||||
static uint8_t first_run = 1;
|
||||
const float sign = gimbal_yaw_sign();
|
||||
const float yaw_meas = sign * att_deg_yaw;//°
|
||||
const float yaw_rate_meas = sign * ((bmi088_gyro[2] - ins_gyro_z_offset) * RAD_TO_ANGLE);//°/s
|
||||
float yaw_rate_cmd;
|
||||
float yaw_rate_ref;
|
||||
float spin_feedforward = 0.0f;
|
||||
float a_deg;
|
||||
float a_rad;
|
||||
|
||||
if (first_run || reset_flag)
|
||||
{
|
||||
int16_t ecd_diff;
|
||||
int16_t pitch_ecd_diff;
|
||||
|
||||
first_run = 0;
|
||||
ecd_diff = gimbal_ecd_shortest_diff(motor_yaw_gimbal.fdb.raw_angle, YAW_GM6020_ENCODER_ZERO);
|
||||
yaw_target_deg = yaw_meas - (float)ecd_diff * (360.0f / 8192.0f);
|
||||
|
||||
pitch_ecd_diff = (int16_t)motor_pitch_gimbal.fdb.raw_angle - (int16_t)PITCH_GM6020_ENCODER_ZERO;
|
||||
pitch_target_deg = -att_deg_roll - (float)pitch_ecd_diff * (360.0f / 8192.0f);
|
||||
|
||||
yaw_target_prev_deg = yaw_target_deg;
|
||||
yaw_rate_ff = 0.0f;
|
||||
|
||||
if (reset_flag)
|
||||
{
|
||||
PID_PositionClean(&yaw_angle_pid);
|
||||
PID_PositionClean(&yaw_speed_pid);
|
||||
PID_PositionClean(&pitch_angle_pid);
|
||||
PID_PositionClean(&pitch_speed_pid);
|
||||
}
|
||||
}
|
||||
|
||||
yaw_target_deg += yaw_delta;
|
||||
pitch_target_deg += pitch_delta;
|
||||
|
||||
//目标角速度前馈(2ms周期*500折算为°/s,一阶低通抑制视觉阶跃噪声)
|
||||
yaw_rate_cmd = (yaw_target_deg - yaw_target_prev_deg) * 500.0f;
|
||||
yaw_target_prev_deg = yaw_target_deg;
|
||||
yaw_rate_ff += GIMBAL_YAW_FF_ALPHA * (yaw_rate_cmd - yaw_rate_ff);
|
||||
|
||||
yaw_angle_pid.Need_Value = yaw_target_deg;
|
||||
PID_PositionCalc(&yaw_angle_pid, yaw_meas);
|
||||
|
||||
yaw_rate_ref = fp32_constrain(
|
||||
yaw_angle_pid.OUT + GIMBAL_YAW_FF_GAIN * yaw_rate_ff,
|
||||
-GIMBAL_YAW_ANGLE_OUT_LIMIT, GIMBAL_YAW_ANGLE_OUT_LIMIT);
|
||||
yaw_speed_pid.Need_Value = yaw_rate_ref;
|
||||
PID_PositionCalc(&yaw_speed_pid, yaw_rate_meas);
|
||||
|
||||
if (cmd_robot.spin_flag || dr16.s2 == 1 || dr16.s2 == 2)
|
||||
spin_feedforward = GIMBAL_YAW_SPIN_FEEDFORWARD_K * (float)motor_yaw_gimbal.fdb.rotor_speed;
|
||||
|
||||
yaw_current = fp32_constrain(
|
||||
yaw_speed_pid.OUT + spin_feedforward,
|
||||
-GIMBAL_YAW_SPEED_OUT_LIMIT, GIMBAL_YAW_SPEED_OUT_LIMIT);
|
||||
|
||||
//Pitch串级闭环(位置环反馈为AHRS Roll)
|
||||
pitch_angle_pid.Need_Value = pitch_target_deg;
|
||||
PID_PositionCalc(&pitch_angle_pid, -att_deg_roll);
|
||||
pitch_speed_pid.Need_Value = pitch_angle_pid.OUT;
|
||||
PID_PositionCalc(&pitch_speed_pid, motor_pitch_gimbal.fdb.rotor_speed);
|
||||
pitch_pid = pitch_speed_pid.OUT;
|
||||
|
||||
//Pitch重力拟合前馈直接叠加到最终电流
|
||||
a_deg = ((float)motor_pitch_gimbal.fdb.raw_angle - GIMBAL_PITCH_GRAVITY_ZERO_ECD) * (360.0f / 8192.0f);
|
||||
a_rad = a_deg * ANGLE_TO_RAD;
|
||||
pitch_g = GIMBAL_PITCH_GRAVITY_K_COS * cosf(a_rad) +
|
||||
GIMBAL_PITCH_GRAVITY_K_SIN * sinf(a_rad) +
|
||||
GIMBAL_PITCH_GRAVITY_K_CONST;
|
||||
|
||||
pitch_current = fp32_constrain(pitch_pid + pitch_g, -16384.0f, 16384.0f);
|
||||
|
||||
dji_motor_send_gm6020_current_low(&hcan2, (int16_t)yaw_current, 0, 0, 0);//Yaw->CAN2 0x1FE槽1
|
||||
dji_motor_send_gm6020_current_low(&hcan1, 0, (int16_t)pitch_current, 0, 0);//Pitch->CAN1 0x1FE槽2
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:直接设定云台绝对目标角(视觉自瞄用)
|
||||
*参数说明:世界系Yaw目标角(°)
|
||||
*参数说明:Pitch目标角(°)
|
||||
*返回类型:无
|
||||
*备注:Yaw自动取距当前目标最近等效角,Pitch按编码器剩余行程做软限位
|
||||
*/
|
||||
void gimbal_set_target_deg(float yaw_deg, float pitch_deg)
|
||||
{
|
||||
const float k = 360.0f / 8192.0f;
|
||||
const float ecd = (float)motor_pitch_gimbal.fdb.raw_angle;
|
||||
const float p_min = pitch_target_deg + ((float)PITCH_GM6020_POSITION_UPPER_LIMIT - ecd) * k;
|
||||
const float p_max = pitch_target_deg + ((float)PITCH_GM6020_POSITION_LOWER_LIMIT - ecd) * k;
|
||||
|
||||
while (yaw_deg - yaw_target_deg > 180.0f)
|
||||
yaw_deg -= 360.0f;
|
||||
while (yaw_deg - yaw_target_deg < -180.0f)
|
||||
yaw_deg += 360.0f;
|
||||
yaw_target_deg = yaw_deg;
|
||||
|
||||
pitch_target_deg = fp32_constrain(pitch_deg, p_min, p_max);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:检查云台与发射机构电机是否掉线
|
||||
*参数说明:无
|
||||
*返回类型:1-有电机掉线,0-全部在线
|
||||
*备注:拨弹电机是否参与判定受GIMBAL_RAMMER_INSTALLED宏控制
|
||||
*/
|
||||
uint8_t gimbal_motor_offline(void)
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
uint8_t rammer_off = 0;
|
||||
|
||||
#if GIMBAL_RAMMER_INSTALLED
|
||||
rammer_off = gimbal_one_offline(&motor_rammer, now);
|
||||
#endif
|
||||
return (gimbal_one_offline(&motor_yaw_gimbal, now) ||
|
||||
gimbal_one_offline(&motor_pitch_gimbal, now) ||
|
||||
gimbal_one_offline(&motor_friction_l, now) ||
|
||||
gimbal_one_offline(&motor_friction_r, now) ||
|
||||
rammer_off);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:云台与发射机构四路CAN电流输出清零
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:失能或异常保护时调用
|
||||
*/
|
||||
void gimbal_output_zero(void)
|
||||
{
|
||||
dji_motor_send_gm6020_current_low(&hcan2, 0, 0, 0, 0);
|
||||
dji_motor_send_gm6020_current_low(&hcan1, 0, 0, 0, 0);
|
||||
dji_motor_send_current_low(&hcan1, 0, 0, 0, 0);
|
||||
dji_motor_send_current_low(&hcan2, 0, 0, 0, 0);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#ifndef GIMBAL_H
|
||||
#define GIMBAL_H
|
||||
|
||||
#include "dji_motor.h"
|
||||
|
||||
extern dji_motor_t motor_yaw_gimbal;//Yaw GM6020(CAN2 0x205)
|
||||
extern dji_motor_t motor_pitch_gimbal;//Pitch GM6020(CAN1 0x206)
|
||||
extern dji_motor_t motor_friction_l;//左摩擦轮M3508(CAN1 0x201)
|
||||
extern dji_motor_t motor_friction_r;//右摩擦轮M3508(CAN1 0x202)
|
||||
extern dji_motor_t motor_rammer;//拨弹盘M2006(CAN2 0x201)
|
||||
|
||||
extern float pitch_pid;//Pitch速度环PID输出(电流码,仅遥测)
|
||||
extern float pitch_g;//Pitch重力拟合前馈(电流码)
|
||||
extern float pitch_current;//Pitch最终电流指令(±16384)
|
||||
|
||||
void gimbal_motor_init(void);//初始化云台/发射电机实例与双轴PID
|
||||
uint8_t gimbal_detect_yaw_sign(void);//上电自检Yaw装配符号(1-成功,0-失败)
|
||||
float gimbal_yaw_sign(void);//获取Yaw装配符号(+1/-1)
|
||||
float gimbal_yaw_speed_out(void);//获取Yaw速度环PID输出(仅标定用)
|
||||
int8_t gimbal_encoder_ccw(void);//获取编码器增向(+1=CCW,-1=CW,0=未自检)
|
||||
void gimbal_control(float yaw_delta, float pitch_delta, uint8_t reset_flag);//云台双轴控制(2ms调用)
|
||||
void gimbal_set_target_deg(float yaw_deg, float pitch_deg);//直接设定绝对目标角(视觉自瞄用)
|
||||
uint8_t gimbal_motor_offline(void);//判定云台/发射任一电机是否掉线(1-掉线,0-正常)
|
||||
void gimbal_output_zero(void);//云台与发射机构输出清零
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,167 @@
|
||||
/*
|
||||
*文件名称:gimbal_task.c
|
||||
*功能描述:云台控制任务(2ms/500Hz),包含等待/运行/自检状态机与门控保护
|
||||
*备注:Yaw/Pitch GM6020采用0x1FE电流帧控制,拨弹M2006由shoot_task独立发0x200
|
||||
*/
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "robot_typedef.h"
|
||||
#include "gimbal.h"
|
||||
#include "bmi088.h"
|
||||
#include "attitude.h"
|
||||
#include "dr16.h"
|
||||
#include "c_comm.h"
|
||||
#include "vision.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任务节拍数
|
||||
|
||||
typedef enum
|
||||
{
|
||||
YAW_WAIT = 0,//等待姿态就绪与云台电机在线
|
||||
YAW_RUN, //正常串级控制
|
||||
YAW_DETECT, //装配符号自检(仅手势显式触发)
|
||||
} yaw_state_e;
|
||||
|
||||
/*
|
||||
*函数简介:检测操作手校准手势并做500ms防抖
|
||||
*参数说明:cnt:防抖计数器指针
|
||||
*返回类型:uint8_t:1=触发自检手势,0=未触发
|
||||
*备注:仅遥控与云台电机均在线时允许计数
|
||||
*/
|
||||
static uint8_t gimbal_check_gesture(uint16_t * cnt)
|
||||
{
|
||||
if (cnt == NULL)
|
||||
return 0U;
|
||||
|
||||
if (cmd_robot.online && !gimbal_motor_offline() &&
|
||||
dr16.rc_l_RL < CALIB_GESTURE_L_RL && dr16.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;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:执行云台运行态单步闭环控制(自瞄与手动控制统一入口)
|
||||
*参数说明:force_reset:强制重锁目标角标志指针
|
||||
*返回类型:无
|
||||
*备注:自瞄时先执行一次零增量控制处理复位同步与PID清理,再覆写视觉绝对目标角
|
||||
*/
|
||||
static void gimbal_run_step(uint8_t * force_reset)
|
||||
{
|
||||
float aim_yaw = 0.0f;
|
||||
float aim_pitch = 0.0f;
|
||||
const uint8_t reset = (uint8_t)((force_reset != NULL && *force_reset) || cmd_robot.gimbal_reset);
|
||||
|
||||
if (cmd_robot.aim && vision_get_target(&aim_yaw, &aim_pitch))
|
||||
{
|
||||
gimbal_control(0.0f, 0.0f, reset);//先跑控制处理复位同步/PID清理
|
||||
gimbal_set_target_deg(aim_yaw, aim_pitch);//覆写视觉绝对目标角
|
||||
}
|
||||
else
|
||||
{
|
||||
gimbal_control(cmd_robot.gimbal_yaw_delta, cmd_robot.gimbal_pitch_delta, reset);
|
||||
}
|
||||
|
||||
if (force_reset != NULL)
|
||||
*force_reset = 0U;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:云台控制任务(2ms/500Hz)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:任意门控失效(遥控离线/电机掉线/姿态不可用/底盘故障)立即清零输出
|
||||
*/
|
||||
void gimbal_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
TickType_t last_wake = xTaskGetTickCount();//绝对延时基准
|
||||
yaw_state_e state = YAW_WAIT;//当前状态机状态
|
||||
uint8_t detect_retry = 0U;//自检重试次数
|
||||
uint16_t gesture_cnt = 0U;//手势防抖计数
|
||||
uint8_t force_reset = 0U;//首次进入或恢复后强制重锁目标标志
|
||||
|
||||
while (!app_ready)
|
||||
osDelay(2);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
vision_update();//视觉姿态反馈(10ms周期,与云台状态无关)
|
||||
|
||||
const uint8_t gates_ok = (uint8_t)(cmd_robot.online && !gimbal_motor_offline() &&
|
||||
bmi088_online && att_ready && !ctoc_chassis_fault());//运行门控总标志
|
||||
const uint8_t gesture = gimbal_check_gesture(&gesture_cnt);//自检手势触发标志
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case YAW_WAIT:
|
||||
gimbal_output_zero();
|
||||
if (!gimbal_motor_offline() && bmi088_online && att_ready)
|
||||
{
|
||||
force_reset = 1U;//首次控制同步目标角
|
||||
state = YAW_RUN;
|
||||
}
|
||||
break;
|
||||
|
||||
case YAW_DETECT:
|
||||
if (gimbal_detect_yaw_sign())
|
||||
{
|
||||
force_reset = 1U;//检测期间云台被动转过,重锁目标
|
||||
state = YAW_RUN;
|
||||
}
|
||||
else if (++detect_retry >= 3U)
|
||||
{
|
||||
detect_retry = 0U;
|
||||
force_reset = 1U;
|
||||
state = YAW_RUN;//失败3次回退参数符号
|
||||
}
|
||||
else
|
||||
{
|
||||
gimbal_output_zero();
|
||||
}
|
||||
break;
|
||||
|
||||
case YAW_RUN:
|
||||
default:
|
||||
if (gesture && !gimbal_motor_offline() && att_ready)
|
||||
{
|
||||
detect_retry = 0U;
|
||||
state = YAW_DETECT;//操作手主动请求自检
|
||||
}
|
||||
else if (gates_ok)
|
||||
{
|
||||
gimbal_run_step(&force_reset);
|
||||
}
|
||||
else
|
||||
{
|
||||
gimbal_output_zero();
|
||||
force_reset = 1U;
|
||||
state = YAW_WAIT;//门控失效回等待,恢复后重锁目标
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
monitor_heartbeat(HB_GIMBAL);
|
||||
vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(2));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
/*
|
||||
*文件名称:referee.c
|
||||
*功能描述:裁判系统串口协议解析模块(0x0201机器人状态/0x0202实时功率/0x0304图传键鼠)
|
||||
*备注:采用CRC8(0x31,0xFF)与CRC16(0x1021,0xFFFF)位运算校验,解析在中断上下文完成
|
||||
*/
|
||||
#include "referee.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;//整帧缓冲写入索引
|
||||
|
||||
/*
|
||||
*函数简介:根据命令码分发并解析校验通过的裁判系统帧
|
||||
*参数说明: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++);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
#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);
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
*文件名称:referee_task.c
|
||||
*功能描述:裁判系统链路监测任务(100ms)
|
||||
*备注:数据解析在USART6空闲中断内由referee_parse_bytes完成,本任务仅做超时监测与心跳上报
|
||||
*/
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "bsp_usart.h"
|
||||
#include "referee.h"
|
||||
#include "robot_param.h"
|
||||
|
||||
/*
|
||||
*函数简介: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);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:裁判系统链路超时监测任务(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;//链路超时置断线
|
||||
}
|
||||
|
||||
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,105 @@
|
||||
/*
|
||||
*文件名称:robot_param.h
|
||||
*功能描述:云台板全局参数配置头文件(整车唯一调参入口)
|
||||
*备注:控制周期2ms,修改周期时需同步重调各环PID参数
|
||||
*/
|
||||
#ifndef ROBOT_PARAM_GIMBAL_H
|
||||
#define ROBOT_PARAM_GIMBAL_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 PITCH_GM6020_POSITION_UPPER_LIMIT 5180 //Pitch轴抬起极限(编码器较小值)
|
||||
#define PITCH_GM6020_POSITION_LOWER_LIMIT 6500 //Pitch轴压低极限(编码器较大值)
|
||||
#define PITCH_GM6020_ENCODER_ZERO 6000 //Pitch轴编码器水平零位(实测中值)
|
||||
|
||||
#define GIMBAL_FRICTION_WHEEL_SPEED 5500.0f //摩擦轮目标转速(RPM)
|
||||
#define GIMBAL_RAMMER_SPEED -5400.0f//拨弹盘转速(RPM,负向发射)
|
||||
#define GIMBAL_RAMMER_EJECT_SPEED 1500.0f //退弹转子转速(RPM,方向与发射相反)
|
||||
#define GIMBAL_RAMMER_EJECT_MAX_ROTOR_DEG 3600.0f //单次退弹行程上限(转子角,约10圈)
|
||||
|
||||
/* 台架调试工况:拨弹盘单机(1=台架调试模式,0=正常整车模式) */
|
||||
#define GIMBAL_BENCH_RAMMER_TUNE 0 //比赛/整车必须为0
|
||||
#define GIMBAL_BENCH_RAMMER_DIR (-1.0f) //台架拨弹方向修正(+1=同向,-1=反向)
|
||||
#define GIMBAL_BENCH_RAMMER_GEAR 36.0f //M2006减速比(转子:拨弹盘)
|
||||
#define GIMBAL_BENCH_EJECT_MAX_DEG 90.0f //单次退弹允许最大拨弹盘转角(°)
|
||||
#define GIMBAL_BENCH_EJECT_MAX_MS 1500 //单次退弹最长时间(ms)
|
||||
#define GIMBAL_BENCH_EJECT_CUR_MAX_A 3.0f //退弹电流软上限(A)
|
||||
#define GIMBAL_BENCH_EJECT_CUR_HARD_A 5.0f //退弹电流硬上限(A)
|
||||
#define GIMBAL_BENCH_EJECT_OVER_MS 40 //电流连续超限判定过流时间(ms)
|
||||
|
||||
/* 遥控与键鼠映射比例 */
|
||||
#define GIMBAL_LEVER_SPEED_MAP_RATE 2.0f //云台拨杆速度映射比例(Yaw用)
|
||||
#define GIMBAL_PITCH_LEVER_RATE 5.0f //Pitch专用比例
|
||||
#define GIMBAL_YAW_PITCH_SPEED_RATE 3.0f //云台偏航/俯仰速度档倍率
|
||||
#define PC_MOUSE_RL_SENSITIVITY 3.0f //鼠标左右灵敏度
|
||||
#define PC_MOUSE_DU_SENSITIVITY 8.0f //鼠标上下灵敏度
|
||||
|
||||
/* Yaw双环(2ms,位置环与速度环均以陀螺仪为反馈) */
|
||||
#define GIMBAL_YAW_ANGLE_KP 20.0f //Yaw角度环Kp(°->°/s)
|
||||
#define GIMBAL_YAW_ANGLE_KI 0.0f //Yaw角度环Ki
|
||||
#define GIMBAL_YAW_ANGLE_KD 0.0f //Yaw角度环Kd
|
||||
#define GIMBAL_YAW_ANGLE_EK_DEADBAND 0.05f //Yaw角度误差死区(°)
|
||||
#define GIMBAL_YAW_ANGLE_OUT_LIMIT 360.0f //Yaw目标角速度上限(°/s)
|
||||
|
||||
#define GIMBAL_YAW_SPEED_KP 85.0f //Yaw速度环Kp(°/s->电流)
|
||||
#define GIMBAL_YAW_SPEED_KI 0.0f //Yaw速度环Ki
|
||||
#define GIMBAL_YAW_SPEED_KD 0.0f //Yaw速度环Kd
|
||||
#define GIMBAL_YAW_SPEED_OUT_LIMIT 16384.0f//GM6020电流限幅(±16384对应±3A)
|
||||
#define GIMBAL_YAW_SPIN_FEEDFORWARD_K 24.0f //小陀螺底盘自旋解耦前馈系数(RPM->电流)
|
||||
#define GIMBAL_YAW_FF_GAIN 1.0f //目标角速度前馈增益(0~1)
|
||||
#define GIMBAL_YAW_FF_ALPHA 0.05f //前馈一阶低通滤波系数(2ms周期)
|
||||
|
||||
/* Yaw装配符号自动检测参数 */
|
||||
#define GIMBAL_YAW_SIGN_AUTO 1 //1=允许手势触发自检,0=直接用FALLBACK
|
||||
#define GIMBAL_YAW_SIGN_FALLBACK (1.0f) //Yaw装配符号回退值
|
||||
#define GIMBAL_YAW_DETECT_CURRENT 3500 //自检脉冲电流(counts)
|
||||
#define GIMBAL_YAW_DETECT_RATE_MIN 20.0f //判定转动的陀螺速率阈值(°/s)
|
||||
|
||||
/* Pitch双环(位置环用IMU Roll,速度环用转子转速) */
|
||||
#define GIMBAL_PITCH_ANGLE_EK_DEADBAND 0.05f //Pitch角度误差死区(°)
|
||||
#define GIMBAL_PITCH_ANGLE_KP 15.0f //Pitch角度环Kp
|
||||
#define GIMBAL_PITCH_ANGLE_KI 0.0f //Pitch角度环Ki
|
||||
#define GIMBAL_PITCH_ANGLE_KD 0.0f //Pitch角度环Kd
|
||||
#define GIMBAL_PITCH_SPEED_KP 40.0f //Pitch速度环Kp
|
||||
#define GIMBAL_PITCH_SPEED_KI 0.0f //Pitch速度环Ki
|
||||
#define GIMBAL_PITCH_SPEED_KD 0.0f //Pitch速度环Kd
|
||||
|
||||
/* Pitch重力拟合前馈参数:I_g = K_COS*cos(a) + K_SIN*sin(a) + K_CONST */
|
||||
#define GIMBAL_PITCH_GRAVITY_ZERO_ECD 6000.0f //重力补偿拟合基准编码器值
|
||||
#define GIMBAL_PITCH_GRAVITY_K_COS 0.0f //cos项系数
|
||||
#define GIMBAL_PITCH_GRAVITY_K_SIN 0.0f //sin项系数
|
||||
#define GIMBAL_PITCH_GRAVITY_K_CONST 0.0f //常数偏置项
|
||||
|
||||
/* 摩擦轮与拨弹盘速度环PID */
|
||||
#define GIMBAL_FRICTION_WHEEL_KP 16.0f //摩擦轮速度环Kp
|
||||
#define GIMBAL_FRICTION_WHEEL_KI 0.0f //摩擦轮速度环Ki
|
||||
#define GIMBAL_FRICTION_WHEEL_KD 30.0f //摩擦轮速度环Kd
|
||||
#define GIMBAL_RAMMER_KP 16.0f //拨弹盘速度环Kp
|
||||
#define GIMBAL_RAMMER_KI 0.0f //拨弹盘速度环Ki
|
||||
#define GIMBAL_RAMMER_KD 0.0f //拨弹盘速度环Kd
|
||||
|
||||
/* ============================ 台架/比赛开关 ============================ */
|
||||
#define DEBUG_SHOOT_WITHOUT_REFEREE 1 //1=无裁判系统时允许开摩擦轮,0=比赛模式
|
||||
#define GIMBAL_RAMMER_INSTALLED 1 //1=拨弹M2006已安装,0=暂缺不报掉线
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,42 @@
|
||||
#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 aim; //自瞄请求(键鼠右键或遥控S1下侧)
|
||||
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,334 @@
|
||||
#include "shoot.h"
|
||||
#include "gimbal.h"
|
||||
#include "pid.h"
|
||||
#include "tim.h"
|
||||
#include "robot_param.h"
|
||||
#include "dr16.h"
|
||||
#include "main.h"
|
||||
#include <math.h>
|
||||
|
||||
extern TIM_HandleTypeDef htim3;//TIM3句柄(CH3=PC8激光PWM)
|
||||
|
||||
uint8_t shoot_friction_flag = 0;//摩擦轮当前开关状态
|
||||
|
||||
static PID_PositionTypeDef friction_l_pid, friction_r_pid, rammer_pid;//左/右摩擦轮与拨弹PID
|
||||
|
||||
static uint8_t rammer_eject_active = 0;//退弹行程激活标志
|
||||
static uint8_t rammer_eject_latched = 0;//退弹余量用尽锁存标志
|
||||
static float rammer_eject_start_deg = 0.0f;//本次退弹起点转子角(°)
|
||||
static float rammer_eject_base_deg = 0.0f;//本次退弹前已累计转角(°)
|
||||
static float rammer_eject_accum_deg = 0.0f;//自上次正转以来的累计退弹角(°)
|
||||
|
||||
#define LASER_ON() __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 8399)//开启激光(100%占空比)
|
||||
#define LASER_OFF() __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_3, 0)//关闭激光
|
||||
|
||||
/*
|
||||
*函数简介:初始化激光PWM输出(默认关闭)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:PC8=TIM3_CH3(10kHz PWM,ARR=8399)
|
||||
*/
|
||||
static void laser_init(void)
|
||||
{
|
||||
LASER_OFF();
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_3);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化单路摩擦轮速度环PID
|
||||
*参数说明:摩擦轮PID结构体指针
|
||||
*返回类型:无
|
||||
*备注:死区±5,输出限幅±15000
|
||||
*/
|
||||
static void shoot_fric_pid_init(PID_PositionTypeDef * pid)
|
||||
{
|
||||
PID_PositionStructureInit(pid, 0);
|
||||
PID_PositionSetParameter(
|
||||
pid, GIMBAL_FRICTION_WHEEL_KP, GIMBAL_FRICTION_WHEEL_KI, GIMBAL_FRICTION_WHEEL_KD);
|
||||
PID_PositionSetEkRange(pid, -5.0f, 5.0f);
|
||||
PID_PositionSetOUTRange(pid, -15000.0f, 15000.0f);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化发射机构PID与激光PWM
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:必须在can_start()之后调用一次
|
||||
*/
|
||||
void shoot_init(void)
|
||||
{
|
||||
shoot_fric_pid_init(&friction_l_pid);
|
||||
shoot_fric_pid_init(&friction_r_pid);
|
||||
|
||||
PID_PositionStructureInit(&rammer_pid, GIMBAL_RAMMER_SPEED);
|
||||
PID_PositionSetParameter(&rammer_pid, GIMBAL_RAMMER_KP, GIMBAL_RAMMER_KI, GIMBAL_RAMMER_KD);
|
||||
PID_PositionSetEkRange(&rammer_pid, -20.0f, 20.0f);
|
||||
PID_PositionSetOUTRange(&rammer_pid, -10000.0f, 10000.0f);
|
||||
|
||||
laser_init();
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:发射机构控制主函数
|
||||
*参数说明:摩擦轮开关(1-开,0-关)
|
||||
*参数说明:发射请求(1-正转发射)
|
||||
*参数说明:退弹请求(1-反转退弹)
|
||||
*参数说明:裁判系统发射机构上电状态(1-上电,0-断电)
|
||||
*返回类型:无
|
||||
*备注:固定2ms调用一次,只计算PID不发CAN(发送由shoot_send_cmd负责)
|
||||
*/
|
||||
void shoot_control(uint8_t friction_on, uint8_t fire, uint8_t eject, uint8_t shooter_power_on)
|
||||
{
|
||||
if (friction_on && (shooter_power_on || DEBUG_SHOOT_WITHOUT_REFEREE))
|
||||
{
|
||||
friction_l_pid.Need_Value = -GIMBAL_FRICTION_WHEEL_SPEED;
|
||||
friction_r_pid.Need_Value = GIMBAL_FRICTION_WHEEL_SPEED;
|
||||
LASER_ON();
|
||||
shoot_friction_flag = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
friction_l_pid.Need_Value = 0.0f;
|
||||
friction_r_pid.Need_Value = 0.0f;
|
||||
LASER_OFF();
|
||||
shoot_friction_flag = 0;
|
||||
}
|
||||
|
||||
if (shoot_friction_flag)
|
||||
{
|
||||
if (fire)
|
||||
{
|
||||
rammer_eject_active = 0;
|
||||
rammer_eject_latched = 0;
|
||||
rammer_eject_accum_deg = 0.0f;
|
||||
rammer_pid.Need_Value = -GIMBAL_RAMMER_SPEED;
|
||||
}
|
||||
else if (eject)
|
||||
{
|
||||
if (!rammer_eject_active)
|
||||
{
|
||||
rammer_eject_active = 1;
|
||||
rammer_eject_start_deg = motor_rammer.fdb.rotor_angle_deg;
|
||||
rammer_eject_base_deg = rammer_eject_accum_deg;
|
||||
}
|
||||
rammer_eject_accum_deg = rammer_eject_base_deg +
|
||||
fabsf(motor_rammer.fdb.rotor_angle_deg - rammer_eject_start_deg);
|
||||
|
||||
if (rammer_eject_accum_deg >= GIMBAL_RAMMER_EJECT_MAX_ROTOR_DEG)
|
||||
rammer_eject_latched = 1;
|
||||
|
||||
rammer_pid.Need_Value = rammer_eject_latched ? 0.0f : -GIMBAL_RAMMER_EJECT_SPEED;
|
||||
}
|
||||
else
|
||||
{
|
||||
rammer_eject_active = 0;
|
||||
rammer_pid.Need_Value = 0.0f;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
rammer_eject_active = 0;
|
||||
rammer_pid.Need_Value = 0.0f;
|
||||
}
|
||||
|
||||
PID_PositionCalc(&friction_l_pid, motor_friction_l.fdb.rotor_speed);
|
||||
PID_PositionCalc(&friction_r_pid, motor_friction_r.fdb.rotor_speed);
|
||||
PID_PositionCalc(&rammer_pid, motor_rammer.fdb.rotor_speed);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:下发摩擦轮与拨弹盘CAN电流指令
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:摩擦轮经CAN1 0x200槽1/2发送,拨弹经CAN2 0x200槽1发送
|
||||
*/
|
||||
void shoot_send_cmd(void)
|
||||
{
|
||||
dji_motor_send_current_low(&hcan1, (int16_t)friction_l_pid.OUT, (int16_t)friction_r_pid.OUT, 0, 0);
|
||||
dji_motor_send_current_low(&hcan2, (int16_t)rammer_pid.OUT, 0, 0, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:发射机构安全停机
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:摩擦轮/拨弹电流清零,关闭激光并清状态标志
|
||||
*/
|
||||
void shoot_output_zero(void)
|
||||
{
|
||||
dji_motor_send_current_low(&hcan1, 0, 0, 0, 0);
|
||||
dji_motor_send_current_low(&hcan2, 0, 0, 0, 0);
|
||||
LASER_OFF();
|
||||
shoot_friction_flag = 0;
|
||||
rammer_eject_active = 0;
|
||||
}
|
||||
|
||||
typedef enum
|
||||
{
|
||||
BENCH_IDLE = 0,//待机/正转发射
|
||||
BENCH_EJECT = 1,//退弹中
|
||||
BENCH_LATCH = 2//退弹被保护中止或余量用尽锁存
|
||||
} bench_state_e;//拨弹单机台架状态枚举
|
||||
|
||||
static bench_state_e bench_state = BENCH_IDLE;//台架当前状态
|
||||
static float bench_eject_start_rotor_deg = 0.0f;//本次退弹起点转子角(°)
|
||||
static float bench_eject_accum_start = 0.0f;//本次退弹开始前累计转角(°)
|
||||
static float bench_eject_accum_deg = 0.0f;//自上次正转以来的累计退弹角(°)
|
||||
static uint32_t bench_eject_start_tick = 0;//本次退弹开始时间戳
|
||||
static uint32_t bench_over_cur_ms = 0;//过流软限累计计时(ms)
|
||||
static int16_t bench_rammer_out_raw = 0;//台架下发电流编码值
|
||||
|
||||
volatile float bench_rammer_out_cur = 0.0f;//台架拨弹PID输出(counts)
|
||||
volatile float bench_eject_travel_deg = 0.0f;//累计退弹转角(°)
|
||||
volatile uint8_t bench_state_dbg = 0;//台架状态诊断量
|
||||
volatile uint8_t bench_eject_abort = 0;//退弹中止原因(0=无,1=软限,2=超时,3=硬限)
|
||||
|
||||
/*
|
||||
*函数简介:拨弹盘单机台架状态机(2ms调用)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:仅GIMBAL_BENCH_RAMMER_TUNE=1时由shoot_task调用,含转角上限/电流软硬限/超时保护
|
||||
*/
|
||||
void shoot_bench_step(void)
|
||||
{
|
||||
uint8_t gate = (dr16.s1 == 1);//S1上为使能
|
||||
uint16_t wheel = dr16.thumb_wheel;//拨轮向下发射/向上退弹
|
||||
float cur_a = motor_rammer.fdb.torque_current;//实测电流(A)
|
||||
float cmd = 0.0f;
|
||||
|
||||
if (dr16_status == 0 || dr16_offline())
|
||||
{
|
||||
bench_state = BENCH_IDLE;
|
||||
bench_eject_accum_deg = 0.0f;
|
||||
bench_rammer_out_raw = 0;
|
||||
bench_rammer_out_cur = 0.0f;
|
||||
bench_eject_travel_deg = 0.0f;
|
||||
bench_eject_abort = 0;
|
||||
bench_state_dbg = (uint8_t)bench_state;
|
||||
return;
|
||||
}
|
||||
|
||||
switch (bench_state)
|
||||
{
|
||||
case BENCH_IDLE:
|
||||
if (!gate)
|
||||
{
|
||||
cmd = 0.0f;
|
||||
break;
|
||||
}
|
||||
|
||||
if (wheel < 1000)
|
||||
{
|
||||
cmd = GIMBAL_RAMMER_SPEED;
|
||||
bench_eject_accum_deg = 0.0f;//正转恢复退弹余量
|
||||
bench_eject_travel_deg = 0.0f;
|
||||
bench_eject_abort = 0;
|
||||
}
|
||||
else if (wheel > 1050)
|
||||
{
|
||||
if (bench_eject_accum_deg >= GIMBAL_BENCH_EJECT_MAX_DEG)
|
||||
{
|
||||
bench_state = BENCH_LATCH;
|
||||
cmd = 0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
bench_state = BENCH_EJECT;
|
||||
bench_eject_start_rotor_deg = motor_rammer.fdb.rotor_angle_deg;
|
||||
bench_eject_accum_start = bench_eject_accum_deg;
|
||||
bench_eject_start_tick = HAL_GetTick();
|
||||
bench_over_cur_ms = 0;
|
||||
bench_eject_abort = 0;
|
||||
cmd = -GIMBAL_RAMMER_SPEED;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case BENCH_EJECT:
|
||||
{
|
||||
uint32_t now = HAL_GetTick();
|
||||
float travel;
|
||||
|
||||
if (!gate || wheel <= 1050)
|
||||
{
|
||||
bench_state = BENCH_IDLE;
|
||||
cmd = 0.0f;
|
||||
break;
|
||||
}
|
||||
|
||||
travel = fabsf(motor_rammer.fdb.rotor_angle_deg -
|
||||
bench_eject_start_rotor_deg) / GIMBAL_BENCH_RAMMER_GEAR;
|
||||
bench_eject_accum_deg = bench_eject_accum_start + travel;
|
||||
bench_eject_travel_deg = bench_eject_accum_deg;
|
||||
|
||||
if (fabsf(cur_a) > GIMBAL_BENCH_EJECT_CUR_HARD_A)
|
||||
bench_eject_abort = 3;
|
||||
else if (fabsf(cur_a) > GIMBAL_BENCH_EJECT_CUR_MAX_A)
|
||||
{
|
||||
if (bench_over_cur_ms < 1000u)
|
||||
bench_over_cur_ms += 2u;
|
||||
if (bench_over_cur_ms >= GIMBAL_BENCH_EJECT_OVER_MS)
|
||||
bench_eject_abort = 1;
|
||||
}
|
||||
else
|
||||
bench_over_cur_ms = 0;
|
||||
|
||||
if (bench_eject_abort != 0)
|
||||
{
|
||||
bench_state = BENCH_LATCH;
|
||||
cmd = 0.0f;
|
||||
}
|
||||
else if (bench_eject_accum_deg >= GIMBAL_BENCH_EJECT_MAX_DEG)
|
||||
{
|
||||
bench_state = BENCH_LATCH;
|
||||
cmd = 0.0f;
|
||||
}
|
||||
else if ((now - bench_eject_start_tick) >= GIMBAL_BENCH_EJECT_MAX_MS)
|
||||
{
|
||||
bench_eject_abort = 2;
|
||||
bench_state = BENCH_LATCH;
|
||||
cmd = 0.0f;
|
||||
}
|
||||
else
|
||||
cmd = -GIMBAL_RAMMER_SPEED;
|
||||
break;
|
||||
}
|
||||
|
||||
case BENCH_LATCH:
|
||||
cmd = 0.0f;
|
||||
if (wheel <= 1050)
|
||||
bench_state = BENCH_IDLE;
|
||||
break;
|
||||
|
||||
default:
|
||||
bench_state = BENCH_IDLE;
|
||||
break;
|
||||
}
|
||||
|
||||
if (bench_state == BENCH_LATCH)
|
||||
{
|
||||
rammer_pid.Need_Value = 0.0f;
|
||||
bench_rammer_out_raw = 0;
|
||||
bench_rammer_out_cur = 0.0f;
|
||||
}
|
||||
else
|
||||
{
|
||||
rammer_pid.Need_Value = cmd;
|
||||
PID_PositionCalc(&rammer_pid, GIMBAL_BENCH_RAMMER_DIR * motor_rammer.fdb.rotor_speed);
|
||||
bench_rammer_out_raw = (int16_t)(GIMBAL_BENCH_RAMMER_DIR * rammer_pid.OUT);
|
||||
bench_rammer_out_cur = (float)bench_rammer_out_raw;
|
||||
}
|
||||
|
||||
bench_state_dbg = (uint8_t)bench_state;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:下发台架拨弹盘CAN电流指令
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:固定走CAN2 0x200槽1
|
||||
*/
|
||||
void shoot_bench_send(void)
|
||||
{
|
||||
dji_motor_send_current_low(&hcan2, bench_rammer_out_raw, 0, 0, 0);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef SHOOT_H
|
||||
#define SHOOT_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
extern uint8_t shoot_friction_flag;//摩擦轮开启标志(供激光联动与调试)
|
||||
|
||||
void shoot_init(void);//初始化摩擦轮与拨弹盘速度环PID及激光PWM
|
||||
void shoot_control(uint8_t friction_on, uint8_t fire, uint8_t eject, uint8_t shooter_power_on);//发射机构控制(2ms调用)
|
||||
void shoot_send_cmd(void);//下发摩擦轮(CAN1 0x200)与拨弹(CAN2 0x200)电流帧
|
||||
void shoot_output_zero(void);//发射机构安全停机(电流清零并关闭激光)
|
||||
|
||||
void shoot_bench_step(void);//拨弹单机台架状态机(2ms调用)
|
||||
void shoot_bench_send(void);//拨弹单机台架CAN2 0x200槽1下发
|
||||
|
||||
extern volatile float bench_rammer_out_cur;//台架拨弹PID输出(counts)
|
||||
extern volatile float bench_eject_travel_deg;//台架本次退弹累计转角(°)
|
||||
extern volatile uint8_t bench_state_dbg;//台架状态(0=IDLE,1=EJECT,2=LATCH)
|
||||
extern volatile uint8_t bench_eject_abort;//台架退弹中止原因(0=无,1=软限,2=超时,3=硬限)
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
*文件名称:shoot_task.c
|
||||
*功能描述:发射机构控制任务(2ms),计算摩擦轮与拨弹电流并发送0x200控制帧
|
||||
*备注:发射机构上电状态来自底盘0x019(ctoc_shooter_power)
|
||||
*/
|
||||
#include "app_tasks.h"
|
||||
#include "cmsis_os.h"
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "robot_typedef.h"
|
||||
#include "gimbal.h"
|
||||
#include "shoot.h"
|
||||
#include "c_comm.h"
|
||||
#include "dr16.h"
|
||||
#include "robot_param.h"//GIMBAL_BENCH_RAMMER_TUNE等开关定义
|
||||
|
||||
/*
|
||||
*函数简介:发射机构控制任务(2ms)
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:支持台架单机拨弹调试模式与整车门控模式
|
||||
*/
|
||||
void shoot_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
TickType_t last_wake = xTaskGetTickCount();//绝对延时基准
|
||||
|
||||
while (!app_ready)
|
||||
osDelay(2);
|
||||
|
||||
for (;;)
|
||||
{
|
||||
#if GIMBAL_BENCH_RAMMER_TUNE
|
||||
/* 台架调试工况:仅M2006+DT7单机拨弹,旁路整车门控 */
|
||||
shoot_bench_step();
|
||||
shoot_bench_send();
|
||||
#else
|
||||
if (cmd_robot.online && !gimbal_motor_offline() && !ctoc_chassis_fault())
|
||||
{
|
||||
shoot_control(cmd_robot.friction_on, cmd_robot.fire, cmd_robot.eject,
|
||||
ctoc_shooter_power);
|
||||
shoot_send_cmd();
|
||||
}
|
||||
else
|
||||
{
|
||||
shoot_output_zero();
|
||||
}
|
||||
#endif
|
||||
|
||||
monitor_heartbeat(HB_SHOOT);
|
||||
vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(2));
|
||||
}
|
||||
}
|
||||
@@ -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,62 @@
|
||||
#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"
|
||||
#include "vision.h"
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
#include "chassis.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)
|
||||
vision_init();//视觉上位机(USART1 DMA)
|
||||
#elif defined(ROBOT_TYPE_CHASSIS)
|
||||
chassis_motor_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,197 @@
|
||||
/*
|
||||
*文件名称:debug_task.c
|
||||
*功能描述:USB虚拟串口(CDC)姿态与控制量遥测输出任务(云台板)
|
||||
*备注:支持欧拉角/健康位/诊断位/控制量CSV或文本格式,以及编码/前馈/台架调试模式
|
||||
*/
|
||||
#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 "gimbal.h"
|
||||
#include "dr16.h"
|
||||
#include "robot_typedef.h"
|
||||
#include "shoot.h"
|
||||
#include "robot_param.h"
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <string.h>
|
||||
|
||||
#define DEBUG_ATT_HEALTH 1 //1=输出健康位(cvg/rdy/imu/an/gn),0=仅欧拉角
|
||||
#define DEBUG_ATT_DIAG 1 //1=附加原始陀螺/零偏/采样计数,0=关闭
|
||||
#define DEBUG_PITCH_ONLY 0 //1=仅输出Pitch调参行,0=输出完整姿态行
|
||||
#define DEBUG_CMD_OUT 1 //1=追加控制量字段,0=不追加
|
||||
#define DEBUG_ENC_CUR 0 //1=仅输出Pitch编码/电流与Yaw编码(互斥)
|
||||
#define DEBUG_YAW_FF_CAL 0 //1=标定Yaw小陀螺前馈K模式(互斥)
|
||||
#define DEBUG_PITCH_GRAV_CAL 0 //1=标定Pitch重力前馈模式(互斥)
|
||||
#define DEBUG_OUT_PERIOD_MS 100//遥测输出周期(ms)
|
||||
|
||||
/*
|
||||
*函数简介:通过USB CDC安全发送snprintf格式化后的字符串
|
||||
*参数说明:buf:待发送字符串缓冲区
|
||||
*参数说明:len:snprintf返回值
|
||||
*参数说明:cap:缓冲区总字节容量
|
||||
*返回类型:无
|
||||
*备注:对截断情况进行长度限幅,防止发送越界
|
||||
*/
|
||||
static void debug_send_line(const char * buf, int len, size_t cap)
|
||||
{
|
||||
if (buf == NULL || len <= 0 || cap == 0U)
|
||||
return;
|
||||
|
||||
uint16_t tx_len = (len < (int)cap) ? (uint16_t)len : (uint16_t)(cap - 1U);
|
||||
CDC_Transmit_FS((uint8_t *)buf, tx_len);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:USB虚拟串口遥测输出任务
|
||||
*参数说明:argument:FreeRTOS任务参数(未使用)
|
||||
*返回类型:无
|
||||
*备注:无
|
||||
*/
|
||||
void debug_task(void const * argument)
|
||||
{
|
||||
(void)argument;
|
||||
|
||||
char line[384];
|
||||
char tail[112];
|
||||
|
||||
for (;;)
|
||||
{
|
||||
#if DEBUG_ENC_CUR
|
||||
{
|
||||
int len = snprintf(line, sizeof(line), "%u,%.1f,%u\n",
|
||||
(unsigned)motor_pitch_gimbal.fdb.raw_angle,
|
||||
pitch_current,
|
||||
(unsigned)motor_yaw_gimbal.fdb.raw_angle);
|
||||
debug_send_line(line, len, sizeof(line));
|
||||
}
|
||||
osDelay(10);
|
||||
continue;
|
||||
#endif
|
||||
|
||||
#if DEBUG_PITCH_GRAV_CAL
|
||||
{
|
||||
int len = snprintf(line, sizeof(line), "%u,%.1f\n",
|
||||
(unsigned)motor_pitch_gimbal.fdb.raw_angle,
|
||||
pitch_pid);
|
||||
debug_send_line(line, len, sizeof(line));
|
||||
}
|
||||
osDelay(5);
|
||||
continue;
|
||||
#endif
|
||||
|
||||
#if DEBUG_YAW_FF_CAL
|
||||
{
|
||||
int len = snprintf(line, sizeof(line), "%.1f,%d\n",
|
||||
gimbal_yaw_speed_out(),
|
||||
(int)motor_yaw_gimbal.fdb.rotor_speed);
|
||||
debug_send_line(line, len, sizeof(line));
|
||||
}
|
||||
osDelay(5);
|
||||
continue;
|
||||
#endif
|
||||
|
||||
#if GIMBAL_BENCH_RAMMER_TUNE
|
||||
{
|
||||
int len = snprintf(line, sizeof(line), "%.3f,%.1f,%.1f,%.2f\n",
|
||||
motor_rammer.fdb.torque_current,
|
||||
(float)motor_rammer.fdb.rotor_speed,
|
||||
bench_rammer_out_cur,
|
||||
bench_eject_travel_deg);
|
||||
debug_send_line(line, len, sizeof(line));
|
||||
}
|
||||
osDelay(5);
|
||||
continue;
|
||||
#endif
|
||||
|
||||
#if DEBUG_PITCH_ONLY
|
||||
int len = snprintf(line, sizeof(line),
|
||||
"p_ecd=%u roll=%.2f p_out=%.1f rc_pitch=%u\r\n",
|
||||
(unsigned)motor_pitch_gimbal.fdb.raw_angle,
|
||||
att_deg_roll,
|
||||
pitch_current,
|
||||
(unsigned)dr16.rc_l_UD);
|
||||
#else
|
||||
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_ATT_HEALTH
|
||||
#if DEBUG_CMD_OUT
|
||||
char ctl[160];
|
||||
snprintf(ctl, sizeof(ctl),
|
||||
" online=%u src=%u spin=%u yaw_d=%.4f pitch_d=%.4f fric=%u fire=%u"
|
||||
" eject=%u reset=%u s2=%u ctrl=%u wheel=%d",
|
||||
(unsigned)cmd_robot.online, (unsigned)cmd_robot.source,
|
||||
(unsigned)cmd_robot.spin_flag,
|
||||
cmd_robot.gimbal_yaw_delta, cmd_robot.gimbal_pitch_delta,
|
||||
(unsigned)cmd_robot.friction_on, (unsigned)cmd_robot.fire,
|
||||
(unsigned)cmd_robot.eject, (unsigned)cmd_robot.gimbal_reset,
|
||||
(unsigned)dr16.s2, (unsigned)dr16.key_push_ctrl,
|
||||
(int)cmd_robot.thumb_wheel);
|
||||
#else
|
||||
const char * ctl = "";
|
||||
#endif
|
||||
int len = snprintf(line, sizeof(line),
|
||||
"ecd=%u yaw=%.2f pitch=%.2f roll=%.2f cvg=%u rdy=%u imu=%u an=%.2f gn=%.3f%s%s\r\n",
|
||||
(unsigned)motor_yaw_gimbal.fdb.raw_angle,
|
||||
att_deg_yaw, att_deg_pitch, att_deg_roll,
|
||||
(unsigned)INS.ConvergeFlag, (unsigned)att_ready,
|
||||
(unsigned)bmi088_online, an, gn, tail, ctl);
|
||||
#else
|
||||
#if DEBUG_CMD_OUT
|
||||
int len = snprintf(line, sizeof(line),
|
||||
"%u,%.1f,%u,%u,%u,%u,%.4f,%.4f,%u,%u,%u,%u,%u,%u,%d,%u,%d,%d,%d,%u,%u,%u,%u,%lu\r\n",
|
||||
(unsigned)motor_pitch_gimbal.fdb.raw_angle,
|
||||
pitch_current,
|
||||
(unsigned)motor_yaw_gimbal.fdb.raw_angle,
|
||||
(unsigned)cmd_robot.online,
|
||||
(unsigned)cmd_robot.source,
|
||||
(unsigned)cmd_robot.spin_flag,
|
||||
cmd_robot.gimbal_yaw_delta,
|
||||
cmd_robot.gimbal_pitch_delta,
|
||||
(unsigned)cmd_robot.friction_on,
|
||||
(unsigned)cmd_robot.fire,
|
||||
(unsigned)cmd_robot.eject,
|
||||
(unsigned)cmd_robot.gimbal_reset,
|
||||
(unsigned)dr16.s2,
|
||||
(unsigned)dr16.key_push_ctrl,
|
||||
(int)cmd_robot.thumb_wheel,
|
||||
(unsigned)dr16.s1,
|
||||
(int)dr16.mouse_rl,
|
||||
(int)dr16.mouse_du,
|
||||
(int)dr16.mouse_wheel,
|
||||
(unsigned)dr16.mouse_key_l,
|
||||
(unsigned)dr16.mouse_key_r,
|
||||
(unsigned)dr16.key_code_lo,
|
||||
(unsigned)dr16.key_code_hi,
|
||||
(unsigned long)dr16_frame_count);
|
||||
#else
|
||||
int len = snprintf(line, sizeof(line), "%u,%.1f,%u\r\n",
|
||||
(unsigned)motor_pitch_gimbal.fdb.raw_angle,
|
||||
pitch_current, (unsigned)motor_yaw_gimbal.fdb.raw_angle);
|
||||
#endif
|
||||
#endif
|
||||
#endif
|
||||
|
||||
debug_send_line(line, len, sizeof(line));
|
||||
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,330 @@
|
||||
/*
|
||||
*文件名称: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 GIMBAL_BENCH_RAMMER_TUNE
|
||||
return LINK_OFFLINE(motor_rammer.fdb.last_online_tick) ? 0 : -1;//台架仅检测拨弹电机
|
||||
#elif 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 GIMBAL_BENCH_RAMMER_TUNE
|
||||
can_alarm = (uint8_t)(motor_rammer.fdb.last_online_tick == 0U ||
|
||||
(HAL_GetTick() - motor_rammer.fdb.last_online_tick > DEVICE_OFFLINE_TIME_MS));
|
||||
#elif 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,277 @@
|
||||
/*
|
||||
*文件名称:vision.c
|
||||
*功能描述:视觉上位机串口通讯协议(USART1,115200-8N1,ASCII文本帧)
|
||||
*备注:接收格式"r/b <yaw> <pitch> #",发送格式"<yaw> <pitch> *"
|
||||
*/
|
||||
#include "vision.h"
|
||||
#include "bsp_usart.h"
|
||||
#include "attitude.h"
|
||||
#include "gimbal.h"
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#define VISION_RX_BUF_SIZE 64//DMA接收缓冲大小(字节)
|
||||
#define VISION_FRAME_MAX 32//单帧最大长度(字节)
|
||||
|
||||
/* 视觉目标接收数据结构体 */
|
||||
typedef struct
|
||||
{
|
||||
float yaw_deg; //目标Yaw绝对角(°)
|
||||
float pitch_deg; //目标Pitch绝对角(°)
|
||||
uint8_t color; //敌方颜色('r'/'b')
|
||||
uint32_t last_tick;//最近有效帧时间戳(ms)
|
||||
} vision_data_t;
|
||||
|
||||
static vision_data_t vision_data;//最新视觉目标数据
|
||||
volatile uint32_t vision_frame_count = 0U;//诊断:视觉有效帧接收计数
|
||||
|
||||
static uint8_t rx_buf[VISION_RX_BUF_SIZE];//USART1循环DMA接收缓冲
|
||||
static char frame_buf[VISION_FRAME_MAX];//单帧流式拼装缓冲
|
||||
static uint8_t frame_len = 0U;//当前帧已接收长度
|
||||
static uint8_t frame_overflow = 0U;//当前帧超长溢出标志
|
||||
|
||||
static char tx_buf[32];//姿态反馈发送缓冲
|
||||
static uint32_t last_tx_tick = 0U;//上次发送时间戳(ms)
|
||||
|
||||
/*
|
||||
*函数简介:手写定长ASCII浮点数解析(避免在中断内调用sscanf/strtof)
|
||||
*参数说明:s:输入字符串指针
|
||||
*参数说明:len:字符串字节长度
|
||||
*参数说明:out:解析结果输出指针
|
||||
*返回类型:uint8_t:1=解析成功,0=格式非法
|
||||
*备注:内部函数,由vision_frame_done调用
|
||||
*/
|
||||
static uint8_t parse_float(const char * s, uint8_t len, float * out)
|
||||
{
|
||||
float sign = 1.0f;
|
||||
float val = 0.0f;
|
||||
float frac = 0.1f;
|
||||
uint8_t i = 0U;
|
||||
uint8_t dot = 0U;
|
||||
uint8_t digits = 0U;
|
||||
|
||||
if (s == NULL || out == NULL || len == 0U)
|
||||
return 0U;
|
||||
|
||||
if (s[0] == '-')
|
||||
{
|
||||
sign = -1.0f;
|
||||
i = 1U;
|
||||
}
|
||||
else if (s[0] == '+')
|
||||
{
|
||||
i = 1U;
|
||||
}
|
||||
|
||||
for (; i < len; i++)
|
||||
{
|
||||
if (s[i] == '.')
|
||||
{
|
||||
if (dot)
|
||||
return 0U;
|
||||
dot = 1U;
|
||||
continue;
|
||||
}
|
||||
if (s[i] < '0' || s[i] > '9')
|
||||
return 0U;
|
||||
|
||||
digits = 1U;
|
||||
if (!dot)
|
||||
{
|
||||
val = val * 10.0f + (float)(s[i] - '0');
|
||||
}
|
||||
else
|
||||
{
|
||||
val += (float)(s[i] - '0') * frac;
|
||||
frac *= 0.1f;
|
||||
}
|
||||
}
|
||||
|
||||
if (!digits)
|
||||
return 0U;
|
||||
|
||||
*out = sign * val;
|
||||
return 1U;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:判断字符是否为空白分隔符
|
||||
*参数说明:c:待判断字符
|
||||
*返回类型:uint8_t:1=空白符,0=非空白符
|
||||
*备注:无
|
||||
*/
|
||||
static uint8_t is_space(char c)
|
||||
{
|
||||
return (uint8_t)(c == ' ' || c == '\t' || c == '\r' || c == '\n');
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:收到帧尾'#'后切分token并校验更新视觉目标
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:期望格式[r|b] [yaw] [pitch],Pitch超出±90°判为脏帧丢弃
|
||||
*/
|
||||
static void vision_frame_done(void)
|
||||
{
|
||||
const char * tok[4];
|
||||
uint8_t tok_len[4];
|
||||
uint8_t n = 0U;
|
||||
uint8_t i = 0U;
|
||||
|
||||
if (frame_overflow)
|
||||
return;
|
||||
|
||||
while (i < frame_len && n < 4U)
|
||||
{
|
||||
while (i < frame_len && is_space(frame_buf[i]))
|
||||
i++;
|
||||
if (i >= frame_len)
|
||||
break;
|
||||
|
||||
tok[n] = &frame_buf[i];
|
||||
uint8_t start = i;
|
||||
while (i < frame_len && !is_space(frame_buf[i]))
|
||||
i++;
|
||||
tok_len[n] = (uint8_t)(i - start);
|
||||
n++;
|
||||
}
|
||||
|
||||
if (n != 3U || tok_len[0] != 1U)
|
||||
return;
|
||||
|
||||
char color = tok[0][0];
|
||||
if (color != 'r' && color != 'b')
|
||||
return;
|
||||
|
||||
float yaw = 0.0f;
|
||||
float pitch = 0.0f;
|
||||
if (!parse_float(tok[1], tok_len[1], &yaw) ||
|
||||
!parse_float(tok[2], tok_len[2], &pitch))
|
||||
return;
|
||||
|
||||
if (pitch > 90.0f || pitch < -90.0f)
|
||||
return;//Pitch物理越界,丢弃脏帧
|
||||
|
||||
vision_data.yaw_deg = yaw;
|
||||
vision_data.pitch_deg = pitch;
|
||||
vision_data.color = (uint8_t)color;
|
||||
vision_data.last_tick = HAL_GetTick();
|
||||
vision_frame_count++;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:流式喂入单字节进行视觉帧拼装
|
||||
*参数说明:byte:接收字节
|
||||
*返回类型:无
|
||||
*备注:在USART1 IDLE整帧回调的中断上下文调用
|
||||
*/
|
||||
static void vision_parse_byte(uint8_t byte)
|
||||
{
|
||||
if (byte == (uint8_t)'#')
|
||||
{
|
||||
vision_frame_done();
|
||||
frame_len = 0U;
|
||||
frame_overflow = 0U;
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame_overflow)
|
||||
return;
|
||||
|
||||
if (frame_len >= VISION_FRAME_MAX)
|
||||
{
|
||||
frame_overflow = 1U;
|
||||
return;
|
||||
}
|
||||
|
||||
frame_buf[frame_len++] = (char)byte;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:USART1视觉整帧接收回调
|
||||
*参数说明:frame:接收帧数据指针
|
||||
*参数说明:len:接收帧字节长度
|
||||
*返回类型:无
|
||||
*备注:由bsp_usart空闲中断回调触发
|
||||
*/
|
||||
static void vision_rx_callback(const uint8_t * frame, uint16_t len)
|
||||
{
|
||||
if (frame == NULL || len == 0U)
|
||||
return;
|
||||
|
||||
for (uint16_t i = 0U; i < len; i++)
|
||||
vision_parse_byte(frame[i]);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:初始化视觉串口(USART1)收发DMA及全局中断
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:补齐CubeMX未开启的USART1_IRQn,优先级与USART3/6一致
|
||||
*/
|
||||
void vision_init(void)
|
||||
{
|
||||
usart1_init(rx_buf, VISION_RX_BUF_SIZE, vision_rx_callback);
|
||||
usart1_tx_dma_init();
|
||||
HAL_NVIC_SetPriority(USART1_IRQn, 5U, 0U);
|
||||
HAL_NVIC_EnableIRQ(USART1_IRQn);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:视觉通讯周期维护(100Hz发送本机姿态反馈)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:截断的帧不含'*'帧尾时放弃发送
|
||||
*/
|
||||
void vision_update(void)
|
||||
{
|
||||
if (!att_ready)
|
||||
return;
|
||||
|
||||
uint32_t now = HAL_GetTick();
|
||||
if (now - last_tx_tick < VISION_TX_PERIOD_MS)
|
||||
return;
|
||||
last_tx_tick = now;
|
||||
|
||||
int n = snprintf(tx_buf, sizeof(tx_buf), "%.2f %.2f *",
|
||||
gimbal_yaw_sign() * att_deg_yaw, -att_deg_roll);
|
||||
if (n > 0 && n < (int)sizeof(tx_buf))
|
||||
usart1_tx_dma_enable((uint8_t *)tx_buf, (uint16_t)n);
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:获取最新视觉自瞄目标角
|
||||
*参数说明:yaw_deg:输出目标Yaw绝对角(°)指针
|
||||
*参数说明:pitch_deg:输出目标Pitch绝对角(°)指针
|
||||
*返回类型:uint8_t:1=数据在线且读取有效,0=视觉掉线或读取冲突
|
||||
*备注:采用前后时间戳比对防止中断写入撕裂
|
||||
*/
|
||||
uint8_t vision_get_target(float * yaw_deg, float * pitch_deg)
|
||||
{
|
||||
if (yaw_deg == NULL || pitch_deg == NULL)
|
||||
return 0U;
|
||||
|
||||
uint32_t tick = vision_data.last_tick;
|
||||
if (tick == 0U || (HAL_GetTick() - tick > VISION_OFFLINE_TIME_MS))
|
||||
return 0U;
|
||||
|
||||
float y = vision_data.yaw_deg;
|
||||
float p = vision_data.pitch_deg;
|
||||
if (vision_data.last_tick != tick)
|
||||
return 0U;//读取期间发生中断更新,本周期回退手动
|
||||
|
||||
*yaw_deg = y;
|
||||
*pitch_deg = p;
|
||||
return 1U;
|
||||
}
|
||||
|
||||
/*
|
||||
*函数简介:获取最近有效帧识别到的敌方装甲颜色
|
||||
*参数说明:无
|
||||
*返回类型:uint8_t:'r'=红方,'b'=蓝方,0=未收到有效帧
|
||||
*备注:无
|
||||
*/
|
||||
uint8_t vision_color(void)
|
||||
{
|
||||
return vision_data.color;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#ifndef VISION_H
|
||||
#define VISION_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define VISION_OFFLINE_TIME_MS 100//超过该时间无有效帧判视觉掉线(ms)
|
||||
#define VISION_TX_PERIOD_MS 10 //姿态反馈发送周期(100Hz,ms)
|
||||
|
||||
/*
|
||||
*函数简介:初始化视觉串口(USART1)收发DMA及全局中断
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:在任务上下文由CAN_Task调用,补齐CubeMX未开启的USART1_IRQn
|
||||
*/
|
||||
extern void vision_init(void);
|
||||
|
||||
/*
|
||||
*函数简介:视觉通讯周期维护(100Hz发送本机姿态反馈)
|
||||
*参数说明:无
|
||||
*返回类型:无
|
||||
*备注:由gimbal_task每2ms调用,内部按VISION_TX_PERIOD_MS分频发送
|
||||
*/
|
||||
extern void vision_update(void);
|
||||
|
||||
/*
|
||||
*函数简介:获取最新视觉自瞄目标角
|
||||
*参数说明:yaw_deg:输出目标Yaw绝对角(°)指针
|
||||
*参数说明:pitch_deg:输出目标Pitch绝对角(°)指针
|
||||
*返回类型:uint8_t:1=数据在线且读取有效,0=视觉掉线或读取冲突
|
||||
*备注:返回0时调用方应回退手动控制
|
||||
*/
|
||||
extern uint8_t vision_get_target(float * yaw_deg, float * pitch_deg);
|
||||
|
||||
/*
|
||||
*函数简介:获取最近有效帧识别到的敌方装甲颜色
|
||||
*参数说明:无
|
||||
*返回类型:uint8_t:'r'=红方,'b'=蓝方,0=未收到有效帧
|
||||
*备注:无
|
||||
*/
|
||||
extern uint8_t vision_color(void);
|
||||
|
||||
extern volatile uint32_t vision_frame_count;//诊断:视觉有效帧接收计数
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user