From 28f60b8464e7b6e9311a1dbffcf14c928ecad9d8 Mon Sep 17 00:00:00 2001 From: kyz Date: Sun, 15 Jun 2025 17:20:52 +0800 Subject: [PATCH] add alarm sound detect --- Core/Inc/main.h | 2 +- MDK-ARM/mcu.uvprojx | 5 ++ user/user_adc.c | 162 +++++++++++++++++++++++++++++------------ user/user_filter.c | 13 +++- user/user_location.c | 10 +++ user/user_tcp.c | 18 ++++- user_debug/test_main.c | 7 +- 7 files changed, 164 insertions(+), 53 deletions(-) diff --git a/Core/Inc/main.h b/Core/Inc/main.h index 414f43a..971c265 100644 --- a/Core/Inc/main.h +++ b/Core/Inc/main.h @@ -90,7 +90,7 @@ void Error_Handler(void); #define SYS_LED_GPIO_Port GPIOE /* USER CODE BEGIN Private defines */ - +///#define TCP_MSS 2000 /* USER CODE END Private defines */ #ifdef __cplusplus diff --git a/MDK-ARM/mcu.uvprojx b/MDK-ARM/mcu.uvprojx index 6e128c5..633adf6 100644 --- a/MDK-ARM/mcu.uvprojx +++ b/MDK-ARM/mcu.uvprojx @@ -1144,6 +1144,11 @@ 1 ..\user\user_lora.c + + user_alarm_sound.c + 1 + ..\user\user_alarm_sound.c + diff --git a/user/user_adc.c b/user/user_adc.c index cdd92d8..5e68f04 100644 --- a/user/user_adc.c +++ b/user/user_adc.c @@ -4,15 +4,16 @@ extern ADC_HandleTypeDef hadc1; extern TIM_HandleTypeDef htim2; -#define FFT_SIZE 1024 +#define FFT_SIZE (1024/2) #define SAMPLE_RATE 10000 uint16_t adc_raw[FFT_SIZE]; +uint16_t adc_raw_copy[FFT_SIZE]; uint16_t out_u16[FFT_SIZE]; + void arm_max_f32_2( const float32_t * pSrc, uint32_t blockSize, float32_t * pResult, uint32_t * pIndex) { - float *p = (float*)pSrc; int i=10; float max_v = *pSrc; for( i=10; i 5 && maxIndex > last_freq_index+3 ){ + //¼ì²éµ±Ç°ÆµÂÊÔÚ100HzÒÔÉÏ£¬Ð¼ÆËã×î´óƵÂʳ¬¹ýǰһ¸ö²ÉÑùµã60Hz£¬ÖØÐ¼ÆËã + //¿¼Âǵ½ÆµÂÊÊÇÁ¬Ðø±ä»¯µÄ£¬ÔÚԭƵÂÊǰ60HzºÍºó60Hz¼äÌáÈ¡×î´óÖµ£¬ + arm_max_f32(&output[ last_freq_index-3], 6, &maxValue, &maxIndex); //·Ö±æÂÊ10000/512=19.53 + maxIndex += last_freq_index-3; + } +#else + //ֻȡ600 µ½900 ¼äÊý¾Ý + arm_max_f32(&output[30], 15, &maxValue, &maxIndex); + maxIndex += 30; +#endif + + //·ùֵСÓÚ1000ʱºöÂÔ + if( maxValue < 5000 && maxIndex > 3 ) + maxIndex = 0; + + last_freq_index = maxIndex; // ¼ÆËãʵ¼ÊƵÂÊ - signalFreq = (maxIndex * SAMPLE_RATE) / FFT_SIZE; + //signalFreq = (maxIndex * SAMPLE_RATE) / FFT_SIZE; + + signalFreq = maxIndex; //printf("Dominant Frequency: %.2f Hz\r\n", signalFreq); if( sound_freq ) *sound_freq = signalFreq; if( sound_amplitude ) *sound_amplitude = maxValue; - fft_ticks += HAL_GetTick() - start; - #endif - - last_freq_f = (last_freq_f*9 + signalFreq*1)/10; - //last_freq_f = maxIndex; - freq = (uint32_t)last_freq_f; - uint32_t buf[2]; - buf[0] = freq=maxIndex*10; - buf[1] = maxValue; - if(1){ - uint32_t s=0; - uint32_t a = 0; - for( int i=0; i<1000; i++ ){ - if( i>0 && out_u16[i]>out_u16[i-1] ) - s+=out_u16[i]; - } - buf[1] = s/1000; - } - //CDC_Transmit_FS( (uint8_t*)&buf[0], 8 ); return freq; } - +static uint32_t n = 0; void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { if( hadc == &hadc1 ){ - + n++; + memcpy( adc_raw_copy, adc_raw, sizeof( adc_raw_copy ) ); adc_finished = 1; - + user_adc_start(); } } void user_adc_start( void ) { - adc_finished = 0; HAL_ADC_Start_DMA( &hadc1, (uint32_t*)&adc_raw[0], FFT_SIZE ); } @@ -122,25 +124,93 @@ void user_adc_init( void ) HAL_TIM_Base_Start( &htim2); } +uint16_t usMBCRC16( uint8_t* pucFrame, uint32_t usLen ); +int32_t user_tcp_write_data( uint8_t *data, uint32_t data_sz ); + + +extern int g_adc_debug; + +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 ); +extern void AnalyzeAlarmPattern(int32_t alarm_detected); +int alarm_detected = 0; + static void user_sound_task(void *argument) { uint16_t freq; uint16_t amplitude; - static uint16_t last_freq = 0; - TickType_t ticks; + //TickType_t ticks; + uint16_t tcp_tx_sn = 0; + uint16_t tcp_tx_sz; + uint16_t tcp_tx_crc; + user_adc_init(); - ticks = xTaskGetTickCount(); + //ticks = xTaskGetTickCount(); + + adc_finished = 0; + user_adc_start(); + while(1){ - ticks += 500; - osDelayUntil( ticks ); - user_adc_start(); - for( int i=0; i<50&&!adc_finished; i++ ){ - osDelay(10); - } - if( adc_finished ){ + if( !adc_finished ){ + osDelay(5); + }else{ + adc_finished = 0; + + static uint8_t tcp_tx_buf[FFT_SIZE*2+6]; + if(g_adc_debug){ //tx raw data for debug + memcpy( &tcp_tx_buf[10], &adc_raw_copy[0], sizeof( adc_raw_copy ) ); + } adc_fft( &freq, &litude ); + alarm_data_add( freq, amplitude ); + if( DetectAlarm() ){ + if( alarm_detected<0) + alarm_detected = 0; + else + alarm_detected ++; + }else{ + if( alarm_detected>0) + alarm_detected = 0; + else + alarm_detected --; + } + AnalyzeAlarmPattern(alarm_detected); + continue; + printf( "%d ", alarm_check() ); + if( num < 1024 ){ + freq_list[num] = freq; + amp_list[num] = amplitude; + num++; + }else{ + memmove( &freq_list[0], &freq_list[1], 1023*2); + memmove( &_list[0], &_list[1], 1023*2); + freq_list[1023] = freq; + amp_list[1023] = amplitude; + } user_sound_info_update( freq, 1 ); + if(g_adc_debug){ //tx raw data for debug + + tcp_tx_sn++; + tcp_tx_sz = FFT_SIZE*2+4; + tcp_tx_buf[0] = 0x55; + tcp_tx_buf[1] = 0xAA; + tcp_tx_buf[2] = (tcp_tx_sz>>8)&0xff; + tcp_tx_buf[3] = (tcp_tx_sz>>0)&0xff; + tcp_tx_buf[6] = (freq>>8)&0xff; + tcp_tx_buf[7] = (freq>>0)&0xff; + tcp_tx_buf[8] = (amplitude>>8)&0xff; + tcp_tx_buf[9] = (amplitude>>0)&0xff; + tcp_tx_crc = usMBCRC16( (uint8_t*)&tcp_tx_buf[6],tcp_tx_sz ); + tcp_tx_buf[4] = (tcp_tx_crc>>8)&0xff; + tcp_tx_buf[5] = (tcp_tx_crc>>0)&0xff; + + //user_tcp_write_data( &tcp_tx_buf[0], tcp_tx_sz + 6 ); + + } } } } diff --git a/user/user_filter.c b/user/user_filter.c index be5213e..231c537 100644 --- a/user/user_filter.c +++ b/user/user_filter.c @@ -3,9 +3,9 @@ -#define TEST_LENGTH_SAMPLES 1024 /* ²ÉÑùµãÊý */ +#define TEST_LENGTH_SAMPLES (1024/2) /* ²ÉÑùµãÊý */ #define BLOCK_SIZE 1 /* µ÷ÓÃÒ»´Îarm_fir_f32´¦ÀíµÄ²ÉÑùµã¸öÊý */ -#define NUM_TAPS 11 /* Â˲¨Æ÷ϵÊý¸öÊý */ +#define NUM_TAPS 21 /* Â˲¨Æ÷ϵÊý¸öÊý */ static float32_t testInput[TEST_LENGTH_SAMPLES]; /* ²ÉÑùµã */ static float32_t testOutput[TEST_LENGTH_SAMPLES]; /* Â˲¨ºóµÄÊä³ö */ @@ -19,6 +19,15 @@ const float32_t firCoeffs32LP[NUM_TAPS] = { 0.2594326735 }; +/*µÍͨ 900 */ +const float32_t firCoeffs32BP[NUM_TAPS] = { + -0,-0.000797945424, -0.00370071386,-0.006774489768,-0.004519771785, + 0.009753367864, 0.03979308903, 0.08286567032, 0.1291577518, 0.1649819911, + 0.1784820855, 0.1649819911, 0.1291577518, 0.08286567032, 0.03979308903, + 0.009753367864,-0.004519771785,-0.006774489768, -0.00370071386,-0.000797945424, + -0 +}; + float in_f, out_f; void user_data_lp( uint16_t *in, uint16_t *out, int len ) { diff --git a/user/user_location.c b/user/user_location.c index 636ba82..8bc29aa 100644 --- a/user/user_location.c +++ b/user/user_location.c @@ -31,6 +31,13 @@ static bd_state_t g_bd_st = BD_ST_INIT; //0:init 1:Ok 2:GOT_POS -1:er extern void user_position_update( float lat, float lon, bd_state_t state ); +static int bd_log_flag = 0; +int bd_log( int on ) +{ + bd_log_flag = on; + printf( "bd log out %s\n", on?"enable":"disable" ); +} + void bd_parse( void ) { int32_t h,m; @@ -51,6 +58,9 @@ void bd_parse( void ) g_bd_st = BD_ST_CONNECTED; char * rmc = strstr( (char*)bd_rx_buf, "RMC," ); if( rmc ){ + if( bd_log_flag ){ + printf( "%s", bd_rx_buf ); + } if(sscanf( rmc, "RMC,%2d%2d%lf,%c,%2d%lf,%c,%3d%lf,%c", &h,&m,&s, &st, diff --git a/user/user_tcp.c b/user/user_tcp.c index bab15ad..1582566 100644 --- a/user/user_tcp.c +++ b/user/user_tcp.c @@ -99,6 +99,8 @@ CMD: 3001: Laki Tx 3002: Laki Rx + FFFF: Debug get adc raw data + */ int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data, uint32_t data_sz ) @@ -126,6 +128,9 @@ int32_t wrap_pkt( uint8_t *buf, uint16_t sn, uint16_t cmd, uint8_t *data, uint32 extern void user_sensor_info_read( sensor_info_t *info ); extern int lora_uart_tx( uint8_t *tx_data, uint16_t tx_size ); +int g_adc_debug = 0; + +extern int32_t g_alarm; int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) { @@ -156,8 +161,10 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) pkt_crc = (p[8+pkt_data_sz]<<8)|p[8+pkt_data_sz+1]; if( (uint16_t)(recv_pkt_sn - rx_pkt_sn) < 0x8FFF && pkt_crc == usMBCRC16(p, 8+pkt_data_sz) ){ - if( tcp_connected_pcb != pcb ) + if( tcp_connected_pcb != pcb ){ tcp_connected_pcb = pcb; + g_adc_debug = 0; + } rx_pkt_sn = recv_pkt_sn; switch( cmd ){ @@ -166,7 +173,8 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) resp_data[0] = sensor.noise.state; memcpy( &resp_data[1], &sensor.noise.db, 4 ); resp_data[5] = sensor.sound.state; - memcpy( &resp_data[6], &sensor.sound.freq, 4 ); + //memcpy( &resp_data[6], &sensor.sound.freq, 4 ); + memcpy( &resp_data[6], &g_alarm, 4 ); resp_data[10] = sensor.pos.state; memcpy( &resp_data[11], &sensor.pos.lat, 4 ); memcpy( &resp_data[15], &sensor.pos.lon, 4 ); @@ -178,6 +186,9 @@ int32_t tcp_recv_cb(struct tcp_pcb *pcb, uint8_t *data, uint32_t len ) resp_data[0] = 1; resp_sz = wrap_pkt( resp, rx_pkt_sn, 0x0103, resp_data, 1 ); break; + case 0xFFFF: + g_adc_debug = 1; + break; } if( resp_sz ){ tcp_write( pcb, resp, resp_sz, TCP_WRITE_FLAG_COPY ); @@ -193,7 +204,7 @@ static err_t tcp_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *tcp_re //struct pbuf *tcp_send_pbuf; struct name *name = (struct name *)arg; uint8_t *rx_data; - printf( "tcp recv, pcb:%p\n", pcb ); +// printf( "tcp recv, pcb:%p\n", pcb ); if (tcp_recv_pbuf != NULL) { /* À©´óÊÕ·¢Êý¾ÝµÄ´°¿Ú */ @@ -233,6 +244,7 @@ static err_t tcp_server_recv(void *arg, struct tcp_pcb *pcb, struct pbuf *tcp_re mem_free(name); printf( "tcp_close: pcb:%p\n", tcp_connected_pcb ); tcp_connected_pcb = 0; + g_adc_debug = 0; return tcp_close(pcb); } diff --git a/user_debug/test_main.c b/user_debug/test_main.c index 0f56e44..e957033 100644 --- a/user_debug/test_main.c +++ b/user_debug/test_main.c @@ -36,7 +36,7 @@ void shell_uart_rx_cb( void ) return; } - shell_rx_buffer[shell_rx_wr] = shell_rx_byte; + shell_rx_buffer[shell_rx_wr] = rx_byte; shell_rx_wr = (shell_rx_wr+1)%sizeof( shell_rx_buffer); } @@ -83,6 +83,11 @@ void fan( int enable ) SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), fan, fan, fan ); +extern int bd_log( int enable ); + + +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC), bd_log, bd_log, bd_log ); + #include "FreeRTOS.h" #include "task.h" -- 2.34.1