增加调试功能,以及yaw补偿

This commit is contained in:
lyf committed 2026-11-18 16:05:46 +08:00
1 parent 267a659242
commit 957c535bda
257 files changed
+239436 -692

No files matched your search

+2 -1
View File
@@ -16,6 +16,7 @@
#if defined(ROBOT_TYPE_GIMBAL)
#include "gimbal.h"
#include "shoot.h"
#include "vision.h"
#elif defined(ROBOT_TYPE_CHASSIS)
#include "chassis.h"
#endif
@@ -45,7 +46,7 @@ void CAN_Task(void const * argument)
usart3_init(dr16_rx_buf, 36, dr16_frame_callback); // DR16 遥控器
usart6_init(referee_rx_buf, 128, referee_frame_callback); // 图传链路(0x0304)
usart1_tx_dma_init(); // 视觉串口发送 DMA(TODO:视觉自瞄)
vision_init(); // 视觉上位机(USART1 收发 DMA + NVIC)
#elif defined(ROBOT_TYPE_CHASSIS)
chassis_motor_init(); // 电机实例与 CAN 回调注册
c_comm_init();
+94 -30
View File
@@ -61,58 +61,122 @@
#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>
/* ===== 2026-10-15 台架调参临时遥测(已停用,保留备查) =====
* 启用时把 #if 0 改 #if 1:本任务改为输出 5 列 CSV
* setpoint_deg, actual_deg, out_current, ecd_deg, gyro_x_dps
* (逗号分隔+换行,可被 Lissio FireWater 自动识别为 5 通道),采样率提到 200Hz,
* 用于定量观测阶跃响应与判断"轴是否真的在转动"。 */
#if 0
#include "robot_param.h"
#define DEBUG_PITCH_TUNE 1
#if DEBUG_PITCH_TUNE
#define DEBUG_OUT_PERIOD_MS 5 /* 200Hz:2ms 控制环的阶跃响应需 ≥100Hz 采样才量得准 */
#endif
#endif
/* 1-输出健康位(cvg/rdy/imu/an/gn); 0-只输出三个欧拉角 */
#define DEBUG_ATT_HEALTH 0
#define DEBUG_ATT_HEALTH 1
/* 1-再附加原始陀螺/零偏/采样计数(排障用); 0-只留上面的通用健康位 */
#define DEBUG_ATT_DIAG 0
#define DEBUG_ATT_DIAG 1
/* 1-仅输出 Pitch 调参行(便于用 VOFA+/串口示波器绘图); 0-同时输出姿态行与 Pitch 调参行 */
#define DEBUG_PITCH_ONLY 0
/* 1-追加控制量字段(见文件头); 0-保持原姿态输出不变 */
#define DEBUG_CMD_OUT 1
/* 1-只输出 Pitch 编码值/Pitch 最终电流/Yaw 编码值(核对转向与零位用);
* 与下面两个标定开关互斥,只能开一个 */
#define DEBUG_ENC_CUR 0
/* 1-只输出 yaw_speed_pid.OUT 与 Yaw 转子转速(标定小陀螺前馈 K 用,优先于台架输出);
* 采集期间必须把 GIMBAL_YAW_SPIN_FEEDFORWARD_K 置 0,否则 OUT 已被前馈削减;
* 0-恢复正常遥测 */
#define DEBUG_YAW_FF_CAL 0
/* 1-只输出 Pitch 原始编码值与速度环输出(标定重力前馈用);
* 与 DEBUG_ENC_CUR / DEBUG_YAW_FF_CAL 互斥,只能开一个;采集期间必须把三个
* GIMBAL_PITCH_GRAVITY_K_* 置 0,否则 pitch_pid 已被前馈抵消 */
#define DEBUG_PITCH_GRAV_CAL 0
/* 输出周期(ms)。姿态观测建议 100ms(10Hz) */
#define DEBUG_OUT_PERIOD_MS 100
/* 调试遥测任务:按 DEBUG_OUT_PERIOD_MS(ms)周期把姿态/控制量打包成
* 一行文本或 CSV,经 USB CDC 发给上位机(VOFA+ 等)。不返回(死循环)。 */
void debug_task(void const * argument)
{
char line[384];
char tail[112];
char pitch_line[128];
for (;;)
{
#if 0
/* ===== 2026-10-15 台架 Pitch 调参遥测(已停用,保留备查) =====
* 5 列 CSV → Lissio FireWater 直接成 5 通道:
* setpoint(位置环设定值含台架偏置), actual(-att_deg_roll),
* out(最终电流), ecd_deg(编码器机械角), gyro_x_dps(陀螺 x 轴角速度) */
const float tune_ecd_deg = ((float)motor_pitch_gimbal.fdb.raw_angle -
(float)PITCH_GM6020_ENCODER_ZERO) * (360.0f / 8192.0f);
const float tune_gyro_dps = bmi088_gyro[0] * 57.29578f;
int len = snprintf(line, sizeof(line), "%.3f,%.3f,%.1f,%.3f,%.2f\n",
gimbal_pitch_setpoint_deg(),
-att_deg_roll,
pitch_current,
tune_ecd_deg,
tune_gyro_dps);
#if DEBUG_ENC_CUR
/* ============ 编码值/电流核对:三通道 ============
* 1) Pitch 编码值 raw_angle(0~8191,水平零位 PITCH_GM6020_ENCODER_ZERO)
* 2) Pitch 最终电流指令 pitch_current(id 电流码,±16384 = ±3 A)
* 3) Yaw 编码值 raw_angle(0~8191,单圈值;零位 YAW_GM6020_ENCODER_ZERO)
* 用手转动云台即可核对"编码器增大方向"与电流正方向。
* 注意:云台被门控(电机掉线/遥控离线)时实际下发为 0,
* 而 pitch_current 保留上次计算值,此时第 2 列不代表真实输出。 */
{
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);
if (len > 0)
CDC_Transmit_FS((uint8_t *)line, (uint16_t)len);
}
osDelay(10); /* 100 Hz:手动转动足够看清方向 */
continue;
#endif
#if DEBUG_PITCH_GRAV_CAL
/* ============ 标定 Pitch 重力前馈:两通道 ============
* 1) raw_angle Pitch 编码值(0~8191,脚本按 ZERO_ECD 折成角度)
* 2) pitch_pid 速度环输出(id 电流码,不含重力前馈)
* 用 Desktop/test/fit_pitch_gravity.py 拟合
* I = K_COS*cos(a) + K_SIN*sin(a) + K_CONST。
* 注意:采集前三个 GIMBAL_PITCH_GRAVITY_K_* 必须为 0。 */
{
int len = snprintf(line, sizeof(line), "%u,%.1f\n",
(unsigned)motor_pitch_gimbal.fdb.raw_angle,
pitch_pid);
if (len > 0)
CDC_Transmit_FS((uint8_t *)line, (uint16_t)len);
}
osDelay(5); /* 200 Hz:静定窗口平均后噪声足够小 */
continue;
#endif
#if DEBUG_YAW_FF_CAL
/* ============ 标定小陀螺前馈 K:两通道 ============
* 1) yaw_speed_pid.OUT 速度环输出(id 电流码,不含前馈)
* 2) rotor_speed Yaw 电机转子转速 (RPM,与云台 1:1)
* 用 Desktop/test/fit_spin_ff.py 拟合 I = K·n。
* 注意:采集前 GIMBAL_YAW_SPIN_FEEDFORWARD_K 必须为 0。 */
{
int len = snprintf(line, sizeof(line), "%.1f,%d\n",
gimbal_yaw_speed_out(),
(int)motor_yaw_gimbal.fdb.rotor_speed);
if (len > 0)
CDC_Transmit_FS((uint8_t *)line, (uint16_t)len);
}
osDelay(5); /* 200 Hz:拟合需要足够的稳态点 */
continue;
#endif
#if GIMBAL_BENCH_RAMMER_TUNE
/* ============ 台架工况:拨弹盘单机,VOFA FireWater 输出 ============
* 4 通道(逗号分隔 + 换行,FireWater 自动识别为 4 条曲线):
* 1) 电机实测电流 (A)
* 2) 转子转速 (RPM)
* 3) PID 输出(电流指令 counts,±16384 对应 ±10 A)
* 4) 累计退弹转角 (°,自上次正转起算) <- 校验 2 发余量限位
* 状态机 bench_state_dbg / 中止码 bench_eject_abort 不占通道(调试器观察):
* state: 0=IDLE(待机/正转) 1=EJECT(退弹中) 2=LATCH(保护锁存)
* abort: 0=正常/无 1=电流软限 2=超时 3=电流硬限 */
{
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);
if (len > 0)
CDC_Transmit_FS((uint8_t *)line, (uint16_t)len);
}
osDelay(5); /* 200Hz:退弹过程的电流突变看得清 */
continue;
#endif
#if DEBUG_PITCH_ONLY
/* 仅输出 Pitch 调参数据(编码值、roll、PID最后输出值、遥控pitch杆量) */
int len = snprintf(line, sizeof(line),
+22 -31
View File
@@ -20,6 +20,7 @@
#include "attitude.h"
#include "dr16.h"
#include "c_comm.h"
#include "vision.h"
#include "robot_param.h"
/* 校准手势:左摇杆推最左 + 右摇杆推最右(正常操控不可能误触) */
@@ -35,6 +36,9 @@ typedef enum
YAW_DETECT,
} yaw_state_e;
/* 云台控制任务:状态机 + 2 ms(500 Hz)周期调用 gimbal_control。
* 参数/返回值由 CMSIS-RTOS 定义,本函数不返回(死循环)。
* 正常态下把摇杆/鼠标增量(°)或视觉绝对角(°)送入双环,并发送 CAN 电流。 */
void gimbal_task(void const * argument)
{
TickType_t last_wake = xTaskGetTickCount();
@@ -48,6 +52,8 @@ void gimbal_task(void const * argument)
for (;;)
{
vision_update(); // 视觉姿态反馈(10ms 周期,与云台状态无关)
const uint8_t gates_ok = (cmd_robot.online && !gimbal_motor_offline() &&
bmi088_online && att_ready && !ctoc_chassis_fault());
@@ -64,34 +70,6 @@ void gimbal_task(void const * argument)
if (gesture)
gesture_cnt = 0;
#if 0
/* ===== 2026-10-15 台架 Pitch 阶跃发生器(已停用,保留备查) =====
* **有界**测试,不是持续抖动:只跑 GIMBAL_BENCH_PITCH_HALF_COUNT 个半周期,
* 跑完自动归零并锁存,之后云台恢复静止(重新上电/改开关才会再跑一次)。
* 配合 gimbal.c 的台架偏置使用;2ms 任务,半周期 tick 数 = HALF_MS / 2 */
{
static uint32_t bench_phase = 0;
static uint8_t bench_done = 0;
const uint32_t half_ticks = (uint32_t)GIMBAL_BENCH_PITCH_HALF_MS / 2u;
if (!bench_done && half_ticks > 0u)
{
const uint32_t total_ticks = half_ticks * (uint32_t)GIMBAL_BENCH_PITCH_HALF_COUNT;
if (bench_phase >= total_ticks)
{
bench_done = 1;
gimbal_bench_pitch_offset = 0.0f; /* 跑完立即停,不再抖动 */
}
else
{
gimbal_bench_pitch_offset = ((bench_phase / half_ticks) % 2u)
? GIMBAL_BENCH_PITCH_STEP_DEG
: 0.0f;
bench_phase++;
}
}
}
#endif
switch (state)
{
case YAW_WAIT:
@@ -129,9 +107,22 @@ void gimbal_task(void const * argument)
}
else if (gates_ok)
{
gimbal_control(cmd_robot.gimbal_yaw_delta,
cmd_robot.gimbal_pitch_delta,
(uint8_t)(force_reset || cmd_robot.gimbal_reset));
float aim_yaw, aim_pitch;
if (cmd_robot.aim && vision_get_target(&aim_yaw, &aim_pitch))
{
/* 自瞄:视觉给绝对目标角。先跑控制(处理复位同步/
* PID 清理),再覆写目标,下一周期闭环即追踪视觉目标;
* 退出自瞄后手动增量从保持的目标角继续,无跳变 */
gimbal_control(0.0f, 0.0f,
(uint8_t)(force_reset || cmd_robot.gimbal_reset));
gimbal_set_target_deg(aim_yaw, aim_pitch);
}
else
{
gimbal_control(cmd_robot.gimbal_yaw_delta,
cmd_robot.gimbal_pitch_delta,
(uint8_t)(force_reset || cmd_robot.gimbal_reset));
}
force_reset = 0;
}
else
+11 -2
View File
@@ -84,7 +84,11 @@ static uint8_t iwdg_should_feed(void)
static int8_t linkcheck_first_offline(void)
{
uint32_t now = HAL_GetTick();
#if defined(ROBOT_TYPE_GIMBAL)
#if GIMBAL_BENCH_RAMMER_TUNE
/* 台架工况(只接拨弹 M2006 + DT7):只有拨弹在场,只报它。
* 拨弹(CAN2 0x201)掉线 -> 响 1 下;其它设备不在场,一律不报 */
return LINK_OFFLINE(motor_rammer.fdb.last_online_tick) ? 0 : -1;
#elif defined(ROBOT_TYPE_GIMBAL)
/* 设备序号:0 摩擦轮L(CAN1 0x201) 1 摩擦轮R(CAN1 0x202) 2 Pitch(CAN1 0x206)
* 3 Yaw(CAN2 0x205) 4 拨弹(CAN2 0x201,安装时) 5 底盘回传(0x019) */
if (LINK_OFFLINE(motor_friction_l.fdb.last_online_tick))
@@ -164,7 +168,12 @@ void LED_Task(void const * argument)
}
/* ---- CAN 设备掉线:红灯常亮 + 2s 周期内响 n 下(n=掉线设备序号+1,legacy 语义) ---- */
#if defined(ROBOT_TYPE_GIMBAL)
#if GIMBAL_BENCH_RAMMER_TUNE
/* 台架工况:报警源只有拨弹 M2006(CAN2 0x201)。
* 摩擦轮/云台/底盘都不在场,不再置红灯、不再蜂鸣 */
can_alarm = (motor_rammer.fdb.last_online_tick == 0 ||
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();
+7 -5
View File
@@ -78,9 +78,6 @@ static void referee_keyboard_hook(const uint8_t data[12])
}
/* ---- 键鼠输入来源(2026-10-16 修) ----
* 背景:客户端键鼠是从 USART3 那 18 字节帧进来的(data[6..15]),dr16_parse 早就
* 解析进 dr16.mouse_rl / dr16.key_w 等字段,但控制逻辑只读 0x0304 填的
* keyboard_data,于是"键鼠控制完全没用"。此开关让两路合并。
* 1 = 除 0x0304 外同时接受帧内键鼠:见到键鼠动作即进入键鼠模式并保持,
* 直到操作手去动云台左摇杆才交还遥控器(右摇杆在底盘,键鼠模式下仍要能用)。
* 0 = 旧行为:只有 0x0304 在线才走键鼠。 */
@@ -176,8 +173,8 @@ void robot_cmd_task(void const * argument)
if (!kb_active)
{
/* 遥控器:左摇杆向上(>1050)为抬头(pitch_delta减小,pos向2260减小),门控须 pos > UPPER_LIMIT(2260);
* 左摇杆向下(<1000)为低头(pitch_delta增大,pos向3950增大),门控须 pos < LOWER_LIMIT(3950) */
/* 遥控器:左摇杆向上(>1050)为抬头(pitch_delta减小,pos向2230减小),门控须 pos > UPPER_LIMIT(2230);
* 左摇杆向下(<1000)为低头(pitch_delta增大,pos向4150增大),门控须 pos < LOWER_LIMIT(4150) */
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;
@@ -208,6 +205,10 @@ void robot_cmd_task(void const * argument)
cmd_robot.gimbal_yaw_delta = yaw_delta;
cmd_robot.gimbal_pitch_delta = pitch_delta;
/* 自瞄请求:键鼠=按住鼠标右键;遥控=S1 左拨杆拨到下侧(2)。
* 是否真正接管云台目标还要看视觉在线(见 gimbal_task) */
cmd_robot.aim = kb_active ? (keyboard_merged.mouse_key_r != 0) : (dr16.s1 == 2);
/* ---- 发射机构 ----
* 键鼠模式用 Q / 鼠标左键 / E(两路来源已合并进 kb_*,0x0304 优先);
* 遥控模式用 S1 与拨轮。kb_push_* 是"按下翻转"的常驻状态,
@@ -230,6 +231,7 @@ void robot_cmd_task(void const * argument)
{
cmd_robot.gimbal_yaw_delta = 0;
cmd_robot.gimbal_pitch_delta = 0;
cmd_robot.aim = 0;
cmd_robot.friction_on = 0;
cmd_robot.fire = 0;
cmd_robot.eject = 0;
+11
View File
@@ -12,6 +12,7 @@
#include "shoot.h"
#include "c_comm.h"
#include "dr16.h"
#include "robot_param.h" /* 必须包含:GIMBAL_BENCH_RAMMER_TUNE 等开关定义在此 */
/* shoot_task(云台板,2ms):摩擦轮/拨弹控制
* 发射机构上电状态来自底盘 0x019(ctoc_shooter_power) */
@@ -25,6 +26,15 @@ void shoot_task(void const * argument)
for (;;)
{
#if GIMBAL_BENCH_RAMMER_TUNE
/* ---- 台架调试工况:单机拨弹 ----------------------------------------
* 只有 M2006(拨弹)+DT7 时使用:旁路 cmd_robot.online / gimbal_motor_offline /
* ctoc_chassis_fault 全部门控,直接由 DT7 驱动拨弹盘。
* 控制方式同整车遥控器模式:S1 上=使能、拨轮下=发射、拨轮上=退弹、中位=停;
* 退弹由 shoot_bench_step() 内部做 转角/电流/超时 保护。 */
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,
@@ -35,6 +45,7 @@ void shoot_task(void const * argument)
{
shoot_output_zero();
}
#endif
monitor_heartbeat(HB_SHOOT);
vTaskDelayUntil(&last_wake, pdMS_TO_TICKS(2));