zhyinch
2019-12-18 3db912651e297c662fcd190b79d2ca90a34a8f66
Ô´Âë/ºËÐİå/Src/application/dw_app.c
@@ -27,7 +27,7 @@
#include "filters.h"
#include <stdio.h>
#include "beep.h"
#include "modbus.h"
/*------------------------------------ Marcos ------------------------------------------*/
/* Inter-ranging delay period, in milliseconds. */
@@ -77,7 +77,8 @@
#define BUTTON_IDX                  16
#define SEQUENCE_IDX               17
//respose
#define TIMECORRE             14
#define ANCTIMEMS             14
#define ANCTIMEUS             16
#define POLL                    0x01
#define RESPONSE                0x02
@@ -93,7 +94,7 @@
   DWT_PAC8,        /* Preamble acquisition chunk size. Used in RX only. */
   9,               /* TX preamble code. Used in TX only. */
   9,               /* RX preamble code. Used in RX only. */
   0,               /* Use non-standard SFD (Boolean) */
   1,               /* Use non-standard SFD (Boolean) */
   DWT_BR_6M8,      /* Data rate. */
   DWT_PHRMODE_STD, /* PHY header mode. */
   (129 + 8 - 8)    /* SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only. */
@@ -106,11 +107,11 @@
static uint8_t tx_final_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
   
//static uint8_t rx_poll_msg[] = {0x00, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x21, 0, 0};
static uint8_t tx_resp_msg[18] = {0};
static uint8_t tx_resp_msg[20] = {0};
//static uint8_t rx_final_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
   
/* Frame sequence number, incremented after each transmission. */
static uint32_t frame_seq_nb = 0;
static uint32_t frame_seq_nb = 0,frame_seq_nb2=0;
   
/* Hold copy of status register state here for reference, so reader can examine it at a breakpoint. */
static uint32_t status_reg = 0;
@@ -133,7 +134,7 @@
static double tof;
   
uint32_t anchor_dist_last_frm[TAG_NUM_IN_SYS],his_dist[TAG_NUM_IN_SYS];   ;
int32_t anchor_dist_last_frm[TAG_NUM_IN_SYS],his_dist[TAG_NUM_IN_SYS];   ;
uint32_t tag_id = 0;
uint32_t tag_id_recv = 0;
uint8_t random_delay_tim = 0;
@@ -144,7 +145,7 @@
float dis_after_filter;            //当前距离值
LPFilter_Frac* p_Dis_Filter;      //测距用的低通滤波器
uint16_t g_Tagdist[TAG_NUM_IN_SYS];
int32_t g_Tagdist[TAG_NUM_IN_SYS];
uint8_t g_flag_Taggetdist[256];
/*------------------------------------ Functions ------------------------------------------*/
@@ -237,7 +238,7 @@
      for(i=0;i<255;i++)
      {
         g_flag_Taggetdist[i]++;
         if(g_flag_Taggetdist[i]>=2)
         if(g_flag_Taggetdist[i]>=20)
         {
            g_Tagdist[i]=0xffff;
         }
@@ -312,15 +313,17 @@
uint16_t g_Resttimer;
uint8_t result;
u8 tag_succ_times=0;
int32_t hex_dist;
int32_t hex_dist,hex_dist2;
u16 checksum;
int8_t tag_delaytime;
extern uint16_t sync_timer;
u16 tmp_time;
void Tag_App(void)//发送模式(TAG标签)
{
   uint32_t frame_len;
   uint32_t final_tx_time;
   u32 start_poll;
   u8 i;
   u8 i,getsync_flag=0;
   //LED0_ON;
   dwt_forcetrxoff();
   g_Resttimer=0;
@@ -330,7 +333,7 @@
   tx_poll_msg[BATTARY_IDX] = Get_Battary();
   tx_poll_msg[BUTTON_IDX] = !READ_KEY0;
   tx_poll_msg[SEQUENCE_IDX] = frame_seq_nb++;
   GPIO_WriteBit(GPIOA, GPIO_Pin_9, Bit_RESET);
   for(i=0;i<g_com_map[MAX_REPORT_ANC_NUM];i++)
   {
   /* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */
@@ -347,7 +350,7 @@
   while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))//不断查询芯片状态直到成功接收或者发生错误
   { if(time32_incr - start_poll>20)
      NVIC_SystemReset();
      UART_CheckReceive();
      IdleTask();
      
   };
@@ -372,21 +375,27 @@
       * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */
      
      if (rx_buffer[MESSAGE_TYPE_IDX] == RESPONSE&&!memcmp(&rx_buffer[TAG_ID_IDX],&dev_id,4)) //判断接收到的数据是否是response数据
      {
      { u16 anc_id_recv;
         /* Retrieve poll transmission and response reception timestamp. */
         poll_tx_ts = get_tx_timestamp_u64();                              //获得POLL发送时间T1
         resp_rx_ts = get_rx_timestamp_u64();                              //获得RESPONSE接收时间T4
         
                     if(i==0)
                        {
                        tag_delaytime=rx_buffer[TIMECORRE];
                     if(tag_delaytime>g_com_map[COM_INTERVAL]/2)
                     {
                     tag_delaytime=tag_delaytime-g_com_map[COM_INTERVAL];
                     }
                        }
         memcpy(&anchor_dist_last_frm[0], &rx_buffer[DIST_IDX], 4);
         if(getsync_flag==0&&g_com_map[DEV_ROLE])
         {
            getsync_flag=1;
         memcpy(&sync_timer,&rx_buffer[ANCTIMEMS],2);
         memcpy(&tmp_time,&rx_buffer[ANCTIMEUS],2);
         tmp_time=tmp_time+450;
         if(tmp_time>999)
         {
            tmp_time-=999;
            sync_timer++;
            if(sync_timer>=1010)
               {sync_timer=0;}
         }
         TIM3->CNT=tmp_time;
      }
         memcpy(&hex_dist2, &rx_buffer[DIST_IDX], 4);
         memcpy(&tx_final_msg[ANCHOR_ID_IDX], &rx_buffer[ANCHOR_ID_IDX], 4);
         /* Compute final message transmission time. See NOTE 9 below. */
         final_tx_time = (resp_rx_ts + (RESP_RX_TO_FINAL_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8;//计算final包发送时间,T5=T4+Treply2
@@ -409,21 +418,26 @@
         tag_succ_times++;
         
            LED0_BLINK;
               memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2);
//               g_Tagdist[anc_id_recv]=   hex_dist;
//               g_flag_Taggetdist[anc_id_recv]=0;
               if(!g_com_map[MODBUS_MODE])
               {
               usart_send[2] = 1;//正常模式
               usart_send[3] = 17;//数据段长度
               usart_send[4] = frame_seq_nb;//数据段长度
               memcpy(&usart_send[5],&dev_id,2);
               memcpy(&usart_send[7],&rx_buffer[ANCHOR_ID_IDX],2);
               hex_dist = anchor_dist_last_frm[0];;
               memcpy(&usart_send[9],&hex_dist,4);
               memcpy(&usart_send[9],&hex_dist2,4);
               usart_send[13] = battary;
               usart_send[14] = button;
               checksum = Checksum_u16(&usart_send[2],17);
               memcpy(&usart_send[19],&checksum,2);
               UART_PushFrame(usart_send,21);
               }
      //         memcpy(&Modbus_HoldReg[anc_id_recv*2],&hex_dist,4);
         /* Poll DW1000 until TX frame sent event set. See NOTE 8 below. */
         if(result==0)
         {while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))//不断查询芯片状态直到发送完成
@@ -460,12 +474,14 @@
}
int8_t correction_time;
extern uint8_t sync_seq;
extern uint16_t sync_timer;
#define TDFILTER
//#define CHECK_UID
extern uint8_t UID_ERROR;
void Anchor_App(void)
{
   uint32_t frame_len;
   uint32_t resp_tx_time;
   static u8 misdist_num;
   /* Clear reception timeout to start next ranging process. */
   dwt_setrxtimeout(0);//设定接收超时时间,0位没有超时时间
@@ -507,10 +523,10 @@
      
      if (rx_buffer[MESSAGE_TYPE_IDX] == POLL&&(anchor_type == rx_buffer[ANC_TYPE_IDX])) //判断是否是poll包数据
      {
         correction_time=tag_timer-tag_id_recv*3;
                     if(correction_time == g_com_map[COM_INTERVAL])
                        correction_time = 0;
                        tx_resp_msg[TIMECORRE] = correction_time;
         tmp_time=TIM3->CNT;
         memcpy(&tx_resp_msg[ANCTIMEMS],&sync_timer,2);
         memcpy(&tx_resp_msg[ANCTIMEUS],&tmp_time,2);
//                     if(correction_time>10)
//                     {correction_time++;}
                     
@@ -535,7 +551,7 @@
         battary = rx_buffer[BATTARY_IDX];
         button = rx_buffer[BUTTON_IDX];
         frame_seq_nb = rx_buffer[SEQUENCE_IDX];
         frame_seq_nb2 = rx_buffer[SEQUENCE_IDX];
         /* We assume that the transmission is achieved correctly, now poll for reception of expected "final" frame or error/timeout.
          * See NOTE 7 below. */
         if(result==0)
@@ -575,6 +591,10 @@
               final_msg_get_ts(&rx_buffer[FINAL_MSG_RESP_RX_TS_IDX], &resp_rx_ts);
               final_msg_get_ts(&rx_buffer[FINAL_MSG_FINAL_TX_TS_IDX], &final_tx_ts);
               #ifdef CHECK_UID
               if(UID_ERROR==1)
                  poll_rx_ts=0;
               #endif
               /* Compute time of flight. 32-bit subtractions give correct answers even if clock has wrapped. See NOTE 10 below. */
               poll_rx_ts_32 = (uint32_t)poll_rx_ts;//使用32位数据计算
               resp_tx_ts_32 = (uint32_t)resp_tx_ts;
@@ -589,38 +609,63 @@
               distance = tof * SPEED_OF_LIGHT;//距离=光速*飞行时间
               dist_no_bias = distance - dwt_getrangebias(config.chan, (float)distance, config.prf); //距离减去矫正系数
               dist_cm = dist_no_bias * 100; //dis ä¸ºå•位为cm的距离
               dist_cm = dist_no_bias * 1000; //dis ä¸ºå•位为cm的距离
//               dist[TAG_ID] = LP(dis, TAG_ID); //LP ä¸ºä½Žé€šæ»¤æ³¢å™¨ï¼Œè®©æ•°æ®æ›´ç¨³å®š
               
               /*--------------------------以下为非测距逻辑------------------------*/
               LED0_BLINK; //每成功一次通讯则闪烁一次
               g_UWB_com_interval = 0;
               dis_after_filter=dist_cm;
               hex_dist = (int16_t)(dist_cm+g_com_map[DIST_OFFSET]);
               hex_dist = dist_cm+(int16_t)g_com_map[DIST_OFFSET];
               if(hex_dist-anchor_dist_last_frm[tag_id_recv-TAG_ID_START]<15000||misdist_num>4)
               {int32_t filter_dist;
                  misdist_num=0;
               if(hex_dist<1000000&&hex_dist>-10000)
               {
                  if(tag_id_recv-TAG_ID_START<=TAG_NUM_IN_SYS)
               if(abs(hex_dist-his_dist[tag_id_recv-TAG_ID_START])<1000)
                  {
               if(abs(hex_dist-his_dist[tag_id_recv-TAG_ID_START])<10000)
               {
                  g_Tagdist[tag_id_recv-TAG_ID_START] = hex_dist;
                  anchor_dist_last_frm[tag_id_recv-TAG_ID_START] = hex_dist;
                  #ifdef TDFILTER
                  TrackingDiffUpdate(pUWBDistanceTrackingDiff, (float)hex_dist);
                  filter_dist=pUWBDistanceTrackingDiff->pos_predict/10;
                  #else
                  filter_dist=hex_dist/10;
                  #endif
                  anchor_dist_last_frm[tag_id_recv-TAG_ID_START]=filter_dist;
                  g_Tagdist[tag_id_recv]=   anchor_dist_last_frm[tag_id_recv-TAG_ID_START];
               }
               his_dist[tag_id_recv-TAG_ID_START]=hex_dist;
               g_flag_Taggetdist[tag_id_recv]=0;
               if(!g_com_map[MODBUS_MODE])
               {
                  usart_send[2] = 1;//正常模式
                  usart_send[3] = 17;//数据段长度
                  usart_send[4] = frame_seq_nb2;//数据段长度
                  memcpy(&usart_send[5],&tag_id_recv,2);
                  memcpy(&usart_send[7],&dev_id,2);
                  
               usart_send[2] = 1;//正常模式
               usart_send[3] = 17;//数据段长度
               usart_send[4] = frame_seq_nb;//数据段长度
               memcpy(&usart_send[5],&tag_id_recv,2);
               memcpy(&usart_send[7],&dev_id,2);
               memcpy(&usart_send[9],&anchor_dist_last_frm[tag_id_recv-TAG_ID_START],4);
               usart_send[13] = battary;
               usart_send[14] = button;
               checksum = Checksum_u16(&usart_send[2],17);
               memcpy(&usart_send[19],&checksum,2);
               UART_PushFrame(usart_send,21);
                  memcpy(&usart_send[9],&anchor_dist_last_frm[tag_id_recv-TAG_ID_START],4);
                  usart_send[13] = battary;
                  usart_send[14] = button;
                  checksum = Checksum_u16(&usart_send[2],17);
                  memcpy(&usart_send[19],&checksum,2);
                  UART_PushFrame(usart_send,21);
               }
               //   memcpy(&Modbus_HoldReg[tag_id_recv*2],&anchor_dist_last_frm[tag_id_recv-TAG_ID_START],4);
               Modbus_HoldReg[tag_id_recv*2]=anchor_dist_last_frm[tag_id_recv-TAG_ID_START]>>16;
               Modbus_HoldReg[tag_id_recv*2+1]=anchor_dist_last_frm[tag_id_recv-TAG_ID_START];
               //dis_after_filter = LP_Frac_Update(p_Dis_Filter, dist_cm);
                  }
               }
               }else{
                  misdist_num++;
               }
            if(anchor_dist_last_frm[tag_id_recv-TAG_ID_START]>1000)
            {anchor_dist_last_frm[tag_id_recv-TAG_ID_START]=anchor_dist_last_frm[tag_id_recv-TAG_ID_START];}
            }
         }else{
            /* Clear RX error events in the DW1000 status register. */
@@ -629,10 +674,11 @@
      }else if(rx_buffer[MESSAGE_TYPE_IDX] == SYNC)
      {
         if(rx_buffer[SYNC_SEQ_IDX]<sync_seq)
      //   if(rx_buffer[SYNC_SEQ_IDX]==2)
         {
            sync_seq=rx_buffer[SYNC_SEQ_IDX]+1;
            SysTick->VAL   =00;
            sync_timer=0;
            TIM3->CNT = sync_seq*325%1000+15;
            sync_timer = sync_seq*325/1000;
            SyncPoll(sync_seq);
         }
      }