167 lines
4.3 KiB
C
167 lines
4.3 KiB
C
#include "anemometer.h"
|
|
#include "FreeRTOS.h"
|
|
|
|
int16_t adc_val[ADC_VAL_LEN];
|
|
int16_t adc_val1[ADC_VAL_LEN];
|
|
|
|
Weather_param weather_info;
|
|
|
|
|
|
/*
|
|
C B A
|
|
0 0 0 X0
|
|
0 0 1 X1
|
|
0 1 0 X2
|
|
0 1 1 X3
|
|
1 0 0 X4
|
|
1 0 1 X5
|
|
*/
|
|
void change_channel(uint32_t channel)
|
|
{
|
|
// 复位全部通道IO
|
|
HAL_GPIO_WritePin(GPIOC, GPIO_PWM_C_Pin|GPIO_PWM_B_Pin|GPIO_PWM_A_Pin, GPIO_PIN_RESET);
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_RX_A_Pin|GPIO_RX_B_Pin|GPIO_RX_C_Pin, GPIO_PIN_RESET);
|
|
|
|
switch(channel)
|
|
{
|
|
case 0x01:
|
|
//HAL_GPIO_WritePin(GPIOC, GPIO_RX_A_Pin, GPIO_PIN_SET);
|
|
// 接受通道
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_RX_A_Pin, GPIO_PIN_SET);
|
|
break;
|
|
|
|
case 0x02:
|
|
HAL_GPIO_WritePin(GPIOC, GPIO_PWM_A_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_RX_B_Pin, GPIO_PIN_SET);
|
|
break;
|
|
|
|
case 0x03:
|
|
HAL_GPIO_WritePin(GPIOC, GPIO_PWM_B_Pin, GPIO_PIN_SET);
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_RX_A_Pin|GPIO_RX_B_Pin, GPIO_PIN_SET);
|
|
break;
|
|
|
|
case 0x04:
|
|
HAL_GPIO_WritePin(GPIOC, GPIO_PWM_A_Pin|GPIO_PWM_B_Pin, GPIO_PIN_SET);
|
|
//HAL_GPIO_WritePin(GPIOB, GPIO_RX_A_Pin, GPIO_PIN_SET);
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
void play_one_measure(int16_t* result_data,uint32_t len)
|
|
{
|
|
// 重新初始化PWM定时器
|
|
MX_TIM15_Init();
|
|
//MX_TIM6_Init();
|
|
// adc驱动定时器,保证每次使用前处于停止状态
|
|
HAL_TIM_Base_Stop(&htim6);
|
|
// 计数器复位
|
|
__HAL_TIM_SET_COUNTER(&htim6,0);
|
|
// adc dma初始化
|
|
HAL_ADC_Start_DMA(&hadc1,(uint32_t *)result_data,len);
|
|
//禁止全局中断
|
|
__set_PRIMASK(1);
|
|
|
|
// 停止定时器
|
|
__HAL_TIM_DISABLE(&htim7);
|
|
// 重置计数器
|
|
__HAL_TIM_SET_COUNTER(&htim7,0);
|
|
|
|
// 发送超声波驱动信号
|
|
HAL_TIM_PWM_Start(&htim15,TIM_CHANNEL_1);
|
|
// 延时300us
|
|
|
|
// 启定时器
|
|
__HAL_TIM_ENABLE(&htim7);
|
|
// 等待定时到达
|
|
while(__HAL_TIM_GET_COUNTER(&htim7)<REV_MUTE_DELAY);
|
|
// 关闭定时
|
|
__HAL_TIM_DISABLE(&htim7);
|
|
//HAL_TIM_PWM_Stop(&htim15,TIM_CHANNEL_1);
|
|
// 开启ADC
|
|
HAL_TIM_Base_Start(&htim6);
|
|
// 使能全局中断
|
|
__set_PRIMASK(0);
|
|
// 等待adc采集完成
|
|
HAL_Delay(1);
|
|
|
|
}
|
|
|
|
void calculate_param(Weather_param *parm ,uint32_t direction , int16_t *adc_buf1,int16_t *adc_buf2,uint32_t len)
|
|
{
|
|
float32_t tofx,tofy,dtof;
|
|
|
|
tofx = cal_tof(adc_buf1,len)/ADC_SAMP_RATE+REV_MUTE_DELAY-1.66;
|
|
tofy = cal_tof(adc_buf2,len)/ADC_SAMP_RATE+REV_MUTE_DELAY-1.66;
|
|
dtof = cal_dtof(adc_buf1,adc_buf2,len)/ADC_SAMP_RATE+0.1;
|
|
|
|
parm->wind_c = 118946/2*(1/tofx+1/tofy);
|
|
// v = L*dtof/2/tofx/tofy/cos
|
|
if(direction == WIND_DIRECTION_X)
|
|
parm->wind_velocity_x = 118946*dtof/1.41422/tofx/tofx;
|
|
else
|
|
parm->wind_velocity_y = 118946*dtof/1.41422/tofx/tofx;
|
|
|
|
}
|
|
|
|
|
|
void wind_task(void const * argument)
|
|
{
|
|
/* USER CODE BEGIN wind_task */
|
|
/* Infinite loop */
|
|
for(;;)
|
|
{
|
|
__HAL_TIM_DISABLE(&htim16);
|
|
// 耗时 22ms
|
|
__HAL_TIM_SET_COUNTER(&htim16,0);
|
|
// 开启定时器
|
|
__HAL_TIM_ENABLE(&htim16);
|
|
|
|
// 通道1发送 通道2接收
|
|
change_channel(0x01);
|
|
// 等待通道切换稳定
|
|
// adc开启采集数据有个固定的延时,这里取消等待
|
|
//HAL_Delay(0);
|
|
// 发送pwm 并启动adc采集数据
|
|
play_one_measure(adc_val,ADC_VAL_LEN);
|
|
|
|
// 通道2发送 通道1接收
|
|
change_channel(0x02);
|
|
// 等待通道切换稳定
|
|
//HAL_Delay(0);
|
|
// 发送pwm 并启动adc采集数据
|
|
play_one_measure(adc_val1,ADC_VAL_LEN);
|
|
|
|
calculate_param(&weather_info,WIND_DIRECTION_X,adc_val,adc_val1,ADC_VAL_LEN);
|
|
|
|
// 通道3发送 通道4接收
|
|
change_channel(0x03);
|
|
// 等待通道切换稳定
|
|
//HAL_Delay(0);
|
|
// 发送pwm 并启动adc采集数据
|
|
play_one_measure(adc_val,ADC_VAL_LEN);
|
|
|
|
// 通道4发送 通道3接收
|
|
change_channel(0x04);
|
|
// 等待通道切换稳定
|
|
//HAL_Delay(0);
|
|
// 发送pwm 并启动adc采集数据
|
|
play_one_measure(adc_val1,ADC_VAL_LEN);
|
|
|
|
calculate_param(&weather_info,WIND_DIRECTION_Y,adc_val,adc_val1,ADC_VAL_LEN);
|
|
|
|
weather_info.wind_velocity = sqrtf(weather_info.wind_velocity_x*weather_info.wind_velocity_x + weather_info.wind_velocity_y*weather_info.wind_velocity_y);
|
|
// 关闭定时器
|
|
//__HAL_TIM_DISABLE(&htim16);
|
|
//HAL_Delay(1);
|
|
osDelay(3);
|
|
}
|
|
/* USER CODE END wind_task */
|
|
}
|
|
|
|
|
|
|
|
|