| | |
| | | #include "mk_sleep_timer.h" |
| | | #include "lib_ranging.h" |
| | | #include "dw_app_anchor.h" |
| | | |
| | | #include "global_param.h" |
| | | #include "board.h" |
| | | |
| | | #if defined(MK_SS_TWR_DW_RESP) |
| | | |
| | | extern int simple_main(void); |
| | | extern int temp_main(void); |
| | | void TagListUpdate(void); |
| | | extern void IO_LED_control_change(uint8_t data); |
| | | extern void IO_control_init(void); |
| | | |
| | |
| | | |
| | | //resp变量 |
| | | static uint8_t frame_seq_nb2,battary,button,rec_nearbase_num,ancidlist_num; |
| | | static uint16_t ancidlist_rec[TAG_NUM_IN_SYS],ancidlist_send[TAG_NUM_IN_SYS],rec_ancidlist[TAG_NUM_IN_SYS],rec_ancdistlist[TAG_NUM_IN_SYS]; |
| | | static uint16_t ancidlist_rec[TAG_NUM_IN_SYS],ancidlist_send[TAG_NUM_IN_SYS],rec_ancidlist[TAG_NUM_IN_SYS]; |
| | | static int16_t rec_ancdistlist[TAG_NUM_IN_SYS]; |
| | | extern uint8_t group_id; |
| | | static uint16_t anc_id_recv,tag_id_recv; |
| | | static int16_t rec_antdelay; |
| | |
| | | extern uint8_t gps_power_state,motor_power_state,uwb_state,air780_power_state,gps_success_state,chaging_state,changed_state,air780_success_state; |
| | | int poll_rx_num,resp_tx_num; |
| | | //resp函数 |
| | | void PushAnchorDataArray(uint16_t ancid,uint16_t dist,uint8_t battary);//找到自己的id信息对应位置更新自己的交互信息 |
| | | void PushAnchorDataArray(uint16_t ancid,int16_t dist,uint8_t battary);//找到自己的id信息对应位置更新自己的交互信息 |
| | | static void resp_msg_set_ts(uint8_t *ts_field, int64_t ts);//用来对应位置放入时间戳 |
| | | static uint16_t tagid_list[TAG_NUM_IN_SYS]; |
| | | uint16_t CmpTagInList(uint16_t tagid); |
| | | uint8_t Anchor_RecNearPoll(uint8_t ancrec_nearbasepos); |
| | | static uint8_t send_buffer[100]; |
| | | static uint8_t tagofflinetime[TAG_NUM_IN_SYS]; |
| | | uint32_t temp_count=0; |
| | | uint32_t temp_count1=0; |
| | | uint32_t temp_count2=0; |
| | | uint32_t temp_count3=0; |
| | | uint32_t temp_internal=0; |
| | | int temp_flag; |
| | | int temp_flag,distance; |
| | | extern uint8_t recev_error_num; |
| | | typedef enum |
| | | { UN_BIND=0, |
| | | LINK_SUCCESS, |
| | | SEARCH_DEV, |
| | | } Operation_step; |
| | | extern Operation_step Operation_state; |
| | | extern Operation_step UWB_Work_State; |
| | | extern int16_t first_search_flag; |
| | | /* Ranging period */ |
| | | #define RANGING_PERIOD_MS (1000) |
| | |
| | | #define RESP_TX_TO_FINAL_RX_DLY_US 500U |
| | | |
| | | /* RX sync window size 50 ms*/ |
| | | #define RX_SYNC_WIN_US 10000U //yuan1000 7000success |
| | | #define RX_SYNC_WIN_US 5000U //yuan1000 7000success |
| | | |
| | | /* RX sync window size 50 ms*/ |
| | | #define RX_SYNC_WIN_US_TEMP 2000000U //yuan1000 7000success |
| | |
| | | /* 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.ch_num = 9, /* Channel number. */ |
| | | .phy_cfg.code_index = 9, /* TX preamble code. */ |
| | | .phy_cfg.mean_prf = MEAN_PRF_64M, /* Data rate 6.8M */ |
| | | .phy_cfg.data_bit_rate = DATA_BR_6M8, /* data rate 6.8M. */ |
| | |
| | | poll_rx_num++; |
| | | receive_flag=1; |
| | | recev_error_num=0; |
| | | sleep_timer_start(__MS_TO_32K_CNT(SLEEP_TIMER_NUM));//测试 |
| | | } |
| | | else |
| | | { |
| | |
| | | if(recev_error_num==20) { |
| | | recev_error_num=0; |
| | | first_search_flag=1;//首次进入搜索标志 |
| | | Operation_state=SEARCH_DEV; |
| | | UWB_Work_State=SEARCH_DEV; |
| | | } |
| | | receive_flag=2; |
| | | } |
| | |
| | | temp_count= phy_timer_count_get(); |
| | | temp_internal=temp_count; |
| | | resp_tx_num++; |
| | | LOG_INFO(TRACE_MODULE_APP, "poll_rx_num is %d,resp_tx_num is %d\r\n",poll_rx_num,resp_tx_num); |
| | | //LOG_INFO(TRACE_MODULE_APP, "poll_rx_num is %d,resp_tx_num is %d\r\n",poll_rx_num,resp_tx_num); |
| | | } |
| | | } |
| | | uint32_t start_receive_count,end_receive_count,poll_timeout,current_count,temp_resp; |
| | | static uint16_t anchordata_id[TAG_NUM_IN_SYS],anchordata_dist[TAG_NUM_IN_SYS]; |
| | | static uint8_t anchordata_bat[TAG_NUM_IN_SYS]; |
| | | static uint8_t anchordata_num = 0; |
| | | uint8_t anchordata_num = 0; |
| | | static int32_t tagdist_list[TAG_NUM_IN_SYS]; |
| | | uint16_t random_time; |
| | | //anchor |
| | | void PushAnchorDataArray(uint16_t ancid,uint16_t dist,uint8_t battary) |
| | | void PushAnchorDataArray(uint16_t ancid,int16_t dist,uint8_t battary) |
| | | { |
| | | uint8_t i; |
| | | for(i=0; i<anchordata_num; i++) |
| | |
| | | anchordata_id[i] = ancid;//与之通信的标签id存入当前表中 |
| | | anchordata_dist[i] = dist; |
| | | anchordata_bat[i] = battary; |
| | | tagofflinetime[i]=0;//不断更新当前TAG对应离线时间 |
| | | distance=dist; |
| | | } |
| | | uint16_t CmpTagInList(uint16_t tagid) |
| | | { uint16_t i; |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | void TagListUpdate(void) |
| | | { |
| | | uint8_t i,j=0; |
| | | for(i=0; i<anchordata_num; i++) |
| | | { |
| | | if(tagofflinetime[i]++<QUIT_SLOT_TIME) |
| | | { |
| | | anchordata_id[j]=anchordata_id[i]; |
| | | tagofflinetime[j++]=tagofflinetime[i]; |
| | | } |
| | | } |
| | | anchordata_num=j; |
| | | } |
| | | uint8_t Anchor_RecNearPoll(uint8_t ancrec_nearbasepos)//根据自己是否为新基站定制消息去发送,根据是否抢占判断 |
| | | { |
| | | temp_count2=poll_rx_en_start_u32; |
| | |
| | | random_time = (phy_timer_count_get()%1000);//多基站抢占一个位置时避免一个也收不到的情况就每一个随机错开几十uS |
| | | //resp_tx_time = (poll_rx_ts+((random_time+rec_nearbase_num*20 + POLL_RX_TO_RESP_TX_DLY_UUS+ancrec_nearbasepos*DELAY_BETWEEN_TWO_FRAME_UUS) * UUS_TO_DWT_TIME)) >> 8;//计算Response发送时间T3。 |
| | | // (resp_tx_en_start_u32) is the moment when TX enable |
| | | resp_tx_en_start_u32 = random_time+poll_rx_en_start_u32+ US_TO_PHY_TIMER_COUNT(POLL_RX_TO_RESP_TX_DLY_US)+rec_nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS);//后面的需要根据已有基站数量进行更改,如果是抢占自己最后一个回复,要有底数 |
| | | resp_tx_en_start_u32 = random_time+phy_timer_count_get()+ US_TO_PHY_TIMER_COUNT(POLL_RX_TO_RESP_TX_DLY_US)+rec_nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS);//后面的需要根据已有基站数量进行更改,如果是抢占自己最后一个回复,要有底数 |
| | | } else { |
| | | //resp_tx_time = (poll_rx_ts + ((rec_nearbase_num*20+POLL_RX_TO_RESP_TX_DLY_UUS+ancrec_nearbasepos*DELAY_BETWEEN_TWO_FRAME_UUS) * UUS_TO_DWT_TIME)) >> 8;//计算Response发送时间T3。 |
| | | resp_tx_en_start_u32 = poll_rx_en_start_u32 + US_TO_PHY_TIMER_COUNT(POLL_RX_TO_RESP_TX_DLY_US)+(ancrec_nearbasepos+1)*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS);//后面的需要根据已有基站数量进行更改,自己非抢占 |
| | | resp_tx_en_start_u32 = phy_timer_count_get() + US_TO_PHY_TIMER_COUNT(POLL_RX_TO_RESP_TX_DLY_US)+(ancrec_nearbasepos+1)*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS);//后面的需要根据已有基站数量进行更改,自己非抢占 |
| | | }//此处设置绝对时间将poll u32改为phy_timer_count_get() |
| | | |
| | | resp_tx_ts_i64 = ranging_tx_time_correct(resp_tx_en_start_u32 + phy_shr_duration());//修正时间戳 |
| | |
| | | temp_flag=uwb_tx(send_buffer, 40,1 ,resp_tx_en_start_u32);//立即发送测试size大小 |
| | | temp_count1=phy_timer_count_get(); |
| | | while(mac_is_busy()); |
| | | gpio_pin_clr(SCL_PIN); |
| | | } |
| | | int simple_main(void) |
| | | { |
| | |
| | | uint8_t i; |
| | | uint16_t tempid; |
| | | temp_count3=phy_timer_count_get(); |
| | | |
| | | gpio_pin_set(SCL_PIN); |
| | | uwb_rx(0, 0, RX_SYNC_WIN_US);//开启接收 |
| | | while(mac_is_busy()); |
| | | temp_count2=phy_timer_count_get(); |
| | |
| | | // { |
| | | if(receive_flag==1)//成功接收 |
| | | { |
| | | |
| | | if(group_id==rx_buf[GROUP_ID_IDX]&&rx_buf[MESSAGE_TYPE_IDX] == MBX_POLL)//判断是否是和自己是同一组通讯的且为poll包 |
| | | { temp_count2=phy_timer_count_get(); |
| | | frame_seq_nb2 = rx_buf[SEQUENCE_IDX];//获取包序 |
| | |
| | | // while(mac_is_busy()); |
| | | } |
| | | //receive_flag=0; |
| | | gpio_pin_clr(SCL_PIN); |
| | | } |
| | | |
| | | |
| | |
| | | uint8_t i; |
| | | uint16_t tempid; |
| | | temp_count3=phy_timer_count_get(); |
| | | gpio_pin_set(SCL_PIN); |
| | | uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//开启接收 |
| | | while(mac_is_busy()); |
| | | |
| | | temp_count2=phy_timer_count_get(); |
| | | if(receive_flag==1)//成功接收 |
| | | start_receive_count=phy_timer_count_get(); |
| | | poll_timeout=rec_nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS)+US_TO_PHY_TIMER_COUNT(DELAY_DEFAULT);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss |
| | | end_receive_count=start_receive_count+poll_timeout; |
| | | if(end_receive_count>=UINT32_MAX) |
| | | { |
| | | if(group_id==rx_buf[GROUP_ID_IDX]&&rx_buf[MESSAGE_TYPE_IDX] == MBX_POLL)//判断是否是和自己是同一组通讯的且为poll包 |
| | | { temp_count2=phy_timer_count_get(); |
| | | frame_seq_nb2 = rx_buf[SEQUENCE_IDX];//获取包序 |
| | | battary = rx_buf[BATTARY_IDX]; |
| | | rec_nearbase_num=rx_buf[FZ_NEARBASENUM_INDEX]; //标签传过来的他与基站交互的基站id数目 |
| | | memcpy(&tag_id_recv,&rx_buf[TAG_ID_IDX],2); |
| | | memcpy(rec_ancidlist,&rx_buf[FZ_NEARBASEID_INDEX],2*rec_nearbase_num); |
| | | memcpy(rec_ancdistlist,&rx_buf[FZ_NEARBASEID_INDEX+2*rec_nearbase_num],2*rec_nearbase_num); |
| | | memcpy(&rec_antdelay,&rx_buf[FZ_NEARBASEID_INDEX+rec_nearbase_num*4],2); |
| | | for(i=0; i<rec_nearbase_num; i++) |
| | | { |
| | | if(rec_ancidlist[i] == dev_id) |
| | | { |
| | | PushAnchorDataArray(tag_id_recv,rec_ancdistlist[i],battary); |
| | | } |
| | | } |
| | | for(i=0; i<rec_nearbase_num; i++) |
| | | { |
| | | memcpy(&tempid,&rx_buf[FZ_NEARBASEID_INDEX+i*2],2); |
| | | if(tempid==dev_id)//也是比id如果有就更新没有就抢占 |
| | | { |
| | | seize_anchor=0; //非抢占。已存在列表中 |
| | | Anchor_RecNearPoll(i); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if(i==rec_nearbase_num) |
| | | { |
| | | seize_anchor=1; //抢占anchor |
| | | Anchor_RecNearPoll(i); |
| | | } |
| | | |
| | | } |
| | | return 1;//返回发送成功标志 |
| | | } else if (receive_flag==2)//接受失败 |
| | | { |
| | | return 0;//返回绑定失败标志 |
| | | end_receive_count-=UINT32_MAX; |
| | | } |
| | | //receive_flag=0; |
| | | current_count=phy_timer_count_get(); |
| | | while(current_count<end_receive_count||current_count>end_receive_count+HALF_SECOND_TIME)//循环接受包体,若为124.8K则是+62400000 |
| | | { |
| | | while(mac_is_busy()); |
| | | |
| | | temp_count2=phy_timer_count_get(); |
| | | if(receive_flag==1)//成功接收 |
| | | { |
| | | if(group_id==rx_buf[GROUP_ID_IDX]&&rx_buf[MESSAGE_TYPE_IDX] == MBX_POLL)//判断是否是和自己是同一组通讯的且为poll包 |
| | | { temp_count2=phy_timer_count_get(); |
| | | frame_seq_nb2 = rx_buf[SEQUENCE_IDX];//获取包序 |
| | | battary = rx_buf[BATTARY_IDX]; |
| | | rec_nearbase_num=rx_buf[FZ_NEARBASENUM_INDEX]; //标签传过来的他与基站交互的基站id数目 |
| | | memcpy(&tag_id_recv,&rx_buf[TAG_ID_IDX],2); |
| | | memcpy(rec_ancidlist,&rx_buf[FZ_NEARBASEID_INDEX],2*rec_nearbase_num); |
| | | memcpy(rec_ancdistlist,&rx_buf[FZ_NEARBASEID_INDEX+2*rec_nearbase_num],2*rec_nearbase_num); |
| | | memcpy(&rec_antdelay,&rx_buf[FZ_NEARBASEID_INDEX+rec_nearbase_num*4],2); |
| | | for(i=0; i<rec_nearbase_num; i++) |
| | | { |
| | | if(rec_ancidlist[i] == dev_id) |
| | | { |
| | | PushAnchorDataArray(tag_id_recv,rec_ancdistlist[i],battary); |
| | | } |
| | | } |
| | | for(i=0; i<rec_nearbase_num; i++) |
| | | { |
| | | memcpy(&tempid,&rx_buf[FZ_NEARBASEID_INDEX+i*2],2); |
| | | if(tempid==dev_id)//也是比id如果有就更新没有就抢占 |
| | | { |
| | | seize_anchor=0; //非抢占。已存在列表中 |
| | | Anchor_RecNearPoll(i); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if(i==rec_nearbase_num) |
| | | { |
| | | seize_anchor=1; //抢占anchor |
| | | Anchor_RecNearPoll(i); |
| | | } |
| | | |
| | | } |
| | | gpio_pin_clr(SCL_PIN);//测试 |
| | | return 1;//返回发送成功标志 |
| | | } else if (receive_flag==2)//接受失败 |
| | | { |
| | | uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//再次开启接收 |
| | | } |
| | | //receive_flag=0; |
| | | } |
| | | gpio_pin_clr(SCL_PIN);//测试 |
| | | return 0;//返回绑定失败标志 |
| | | } |
| | | //} |
| | | #endif |