1
zhyinch
2021-10-25 c5193cf1c2e36894cbb2d7e9cd171b64f81ae8ea
Src/application/dw_app.c
@@ -69,7 +69,7 @@
#define FINAL_MSG_FINAL_TX_TS_IDX 18
#define FINAL_MSG_TS_LEN 4
#define STARTPOLL  SINGLEPOLL//#define SWITCHBASE_DIST
#define STARTPOLL  REGPOLL//#define SWITCHBASE_DIST
//#define SWITCHBASE_ZHUANDIAN
#define SWITCHBASE_DIST
enum enumtagstate
@@ -562,7 +562,7 @@
                           {flag_getresponse=1;}
                           lastsync_timer=sync_timer;
                           //offsettimeus=ancsync_time-current_count*LPTIMER_LSB+offset;
                           SetNextPollTime(tagslotpos);
//                           SetNextPollTime(tagslotpos);
      }
         
         memcpy(&temp_dist, &rx_buffer[DIST_IDX], 4);
@@ -628,12 +628,17 @@
void GetNearMsg(void)
{
   u32 start_poll,frame_len;
    u8 nearmsg_i=0;
    for(nearmsg_i=0;nearmsg_i<20;nearmsg_i++)
    {
        nearbase_distlist[nearmsg_i] = 0x1ffff;
    }
//mainbase_id = 0x9724;
   memcpy(&tx_near_msg[ANCHOR_ID_IDX],&mainbase_id,2);
   memcpy(&tx_near_msg[TAG_ID_IDX],&dev_id,2);
   tx_near_msg[MESSAGE_TYPE_IDX] = NEAR_MSG;
   
   dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);         //设置发送后开启接收,并设定延迟时间
  dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);   
   dwt_writetxdata(12, tx_near_msg, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
@@ -670,7 +675,46 @@
//         tyncpoll_time = (tagslotpos--%max_slotpos)*slottime;
   }
}
extern u32 synctimer;
u8 pd_i;
u32 temptime,delaytime;
u16 delaycount;
int16_t poll_offsettime=-6900;
extern u16 slotpos_intoatl, slotpos;
void NextPollDelay(u32 anchor_time)
{
      tagslotpos = 0 ;
    for(pd_i=0;pd_i<tag_frequency;pd_i++)
    {
        slotpos_intoatl = pd_i*bigslot_num+tagslotpos;
        temptime = (slotpos_intoatl*SLOTTIME_MS+100)*100;
        if(anchor_time<temptime-5000)
        {
            delaytime = temptime-anchor_time+poll_offsettime;
            return ;
        }
    }
    temptime = (tagslotpos*SLOTTIME_MS+100+10000)*100;
    delaytime = temptime-anchor_time+poll_offsettime;
   return ;
}
u16 lpcount,poll_startcount;
u16 waketopolltimeus = 4000;
#define BASENUM_COMTIME 244
u32 last_anchor_time;
void SetLPTimer(u32 anchor_time)
{
    NextPollDelay(anchor_time);
    current_count=HAL_LPTIM_ReadCounter(&hlptim1);
    lpcount = current_count+delaytime/LPTIMER_LSB+(10-nearbase_num)*8;
      poll_startcount = lpcount+(waketopolltimeus)/LPTIMER_LSB;
      if(poll_startcount>=32768)
      {
      poll_startcount -=32768;
      }
    __HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
      last_anchor_time = anchor_time;
}
void NearPoll(void)
{
   
@@ -812,13 +856,7 @@
                                 {sync_timer=0;}
                           }
                           ancsync_time=((sync_timer)*1000+tmp_time);   
                           last_slotnum=current_slotnum;
                           current_slotnum=((float)(sync_timer*10 - tagslotpos*SLOTTIME_MS)/(SLOTTIME_MS*bigslot_num))+1;
                           if(current_slotnum==last_slotnum-1)
                           {flag_getresponse=1;}
                           lastsync_timer=sync_timer;
                           offsettimeus=ancsync_time-current_count*LPTIMER_LSB+offset;
                           SetNextPollTime(tagslotpos);
                           SetLPTimer(ancsync_time);
                           if(result==0)
                                 {while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))//不断查询芯片状态直到发送完成
                                 { };}                        
@@ -831,14 +869,14 @@
                           memcpy(&nearbase_switchdistlist[rec_nearbasepos],&rx_buffer[NR_NEARSWITCH_DISTANCE],2);
                           nearbase_distlist[rec_nearbasepos+1]=temp_dist;   //   nearbase_distlist[1]对应   rec_nearbaseid[0]的距离               
                           final_msg_set_ts(&tx_near_msg[FINAL_MSG_RESP_RX_NEARBASE_IDX+(rec_nearbasepos)*4], resp_rx_ts);
                           if(temp_dist<salvebase_mindist)
                           if(temp_dist<salvebase_mindist&&nearbase_switchdistlist[rec_nearbasepos]==1)
                           {
                              salvebase_mindist = temp_dist;
                              mindist_slavebaseid = rec_nearbaseid;
                           }
                           #ifdef SWITCHBASE_ZHUANDIAN
                                        if(temp_dist<nearbase_switchdistlist[rec_nearbasepos]&&nearbase_switchdistlist[rec_nearbasepos]!=0&&temp_dist>0)
                                        {
                                       if(temp_dist<nearbase_switchdistlist[rec_nearbasepos]&&nearbase_switchdistlist[rec_nearbasepos]>1&&temp_dist>0)
                             {
                                            mainbase_id = rec_nearbaseid;
                                            tag_state = GETNEARMSG;
                                        }
@@ -877,11 +915,12 @@
               changemainbase_count = 0;
            }
         #endif
         }else if(mainbase_lost_count>10*tag_frequency)
         }else if(mainbase_lost_count>5*tag_frequency)
         {
            tag_state = STARTPOLL;
         }         
   if(mainbase_lost_count!=0)
         {SetLPTimer(last_anchor_time);}
         if(para_update)
         {
         uint8_t pack_length = 0,pack_index = 0,pack_msgtype = 0;
@@ -931,7 +970,7 @@
         if(tagslotpos--<2)
            tagslotpos=TOTAL_SLOTNUM;
      }
      SetNextPollTime(tagslotpos);
//      SetNextPollTime(tagslotpos);
      dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);         //设置发送后开启接收,并设定延迟时间
    dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);