From: kyz Date: Thu, 17 Jul 2025 08:25:09 +0000 (+0800) Subject: 增加风速风向检测 X-Git-Url: http://kyz.pub/gitweb/?a=commitdiff_plain;h=refs%2Fheads%2Fmain;p=yn%2Falarmmonitor_mcu.git 增加风速风向检测 --- diff --git a/MDK-ARM/mcu.uvprojx b/MDK-ARM/mcu.uvprojx index 633adf6..199fd44 100644 --- a/MDK-ARM/mcu.uvprojx +++ b/MDK-ARM/mcu.uvprojx @@ -1134,11 +1134,6 @@ 1 ..\user\user_filter.c - - user_temperture.c - 1 - ..\user\user_temperture.c - user_lora.c 1 @@ -1149,6 +1144,11 @@ 1 ..\user\user_alarm_sound.c + + user_wind_sensor.c + 1 + ..\user\user_wind_sensor.c + diff --git a/user/user.h b/user/user.h index 469191b..a3843a6 100644 --- a/user/user.h +++ b/user/user.h @@ -7,6 +7,9 @@ #define SW_VER_MAJOR 1 #define SW_VER_MINOR 0 +#define USER_SHELL_USE_UART1 0 +#define WIND_SENSOR_USE_UART1 1 + typedef osMutexId_t user_lock_t; @@ -21,10 +24,18 @@ typedef struct{ int8_t state; float db; }noise_t; + +typedef struct{ + int8_t state; //0:normal, 1:strength err 2:direction err + float strength; + float direction; +}wind_t; + typedef struct{ int8_t state; float freq; }sound_t; + typedef struct{ bd_state_t state; float lat; @@ -35,6 +46,7 @@ typedef struct{ noise_t noise; sound_t sound; position_t pos; + wind_t wind; }sensor_info_t; typedef struct{ @@ -48,6 +60,7 @@ void user_adc_start( void ); // void user_noise_info_update( float db, int8_t state ); +void user_wind_info_update( float strength, float direction, int8_t state ); void user_sound_info_update( float freq, int8_t state ); void user_position_update( float lat, float lon, bd_state_t state ); void user_sensor_info_read( sensor_info_t *info ); diff --git a/user/user_adc.c b/user/user_adc.c index 650f244..18ad0fa 100644 --- a/user/user_adc.c +++ b/user/user_adc.c @@ -130,9 +130,9 @@ int32_t user_tcp_write_data( uint8_t *data, uint32_t data_sz ); extern UART_HandleTypeDef huart1; extern int g_adc_debug; -static uint16_t freq_list[1024]; -static uint16_t amp_list[1024]; -static uint32_t num = 0; +//static uint16_t freq_list[1024]; +//static uint16_t amp_list[1024]; +//static uint32_t num = 0; extern int alarm_data_add( uint16_t in_freq, uint16_t in_amp ); extern int DetectAlarm( void ); @@ -143,6 +143,7 @@ int alarm_detected = 0; int adc_debug( int enable ) { g_adc_debug = enable; + return 0; } extern void init_fft(void); diff --git a/user/user_app.c b/user/user_app.c index 3c34880..1400551 100644 --- a/user/user_app.c +++ b/user/user_app.c @@ -9,10 +9,12 @@ extern void user_sound_task_start( void ); extern void user_noise_task_start( void ); extern void user_bd_task_start( void ); extern void user_lora_task_start( void ); +extern void user_wind_task_start( void ); extern void bd_rx_cb( void ); extern void shell_uart_rx_cb( void ); extern void lora_rx_cb( void ); +extern void user_rs485_rx_cb( void ); extern uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len); extern uint32_t adc_finished; @@ -26,7 +28,11 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if( huart->Instance == USART1 ){ //USART1 debug shell +#if USER_SHELL_USE_UART1 shell_uart_rx_cb(); +#elif WIND_SENSOR_USE_UART1 + user_rs485_rx_cb(); +#endif }else if( huart->Instance == USART2 ){ //USART2 BD Tx/Rx @@ -47,7 +53,13 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) int fputc( int ch, FILE *fd ) { +#if USER_SHELL_USE_UART1 + +#elif WIND_SENSOR_USE_UART1 + +#else HAL_UART_Transmit( &huart1, (uint8_t*)&ch, 1, 100 ); +#endif return 0; } @@ -73,6 +85,15 @@ void user_noise_info_update( float db, int8_t state ) user_unlock( g_dev.lock ); } +void user_wind_info_update( float strength, float direction, int8_t state ) +{ + user_lock( g_dev.lock ); + g_dev.sensor.wind.strength = strength; + g_dev.sensor.wind.direction = direction; + g_dev.sensor.wind.state = state; + user_unlock( g_dev.lock ); +} + void user_sound_info_update( float freq, int8_t state ) { user_lock( g_dev.lock ); @@ -134,7 +155,7 @@ void main_task(void *argument) HAL_GPIO_WritePin( PB15_ETH_RESET_GPIO_Port, PB15_ETH_RESET_Pin, GPIO_PIN_RESET ); osDelay( 100 ); HAL_GPIO_WritePin( PB15_ETH_RESET_GPIO_Port, PB15_ETH_RESET_Pin, GPIO_PIN_SET ); - + MX_LWIP_Init(); Tcp_Server_Init(); @@ -144,6 +165,7 @@ void main_task(void *argument) user_sound_task_start(); user_lora_task_start(); user_bd_task_start(); + user_wind_task_start(); delay_ticks = xTaskGetTickCount(); for(;;) diff --git a/user/user_filter.c b/user/user_filter.c index 231c537..0ee90fa 100644 --- a/user/user_filter.c +++ b/user/user_filter.c @@ -7,8 +7,8 @@ #define BLOCK_SIZE 1 /* µ÷ÓÃÒ»´Îarm_fir_f32´¦ÀíµÄ²ÉÑùµã¸öÊý */ #define NUM_TAPS 21 /* Â˲¨Æ÷ϵÊý¸öÊý */ -static float32_t testInput[TEST_LENGTH_SAMPLES]; /* ²ÉÑùµã */ -static float32_t testOutput[TEST_LENGTH_SAMPLES]; /* Â˲¨ºóµÄÊä³ö */ +//static float32_t testInput[TEST_LENGTH_SAMPLES]; /* ²ÉÑùµã */ +//static float32_t testOutput[TEST_LENGTH_SAMPLES]; /* Â˲¨ºóµÄÊä³ö */ static float32_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1]; /* ״̬»º´æ£¬´óСnumTaps + blockSize - 1*/ @@ -33,11 +33,11 @@ void user_data_lp( uint16_t *in, uint16_t *out, int len ) { uint32_t i; arm_fir_instance_f32 S; - float32_t *inputF32, *outputF32; +// float32_t *inputF32, *outputF32; /* ³õʼ»¯ÊäÈëÊä³ö»º´æÖ¸Õë */ - inputF32 = &testInput[0]; - outputF32 = &testOutput[0]; +// inputF32 = &testInput[0]; +// outputF32 = &testOutput[0]; /* ³õʼ»¯½á¹¹ÌåS */ arm_fir_init_f32(&S, diff --git a/user/user_location.c b/user/user_location.c index 8bc29aa..07785f5 100644 --- a/user/user_location.c +++ b/user/user_location.c @@ -36,6 +36,7 @@ int bd_log( int on ) { bd_log_flag = on; printf( "bd log out %s\n", on?"enable":"disable" ); + return 0; } void bd_parse( void ) diff --git a/user/user_noise_sensor.c b/user/user_noise_sensor.c index 83b480b..ad029a4 100644 --- a/user/user_noise_sensor.c +++ b/user/user_noise_sensor.c @@ -2,9 +2,6 @@ #include #include "user.h" -extern void user_noise_update( float noise, int8_t state ); - -static float noise_value = 0; static int8_t noise_state = 0; static uint8_t recv_byte; diff --git a/user/user_tcp.c b/user/user_tcp.c index d7d46e7..7c69d29 100644 --- a/user/user_tcp.c +++ b/user/user_tcp.c @@ -48,7 +48,7 @@ int32_t user_tcp_write_data_sync( uint8_t *data, uint32_t data_sz ) if( tcp_connected_pcb ){ for( int i=0; i<5; i++ ){ - err = tcp_write( tcp_connected_pcb, data, data_sz, 0); //TCP_WRITE_FLAG_COPY + err = (err_enum_t)tcp_write( tcp_connected_pcb, data, data_sz, 0); //TCP_WRITE_FLAG_COPY if( err == ERR_OK ){ ret = 0; tcp_output( tcp_connected_pcb ); @@ -114,7 +114,7 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ); 55AA 0002 0001 0008 XX... XXXX CMD: 0001: query LEN 0 query period:5s - 0002: resp LEN 8 , DATA: noise + alarm + 0002: resp LEN 32 , DATA: noise + alarm + pos + wind 1001: Lora Tx 1002: Lora Rx 2001: BD Tx @@ -201,7 +201,10 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) resp_data[10] = sensor.pos.state; memcpy( &resp_data[11], &sensor.pos.lat, 4 ); memcpy( &resp_data[15], &sensor.pos.lon, 4 ); - memset( &resp_data[19], 0, 13 ); + resp_data[19] = sensor.wind.state; + memcpy( &resp_data[20], &sensor.wind.strength, 4 ); + memcpy( &resp_data[24], &sensor.wind.direction, 4 ); + memset( &resp_data[28], 0, 4 ); resp_sz = wrap_pkt( resp, rx_pkt_sn, 0x0101, resp_data, 32 ); break; case 0x0003: //lora transmit diff --git a/user/user_temperture.c b/user/user_temperture.c deleted file mode 100644 index 49b6e31..0000000 --- a/user/user_temperture.c +++ /dev/null @@ -1,125 +0,0 @@ - -#include "user.h" - -#if 0 - -/* -ÅäÖÃTIM3µÄCOUNTER À´×ö¼ÆÊ± µ¥Î»us¡£ -GPIO PE3 ÅäÖÃΪÖжϴ¥·¢£¬ÖжϺ¯ÊýÖмǼ¸ßµçƽ±£³Öʱ¼ä - -¶ÁȡָÁ ·¢ËÍÆô¶¯Ö¸ÁÆô¶¯TIM3 Æô¶¯IOÖжϣ¬ÖжÏÖжÁÈ¡TIM3µÄcounter¼Ä´æÆ÷£¬¼Ç¼¸ßµçƽ±£³Öʱ¼ä -¶ÁȡָÁîºóÑÓ³Ù10ms £¬¼ì²é¼Ç¼Êý¾Ý£¬½âÎöζÈÄ£¿éµÄ»ØÓ¦±¨ÎÄÀ´»ñÈ¡ÎÂʪ¶ÈÊý¾Ý -*/ - -extern TIM_HandleTypeDef htim3; - -int user_temperature_read( float *tem, float * hum ); - -static void temp_read_start( void ); -static void temp_read_stop( void ); - -static float s_tem; -static float s_hum; - -struct tem_bit{ - uint8_t h_us; -}; - -struct tem_bit tem_bit_data[ 50 ]; //ack + bit x 40 + end -int tem_bit_count; - - -void tem_int_cb( void ) -{ - int us; - int level; - - level = HAL_GPIO_ReadPin( DQ_IN_GPIO_Port, DQ_IN_Pin )==GPIO_PIN_SET?1:0; - us = __HAL_TIM_GET_COUNTER( &htim3 ); - - if( tem_bit_count > 42 ) - return; - - if( level==0 ){ - tem_bit_data[tem_bit_count].h_us = us; - }else{ - tem_bit_count++; - } - - __HAL_TIM_SET_COUNTER( &htim3, 0 ); -} - -void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) -{ - if( GPIO_Pin==DQ_IN_Pin ){ - tem_int_cb(); - } -} - -void tem_pin_cfg_out( int level ) -{ - GPIO_InitTypeDef GPIO_InitStruct = {0}; - - HAL_NVIC_DisableIRQ(EXTI3_IRQn); - HAL_GPIO_WritePin(SYS_LED_GPIO_Port, SYS_LED_Pin, GPIO_PIN_RESET); - - /*Configure GPIO pin : DQ_IN_Pin */ - GPIO_InitStruct.Pin = DQ_IN_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(DQ_IN_GPIO_Port, &GPIO_InitStruct); - - -} - -void tem_pin_cfg_input( void ) -{ - GPIO_InitTypeDef GPIO_InitStruct = {0}; - - /*Configure GPIO pin : DQ_IN_Pin */ - GPIO_InitStruct.Pin = DQ_IN_Pin; - GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING; - GPIO_InitStruct.Pull = GPIO_NOPULL; - HAL_GPIO_Init(DQ_IN_GPIO_Port, &GPIO_InitStruct); - __HAL_GPIO_EXTI_CLEAR_IT(DQ_IN_Pin); - -} - -void temp_read_start( void ) -{ - tem_pin_cfg_out( 0 ); - osDelay(20); - tem_pin_cfg_input(); - - tem_bit_count = 0; - __HAL_TIM_SET_COUNTER( &htim3, 0 ); - HAL_TIM_Base_Start( &htim3 ); - - HAL_NVIC_SetPriority(EXTI3_IRQn, 5, 0); - HAL_NVIC_EnableIRQ(EXTI3_IRQn); -} - -void temp_read_stop( void ) -{ - HAL_NVIC_DisableIRQ(EXTI3_IRQn); - HAL_TIM_Base_Stop( &htim3 ); - __HAL_TIM_SET_COUNTER( &htim3, 0 ); - -} - -int user_temperature_read( float *tem, float * hum ) -{ - temp_read_start(); - osDelay(10*100); - temp_read_stop(); - - if( tem_bit_count > 42 ){ - - *tem = s_tem; - *hum = s_hum; - return 0; - } - return -1; -} - -#endif diff --git a/user/user_wind_sensor.c b/user/user_wind_sensor.c new file mode 100644 index 0000000..c487fdb --- /dev/null +++ b/user/user_wind_sensor.c @@ -0,0 +1,219 @@ + +#include "user.h" + +extern UART_HandleTypeDef huart1; + +#define WIND_STRENGTH_ADDR 1 +#define WIND_DIRECTION_ADDR 2 +#define WIND_SENSOR_BAUD 9600 + +/****************************************************************************************** +·çËÙ/·çÏò´«¸ÐÆ÷ĬÈϲÎÊý µØÖ·1 ²¨ÌØÂÊ 4800 + +д«¸ÐÆ÷ͨ¹ýPC¹¤¾ßÅäÖòÎÊý +1. ·çËÙ´«¸ÐÆ÷ µØÖ· 1 ²¨ÌØÂÊ 9600 +2. ·çÏò´«¸ÐÆ÷ µØÖ· 2 ²¨ÌØÂÊ 9600 + +·çËÙ´«¸ÐÆ÷ ¼Ä´æÆ÷0 µ¥Î» 0.1 m/s +·çÏò´«¸ÐÆ÷ ¼Ä´æÆ÷0 µ¥Î» 0.1 ¶È +******************************************************************************************/ + +static void wind_sensor_uart_init( void ) +{ + huart1.Instance = USART1; + huart1.Init.BaudRate = WIND_SENSOR_BAUD; + huart1.Init.WordLength = UART_WORDLENGTH_8B; + huart1.Init.StopBits = UART_STOPBITS_1; + huart1.Init.Parity = UART_PARITY_NONE; + huart1.Init.Mode = UART_MODE_TX_RX; + huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; + huart1.Init.OverSampling = UART_OVERSAMPLING_16; + if (HAL_UART_Init(&huart1) != HAL_OK) + { + Error_Handler(); + } + +} + +static int32_t rs485_rx_flag = 0; +void user_rs485_rx_cb( void ) +{ + rs485_rx_flag = 1; +} + +int32_t rs485_tx( uint8_t *data, uint16_t sz ) +{ + uint8_t dummy; + + while( HAL_UART_Receive( &huart1, &dummy, 1, 0 ) == HAL_OK){ + }; + + HAL_UART_Transmit( &huart1, data, sz, 100 ); + + return 0; +} + +int32_t rs485_rx( uint8_t *rx_data, uint8_t rx_n, uint32_t timeout_ms ) +{ + uint32_t tick; + rs485_rx_flag = 0; + tick = xTaskGetTickCount(); + + if( HAL_UART_Receive_IT( &huart1, rx_data, rx_n ) != HAL_OK){ + return -1; + }; + while( xTaskGetTickCount() - tick < timeout_ms && !rs485_rx_flag){ + osDelay(2); + } + if( !rs485_rx_flag ){ + HAL_UART_AbortReceive_IT(&huart1); + } + return rs485_rx_flag?0:-1; +} + +int32_t mb_master_write_reg( uint8_t device_addr, uint16_t reg, uint16_t reg_value ) +{ + uint8_t cmd[16]; + uint16_t crc; + uint16_t n = 0; + //int32_t rx_n; + + cmd[n++] = device_addr; + cmd[n++] = 0x06; + cmd[n++] = (reg>>8)&0xff; + cmd[n++] = reg &0xff; + cmd[n++] = (reg_value>>8)&0xff; + cmd[n++] = reg_value &0xff; + crc = usMBCRC16( cmd, n ); + cmd[n++] = crc &0xff; + cmd[n++] = (crc>>8)&0xff; + + uint8_t rx_data[50]; + rs485_tx( cmd, n ); + if( rs485_rx( rx_data, 8, 200 ) == 0 ){ + }else{ + //printf( "rx err!\n" ); + } + return 0; +} + + +int32_t mb_master_read_regs( uint8_t device_addr, uint16_t reg, uint16_t* reg_values, uint16_t reg_nr ) +{ + uint8_t cmd[16]; + uint16_t crc; + uint16_t n = 0; + int32_t rx_n; + + cmd[n++] = device_addr; + cmd[n++] = 0x03; + cmd[n++] = (reg>>8)&0xff; + cmd[n++] = reg &0xff; + cmd[n++] = (reg_nr>>8)&0xff; + cmd[n++] = reg_nr &0xff; + crc = usMBCRC16( cmd, n ); + cmd[n++] = crc &0xff; + cmd[n++] = (crc>>8)&0xff; + + uint8_t rx_data[50]; + memset( rx_data, 0x55, 20 ); + rs485_tx( cmd, n ); + rx_n = reg_nr*2+5; + if( rs485_rx( rx_data, rx_n, 200 ) == 0 ){ + if( rx_data[0] == device_addr && rx_data[1] == 3 && rx_data[2] == reg_nr*2 ){ + //check modbus-crc + if( usMBCRC16( rx_data, rx_n-2 ) == rx_data[rx_n-2]| ( rx_data[rx_n-1]<<8) ){ + for( int i=0; i 10 )?1:0; + wind_state = ( strength_err > 10 )?2:0; + user_wind_info_update( strength, direction, wind_state ); + } +} + +/* Definitions for defaultTask */ +osThreadId_t windTaskHandle; +const osThreadAttr_t windTask_attributes = { + .name = "windTask", + .stack_size = 1024, + .priority = (osPriority_t) osPriorityNormal, +}; + +void user_wind_task_start( void ) +{ + windTaskHandle = osThreadNew(user_wind_task, NULL, &windTask_attributes); +} + diff --git a/user_debug/test_main.c b/user_debug/test_main.c index 3f5cebd..c07d22e 100644 --- a/user_debug/test_main.c +++ b/user_debug/test_main.c @@ -1,9 +1,6 @@ #include "user.h" #include "shell.h" -#define SW_VER_MAJOR 1 -#define SW_VER_MINOR 2 - extern UART_HandleTypeDef huart1; #define huart_shell huart1 @@ -16,7 +13,6 @@ static uint32_t shell_rx_wr; static uint32_t shell_rx_rd; static uint8_t shell_rx_byte; -static uint8_t uart_rx_flag; void shell_uart_rx_start( void ) { @@ -105,7 +101,9 @@ const osThreadAttr_t shellTask_attributes = { void user_shell_start( void ) { +#if USER_SHELL_USE_UART1 shellTaskHandle = osThreadNew(user_shell_task, NULL, &shellTask_attributes); +#endif }