将警报声添加到DaemonTask

This commit is contained in:
chenfu 2023-06-23 17:05:55 +08:00
parent 4a45331d31
commit c85c13f959
7 changed files with 42 additions and 46 deletions

View File

@ -153,6 +153,7 @@ modules/can_comm/can_comm.c \
modules/message_center/message_center.c \
modules/daemon/daemon.c \
modules/vofa/vofa.c \
modules/alarm/buzzer.c \
application/gimbal/gimbal.c \
application/chassis/chassis.c \
application/shoot/shoot.c \
@ -268,6 +269,7 @@ C_INCLUDES = \
-Imodules/message_center \
-Imodules/daemon \
-Imodules/vofa \
-Imodules/alarm \
-Imodules

View File

@ -2,7 +2,7 @@
#include "bsp_log.h"
#include "bsp_dwt.h"
#include "bsp_usb.h"
#include "bsp_buzzer.h"
#include "buzzer.h"
#include "bsp_led.h"
#include "bsp_temperature.h"
@ -16,5 +16,5 @@ void BSPInit()
// legacy support待删除,将在实现了led/tempctrl/buzzer的module之后移动到app层进行XXXRegister()
LEDInit();
IMUTempInit();
BuzzerInit();
buzzer_init();
}

View File

@ -1,28 +0,0 @@
#include "bsp_buzzer.h"
#include "main.h"
#warning this is a legacy support file, please use the new version
extern TIM_HandleTypeDef htim4;
static uint8_t tmp_warning_level = 0;
void BuzzerInit()
{
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
}
void BuzzerOn(uint16_t psc, uint16_t pwm, uint8_t level)
{
if (level > tmp_warning_level)
{
tmp_warning_level = level;
__HAL_TIM_PRESCALER(&htim4, psc);
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, pwm);
}
}
void BuzzerOff(void)
{
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, 0);
tmp_warning_level = 0;
}

View File

@ -1,10 +0,0 @@
#ifndef BSP_BUZZER_H
#define BSP_BUZZER_H
#include <stdint.h>
void BuzzerInit();
extern void BuzzerOn(uint16_t psc, uint16_t pwm, uint8_t level);
extern void BuzzerOff(void);
#endif

View File

@ -15,7 +15,7 @@ void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
for (uint8_t i = 0; i < idx; i++)
{ // 来自同一个定时器的中断且通道相同
if (pwm_instance[i]->htim == htim && htim->Channel == pwm_instance[i]->channel)
if (pwm_instance[i]->htim == htim && htim->Channel == (1<<(pwm_instance[i]->channel/4)))
{
if (pwm_instance[i]->callback) // 如果有回调函数
pwm_instance[i]->callback(pwm_instance[i]);
@ -39,7 +39,7 @@ PWMInstance *PWMRegister(PWM_Init_Config_s *config)
pwm->callback = config->callback;
pwm->id = config->id;
// 启动PWM
HAL_TIM_PWM_Start(pwm->htim, pwm->channel);
HAL_TIM_PWM_Start_IT(pwm->htim, pwm->channel);
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse); // 设置占空比
pwm_instance[idx++] = pwm;
@ -65,7 +65,11 @@ void PWMSetPulse(PWMInstance *pwm, uint32_t pulse)
pwm->pulse = pulse;
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse);
}
void PWMSetPeriod(PWMInstance *pwm, uint32_t period)
{
pwm->period = period;
__HAL_TIM_PRESCALER(pwm->htim, pwm->period);
}
/* 只是对HAL的函数进行了形式上的封装 */
void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size)
{

View File

@ -2,6 +2,7 @@
#include "bsp_dwt.h" // 后续通过定时器来计时?
#include "stdlib.h"
#include "memory.h"
#include "buzzer.h"
// 用于保存所有的daemon instance
static DaemonInstance *daemon_instances[DAEMON_MX_CNT] = {NULL};
@ -15,7 +16,9 @@ DaemonInstance *DaemonRegister(Daemon_Init_Config_s *config)
instance->owner_id = config->owner_id;
instance->reload_count = config->reload_count == 0 ? 100 : config->reload_count;
instance->callback = config->callback;
instance->alarm_state = config->alarm_state;
instance->alarm_level = config->alarm_level;
instance->temp_count = config->reload_count;
daemon_instances[idx++] = instance;
return instance;
}
@ -36,6 +39,7 @@ void DaemonTask()
DaemonInstance *dins; // 提高可读性同时降低访存开销
for (size_t i = 0; i < idx; ++i)
{
dins = daemon_instances[i];
if (dins->temp_count > 0) // 如果计数器还有值,说明上一次喂狗后还没有超时,则计数器减一
dins->temp_count--;
@ -43,6 +47,11 @@ void DaemonTask()
{
dins->callback(dins->owner_id); // module内可以将owner_id强制类型转换成自身类型从而调用特定module的offline callback
// @todo 为蜂鸣器/led等增加离线报警的功能,非常关键!
if(dins->alarm_state == ALARM_ON)
{
BuzzerPlay(dins->alarm_level);
}
}
}
}

View File

@ -2,18 +2,34 @@
#define MONITOR_H
#include "stdint.h"
#include "string.h"
#define DAEMON_MX_CNT 64
/* 模块离线处理函数指针 */
typedef void (*offline_callback)(void *);
typedef enum
{
ALARM_OFF = 0,
ALARM_ON = 1,
}alarm_state_e;
typedef enum
{
ALARM_LEVEL_LOW = 0,
ALARM_LEVEL_BELOW_MEDIUM = 1,
ALARM_LEVEL_MEDIUM = 2,
ALARM_LEVEL_ABOVE_MEDIUM = 3,
ALARM_LEVEL_HIGH = 4,
ALARM_OFFLINE = 5,
}alarm_level_e;
/* daemon结构体定义 */
typedef struct daemon_ins
{
uint16_t reload_count; // 重载值
offline_callback callback; // 异常处理函数,当模块发生异常时会被调用
alarm_state_e alarm_state; // 蜂鸣器状态
alarm_level_e alarm_level; //警报级别
uint16_t temp_count; // 当前值,减为零说明模块离线或异常
void *owner_id; // daemon实例的地址,初始化的时候填入
} DaemonInstance;
@ -23,6 +39,9 @@ typedef struct
{
uint16_t reload_count; // 实际上这是app唯一需要设置的值?
offline_callback callback; // 异常处理函数,当模块发生异常时会被调用
alarm_state_e alarm_state; // 蜂鸣器状态
alarm_level_e alarm_level; //警报级别
void *owner_id; // id取拥有daemon的实例的地址,如DJIMotorInstance*,cast成void*类型
} Daemon_Init_Config_s;