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++ ){
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;
//ÓÃ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);
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 );
}
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 );
+
+ }
}
}
}
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 )
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 )
{
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 ){
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 );
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 );
//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)
{
/* À©´óÊÕ·¢Êý¾ÝµÄ´°¿Ú */
mem_free(name);
printf( "tcp_close: pcb:%p\n", tcp_connected_pcb );
tcp_connected_pcb = 0;
+ g_adc_debug = 0;
return tcp_close(pcb);
}