zhyinch
2020-11-06 1abba6d0da35b3a89fead56896bf82c1b9aa7831
Src/application/dw_app.c
@@ -50,7 +50,7 @@
#define RESP_RX_TO_FINAL_TX_DLY_UUS 510
/* Receive response timeout. See NOTE 5 below. */
#define RESP_RX_TIMEOUT_UUS 0
#define RESP_RX_TIMEOUT_UUS 1000
#define DELAY_BETWEEN_TWO_FRAME_UUS 240
@@ -69,13 +69,14 @@
#define FINAL_MSG_FINAL_TX_TS_IDX 18
#define FINAL_MSG_TS_LEN 4
#define STARTPOLL  REGPOLL
enum enumtagstate
{
   REGPOLL,
   DISCPOLL,
   GETNEARMSG,
   NEARPOLL,
}tag_state=DISCPOLL;
}tag_state=STARTPOLL;
static dwt_config_t config = {
   2,               /* Channel number. */
   DWT_PRF_64M,     /* Pulse repetition frequency. */
@@ -465,7 +466,7 @@
extern u16 bigslot_num;
u8 nearbase_num=0,last_nearbase_num,next_nearbase_num,last_slotnum, para_update,para_len,stationary_flag=0;
u32 rec_tagpos_binary;
int16_t offset=4700;
int16_t offset=4700,temptimer;
u8 motor_state,rec_remotepara_state,rec_remotepara[80];
void Poll(void)
{
@@ -537,12 +538,15 @@
         
                           ancsync_time=((sync_timer)*1000+tmp_time);   
                           last_slotnum=current_slotnum;
                           current_slotnum=((float)sync_timer/(SLOTTIME_MS*bigslot_num))+1;
                           temptimer = sync_timer-10;
                           if(temptimer<0)
                           {temptimer+=1000;}
                           current_slotnum=((float)temptimer/(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(tyncpoll_time);
                           SetNextPollTime(tagslotpos);
      
         if(temp_dist<mindist&&temp_dist!=0)
         {
@@ -580,7 +584,7 @@
      tyncpoll_time=(tagslotpos-1)*slottime;
   }
   mainbase_id=minid;
   //mainbase_id = 0x2;
   //mainbase_id = 0x1;
}
u8 nearmsg_mainbase=0,rec_tagpos_emptylist[32],mainbase_type;;
void GetNearMsg(void)
@@ -615,18 +619,19 @@
      {
         nearbase_num=rx_buffer[NEARBASENUM_INDEX];
         memcpy(&rec_tagpos_binary,&rx_buffer[NEARMSG_EMPTYSLOTPOS_INDEX],4);
         tagslotpos=GetRandomSlotPos(rec_tagpos_binary);
      //   tagslotpos=GetRandomSlotPos(rec_tagpos_binary);
         //tagslotpos=rx_buffer[TAGSLOTPOS];
         memcpy(nearbaseid_list,&rx_buffer[NEARBASEID_INDEX],nearbase_num*2);
         //slottime=ceil((nearbase_num+2)*0.3)+1;
         //tyncpoll_time=tagslotpos*slottime;
         tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;
         //tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;
         tag_state=NEARPOLL;
      }
   }else{
         tyncpoll_time = (tagslotpos--%max_slotpos)*slottime;
   }
}
extern u8 userkey_state;
uint8_t changemainbase_count=0,gotosleep_flag;
uint32_t frame_len;
void NearPoll(void)
@@ -648,17 +653,21 @@
   HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_SET);
   recbase_num=0;
   motor_state=0;
//   motor_state=0;
   
   tx_near_msg[BATTARY_IDX] = bat_percent;
   tx_near_msg[BUTTON_IDX] = !GET_USERKEY|stationary_flag<<1|gotosleep_flag<<2;
   tx_near_msg[BUTTON_IDX] = userkey_state|stationary_flag<<1|gotosleep_flag<<2;
   tx_near_msg[SEQUENCE_IDX] = frame_seq_nb++;
   tx_near_msg[NEARBASENUM_INDEX] = nearbase_num;
   memcpy(&tx_near_msg[NEARBASEID_INDEX],&nearbaseid_list,nearbase_num*2);
   tx_near_msg[MESSAGE_TYPE_IDX] = NEAR_POLL;   
   tx_near_msg[NEARP_TAGFREQ_INDEX] = tag_frequency;
   tx_near_msg[NEARP_TAGSLOTPOS_INDEX] = tagslotpos;
   memcpy(&tx_near_msg[ANCHOR_ID_IDX],&mainbase_id,2);   
   dwt_writetxdata(13+2*nearbase_num, tx_near_msg, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
   dwt_writetxfctrl(13+2*nearbase_num, 0);//设置超宽带发送数据长度
   dwt_writetxdata(21+2*nearbase_num, tx_near_msg, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
   dwt_writetxfctrl(21+2*nearbase_num, 0);//设置超宽带发送数据长度
   dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);//开启发送,发送完成后等待一段时间开启接收,等待时间在dwt_setrxaftertxdelay中设置
   
   tx_near_msg[TAGCONFIGSUCCESS_INDEX] =0;
@@ -747,12 +756,12 @@
                           //时间同步
                           ancsync_time=((sync_timer)*1000+tmp_time);   
                           last_slotnum=current_slotnum;
                           current_slotnum=((float)sync_timer/(SLOTTIME_MS*bigslot_num))+1;
                           current_slotnum=((float)(sync_timer*10)/(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(tyncpoll_time);
                           SetNextPollTime(tagslotpos);
                        }else{
                           
@@ -774,6 +783,7 @@
                           dwt_writetxfctrl(28+nearbase_num*4, 0);//设定发送数据长度
                           dwt_setdelayedtrxtime(final_tx_time);//设置final包发送时间T5
                           result=dwt_starttx(DWT_START_TX_DELAYED);//设定为延迟发送   
                           userkey_state = !GET_USERKEY;
                           if(result==0)
                                 {while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))//不断查询芯片状态直到发送完成
                                 { };}                        
@@ -784,7 +794,7 @@
                     }
                  }else{
                  dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_RXFCG| SYS_STATUS_ALL_RX_ERR);
                  if(recbase_num!=nearbase_num)
                  if(recbase_num!=nearbase_num+1)
                     {dwt_rxenable(0);   }
                     //                  if(flag_rxon)
//                     {
@@ -793,20 +803,12 @@
                  }         
               //   dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_RXFCG| SYS_STATUS_ALL_RX_ERR);
         }
         dwt_forcetrxoff();
dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_RXFCG| SYS_STATUS_ALL_RX_ERR);
      j=0;
         if(exsistbase_list[0]==0)
         {
//          u8 temp_adc,random_value;
//            random_value=0;
//            for(i=0;i<8;i++)
//            {
//               temp_adc=Get_ADC_Value();
//               random_value=random_value|((temp_adc&0x01)<<i);
//            }
//            tagslotpos=GetRandomSlotPos(rec_tagpos_binary);
//            tyncpoll_time = (tagslotpos--%max_slotpos)*slottime;
            SetNextPollTime(tyncpoll_time);
//            SetNextPollTime(tagslotpos);
         }
      //   tyncpoll_time=0;
         next_nearbase_num=0;
@@ -905,21 +907,32 @@
HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_RESET);
}
extern uint8_t module_power;
uint16_t tag_slotpos;
extern uint8_t module_power,imu_enable,motor_enable;
void Registor_Poll(void)
{
   static u8 regpoll_count=0;
      tag_frequency = REGISTER_FREQUENCY;
      bigslot_num = TOTAL_SLOTNUM/tag_frequency;
      regpoll_count++;
      if(regpoll_count%2)
      {
         if(tagslotpos--<2)
            tagslotpos=TOTAL_SLOTNUM;
      }
      SetNextPollTime(tagslotpos);
      dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);         //设置发送后开启接收,并设定延迟时间
    dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);      
      tx_near_msg[REGP_TAGSTATE_INDEX] = !GET_USERKEY<<2|imu_enable<<1|motor_enable;
      tx_near_msg[MESSAGE_TYPE_IDX] = REG_POLL;   
      tx_near_msg[REGP_FREQUENCY_INDEX] = tag_frequency;
      tx_near_msg[REGP_FREQUENCY_INDEX] = 1000/g_com_map[COM_INTERVAL];
      tx_near_msg[REGP_POWER_INDEX] = module_power;
      memcpy(&tx_near_msg[REGP_VERSION_INDEX],&g_com_map[VERSION],2);   
      memcpy(&tx_near_msg[REGP_IMUTHRES_INDEX],&g_com_map[IMU_THRES],2);
      memcpy(&tx_near_msg[REGP_NOMOVESLEEPTIME_INDEX],&g_com_map[NOMOVESLEEP_TIME],2);
      dwt_writetxdata(13+2*nearbase_num, tx_near_msg, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
      dwt_writetxfctrl(13+2*nearbase_num, 0);//设置超宽带发送数据长度
      dwt_writetxdata(21, tx_near_msg, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
      dwt_writetxfctrl(21, 0);//设置超宽带发送数据长度
      dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);//开启发送,发送完成后等待一段时间开启接收,等待时间在dwt_setrxaftertxdelay中设置
   while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))//不断查询芯片状态直到成功接收或者发生错误
   { 
@@ -941,7 +954,7 @@
                        memcpy(&rec_nearbaseid,&rx_buffer[ANCHOR_ID_IDX],2);
                        tag_frequency = rx_buffer[REGR_TAGFREQ_INDEX];
                        bigslot_num = TOTAL_SLOTNUM/tag_frequency;
                        tag_slotpos = rx_buffer[REGR_TAGSLOTPOS_INDEX];
                        tagslotpos = rx_buffer[REGR_TAGSLOTPOS_INDEX];
                           //////////////////////////////////时间同步
                           memcpy(&sync_timer,&rx_buffer[ANCTIMEMS],2);
                           memcpy(&tmp_time,&rx_buffer[ANCTIMEUS],2);
@@ -955,17 +968,21 @@
                              if(sync_timer>=1010)
                                 {sync_timer=0;}
                           }
                        ancsync_time=((sync_timer)*1000+tmp_time);
                           ancsync_time=((sync_timer)*1000+tmp_time);
                           last_slotnum=current_slotnum;
                           current_slotnum=((float)sync_timer/(SLOTTIME_MS*bigslot_num))+1;
                           temptimer = sync_timer-10;
                           if(temptimer<0)
                           {temptimer+=1000;}
                           current_slotnum=((float)temptimer/(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(tyncpoll_time);
//                           SetNextPollTime(tagslotpos);
                           mainbase_id=rec_nearbaseid;
                           nearbase_num=rx_buffer[NEARBASENUM_INDEX];
                           memcpy(nearbaseid_list,&rx_buffer[NEARBASEID_INDEX],nearbase_num*2);
                           tag_state = NEARPOLL;
                        }
                     }
}
@@ -981,9 +998,13 @@
    }
   switch(tag_state)
   {
      case REGPOLL:
         LED_LG_ON;
         Registor_Poll();
         LED_LG_OFF;
         break;
      case DISCPOLL:
         LED_LG_ON;      
         //Registor_Poll();
         Poll();
         LED_LG_OFF;
         break;