add alarm sound detect
authorkyz <kongyuzhong@google.com>
Sun, 15 Jun 2025 09:20:52 +0000 (17:20 +0800)
committerkyz <kongyuzhong@google.com>
Sun, 15 Jun 2025 09:20:52 +0000 (17:20 +0800)
Core/Inc/main.h
MDK-ARM/mcu.uvprojx
user/user_adc.c
user/user_filter.c
user/user_location.c
user/user_tcp.c
user_debug/test_main.c

index 414f43a9c8d862b6f2be77dec44548cdde3dddba..971c2657a960ed3a867d356f98ea4dda3bae4be8 100644 (file)
@@ -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
index 6e128c57c460981420ab1b8eb533b8fc9b45a799..633adf6b3d654dd72484f9447fc3c4aa699d1871 100644 (file)
               <FileType>1</FileType>
               <FilePath>..\user\user_lora.c</FilePath>
             </File>
+            <File>
+              <FileName>user_alarm_sound.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\user\user_alarm_sound.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>
index cdd92d85a8b9ba60f8bcb0aead75cda835937b87..5e68f041223618ff34c5b87df3e7f9ba2cb4413a 100644 (file)
@@ -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<blockSize; i++ ){
@@ -30,6 +31,8 @@ float signalFreq;
 float32_t maxValue;
 uint32_t maxIndex;
 
+static uint32_t last_freq_index = 0;
+
 static float32_t input[FFT_SIZE*2];  // Êµ²¿+Ð鲿
 static float32_t output[FFT_SIZE];
 static arm_cfft_instance_f32 fft_inst;
@@ -41,19 +44,15 @@ uint32_t adc_finished = 0;
 //ÓÃfft¼ÆË㵱ǰÉùÒôADCÊý¾ÝµÄ400HzÒÔϵÄÉùÒôƵÂÊ£¬µ¥Î»Hz
 uint32_t adc_fft( uint16_t * sound_freq, uint16_t *sound_amplitude )
 {
-       static float  last_freq_f  = 0;
        uint32_t freq;
-#if 1
+#if 0
        //ÏȵÍͨÂ˲¨´¦Àí£¬È¥µô¸ßƵÐÅÏ¢
-       user_data_lp(adc_raw, out_u16,1024 );
+       user_data_lp(adc_raw_copy, out_u16,FFT_SIZE );
        //CDC_Transmit_FS( (uint8_t*)out_u16, 1024*2 );
 #else
-       memcpy( out_u16, adc_raw, 1024*2 );
+       memcpy( out_u16, adc_raw_copy, 1024*2 );
 #endif
 
-#if 1
-       uint32_t start = HAL_GetTick();
-
        // ³õʼ»¯FFTʵÀý
        arm_cfft_init_f32(&fft_inst, FFT_SIZE);
 
@@ -70,50 +69,53 @@ uint32_t adc_fft( uint16_t * sound_freq, uint16_t *sound_amplitude )
        arm_cmplx_mag_f32(input, output, FFT_SIZE);
 
        // Ñ°ÕÒ×î´ó·ùÖµ¶ÔÓ¦µÄƵÂÊ
-       //ÕâÀï²ÉÑùÂÊÊÇ10Khz£¬²ÉÑùÊý¾ÝÊÇ0.1Ãë 1000¸öµã£¬
-       //FFTÊä³öÊý¾Ý·Ö±æÂÊÊÇ10Hz£¬µÚÒ»¸öµãÊÇÖ±Á÷·ÖÁ¿£¬ ÕâÀïֻͳ¼ÆÇ°Ãæ50¸öµã¾ÍÊÇ500HzÊý¾Ý
-       arm_max_f32(&output[1], 42, &maxValue, &maxIndex);
+       //ÕâÀï²ÉÑùÂÊÊÇ10Khz£¬²ÉÑùÊý¾ÝÊÇ0.1Ãë Ã¿´ÎFFT_SIZ 512¸öµã¶ÔÓ¦512ms
+       //FFTÊä³öÊý¾Ý·Ö±æÂÊÊÇ10000/512=19.53Hz
+       //µÚÒ»¸öµãÊÇÖ±Á÷·ÖÁ¿£¬ ÕâÀïֻͳ¼ÆÇ°Ãæ45¸öµã¾ÍÊÇ900HzÒÔÏÂÊý¾Ý
+#if 1
+       arm_max_f32(&output[1], 45, &maxValue, &maxIndex);
+       
+       if( last_freq_index > 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, &amplitude );
+                       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( &amp_list[0], &amp_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 );
+                               
+                       }
                }
        }
 }
index be5213eb8819f72e533064db96446f4c93ee61be..231c537955855b658968fe40b6b2e4df948023d2 100644 (file)
@@ -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 )
 {
index 636ba8289d844e9ba95b51072c7975fa9a360aae..8bc29aac20df06efc634410c32d3dea2b8542a96 100644 (file)
@@ -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,
index bab15ad553ae990a35033d752ea8a70512ffa99c..15825667171ada131c6c17a13f88fd12e26b0442 100644 (file)
@@ -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);
         }
 
index 0f56e444ace151850f5b42519fe162b92a878dd3..e957033e68a3548650ad1b25afc2d1cf0c0aa435 100644 (file)
@@ -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"