mirror of
https://gitee.com/dlmu-cone/bf_original_balance_chassis
synced 2026-07-24 03:27:45 +08:00
云台跟随,连续发射
This commit is contained in:
@@ -1,93 +1,31 @@
|
||||
#include "bsp_pwm.h"
|
||||
#include "buzzer.h"
|
||||
#include "bsp_dwt.h"
|
||||
#include "string.h"
|
||||
#include "main.h"
|
||||
|
||||
static PWMInstance *buzzer;
|
||||
// static uint8_t idx;
|
||||
static BuzzzerInstance *buzzer_list[BUZZER_DEVICE_CNT] = {0};
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
static uint8_t tmp_warning_level = 0;
|
||||
|
||||
/**
|
||||
* @brief 蜂鸣器初始化
|
||||
*
|
||||
*/
|
||||
void BuzzerInit()
|
||||
{
|
||||
PWM_Init_Config_s buzzer_config = {
|
||||
.htim = &htim4,
|
||||
.channel = TIM_CHANNEL_3,
|
||||
.dutyratio = 0,
|
||||
.period = 0.001,
|
||||
};
|
||||
buzzer = PWMRegister(&buzzer_config);
|
||||
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);
|
||||
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, 0);
|
||||
}
|
||||
|
||||
BuzzzerInstance *BuzzerRegister(Buzzer_config_s *config)
|
||||
void BuzzerOn( )
|
||||
{
|
||||
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)
|
||||
{
|
||||
buzz = buzzer_list[i];
|
||||
if (buzz->alarm_level > ALARM_LEVEL_LOW)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if (buzz->alarm_state == ALARM_OFF)
|
||||
{
|
||||
PWMSetDutyRatio(buzzer, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
PWMSetDutyRatio(buzzer, buzz->loudness);
|
||||
switch (buzz->octave)
|
||||
{
|
||||
case OCTAVE_1:
|
||||
PWMSetPeriod(buzzer, (float)1 / DoFreq);
|
||||
break;
|
||||
case OCTAVE_2:
|
||||
PWMSetPeriod(buzzer, (float)1 / ReFreq);
|
||||
break;
|
||||
case OCTAVE_3:
|
||||
PWMSetPeriod(buzzer, (float)1 / MiFreq);
|
||||
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;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
static int16_t temp = 4000 ;
|
||||
if(temp < 1000)
|
||||
{
|
||||
BuzzerOff();
|
||||
return;
|
||||
}
|
||||
__HAL_TIM_PRESCALER(&htim4,(int)(temp/1000));
|
||||
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, 10000);
|
||||
temp -= 5 ;
|
||||
|
||||
}
|
||||
|
||||
void BuzzerOff(void)
|
||||
{
|
||||
__HAL_TIM_SetCompare(&htim4, TIM_CHANNEL_3, 0);
|
||||
tmp_warning_level = 0;
|
||||
}
|
||||
|
||||
@@ -1,60 +1,10 @@
|
||||
#ifndef BUZZER_H
|
||||
#define BUZZER_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;
|
||||
#ifndef BSP_BUZZER_H
|
||||
#define BSP_BUZZER_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void BuzzerInit();
|
||||
void BuzzerTask();
|
||||
BuzzzerInstance *BuzzerRegister(Buzzer_config_s *config);
|
||||
void AlarmSetStatus(BuzzzerInstance *buzzer, AlarmState_e state);
|
||||
#endif // !BUZZER_H
|
||||
extern void BuzzerOn();
|
||||
extern void BuzzerOff(void);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user