/* *文件名称:monitor_task.c *功能描述:系统监控任务(LED_Task强定义),负责任务心跳检测、LED/蜂鸣器报警与IWDG看门狗 *备注:上电前5s宽限期无条件喂狗,之后仅当全部任务心跳正常时喂狗 *灯语:蓝灯每2s短闪=系统健康;红灯常亮+蜂鸣=CAN设备掉线;红灯1s内双闪=遥控器掉线(云台板); * 绿灯1s内连闪两下=BMI088失败、慢闪=IST8310失败、常亮=IMU过温或两个传感器都失败 *蜂鸣:CAN掉线时按最先掉线设备的序号n每2s响n下(序号表见linkcheck_first_offline);DEBUG_BUZZER_MELODY=1时改为循环旋律,灯语不变; * 遥控器上线瞬间两声上扬(云台板) *看门狗:IWDG约2.5s溢出;5s宽限期后,任一任务心跳超过500ms没刷新就不再喂狗,整车复位,不会失控 */ #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); } }