将蜂鸣器警报和daemonTask分开

This commit is contained in:
chenfu 2023-07-07 19:27:24 +08:00
parent 2404edd4eb
commit c2fb92edd8
10 changed files with 223 additions and 133 deletions

View File

@ -12,6 +12,7 @@
#include "master_process.h" #include "master_process.h"
#include "daemon.h" #include "daemon.h"
#include "HT04.h" #include "HT04.h"
#include "buzzer.h"
#include "bsp_log.h" #include "bsp_log.h"
@ -88,12 +89,14 @@ void StartMOTORTASK(void const *argument)
void StartDAEMONTASK(void const *argument) void StartDAEMONTASK(void const *argument)
{ {
static float daemon_dt, daemon_start; static float daemon_dt, daemon_start;
buzzer_init();
LOGINFO("[freeRTOS] Daemon Task Start"); LOGINFO("[freeRTOS] Daemon Task Start");
while (1) while (1)
{ {
// 100Hz // 100Hz
daemon_start = DWT_GetTimeline_ms(); daemon_start = DWT_GetTimeline_ms();
DaemonTask(); DaemonTask();
BuzzerTask();
daemon_dt = DWT_GetTimeline_ms() - daemon_start; daemon_dt = DWT_GetTimeline_ms() - daemon_start;
if (daemon_dt > 10) if (daemon_dt > 10)
LOGERROR("[freeRTOS] Daemon Task is being DELAY! dt = [%f]", &daemon_dt); LOGERROR("[freeRTOS] Daemon Task is being DELAY! dt = [%f]", &daemon_dt);

View File

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

View File

@ -5,12 +5,10 @@
// 配合中断以及初始化 // 配合中断以及初始化
static uint8_t idx; static uint8_t idx;
static PWMInstance *pwm_instance[PWM_DEVICE_CNT] = {NULL}; // 所有的pwm instance保存于此,用于callback时判断中断来源 static PWMInstance *pwm_instance[PWM_DEVICE_CNT] = {NULL}; // 所有的pwm instance保存于此,用于callback时判断中断来源
static uint32_t PWMSelectTclk(TIM_HandleTypeDef *htim );
/** /**
* @brief pwm dma传输完成回调函数 * @brief pwm dma传输完成回调函数
* @attention HAL库的设计问题,pulse完成(tim的计数超过比较寄存器) *
* PWM的TIM,,DMA传输完成中断打开
*
* @param htim * @param htim
*/ */
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim) void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
@ -37,13 +35,14 @@ PWMInstance *PWMRegister(PWM_Init_Config_s *config)
pwm->htim = config->htim; pwm->htim = config->htim;
pwm->channel = config->channel; pwm->channel = config->channel;
pwm->period = config->period; pwm->period = config->period;
pwm->pulse = config->pulse; pwm->dutyratio = config->dutyratio;
pwm->callback = config->callback; pwm->callback = config->callback;
pwm->id = config->id; pwm->id = config->id;
pwm->tclk = PWMSelectTclk(pwm->htim);
// 启动PWM // 启动PWM
HAL_TIM_PWM_Start_IT(pwm->htim, pwm->channel); HAL_TIM_PWM_Start(pwm->htim, pwm->channel);
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse); // 设置占空比 PWMSetPeriod(pwm, pwm->period);
PWMSetDutyRatio(pwm, pwm->dutyratio);
pwm_instance[idx++] = pwm; pwm_instance[idx++] = pwm;
return pwm; return pwm;
} }
@ -52,7 +51,6 @@ PWMInstance *PWMRegister(PWM_Init_Config_s *config)
void PWMStart(PWMInstance *pwm) void PWMStart(PWMInstance *pwm)
{ {
HAL_TIM_PWM_Start(pwm->htim, pwm->channel); HAL_TIM_PWM_Start(pwm->htim, pwm->channel);
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse);
} }
/* 只是对HAL的函数进行了形式上的封装 */ /* 只是对HAL的函数进行了形式上的封装 */
@ -61,19 +59,51 @@ void PWMStop(PWMInstance *pwm)
HAL_TIM_PWM_Stop(pwm->htim, pwm->channel); HAL_TIM_PWM_Stop(pwm->htim, pwm->channel);
} }
/* 只是对HAL的函数进行了形式上的封装 */ /*
void PWMSetPulse(PWMInstance *pwm, uint32_t pulse) * @brief pwm周期
*
* @param pwm pwm实例
* @param period s
*/
void PWMSetPeriod(PWMInstance *pwm, float period)
{ {
pwm->pulse = pulse; __HAL_TIM_SetAutoreload(pwm->htim, period*((pwm->tclk)/(pwm->htim->Init.Prescaler+1)));
__HAL_TIM_SetCompare(pwm->htim, pwm->channel, pwm->pulse);
} }
void PWMSetPeriod(PWMInstance *pwm, uint32_t period) /*
* @brief pwm占空比
*
* @param pwm pwm实例
* @param dutyratio 0~1
*/
void PWMSetDutyRatio(PWMInstance *pwm, float dutyratio)
{ {
pwm->period = period; __HAL_TIM_SetCompare(pwm->htim, pwm->channel, dutyratio * (pwm->htim->Instance->ARR));
__HAL_TIM_PRESCALER(pwm->htim, pwm->period);
} }
/* 只是对HAL的函数进行了形式上的封装 */ /* 只是对HAL的函数进行了形式上的封装 */
void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size) void PWMStartDMA(PWMInstance *pwm, uint32_t *pData, uint32_t Size)
{ {
HAL_TIM_PWM_Start_DMA(pwm->htim, pwm->channel, pData, Size); HAL_TIM_PWM_Start_DMA(pwm->htim, pwm->channel, pData, Size);
}
// 设置pwm对应定时器时钟源频率
//tim2~7,12~14:APB1 tim1,8~11:APB2
static uint32_t PWMSelectTclk(TIM_HandleTypeDef *htim )
{
uintptr_t tclk_temp = ((uintptr_t)((htim)->Instance));
if (
(tclk_temp <= (APB1PERIPH_BASE + 0x2000UL)) &&
(tclk_temp >= (APB1PERIPH_BASE + 0x0000UL)))
{
return (HAL_RCC_GetPCLK1Freq() * (APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> RCC_CFGR_PPRE1_Pos] == 0 ? 1 : 2));
}
else if (
((tclk_temp <= (APB2PERIPH_BASE + 0x0400UL)) &&
(tclk_temp >= (APB2PERIPH_BASE + 0x0000UL))) ||
((tclk_temp <= (APB2PERIPH_BASE + 0x4800UL)) &&
(tclk_temp >= (APB2PERIPH_BASE + 0x4000UL))))
{
return (HAL_RCC_GetPCLK2Freq() * (APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> RCC_CFGR_PPRE1_Pos] == 0 ? 1 : 2));
}
return 0;
} }

View File

@ -4,7 +4,6 @@
* @brief * @brief
* @version 0.1 * @version 0.1
* @date 2023-02-14 * @date 2023-02-14
* @todo ,module修改tim的分频器和自动重装载寄存器?
* *
* @copyright Copyright (c) 2023 * @copyright Copyright (c) 2023
* *
@ -15,7 +14,8 @@
#include "tim.h" #include "tim.h"
#include "stdint.h" #include "stdint.h"
#include "stm32f4xx_hal_rcc.h"
#include "stm32f407xx.h"
#define PWM_DEVICE_CNT 16 // 最大支持的PWM实例数量 #define PWM_DEVICE_CNT 16 // 最大支持的PWM实例数量
/* pwm实例结构体 */ /* pwm实例结构体 */
@ -23,21 +23,19 @@ typedef struct pwm_ins_temp
{ {
TIM_HandleTypeDef *htim; // TIM句柄 TIM_HandleTypeDef *htim; // TIM句柄
uint32_t channel; // 通道 uint32_t channel; // 通道
uint32_t period; // 周期 uint32_t tclk; // 时钟频率
uint32_t pulse; // 脉宽 float period; // 周期
float dutyratio; // 占空比
void (*callback)(struct pwm_ins_temp *); // DMA传输完成回调函数 void (*callback)(struct pwm_ins_temp *); // DMA传输完成回调函数
void *id; // 实例ID void *id; // 实例ID
// 后续还要添加更多的参数,以提供更直观的封装,比如直接按照百分比设置占空比,直接设置频率等
// ...
} PWMInstance; } PWMInstance;
typedef struct typedef struct
{ {
TIM_HandleTypeDef *htim; // TIM句柄 TIM_HandleTypeDef *htim; // TIM句柄
uint32_t channel; // 通道 uint32_t channel; // 通道
uint32_t period; // 周期 float period; // 周期
uint32_t pulse; // 脉宽 float dutyratio; // 占空比
void (*callback)(PWMInstance*); // DMA传输完成回调函数 void (*callback)(PWMInstance*); // DMA传输完成回调函数
void *id; // 实例ID void *id; // 实例ID
} PWM_Init_Config_s; } PWM_Init_Config_s;
@ -56,7 +54,14 @@ PWMInstance *PWMRegister(PWM_Init_Config_s *config);
* @param pwm pwm实例 * @param pwm pwm实例
*/ */
void PWMStart(PWMInstance *pwm); void PWMStart(PWMInstance *pwm);
/**
* @brief pwm占空比
*
* @param pwm pwm实例
* @param dutyratio 0~1
*/
void PWMSetDutyRatio(PWMInstance *pwm, float dutyratio);
/** /**
* @brief pwm * @brief pwm
* *
@ -64,17 +69,13 @@ void PWMStart(PWMInstance *pwm);
*/ */
void PWMStop(PWMInstance *pwm); void PWMStop(PWMInstance *pwm);
// @todo 这三个函数还需要进一步封装,务必协调好三者之间的关系
/** /**
* @brief pwm脉宽 * @brief pwm周期
* *
* @param pwm pwm实例 * @param pwm pwm实例
* @param pulse * @param period s
*/ */
void PWMSetPulse(PWMInstance *pwm, uint32_t pulse); void PWMSetPeriod(PWMInstance *pwm, float period);
void PWMSetPeriod(PWMInstance *pwm, uint32_t period); // 未实现
void PWMSetPrescaler(PWMInstance *pwm, uint32_t prescaler); // 未实现
/** /**
* @brief pwm dma传输 * @brief pwm dma传输

View File

@ -1,5 +1,5 @@
# bsp pwm # bsp pwm
当前模块的实现有较大问题是否应该允许多个使用相同tim的channel修改预分频计数器和重装载计数器 同一定时器下的pwm通道更改周期时请注意该定时器下是否有其他pwm示例如果有请注意不要影响其他pwm通道的周期。
使用pwm dma传输中断时注意占空比的设置设为0将不会进入中断函数
以及,是否给用户权限修改脉宽? 默认tim psc已在cubemx设置好详情可参考cubemx配置文件

View File

@ -1,88 +1,97 @@
#include "bsp_pwm.h" #include "bsp_pwm.h"
#include "buzzer.h" #include "buzzer.h"
#include "bsp_dwt.h" #include "bsp_dwt.h"
#include "string.h"
static PWMInstance *buzzer; static PWMInstance *buzzer;
static uint8_t idx;
static alarm_level_e now_alarm_level = ALARM_OFFLINE; static BuzzzerInstance *buzzer_list[BUZZER_DEVICE_CNT] = {0};
void BuzzerOn(PWMInstance *buzzer );
/**
*
* @brief
* @param alarm_level
*/
void BuzzerPlay(alarm_level_e alarm_level)
{
static alarm_level_e last_alarm_level = ALARM_LEVEL_LOW;
if(((int)DWT_GetTimeline_s() % 5)<1) //每5秒检查一次
{
last_alarm_level = ALARM_LEVEL_LOW;
now_alarm_level = ALARM_OFFLINE;
}
if(last_alarm_level <= now_alarm_level) //如果当前报警级别大于等于上一次报警级别,则更新报警级别
{
now_alarm_level = alarm_level;
}
last_alarm_level = alarm_level;
}
/** /**
* @brief * @brief
* *
*/ */
void buzzer_init() void buzzer_init()
{ {
PWM_Init_Config_s buzzer_config = { PWM_Init_Config_s buzzer_config = {
.htim = &htim4, .htim = &htim4,
.channel= TIM_CHANNEL_3, .channel= TIM_CHANNEL_3,
.period = 1, .dutyratio = 0,
.pulse = 10000, .period = 0.001,
.callback = BuzzerOn,
}; };
buzzer = PWMRegister(&buzzer_config); buzzer = PWMRegister(&buzzer_config);
} }
/**
* @brief BuzzzerInstance *BuzzerRegister(Buzzer_config_s *config)
*
* @param buzzer
*/
//*@todo: 优化报警声应类似D__,DDD,B__,BBB等报警声
void BuzzerOn(PWMInstance *buzzer )
{ {
switch (now_alarm_level) if (config->alarm_level > BUZZER_DEVICE_CNT) // 超过最大实例数,考虑增加或查看是否有内存泄漏
while (1)
;
BuzzzerInstance *buzzer_temp = (BuzzzerInstance *)malloc(sizeof(BuzzzerInstance));
memset(buzzer_temp, 0, sizeof(BuzzzerInstance));
buzzer_temp->alarm_level = config->alarm_level;
buzzer_temp->loudness = config->loudness;
buzzer_temp->octave = config->octave;
buzzer_temp->alarm_state = ALARM_OFF;
buzzer_list[config->alarm_level] = buzzer_temp;
return buzzer_temp;
}
void AlarmSetStatus(BuzzzerInstance *buzzer, AlarmState_e state)
{
buzzer->alarm_state = state;
}
void BuzzerTask()
{
BuzzzerInstance *buzz;
for (size_t i = 0; i < BUZZER_DEVICE_CNT; ++i)
{ {
case ALARM_LEVEL_LOW: buzz = buzzer_list[i];
PWMSetPeriod(buzzer, 1); if(buzz->alarm_level > ALARM_LEVEL_LOW)
PWMSetPulse(buzzer, 10000); {
break; continue;
case ALARM_LEVEL_BELOW_MEDIUM: }
PWMSetPeriod(buzzer, 2); if(buzz->alarm_state == ALARM_OFF)
PWMSetPulse(buzzer, 10000); {
break; PWMSetDutyRatio(buzzer, 0);
case ALARM_LEVEL_MEDIUM: }
PWMSetPeriod(buzzer, 3); else
PWMSetPulse(buzzer, 10000); {
break; PWMSetDutyRatio(buzzer, buzz->loudness);
case ALARM_LEVEL_ABOVE_MEDIUM: switch (buzz->octave)
PWMSetPeriod(buzzer, 4); {
PWMSetPulse(buzzer, 10000); case OCTAVE_1:
break; PWMSetPeriod(buzzer, (float)1/DoFreq);
case ALARM_LEVEL_HIGH: break;
PWMSetPeriod(buzzer, 5); case OCTAVE_2:
PWMSetPulse(buzzer, 10000); PWMSetPeriod(buzzer, (float)1/ReFreq);
break; break;
case OCTAVE_3:
default: PWMSetPeriod(buzzer, (float)1/MiFreq);
PWMSetPulse(buzzer, 0); break;
break; case OCTAVE_4:
PWMSetPeriod(buzzer, (float)1/FaFreq);
break;
case OCTAVE_5:
PWMSetPeriod(buzzer, (float)1/SoFreq);
break;
case OCTAVE_6:
PWMSetPeriod(buzzer, (float)1/LaFreq);
break;
case OCTAVE_7:
PWMSetPeriod(buzzer, (float)1/SiFreq);
break;
}
break;
}
} }
} }

View File

@ -1,7 +1,60 @@
#ifndef BUZZER_H #ifndef BUZZER_H
#define BUZZER_H #define BUZZER_H
#include "daemon.h" #include "bsp_pwm.h"
#define BUZZER_DEVICE_CNT 5
#define DoFreq 523
#define ReFreq 587
#define MiFreq 659
#define FaFreq 698
#define SoFreq 784
#define LaFreq 880
#define SiFreq 988
typedef enum
{
OCTAVE_1 = 0,
OCTAVE_2,
OCTAVE_3,
OCTAVE_4,
OCTAVE_5,
OCTAVE_6,
OCTAVE_7,
OCTAVE_8,
}octave_e;
typedef enum
{
ALARM_LEVEL_HIGH = 0,
ALARM_LEVEL_ABOVE_MEDIUM,
ALARM_LEVEL_MEDIUM,
ALARM_LEVEL_BELOW_MEDIUM,
ALARM_LEVEL_LOW,
}AlarmLevel_e;
typedef enum
{
ALARM_OFF = 0,
ALARM_ON,
}AlarmState_e;
typedef struct
{
AlarmLevel_e alarm_level;
octave_e octave;
float loudness;
}Buzzer_config_s;
typedef struct
{
float loudness;
octave_e octave;
AlarmLevel_e alarm_level;
AlarmState_e alarm_state;
}BuzzzerInstance;
void buzzer_init(); void buzzer_init();
void BuzzerTask();
BuzzzerInstance *BuzzerRegister(Buzzer_config_s *config);
void AlarmSetStatus(BuzzzerInstance *buzzer, AlarmState_e state);
#endif // !BUZZER_H #endif // !BUZZER_H

20
modules/alarm/buzzer.md Normal file
View File

@ -0,0 +1,20 @@
# buzzer
用于拉响蜂鸣器警报
## 使用范例
```c
Buzzer_config_s buzzer_config ={
.alarm_level = ALARM_LEVEL_HIGH, //设置警报等级 同一状态下 高等级的响应
.loudness= 0.4, //设置响度
.octave= OCTAVE_1, // 设置音阶
};
robocmd_alarm = BuzzerRegister(&buzzer_config);
AlarmSetStatus(robocmd_alarm, ALARM_ON);
AlarmSetStatus(robocmd_alarm, ALARM_OFF);
```
@todo: 将音阶改为可供选择的搭配 如 Do Re Mi Fa So La Si 自由组合 用户只需输入字符串"DoReMi"即可

View File

@ -18,8 +18,6 @@ DaemonInstance *DaemonRegister(Daemon_Init_Config_s *config)
instance->callback = config->callback; instance->callback = config->callback;
instance->temp_count = config->init_count == 0 ? 100 : config->init_count; // 默认值为100,初始计数 instance->temp_count = config->init_count == 0 ? 100 : config->init_count; // 默认值为100,初始计数
instance->alarm_state = config->alarm_state;
instance->alarm_level = config->alarm_level;
instance->temp_count = config->reload_count; instance->temp_count = config->reload_count;
daemon_instances[idx++] = instance; daemon_instances[idx++] = instance;
return instance; return instance;
@ -49,11 +47,6 @@ void DaemonTask()
{ {
dins->callback(dins->owner_id); // module内可以将owner_id强制类型转换成自身类型从而调用特定module的offline callback dins->callback(dins->owner_id); // module内可以将owner_id强制类型转换成自身类型从而调用特定module的offline callback
// @todo 为蜂鸣器/led等增加离线报警的功能,非常关键! // @todo 为蜂鸣器/led等增加离线报警的功能,非常关键!
if(dins->alarm_state == ALARM_ON)
{
BuzzerPlay(dins->alarm_level);
}
} }
} }
} }

View File

@ -8,28 +8,13 @@
/* 模块离线处理函数指针 */ /* 模块离线处理函数指针 */
typedef void (*offline_callback)(void *); 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结构体定义 */ /* daemon结构体定义 */
typedef struct daemon_ins typedef struct daemon_ins
{ {
uint16_t reload_count; // 重载值 uint16_t reload_count; // 重载值
offline_callback callback; // 异常处理函数,当模块发生异常时会被调用 offline_callback callback; // 异常处理函数,当模块发生异常时会被调用
alarm_state_e alarm_state; // 蜂鸣器状态
alarm_level_e alarm_level; //警报级别
uint16_t temp_count; // 当前值,减为零说明模块离线或异常 uint16_t temp_count; // 当前值,减为零说明模块离线或异常
void *owner_id; // daemon实例的地址,初始化的时候填入 void *owner_id; // daemon实例的地址,初始化的时候填入
} DaemonInstance; } DaemonInstance;
@ -40,9 +25,7 @@ typedef struct
uint16_t reload_count; // 实际上这是app唯一需要设置的值? uint16_t reload_count; // 实际上这是app唯一需要设置的值?
uint16_t init_count; // 上线等待时间,有些模块需要收到主控的指令才会反馈报文,或pc等需要开机时间 uint16_t init_count; // 上线等待时间,有些模块需要收到主控的指令才会反馈报文,或pc等需要开机时间
offline_callback callback; // 异常处理函数,当模块发生异常时会被调用 offline_callback callback; // 异常处理函数,当模块发生异常时会被调用
alarm_state_e alarm_state; // 蜂鸣器状态
alarm_level_e alarm_level; //警报级别
void *owner_id; // id取拥有daemon的实例的地址,如DJIMotorInstance*,cast成void*类型 void *owner_id; // id取拥有daemon的实例的地址,如DJIMotorInstance*,cast成void*类型
} Daemon_Init_Config_s; } Daemon_Init_Config_s;