Îļþ´Ó keil/uwb_tag.c ¸´ÖÆ |
| | |
| | | #include "mk_power.h" |
| | | #include "mk_sleep_timer.h" |
| | | #include "lib_ranging.h" |
| | | #include "dw_tag.h" |
| | | #include "dw_app.h" |
| | | #include "board.h" |
| | | #include "mk_calib.h" |
| | | |
| | | extern int TagRange(void); |
| | | extern Operation_step UWB_work_state; |
| | | #if defined(MK_SS_TWR_DW_INIT) |
| | | |
| | | #define CALCULATE_TIMECOUNT |
| | | extern int TagRange(void); |
| | | |
| | | /* Ranging period: 1s */ |
| | | #define RANGING_PERIOD_MS (1000) |
| | | |
| | |
| | | #define POLL_DELAY 100U //yuan100U |
| | | |
| | | /* Receive response timeout */ |
| | | #define RESP_RX_TIMEOUT_US 2000U //Yuan500 10mssuccess 300jixian |
| | | #define RESP_RX_TIMEOUT_US 2500U //Yuan500 10mssuccess 300jixian |
| | | |
| | | /* Field index in frame */ |
| | | #define MSG_SEQ_NUM_IDX 2 |
| | | #define FINAL_MSG_POLL_TX_TS_IDX 10 |
| | | #define FINAL_MSG_RESP_RX_TS_IDX 14 |
| | | #define FINAL_MSG_FINAL_TX_TS_IDX 18 |
| | | #define DELAY_DEFAULT 1000 |
| | | #define DELAY_DEFAULT 50000 |
| | | #define DELAY_BETWEEN_TWO_FRAME_UUS 600 //yuan1400 |
| | | #define HALF_SECOND_TIME 62400000 |
| | | |
| | | static uint16_t CmpTagInList(uint16_t tagid); |
| | | uint16_t CmpTagInList(uint16_t tagid); |
| | | uint16_t AddNewTagIntoList(uint16_t tagid); |
| | | void AnchorListUpdate(void); |
| | | void Rank_ANchor_list_by_dis(void); |
| | |
| | | uint8_t phy_work_mode; /* PHY_TX / PHY_RX / PHT_TX|PHY_RX */ |
| | | struct UWB_CONFIG_T phy_cfg; |
| | | }; |
| | | extern uint32_t dev_id; |
| | | extern uint8_t group_id; |
| | | uint32_t dev_id; |
| | | uint8_t group_id; |
| | | /* Default communication configuration. */ |
| | | static struct mk_uwb_configure config = {//yuan |
| | | .phy_work_mode = (uint8_t)(PHY_TX | PHY_RX), |
| | |
| | | static uint8_t receive_flag=0; |
| | | static uint8_t rec_tag_index,tag_num_tosend; |
| | | |
| | | static uint32_t start_receive_count,end_receive_count,poll_timeout,current_count,temp_resp; |
| | | uint32_t start_receive_count,end_receive_count,poll_timeout,current_count,temp_resp; |
| | | uint16_t taglist_total_num,taglist_current_index; //å½ååè¡¨æ»æ°é |
| | | uint8_t taglist_keeptime[MAX_TAG_LIST_NUM]; //æ¯ä¸ªæ ç¾åæ´»æ¶é´ |
| | | uint16_t taglist_id[MAX_TAG_LIST_NUM],taglist_dist[MAX_TAG_LIST_NUM]; //æ ç¾è·ç¦»åæ ç¾çID |
| | |
| | | uint8_t anclost_times=0 , mainbase_lost_count=0,exsistbase_list[MAX_NEARBASE_NUM],get_newbase=0,nearbase_num,last_nearbase_num,next_nearbase_num; |
| | | uint16_t nearbaseid_list[MAX_NEARBASE_NUM],mainbase_id,true_nearbase_idlist[MAX_NEARBASE_NUM],rec_nearbaseid,rec_nearbasepos; |
| | | uint16_t u16_nearbase_distlist[MAX_NEARBASE_NUM]; |
| | | uint32_t temp_tag_count1=0; |
| | | uint32_t temp_tag_count2=0; |
| | | uint32_t temp_tag_count3=0; |
| | | uint32_t temp_count1=0; |
| | | uint32_t temp_count2=0; |
| | | uint32_t temp_count3=0; |
| | | uint32_t temp_count4=0; |
| | | uint32_t tempflag=0; |
| | | int32_t freq_offset,freq_offset_filter; |
| | | int temp_flag,poll_tx_num,resp_rx_num; |
| | | extern double distance; |
| | | uint32_t count1,count2; |
| | | extern double distance_tag; |
| | | enum SIMPLE_FSM_T |
| | | { |
| | | SIMPLE_IDLE = 0, |
| | |
| | | memcpy(rx_buf, rx_report->pkt_data, rx_length); |
| | | /* Calculate rx timestamp */ |
| | | resp_rx_ts_i64 = ranging_rx_time_correct(rx_report); |
| | | temp_tag_count1=phy_timer_count_get(); |
| | | temp_count1=phy_timer_count_get(); |
| | | //è·ååå°ç«¯æ¶éåå·® |
| | | resp_rx_num++; |
| | | freq_offset=phy_freq_offset_get(); |
| | | freq_offset_filter=average_filter(freq_offset);//è·åé¢å |
| | | // int32_t ppm = freq_offset_filter / (int32_t)(ch_center_freq_map[UWB_CH_NUM] * 1e-6); |
| | | // calib_xtal38m4_load_cap_auto_tune(ppm);//å©ç¨çµå®¹è°æ´æ¶æ¯éé
é¢ååºå¨å®æ´çä¸å
ä¹åè°æ´ï¼éè¦å
³éXTAL_AUTO_TUNE_EN å®å®ä¹é¿å
æ¶å
ä¸éæ ¡åå¯¼è´æµè·é误 |
| | | |
| | | // freq_offset_filter=average_filter(freq_offset);//è·åé¢å |
| | | //int32_t ppm = freq_offset_filter / (int32_t)(ch_center_freq_map[UWB_CH_NUM] * 1e-6); |
| | | //calib_xtal38m4_load_cap_auto_tune(ppm);//å©ç¨çµå®¹è°æ´æ¶æ¯éé
é¢ååºå¨å®æ´çä¸å
ä¹åè°æ´ï¼éè¦å
³éXTAL_AUTO_TUNE_EN å®å®ä¹é¿å
æ¶å
ä¸éæ ¡åå¯¼è´æµè·é误 |
| | | //LOG_INFO(TRACE_MODULE_APP, "poll_tx_num is %d,resp_rx_num is %d,distance is %lf\r\n",poll_tx_num,resp_rx_num,distance); |
| | | receive_flag=1; |
| | | |
| | |
| | | rx_state1=rx_report->err_code; |
| | | rx_length = 0; |
| | | receive_flag=2; |
| | | temp_tag_count2=phy_timer_count_get(); |
| | | temp_count2=phy_timer_count_get(); |
| | | |
| | | } |
| | | |
| | |
| | | temp_flag=0; |
| | | return; |
| | | } |
| | | count1=temp_tag_count1; |
| | | count1=temp_count1; |
| | | temp_flag=1; |
| | | } |
| | | } |
| | |
| | | true_nearbase_distlist[j++]=nearbase_distlist[i]; |
| | | |
| | | } |
| | | nearbase_num = next_nearbase_num;//æ´æ°ç°ååºç«æ° |
| | | last_nearbase_num = next_nearbase_num;//æ´æ°å½ååºç«æ° |
| | | } |
| | | nearbase_num = next_nearbase_num;//æ´æ°ç°ååºç«æ° |
| | | last_nearbase_num = next_nearbase_num;//æ´æ°å½ååºç«æ° |
| | | } |
| | | |
| | | void Rank_ANchor_list_by_dis(void)//æç
§ç®åçåºç«è·ç¦»è¿è¡å泡æåºç¨ä¸´æ¶æ°ç»æ¹å¼å°æè¿çæ¾å°æåé¢ï¼æè¿çæ¾å°æåé¢ |
| | |
| | | exsistbase_list[i] = true_exsistbase_list[i]; |
| | | } |
| | | } |
| | | static uint16_t CmpTagInList(uint16_t tagid)//æ¾è¿ä¸ªæ°ä¸å
çåºç«å¨ä¸å¨ç°åçtaglistä¸å¨çè¯å¯¹åºä½ç½®æ´æ°åæ´»æ¶é´ï¼ä¸å¨å°±å¢å ï¼ç¡®ä¿æææ°éå°äºæå¤§è®¾ç½®æ°ï¼ |
| | | uint16_t CmpTagInList(uint16_t tagid)//æ¾è¿ä¸ªæ°ä¸å
çåºç«å¨ä¸å¨ç°åçtaglistä¸å¨çè¯å¯¹åºä½ç½®æ´æ°åæ´»æ¶é´ï¼ä¸å¨å°±å¢å ï¼ç¡®ä¿æææ°éå°äºæå¤§è®¾ç½®æ°ï¼ |
| | | { |
| | | uint16_t temp; |
| | | for(uint8_t i=0;i<taglist_total_num;i++) |
| | |
| | | temp = AddNewTagIntoList(tagid); |
| | | return temp; |
| | | } |
| | | |
| | | uint32_t count_index; |
| | | int tt=1; |
| | | int TagRange(void) |
| | | { |
| | | // The following peripherals will be initialized in the uwb_open function |
| | |
| | | temp_tag_num=0;//ä¸´æ¶æ°é为0 |
| | | poll_tx_en_start_u32 = phy_timer_count_get()+US_TO_PHY_TIMER_COUNT(POLL_DELAY);//åéå¿
é¡»è¦å»¶æ¶åéæå¯ä»¥ç¨äºæµè·å¦åç«å³åéä¼è·åæ¶é´æ³ä¸å¯¹ï¼éè¦è®¡ç®ç¨åºè¿è¡æ¶é´ï¼é¿å
设置è¿å»æ¶é´ |
| | | tempflag=uwb_tx(uwb_sendbuffer,13+4*nearbase_num,1,poll_tx_en_start_u32);//ç«å³åé |
| | | |
| | | //gpio_pin_set(IO_PIN_5);//æµè¯ |
| | | #ifdef BOXING |
| | | gpio_pin_set(IO_PIN_5);//æµè¯ |
| | | #endif |
| | | //LOG_INFO(TRACE_MODULE_APP, "è¿å
¥æµè·",poll_tx_num,resp_rx_num,distance); |
| | | poll_tx_ts_i64 = ranging_tx_time_correct(poll_tx_en_start_u32 + phy_shr_duration());//ä¿®æ£æ¶é´æ³ |
| | | temp_tag_count1=phy_timer_count_get(); |
| | | temp_count1=phy_timer_count_get(); |
| | | while(mac_is_busy());//çå¾
åé宿 |
| | | temp_tag_count2=phy_timer_count_get(); |
| | | gpio_pin_clr(IO_PIN_5);//æµè¯ |
| | | gpio_pin_set(IO_PIN_5);//æµè¯ |
| | | temp_count2=phy_timer_count_get(); |
| | | resp_rx_en_start_u32 = poll_tx_en_start_u32+ US_TO_PHY_TIMER_COUNT(POLL_TX_TO_RESP_RX_DLY_US);//设置åéå¤å°såæ¥æ¶ætargetç¬ç«æ¶é´èµ·ç¹ç®+ US_TO_PHY_TIMER_COUNT(POLL_TX_TO_RESP_RX_DLY_US为åéåè¦å¤ä¹
å¼å¯æ¥æ¶æ¶é´ |
| | | temp_resp=resp_rx_en_start_u32; |
| | | //tempflag=uwb_rx(1, resp_rx_en_start_u32, RESP_RX_TIMEOUT_US);//å¼å¯æ¥åå¹¶è®¾ç½®æ¥æ¶è¶
æ¶ |
| | | tempflag=uwb_rx(0,0, RESP_RX_TIMEOUT_US); |
| | | |
| | | receive_flag =0; |
| | | |
| | | start_receive_count=phy_timer_count_get(); |
| | | poll_timeout=nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS)+US_TO_PHY_TIMER_COUNT(RESP_RX_TIMEOUT_US);//å¤ä¸ä¸ªå¤0.4msé»è®¤0.4ms计ç®ä¸º0.125*4*100000,é»è®¤å¼å¯1mss |
| | | end_receive_count=start_receive_count+poll_timeout; |
| | | |
| | | if(end_receive_count>=UINT32_MAX) |
| | | {end_receive_count-=UINT32_MAX;} |
| | | current_count=phy_timer_count_get(); |
| | | count_index=end_receive_count+HALF_SECOND_TIME; |
| | | // if(tt){ |
| | | while(current_count<end_receive_count||current_count>end_receive_count+HALF_SECOND_TIME)//å¾ªç¯æ¥åå
ä½,è¥ä¸º124.8K忝+62400000 |
| | | { |
| | | |
| | | current_count=phy_timer_count_get();//䏿巿°å½å计æ°å¨å¼ |
| | | temp_tag_count2=phy_timer_count_get(); |
| | | temp_count2=phy_timer_count_get(); |
| | | while(mac_is_busy());//çå¾
æ¥æ¶å®æ |
| | | temp_tag_count3=phy_timer_count_get(); |
| | | temp_count3=phy_timer_count_get(); |
| | | if(receive_flag==1)//æåæ¥æ¶æ°æ® |
| | | { |
| | | receive_flag=0; |
| | | |
| | | tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//ç«å³å¼å¯æ¥å并设置0è¶
æ¶ |
| | | //æ¥æ¶æåå夿æ¯å¦ä¸ºåä¸ç» |
| | | if (rx_buf[MESSAGE_TYPE_IDX] == MBX_RESPONSE&&!memcmp(&rx_buf[TAG_ID_IDX],&dev_id,2)&&rx_buf[GROUP_ID_IDX]==group_id) //å¤ææ¥æ¶å°çæ°æ®æ¯å¦æ¯responseæ°æ® |
| | | { |
| | | memcpy(&rec_nearbaseid,&rx_buf[ANCHOR_ID_IDX],2); |
| | | if(last_nearbase_num==0) |
| | | { |
| | | nearbaseid_list[0]=rec_nearbaseid; |
| | | nearbase_num=1; |
| | | exsistbase_list[0]=KEEP_TIMES; |
| | | SetANCTimestap(0,&rx_buf[RESP_MSG_POLL_RX_TS_IDX],&rx_buf[RESP_MSG_RESP_TX_TS_IDX],(uint32_t)resp_rx_ts_i64,&rx_buf[RESP_MSG_ANC_DISTOFFSET],test2,(uint32_t)poll_tx_ts_i64);//è¿ä¸ªæ¯æ¹è¿çåæ° |
| | | } |
| | | // if(rec_nearbaseid==nearbaseid_list[0]) |
| | | // { |
| | | // exsistbase_list[0]=KEEP_TIMES; |
| | | // SetANCTimestap(0,&rx_buf[RESP_MSG_POLL_RX_TS_IDX],&rx_buf[RESP_MSG_RESP_TX_TS_IDX],(uint32_t)resp_rx_ts_i64,&rx_buf[RESP_MSG_ANC_DISTOFFSET],test2);//è¿ä¸ªæ¯æ¹è¿çåæ° |
| | | // } |
| | | else |
| | | { |
| | | memcpy(&rec_nearbaseid,&rx_buf[ANCHOR_ID_IDX],2); |
| | | rec_nearbasepos=FindNearBasePos(rec_nearbaseid); |
| | | SetANCTimestap(rec_nearbasepos,&rx_buf[RESP_MSG_POLL_RX_TS_IDX],&rx_buf[RESP_MSG_RESP_TX_TS_IDX],(uint32_t)resp_rx_ts_i64,&rx_buf[RESP_MSG_ANC_DISTOFFSET],test2,(uint32_t)poll_tx_ts_i64); |
| | | if(rec_nearbasepos>=last_nearbase_num) //åç°æ°çåºç« |
| | | if(rec_nearbasepos>=nearbase_num) //åç°æ°çåºç« |
| | | { |
| | | //get_newbase=1; |
| | | nearbase_num++; |
| | | nearbaseid_list[rec_nearbasepos] = rec_nearbaseid; |
| | | } |
| | | exsistbase_list[rec_nearbasepos]=KEEP_TIMES;//æ´æ°åæ´»æ¶é´ |
| | | } |
| | | |
| | | memset(rx_buf, 0, sizeof(rx_buf));//æ¸
ç©ºæ¥æ¶å°çç¨å®çæ°ç» |
| | | rx_length=0;//æ¸
0é¿åº¦ |
| | | // uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//ç«å³å¼å¯æ¥å并设置0è¶
æ¶ |
| | | // while(mac_is_busy()); |
| | | //break;//æåæ¥æ¶å°±éåº |
| | | |
| | | } |
| | | tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//ç«å³å¼å¯æ¥å并设置0è¶
æ¶ |
| | | //gpio_pin_clr(IO_PIN_5);//æµè¯ |
| | | |
| | | |
| | | // #ifdef BOXING |
| | | // gpio_pin_clr(IO_PIN_5);//æµè¯ |
| | | // #endif |
| | | }else if(receive_flag==2){//æ¥æ¶åºé |
| | | receive_flag=0; |
| | | tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//ç«å³å¼å¯æ¥å并设置0è¶
æ¶ |
| | | temp_tag_count3=phy_timer_count_get(); |
| | | temp_count3=phy_timer_count_get(); |
| | | // while(mac_is_busy()); |
| | | temp_tag_count1=phy_timer_count_get(); |
| | | temp_count1=phy_timer_count_get(); |
| | | } |
| | | //gpio_pin_clr(IO_PIN_5);//æµè¯ |
| | | #ifdef BOXING |
| | | gpio_pin_clr(IO_PIN_5);//æµè¯ |
| | | gpio_pin_set(IO_PIN_5);//æµè¯ |
| | | #endif |
| | | } |
| | | // } |
| | | #ifdef BOXING |
| | | gpio_pin_clr(IO_PIN_5);//æµè¯ |
| | | #endif |
| | | delay_us(1); |
| | | uwb_rx_force_off(1); |
| | | //dwt_forecetrxoff(); |
| | | CalculateDists(poll_tx_ts_i64); //计ç®è·ç¦»å¹¶æ´æ°è·ç¦»è¡¨ |
| | | AnchorListUpdate();//æ´æ°åæ´»åºç«å表 |
| | | Rank_ANchor_list_by_dis();//åæ³¡æåºéæ°æè·ç¦»éæ°æååºç«å表æå°è·ç¦»åè´§åºç«æ¾å°æåé¢ |
| | | LOG_INFO(TRACE_MODULE_APP,"åºç«IDï¼%X,è·ç¦»: %d\r\n",rec_nearbaseid,nearbase_distlist[0]); |
| | | // Rank_ANchor_list_by_dis();//åæ³¡æåºéæ°æè·ç¦»éæ°æååºç«å表æå°è·ç¦»åè´§åºç«æ¾å°æåé¢ |
| | | } |
| | | |
| | | |
| | | void UWB_Tag_Poll(void) |
| | | { |
| | | switch(UWB_work_state) |
| | | { |
| | | case LINK_SUCCESS: |
| | | case SEARCH_DEV: |
| | | case UN_BIND: |
| | | { |
| | | uwb_led_on(); |
| | | TagRange(); |
| | | uwb_led_off(); |
| | | } |
| | | break; |
| | | } |
| | | // update_led_power_state();//æ´æ°ç¯ç¶æ |
| | | } |
| | | #endif |