#include "mk_trace.h" #include "mk_uwb.h" #include "mk_phy.h" #include "mk_misc.h" #include "mk_power.h" #include "mk_sleep_timer.h" #include "lib_ranging.h" #include "dw_tag.h" #include "board.h" #include "mk_calib.h" extern int TagRange(void); extern Operation_step UWB_work_state; #define CALCULATE_TIMECOUNT /* Ranging period: 1s */ #define RANGING_PERIOD_MS (1000) /* This is the delay from the end of the poll frame transmission to the enable of the receiver */ #define POLL_TX_TO_RESP_RX_DLY_US 750U //yuan700 #define RESP_RX_TO_FINAL_TX_DLY_US 550U /* Poll delay: 1s */ #define POLL_DELAY 100U //yuan100U /* Receive response timeout */ #define RESP_RX_TIMEOUT_US 2000U //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_BETWEEN_TWO_FRAME_UUS 600 //yuan1400 #define HALF_SECOND_TIME 62400000 static uint16_t CmpTagInList(uint16_t tagid); uint16_t AddNewTagIntoList(uint16_t tagid); void AnchorListUpdate(void); void Rank_ANchor_list_by_dis(void); struct mk_uwb_configure { uint8_t phy_work_mode; /* PHY_TX / PHY_RX / PHT_TX|PHY_RX */ struct UWB_CONFIG_T phy_cfg; }; extern uint16_t dev_id; extern uint8_t group_id; /* Default communication configuration. */ static struct mk_uwb_configure config = {//yuan .phy_work_mode = (uint8_t)(PHY_TX | PHY_RX), .phy_cfg.ch_num = 5, /* Channel number. */ .phy_cfg.code_index = 9, /* TRX preamble code */ .phy_cfg.mean_prf = MEAN_PRF_64M, /* Mean prf 64/128/256M */ .phy_cfg.data_bit_rate = DATA_BR_6M8, /* Data rate 6.8M */ .phy_cfg.sync_sym = PREAM_LEN_128, /* Preamble duration, length of preamble 128 */ .phy_cfg.sfd_sym = NON_STD_NSFD5_8, /* Identifier for SFD sequence */ .phy_cfg.ranging_bit = 1, /* ranging bit set 1 */ .phy_cfg.trx_mode = TRX_MODE_15_4A, /* IEEE802.15.4z - BPRF mode */ .phy_cfg.sts_pkt_cfg = STS_PKT_CFG_0, /* SP0 Frame */ .phy_cfg.sts_segnum = STS_SEGNUM_BPRF_1, /* Number of STS segments in the frame */ .phy_cfg.sts_seglen = STS_SEGLEN_BPRF_64, /* Number of symbols in an STS segment */ .phy_cfg.rx_ant_id = UWB_RX_ANT_3, /* UWB RX antenna port */ }; ///* Default communication configuration. */ //static struct mk_uwb_configure config = { // .phy_work_mode = (uint8_t)(PHY_TX | PHY_RX), // .phy_cfg.ch_num = 5, /* Channel number. */ // .phy_cfg.code_index = 3, /* TX preamble code. */ // .phy_cfg.mean_prf = MEAN_PRF_16M, /* Data rate 6.8M */ // .phy_cfg.data_bit_rate = DATA_BR_110K, /* data rate 6.8M. */ // .phy_cfg.sync_sym = PREAM_LEN_1024, /* Preamble duration, length of preamble 128 */ // .phy_cfg.sfd_sym = NSFD_64, /* Identifier for SFD sequence */ // .phy_cfg.ranging_bit = 1, /* ranging bit set. */ // .phy_cfg.trx_mode = TRX_MODE_15_4A, /* IEEE802.15.4z - BPRF mode */ // .phy_cfg.sts_pkt_cfg = STS_PKT_CFG_0, /* SP0 Frame */ // .phy_cfg.sts_segnum = STS_SEGNUM_BPRF_1, /* Number of STS segments in the frame */ // .phy_cfg.sts_seglen = STS_SEGLEN_BPRF_64, /* Number of symbols in an STS segment */ // .phy_cfg.rx_ant_id = UWB_RX_ANT_3, /* UWB RX antenna port */ //}; /* Buffer to store received frame */ static uint8_t rx_buf[150]; static uint8_t uwb_sendbuffer[150]; /* Frames used in the ranging process * Poll message: * - byte 0 - 1: 0x8841 to indicate a data frame using 16-bit addressing. * - byte 2: sequence number, incremented for each new frame. * - byte 3 - 4: PAN Id 0x4B4d * - byte 5 - 6: Destination address * - byte 7 - 8: Source address * - byte 9: Message type (0x02 RANGING_POLL / 0x03 RANGING_RESPONSE / 0x04 RANGING_FINAL) * Response message: * - byte 10: activity code (0x07 to tell the initiator to go on with the ranging exchange) * Final message: * - byte 10 - 13: poll message transmission timestamp. * - byte 14 - 17: response message reception timestamp. * - byte 18 - 21: final message transmission timestamp. */ static uint8_t tx_poll_msg[] = {0x41, 0x88, 0, 0x4D, 0x4B, 0x53, 0x45, 0x4D, 0x49, 0x02}; static uint8_t rx_resp_msg[] = {0x41, 0x88, 0, 0x4D, 0x4B, 0x4D, 0x49, 0x53, 0x45, 0x03, 0x07}; static uint8_t tx_final_msg[] = {0x41, 0x88, 0, 0x4D, 0x4B, 0x53, 0x45, 0x4D, 0x49, 0x04, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; /* Count value of phy counter when transmitting and receiving frames */ static uint32_t poll_tx_en_start_u32; static uint32_t resp_rx_en_start_u32; static uint32_t final_tx_en_start_u32; /* 41 bits timestamps of frames transmission/reception. */ static int64_t poll_tx_ts_i64; static int64_t resp_rx_ts_i64; static int64_t final_tx_ts_i64; /* Frame sequence number, incremented after each transmission. */ static uint8_t frame_seq_nb = 0; static volatile uint16_t rx_state; static volatile uint16_t rx_state1; static volatile uint16_t rx_length; //DW uint16_t rec_nearbaseid; uint8_t temp_tag_num; int32_t test2; //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; 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 int32_t mainbase_dist,nearbase_distlist[MAX_NEARBASE_NUM],true_nearbase_distlist[MAX_NEARBASE_NUM],true_exsistbase_list[MAX_NEARBASE_NUM],ancsync_time,nextpoll_delaytime,offsettimeus; 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_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, SIMPLE_POLL = 1, SIMPLE_RESPONSE = 2, SIMPLE_FINAL = 3, }; void uwb_poll_buffer_construct(void) { for(uint8_t i=0;ierr_code; // /** UWB RX success */ // if (rx_state == UWB_RX_OK) // { // /* Received data does not contain FCS */ // rx_length = rx_report->pkt_len; // 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(); // //»ñÈ¡·¢Éä¶ËʱÖÓÆ«²î // 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 ºê¶¨Òå±ÜÃâÊÕ°üÖÐ;У׼µ¼Ö²â¾à´íÎó // //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; // // } // else // { // /* UWB_PLD_ERR payload error */ // /* UWB_PHR_ERR PHR error */ // /* UWB_SFD_ERR Sfd error */ // /* UWB_BD_ERR Preamble detection error */ // /* UWB_TO_ERR Receive timeout */ // /* UWB_STS_ERR STS error */ // rx_state1=rx_report->err_code; // rx_length = 0; // receive_flag=2; // temp_tag_count2=phy_timer_count_get(); // // } // //} ///* TX done process handler. */ //static void tx_int_callback(struct MAC_HW_REPORT_T *tx_report) //{ // // Power off radio // power_off_radio(); // /** UWB TX success */ // if (tx_report->err_code == UWB_TX_OK) // { // poll_tx_num++; // temp_count4=phy_timer_count_get();//²âÊÔ // if(temp_flag){ // count2=phy_timer_count_get(); // temp_flag=0; // return; // } // count1=temp_tag_count1; // temp_flag=1; // } //} //×¢Ò⣺ÒòΪÐÂ×¢²áµÄ±êÇ©²¢²»ÖªµÀ»ùÕ¾ÒѾ­ÊÕµ½ÁË£¬ËùÒÔÒªÔÚÏÂÒ»°üÁ¢¿Ì½«ÐµıêÇ©ID·¢³ö£¬·ñÔò±êÇ©»áÒ»Ö±ÇÀÕ¼»ùÕ¾µÄpoll°ü¡£ //·½·¨Êǽ«ÐÂ×¢²áµÄ±êÇ©Ìí¼Óµ½Ï´η¢Ë͵ıêÇ©ÁбíÍ·²¿£¬ÆäËû±êÇ©ÒÀ´ÎÏòºóŲ¶¯¡£ uint16_t AddNewTagIntoList(uint16_t tagid) { uint16_t newindex_cal = taglist_current_index+25;//µ±Ç°Ë÷Òý+25£¬ÎªÏ´ÎË÷ÒýÒª·¢³öµÄÖµµÄ×î´óÖµ£¬Èç¹û×ÜÊýСÓÚÕâ¸öÖµ£¬¼´¿É·Åµ½×îºóÃæ¡£ if(newindex_cal>=taglist_total_num) //Èç¹ûµ±Ç°Ë÷ÒýÊÇ×îºóÒ»°ü£¬ÄÇô¾Í¼Óµ½×ÜË÷ÒýµÄºóÃæ£¬Èç¹û²»ÊǾͼӵ½ÏÂÒ»´ÎÆðʼË÷ÒýλÖᣠ{ newindex_cal = taglist_total_num; }else{ for(uint16_t i=taglist_total_num;i>newindex_cal;i--) //´ÓºóÍùǰŲµØ·½??¾ÍÊDzåÈë { taglist_id[i]=taglist_id[i-1]; taglist_dist[i]=taglist_dist[i-1]; taglist_keeptime[i]=taglist_keeptime[i-1]; } } taglist_total_num++; taglist_id[newindex_cal] = tagid; taglist_dist[newindex_cal] = 0; taglist_keeptime[newindex_cal] = TAG_KEEPTIMES; return newindex_cal; } uint8_t FindNearBasePos(uint16_t baseid)//ѰÕÒµ±Ç°ÁбíÖеĻùÕ¾·µ»ØË÷Òý { uint8_t i; for(i=0;i0) { exsistbase_list[i]--; next_nearbase_num++; true_exsistbase_list[j]=exsistbase_list[i]; true_nearbase_idlist[j]=nearbaseid_list[i]; true_nearbase_distlist[j++]=nearbase_distlist[i]; } nearbase_num = next_nearbase_num;//¸üÐÂÏÖ´æ»ùÕ¾Êý last_nearbase_num = next_nearbase_num;//¸üе±Ç°»ùÕ¾Êý } } void Rank_ANchor_list_by_dis(void)//°´ÕÕĿǰµÄ»ùÕ¾¾àÀë½øÐÐðÅÝÅÅÐòÓÃÁÙʱÊý×鷽ʽ½«×î½üµÄ·Åµ½×îÇ°Ãæ£¬×îÔ¶µÄ·Åµ½×îºóÃæ { for(int i=0;itrue_nearbase_distlist[j+1]) { uint32_t temp_dist,temp_id,temp_exsis; temp_dist=true_nearbase_distlist[j]; temp_id = true_nearbase_idlist[j]; temp_exsis=true_exsistbase_list[j]; true_nearbase_distlist[j]=true_nearbase_distlist[j+1]; true_nearbase_idlist[j]=true_nearbase_idlist[j+1]; true_exsistbase_list[j]=true_exsistbase_list[j+1]; true_nearbase_distlist[j+1]=temp_dist; true_nearbase_idlist[j+1]=temp_id; true_exsistbase_list[j+1]=temp_exsis; } } } for (int i=0;i=UINT32_MAX) // {end_receive_count-=UINT32_MAX;} // current_count=phy_timer_count_get(); // while(current_countend_receive_count+HALF_SECOND_TIME)//Ñ­»·½ÓÊܰüÌå,ÈôΪ124.8KÔòÊÇ+62400000 // { // // current_count=phy_timer_count_get();//²»¶Ïˢе±Ç°¼ÆÊýÆ÷Öµ // temp_tag_count2=phy_timer_count_get(); // while(mac_is_busy());//µÈ´ý½ÓÊÕÍê³É // temp_tag_count3=phy_timer_count_get(); //// if(receive_flag==1)//³É¹¦½ÓÊÕÊý¾Ý // { //// receive_flag=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 // { // 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) //·¢ÏÖеĻùÕ¾ // { // //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);//²âÊÔ // // }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(); //// while(mac_is_busy()); // temp_tag_count1=phy_timer_count_get(); // } // //gpio_pin_clr(IO_PIN_5);//²âÊÔ // } // delay_us(1); // uwb_rx_force_off(1); // 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]); } 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();//¸üеÆ×´Ì¬ }