Files
Simba-OmniInfantry/云台/App/system/debug_task.c
T
tmr.textandClaude Opus 5.5 4b75e4ec6e 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>
2026-11-30 11:38:19 +08:00

198 lines
7.6 KiB
C

/*
*文件名称: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);
}
}