#include "main.h" #include "stm32l0xx_it.h" #include "deca_device_api.h" #include "deca_regs.h" #include "dw_driver.h" #include #define SPEED_OF_LIGHT 299702547 #define UUS_TO_DWT_TIME 65536 #define POLL_TX_TO_RESP_RX_DLY_UUS 150 #define RESP_RX_TO_FINAL_TX_DLY_UUS 410 #define RESP_RX_TIMEOUT_UUS 2000 #define DELAY_BETWEEN_TWO_FRAME_UUS 400 #define POLL_RX_TO_RESP_TX_DLY_UUS 670 #define RESP_TX_TO_FINAL_RX_DLY_UUS 10 #define FINAL_RX_TIMEOUT_UUS 4300 #define WHITELIST1_START 0x25 #define WHITELIST1_END 0x26 #define MODBUS_ID1 0x11 #define FZ_NEARBASENUM_INDEX 10 #define FZ_NEARBASEID_INDEX 11 static uint64_t resp_tx_ts; static uint64_t final_rx_ts; static uint32_t status_reg,resp_tx_time,tagpos_binary; static uint8_t frame_len,recpoll_len,current_syncid,new_tagid,seize_anchor,result,g_start_sync_flag; static uint8_t rx_buffer[150]; static uint16_t anc_id_recv,tag_id_recv; static uint8_t flag_syncbase,sync_seq,synclost_timer,temp_nearbaspos; static uint16_t sync_timer; static uint8_t send_buffer[150]; static uint8_t frame_seq_nb2,battary,button,rec_nearbase_num,ancidlist_num; static uint16_t ancidlist_rec[20],ancidlist_send[20],rec_ancidlist[20],rec_ancdistlist[20]; static int16_t rec_antdelay; static uint16_t taglist_num=0,taglist_pos,tmp_time; static uint16_t tagid_list[TAG_NUM_IN_SYS]; static uint8_t tagofflinetime[TAG_NUM_IN_SYS]; static int32_t tagdist_list[TAG_NUM_IN_SYS]; static uint8_t tagpos_rec[50]; static uint64_t poll_rx_ts; uint8_t g_flag_Taggetdist[TAG_NUM_IN_SYS]; static uint8_t flag_tag_distsmooth[TAG_NUM_IN_SYS],misdist_num[TAG_NUM_IN_SYS]; static uint32_t poll_tx_ts, resp_rx_ts, final_tx_ts; static uint32_t poll_rx_ts_32, resp_tx_ts_32, final_rx_ts_32; static double Ra, Rb, Da, Db; static int64_t tof_dtu; static double tof; static double distance, dist_no_bias, dist_cm; extern dwt_config_t config; static double firstpath_power, rx_power,rec_firstpath_power; static double f1, f2, r1, r2; static uint16_t F1,F2,F3,N,C; const double B = 131072; const double A = 121.74; double min_power; static dwt_rxdiag_t d1; static int32_t hex_dist,his_dist[TAG_NUM_IN_SYS]; extern uint16_t g_Resttimer; int32_t filter_dist,g_Tagdist[TAG_NUM_IN_SYS]; double LOS(dwt_rxdiag_t *dia) { F1 = dia->firstPathAmp1; F2 = dia->firstPathAmp2; F3 = dia->firstPathAmp3; N = dia->rxPreamCount; C = dia->maxGrowthCIR; firstpath_power=10* log10((F1*F1+F2*F2+F3*F3)/(N*N))-A; rx_power=10*log10(C*B/(N*N))-A; // min_power = - 10 * log10((F1 *F1 + F2 * F2 + F3 * F3) / (C *B)); return min_power; } void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) { if(GPIO_Pin==GPIO_PIN_8) { Anchor_App(); } } static uint64_t get_tx_timestamp_u64(void) { uint8_t ts_tab[5]; uint64_t ts = 0; int i; dwt_readtxtimestamp(ts_tab); for (i = 4; i >= 0; i--) { ts <<= 8; ts |= ts_tab[i]; } return ts; } static uint64_t get_rx_timestamp_u64(void) { uint8_t ts_tab[5]; uint64_t ts = 0; int i; dwt_readrxtimestamp(ts_tab); for (i = 4; i >= 0; i--) { ts <<= 8; ts |= ts_tab[i]; } return ts; } static void final_msg_set_ts(uint8_t *ts_field, uint64_t ts) { int i; for (i = 0; i < FINAL_MSG_TS_LEN; i++) { ts_field[i] = (uint8_t) ts; ts >>= 8; } } static void final_msg_get_ts(const uint8_t *ts_field, uint32_t *ts) { int i; *ts = 0; for (i = 0; i < FINAL_MSG_TS_LEN; i++) { *ts += ts_field[i] << (i * 8); } } void TagListUpdate(void) { uint16_t i,j=0,temp[TAG_NUM_IN_SYS]; for(i=0;i> 8;//¼ÆËãResponse·¢ËÍʱ¼äT3¡£ dwt_setdelayedtrxtime(resp_tx_time);//ÉèÖÃResponse·¢ËÍʱ¼äT3 dwt_setrxaftertxdelay(RESP_TX_TO_FINAL_RX_DLY_UUS+(rec_nearbase_num+1-ancrec_nearbasepos)*DELAY_BETWEEN_TWO_FRAME_UUS);//ÉèÖ÷¢ËÍÍê³Éºó¿ªÆô½ÓÊÕÑÓ³Ùʱ¼ä dwt_setrxtimeout(FINAL_RX_TIMEOUT_UUS);//½ÓÊÕ³¬Ê±Ê±¼ä if(new_tagid) { tagdist_list[taglist_pos]=0x1ffff; memcpy(&send_buffer[DIST_IDX], &tagdist_list[taglist_pos], 4); }else{ memcpy(&send_buffer[DIST_IDX], &tagdist_list[taglist_pos], 4); } motorstate =0; if(tagdist_list[taglist_pos]CNT; poll_rx_ts_32 = (uint32_t)poll_rx_ts;//ʹÓÃ32λÊý¾Ý¼ÆËã resp_tx_ts_32 = (uint32_t)resp_tx_ts; final_rx_ts_32 = (uint32_t)final_rx_ts; Ra = (double)(resp_rx_ts - poll_tx_ts);//Tround1 = T4 - T1 Rb = (double)(final_rx_ts_32 - resp_tx_ts_32);//Tround2 = T6 - T3 Da = (double)(final_tx_ts - resp_rx_ts);//Treply2 = T5 - T4 Db = (double)(resp_tx_ts_32 - poll_rx_ts_32);//Treply1 = T3 - T2 tof_dtu = (int64_t)((Ra * Rb - Da * Db) / (Ra + Rb + Da + Db));//¼ÆË㹫ʽ tof = tof_dtu * DWT_TIME_UNITS; distance = tof * SPEED_OF_LIGHT;//¾àÀë=¹âËÙ*·ÉÐÐʱ¼ä #ifdef DW3000 dist_no_bias = distance; #else dist_no_bias = distance - dwt_getrangebias(config.chan, (float)distance, config.prf); //¾àÀë¼õÈ¥½ÃÕýϵÊý #endif dist_cm = dist_no_bias * 1000; //dis Ϊµ¥Î»ÎªcmµÄ¾àÀë dwt_readdiagnostics(&d1); LOS(&d1); /*--------------------------ÒÔÏÂΪ·Ç²â¾àÂß¼­------------------------*/ //dist_cm=33000; // LED0_BLINK; //ÿ³É¹¦Ò»´ÎͨѶÔòÉÁ˸һ´Î // UWBLED_BLINK; dis_after_filter=dist_cm; hex_dist = dist_cm+(int16_t)g_com_map[DIST_OFFSET]*10; if(recpoll_len==rec_nearbase_num*4+15) { hex_dist = dist_cm+(int16_t)g_com_map[DIST_OFFSET]*10-rec_antdelay*10; } g_flag_Taggetdist[taglist_pos]=0; if(hex_dist>-100000&&hex_dist<2000000) { if(abs(hex_dist-his_dist[taglist_pos])<10000) { flag_tag_distsmooth[taglist_pos] =1; }else{ flag_tag_distsmooth[taglist_pos] =0; } if(abs(hex_dist-his_dist[taglist_pos])<10000||misdist_num[taglist_pos]>3) { uint16_t checksum; g_Resttimer=0; // IWDG_Feed(); #ifdef TDFILTER NewTrackingDiffUpdate(taglist_pos, (float)hex_dist); filter_dist = pos_predict[taglist_pos]/10; filter_speed = vel_predict[taglist_pos]/10; newmeasure = 1; #else filter_dist=hex_dist/10; #endif misdist_num[taglist_pos]=0; tagdist_list[taglist_pos] = filter_dist; his_dist[taglist_pos]=hex_dist; if(hex_dist>0) { g_Tagdist[taglist_pos]=hex_dist/10; }else{ g_Tagdist[taglist_pos]= 0x2ffff; } usart_send[0]=0x55; usart_send[1]=0xAA; usart_send[2] = 1;//Õý³£Ä£Ê½ usart_send[3] = 17;//Êý¾Ý¶Î³¤¶È usart_send[4] = frame_seq_nb2;//Êý¾Ý¶Î³¤¶È memcpy(&usart_send[5],&tag_id_recv,2); memcpy(&usart_send[7],&dev_id,2); if(tagdist_list[taglist_pos]<=0) tagdist_list[taglist_pos]=0x2ffff; memcpy(&usart_send[9],&tagdist_list[taglist_pos],4); usart_send[13] = battary; usart_send[14] = button; usart_send[15] = firstpath_power; usart_send[16] = rx_power; checksum = Checksum_u16(&usart_send[2],17); memcpy(&usart_send[19],&checksum,2); USART_puts(usart_send,21); }else{ if(g_com_map[MODBUS_ID1]==1) { #ifdef DBGMSG_OUTPUT dbgmsg_len = sprintf(dbgmsg,"%s errordist:%d",__debug_info__,hex_dist); SendDbgMsg(dbgmsg,dbgmsg_len); #endif } // printf("%d",hex_dist); misdist_num[taglist_pos]++; } } } }else{ if(g_com_map[MODBUS_ID1]==1) { #ifdef DBGMSG_OUTPUT dbgmsg_len = sprintf(dbgmsg,"%s status_reg:%x",__debug_info__,status_reg); SendDbgMsg(dbgmsg,dbgmsg_len); #endif } //printf("%x/n",status_reg); dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); } } void Anchor_App(void) { u8 send_len,i; u16 tempid; uint32_t rec_syncid; dwt_setinterrupt( DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 0); // delay_us(t23); // HAL_NVIC_DisableIRQ(EXTI9_5_IRQn); status_reg = dwt_read32bitreg(SYS_STATUS_ID); if (status_reg & SYS_STATUS_RXFCG)//³É¹¦½ÓÊÕ { uint16_t tag_recv_interval; float temp_tagpos; dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG);//Çå³ý±ê־λ frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023;//»ñµÃ½ÓÊÕÊý¾Ý³¤¶È recpoll_len = frame_len; dwt_readrxdata(rx_buffer, frame_len, 0);//¶ÁÈ¡½ÓÊÕÊý¾Ý memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2); //½«ÊÕµ½µÄtag_id·Ö±ðдÈë¸÷´ÎͨѶµÄ°üÖУ¬Îª¶à±êǩͨѶ·þÎñ£¬·ÀÖ¹Ò»´ÎͨѶÖнÓÊÕµ½²»Í¬ID±êÇ©µÄÊý¾Ý memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],2); if(group_id== rx_buffer[GROUP_ID_IDX]) { switch(rx_buffer[MESSAGE_TYPE_IDX]) { case NEAR_POLL: frame_seq_nb2 = rx_buffer[SEQUENCE_IDX]; battary = rx_buffer[BATTARY_IDX]; button = rx_buffer[BUTTON_IDX]; rec_nearbase_num=rx_buffer[FZ_NEARBASENUM_INDEX]; memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],2); memcpy(&rec_ancidlist,&rx_buffer[FZ_NEARBASEID_INDEX],2*rec_nearbase_num); memcpy(&rec_ancdistlist,&rx_buffer[FZ_NEARBASEID_INDEX+2*rec_nearbase_num],2*rec_nearbase_num); memcpy(&rec_antdelay,&rx_buffer[FZ_NEARBASEID_INDEX+rec_nearbase_num*4],2); #ifdef USART_ALL_OUTPUT usart_send[2] = 0x0c;//Õý³£Ä£Ê½ usart_send[3] = 8+rec_nearbase_num*4;//Êý¾Ý¶Î³¤¶È usart_send[4] = frame_seq_nb2;//Êý¾Ý¶Î³¤¶È usart_send[5] = battary; usart_send[6] = button; usart_send[7] = rec_nearbase_num; memcpy(&usart_send[8],&tag_id_recv,2); memcpy(&usart_send[10],&rec_ancidlist,2*rec_nearbase_num); memcpy(&usart_send[10+rec_nearbase_num*2],&rec_ancdistlist,2*rec_nearbase_num); checksum = Checksum_u16(&usart_send[2],8+4*rec_nearbase_num); memcpy(&usart_send[10+4*rec_nearbase_num],&checksum,2); UART_PushFrame(usart_send,12+rec_nearbase_num*4); #endif taglist_pos=CmpTagInList(tag_id_recv); if(taglist_pos==taglist_num) { taglist_pos=taglist_num; tagid_list[taglist_num++]=tag_id_recv; new_tagid=1; }else{ new_tagid=0; } tagofflinetime[taglist_pos]=0; temp_tagpos=round((float)(sync_timer%g_com_map[COM_INTERVAL])/slottime); tagpos_rec[(uint8_t)temp_tagpos]=1; if(rec_nearbase_num>ancidlist_num) { ancidlist_num=rec_nearbase_num; memcpy(ancidlist_rec,&rx_buffer[FZ_NEARBASEID_INDEX],rec_nearbase_num*2); } for(i=0;i