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:
tmr.textandClaude Opus 5.5 committed 2026-11-30 11:38:19 +08:00
1 parent 9de485e4ef
commit 4b75e4ec6e
115 files changed
+1200 -818

No files matched your search

+330
View File
@@ -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);
}
}