Files
Simba-OmniInfantry/module_test/Application/Task/Src/monitor_task.c
T

122 lines
3.4 KiB
C

/* =============================================================================
* monitor_task.c - LED_Task 强定义:任务心跳 + LED/蜂鸣报警 + IWDG 看门狗
*
* 精简版(原车载监控已移除 CToC/裁判/IMU/云台检查):
* - 只检查两个任务心跳:HB_TEST(2ms 测试循环)、HB_DEBUG(FireWater 输出);
* - 报警源只有两个:被测电机掉线、DT7 遥控掉线;
* - IWDG 约 2.5s:上电 5s 宽限期无条件喂狗,之后仅心跳全正常才喂。
*
* 灯语:
* 蓝灯每 2s 短闪 = 系统健康
* 红灯常亮 + 每 2s 响 1 声 = 被测电机掉线(查 CAN 线/电调 ID/波特率)
* 红灯 1s 双闪 = DT7 遥控掉线
* ========================================================================== */
#include "app_tasks.h"
#include "cmsis_os.h"
#include "stm32f4xx_hal.h"
#include "led.h"
#include "buzzer.h"
#include "dr16.h"
#include "module_test.h"
#define HEARTBEAT_TIMEOUT_MS 500
static volatile uint32_t heartbeat_tick[HB_COUNT];
void monitor_heartbeat(heartbeat_id_e id)
{
if ((uint32_t)id < (uint32_t)HB_COUNT)
heartbeat_tick[id] = HAL_GetTick();
}
static uint8_t all_heartbeats_alive(void)
{
static const heartbeat_id_e expected[] = {HB_TEST, HB_DEBUG};
uint32_t now = HAL_GetTick();
for (uint32_t i = 0; i < sizeof(expected) / sizeof(expected[0]); i++)
{
if (heartbeat_tick[expected[i]] == 0 ||
now - heartbeat_tick[expected[i]] > HEARTBEAT_TIMEOUT_MS)
return 0;
}
return 1;
}
/* IWDG:直接写寄存器(HAL IWDG 模块未在 CubeMX 中启用)
* PR=4 -> LSI 32kHz / 64 分频 = 500Hz,重装载 1250 -> 约 2.5s 溢出 */
static void iwdg_init(void)
{
IWDG->KR = 0x5555; /* 解除写保护 */
IWDG->PR = 4;
IWDG->RLR = 1250;
IWDG->KR = 0xAAAA; /* 重装载 */
IWDG->KR = 0xCCCC; /* 启动 */
}
void LED_Task(void const * argument)
{
uint8_t tick_10ms = 0;
uint8_t first_ready = 1;
(void)argument;
iwdg_init();
/* 等待硬件初始化完成(CAN_Task 的蓝灯常亮即初始化中) */
while (!app_ready)
{
IWDG->KR = 0xAAAA;
osDelay(10);
}
for (;;)
{
const uint8_t motor_offline = (mt_motor_online() == 0);
const uint8_t rc_offline = (dr16_status == 0) || dr16_offline();
if (first_ready)
{
first_ready = 0;
led_set_all_off();
}
tick_10ms = (tick_10ms >= 199) ? 0 : (uint8_t)(tick_10ms + 1);
if (motor_offline)
{
/* 被测电机掉线:红灯常亮 + 2s 响 1 声 */
led_set_blue(0);
led_set_green(0);
led_set_red(1);
if (tick_10ms == 0)
buzzer_on(BUZZER_H3);
else if (tick_10ms == 5)
buzzer_off();
}
else if (rc_offline)
{
/* 遥控掉线:红灯 1s 双闪 */
led_set_blue(0);
led_set_green(0);
led_set_red((uint8_t)((tick_10ms % 50) < 10));
buzzer_off();
}
else
{
/* 健康:蓝灯每 2s 短闪 30ms */
led_set_red(0);
led_set_green(0);
led_set_blue((uint8_t)(tick_10ms < 3));
if (tick_10ms == 3)
buzzer_off();
}
/* 喂狗:上电 5s 宽限;之后必须心跳正常 */
if (HAL_GetTick() < 5000U || all_heartbeats_alive())
IWDG->KR = 0xAAAA;
osDelay(10);
}
}