WXK
2024-01-26 ae42ba04bfca83ddca9df9d41061a365d4f57618
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#include <string.h>
//#include "stm32f4xx_hal.h"
#include "filters.h"
#include "ADC.h"
#include "deca_device_api.h"
#include "deca_regs.h"
#include "dw_driver.h"
#include "Spi.h"
#include "led.h"
#include "serial_at_cmd_app.h"
#include "Usart.h"
#include "global_param.h"
#include "filters.h"
#include <stdio.h>
#include "beep.h"
#include <math.h>
#include "TrackingDiff.h"
//#include "UDPClient.h"
//#include "Uart.h"
//#include "DBG.h"
#include "CRC.h"
#define DBGMSG_OUTPUT
#define SENDANCPARA_LEN 102
#define SPEED_OF_LIGHT 299702547
#define UUS_TO_DWT_TIME 65536
#define POLL_TX_TO_RESP_RX_DLY_UUS 150
#define RESP_RX_TO_FINAL_TX_DLY_UUS 410
#define RESP_RX_TIMEOUT_UUS 2000
#define DELAY_BETWEEN_TWO_FRAME_UUS 400
#define POLL_RX_TO_RESP_TX_DLY_UUS 2000
#define RESP_TX_TO_FINAL_RX_DLY_UUS 10
#define FINAL_RX_TIMEOUT_UUS 4300
 
#define     WHITELIST1_START                  0x25
#define     WHITELIST1_END                  0x26
#define     MODBUS_ID1                      0x11
#define FZ_NEARBASENUM_INDEX    10
#define FZ_NEARBASEID_INDEX         11
 
#define RESP_MSG_POLL_RX_TS_IDX  26
#define RESP_MSG_RESP_TX_TS_IDX  30
#define RESP_MSG_ANC_DISTOFFSET  34
#define MBX_ANC_NUM_IN_SYS       50
#define QUIT_SLOT_TIME    5
uint8_t lora_yingda_flag;
static uint64_t resp_tx_ts;
static uint64_t final_rx_ts;
static uint32_t status_reg,resp_tx_time,tagpos_binary;
static uint8_t frame_len,recpoll_len,current_syncid,new_tagid,seize_anchor,result,g_start_sync_flag;
static uint8_t rx_buffer[150];
static uint16_t anc_id_recv,tag_id_recv;
static uint8_t flag_syncbase,sync_seq,synclost_timer,temp_nearbaspos;
static uint16_t sync_timer;
static uint8_t send_buffer[150];
static uint8_t battary,button,ancidlist_num;
uint8_t frame_seq_nb2;
uint8_t rec_nearbase_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 int16_t rec_antdelay;
static uint16_t taglist_num=0,taglist_pos,tmp_time;
static uint16_t tagid_list[TAG_NUM_IN_SYS];
uint8_t tagbattary_list[TAG_NUM_IN_SYS];
static uint8_t tagofflinetime[TAG_NUM_IN_SYS];
static int32_t tagdist_list[TAG_NUM_IN_SYS];
static uint8_t tagpos_rec[50];
static uint64_t poll_rx_ts;
static uint8_t g_flag_Taggetdist[TAG_NUM_IN_SYS],flag_tag_distsmooth[TAG_NUM_IN_SYS],misdist_num[TAG_NUM_IN_SYS];
static uint32_t poll_tx_ts, resp_rx_ts, final_tx_ts;
static uint32_t poll_rx_ts_32, resp_tx_ts_32, final_rx_ts_32;
static double Ra, Rb, Da, Db;
static int64_t tof_dtu;
static double tof;
static double distance, dist_no_bias, dist_cm;
extern dwt_config_t config;
static double firstpath_power, rx_power,rec_firstpath_power;
static double f1, f2, r1, r2;
static uint16_t F1,F2,F3,N,C;
const double B = 131072;
const double A = 121.74;
double min_power;
static dwt_rxdiag_t d1;
static int32_t hex_dist,his_dist[TAG_NUM_IN_SYS];
extern uint16_t g_Resttimer;
static int32_t filter_dist,g_Tagdist[TAG_NUM_IN_SYS];
double LOS(dwt_rxdiag_t *dia) {
     F1 = dia->firstPathAmp1;
     F2 = dia->firstPathAmp2;
     F3 = dia->firstPathAmp3;
     N = dia->rxPreamCount;
     C = dia->maxGrowthCIR;
    
    firstpath_power=10* log10((F1*F1+F2*F2+F3*F3)/(N*N))-A;
    rx_power=10*log10(C*B/(N*N))-A;
 
 //   min_power =  - 10 * log10((F1 *F1 + F2 * F2 + F3 * F3) / (C *B));
    return min_power;
  }
static uint64_t get_tx_timestamp_u64(void)
{
    uint8_t ts_tab[5];
    uint64_t ts = 0;
    int i;
    dwt_readtxtimestamp(ts_tab);
    for (i = 4; i >= 0; i--)
    {
        ts <<= 8;
        ts |= ts_tab[i];
    }
    return ts;
}
 
static uint64_t get_rx_timestamp_u64(void)
{
    uint8_t ts_tab[5];
    uint64_t ts = 0;
    int i;
    dwt_readrxtimestamp(ts_tab);
    for (i = 4; i >= 0; i--)
    {
        ts <<= 8;
        ts |= ts_tab[i];
    }
    return ts;
}
 
static void final_msg_set_ts(uint8_t *ts_field, uint64_t ts)
{
    int i;
    for (i = 0; i < FINAL_MSG_TS_LEN; i++)
    {
        ts_field[i] = (uint8_t) ts;
        ts >>= 8;
    }
}
 
static void final_msg_get_ts(const uint8_t *ts_field, uint32_t *ts)
{
    int i;
    *ts = 0;
    for (i = 0; i < FINAL_MSG_TS_LEN; i++)
    {
        *ts += ts_field[i] << (i * 8);
    }
}
void TagListUpdate(void)
{
    uint16_t i,j=0,temp[TAG_NUM_IN_SYS];
    for(i=0;i<taglist_num;i++)
    {
        if(tagofflinetime[i]++<QUIT_SLOT_TIME)
        {
            tagid_list[j]=tagid_list[i];
            tagofflinetime[j++]=tagofflinetime[i];
        }
    }
    taglist_num=j;
}
uint16_t CmpTagInList(uint16_t tagid)
{uint16_t i;
    for(i=0;i<taglist_num;i++)
    {
        if(memcmp(&tagid,&tagid_list[i],2)==0)
            break;
    }
    tagofflinetime[i] = 0;
    return i;
}
static void resp_msg_set_ts(uint8_t *ts_field, const uint64_t ts)
{
    int i;
    for (i = 0; i < 4; i++)
    {
        ts_field[i] = (ts >> (i * 8)) & 0xFF;
    }
}
extern uint16_t current_time,start_time,end_time;
uint32_t uwb_timeout;
extern uint32_t lp_time;
//extern u8 uwb_timeout;
u16 basesystimer,uwb_losttimer;
uint8_t remotesend_state,remotesend_index;
uint16_t random_time;
u32 value;
uint8_t Anchor_RecNearPoll(uint8_t ancrec_nearbasepos) //0 mainbase  1 first near_base
{
    uint8_t motorstate;
//            tmp_time=TIM3->CNT;
            temp_nearbaspos = ancrec_nearbasepos;
            memcpy(&send_buffer[ANCHOR_ID_IDX],&dev_id,2);
            memcpy(&send_buffer[ANCTIMEMS],&basesystimer,2);
//            memcpy(&send_buffer[ANCTIMEUS],&tmp_time,2);
            send_buffer[TAGFREQ_IDX] = tag_frequency;
            memcpy(&send_buffer[TAG_ID_IDX],&tag_id_recv,2);
            poll_rx_ts = get_rx_timestamp_u64();//»ñµÃPoll°ü½ÓÊÕʱ¼äT2
 
    if(rec_nearbase_num == ancrec_nearbasepos)//ÇÀռģʽ
    {
        random_time = (GetLPTime()%100);
            resp_tx_time = (poll_rx_ts+((/*value+*/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¡£
  
    }else{
              resp_tx_time = (poll_rx_ts + ((/*value+ */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¡£
    }
            dwt_setdelayedtrxtime(resp_tx_time);//ÉèÖÃResponse·¢ËÍʱ¼äT3
            //dwt_setrxaftertxdelay(RESP_TX_TO_FINAL_RX_DLY_UUS+(rec_nearbase_num+1-ancrec_nearbasepos)*DELAY_BETWEEN_TWO_FRAME_UUS);//ÉèÖ÷¢ËÍÍê³Éºó¿ªÆô½ÓÊÕÑÓ³Ùʱ¼ä
            //dwt_setrxtimeout(FINAL_RX_TIMEOUT_UUS);//½ÓÊÕ³¬Ê±Ê±¼ä
            resp_tx_ts = (((uint64_t)(resp_tx_time & 0xFFFFFFFEUL)) << 8) ;
 
            /* Write all timestamps in the final message. See NOTE 8 below. */
            resp_msg_set_ts(&send_buffer[RESP_MSG_POLL_RX_TS_IDX], poll_rx_ts);
            resp_msg_set_ts(&send_buffer[RESP_MSG_RESP_TX_TS_IDX], resp_tx_ts);
            memcpy(&send_buffer[RESP_MSG_ANC_DISTOFFSET],&g_com_map[DIST_OFFSET],2);
 
 
            send_buffer[GROUP_ID_IDX] = group_id;
//            send_buffer[MAINBASE_INDEX]=flag_syncbase;
            send_buffer[MESSAGE_TYPE_IDX]=MBX_RESPONSE;
            send_buffer[MOTORSTATE_INDEX]=motorstate;//(remotesend_state<<4)|motorstate;
            send_buffer[MOTORSTATE_INDEX]&=0x0f;
            remotesend_state = 0;
          
            dwt_forcetrxoff();
            dwt_writetxdata(38, send_buffer, 0);//дÈë·¢ËÍÊý¾Ý
            dwt_writetxfctrl(38, 0);//É趨·¢Ëͳ¤¶È
            result = dwt_starttx(DWT_START_TX_DELAYED);
//           if(remotesend_state==0)
//           {
//                dwt_writetxdata(38, send_buffer, 0);//дÈë·¢ËÍÊý¾Ý
//                dwt_writetxfctrl(38, 0);//É趨·¢Ëͳ¤¶È
//               result = dwt_starttx(DWT_START_TX_DELAYED);
////               uwb_timeout = 0;
//               if(result == 0)
//               {
             uwb_timeout=lp_time;
               while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & SYS_STATUS_TXFRS ))///²»¶Ï²éѯоƬ״ֱ̬µ½½ÓÊճɹ¦»òÕß³öÏÖ´íÎó
            {
 
//                        current_time=HAL_LPTIM_ReadCounter(&hlptim1);
//        if(current_time>end_time+((32768/1000)*3)||current_time<end_time+15000)
//        { 
////        printf("debug2 timec:%d timee:%d\r\n",current_time,end_time);
//        break;}
                    if(lp_time-uwb_timeout>2)
                        break;
            };
//        }
//            dwt_rxenable(0);
 
}
 
uint16_t target_tagid[12] = {0x1316,0x1394,0x1265,0x1318,0x1306,0x1304,0x1376,0x1312,0x1277,0x1388,0x1140,0x1392};
uint16_t Lora_tx_ancid[50];
uint16_t Lora_tx_ancdist[50];
uint8_t Lora_tx_anc_electricity[50];
uint8_t Lora_tx_ancnum=0;
uint8_t break_flag ;
uint16_t RX_TIMEOUTs_UWB;
void Anchor_App(void)
{
    
    u8 send_len,i;
    u16 tempid;
    uint32_t rec_syncid;
    uint16_t checksum;
   uint8_t kk;
    
    dwt_setrxtimeout(0);//É趨½ÓÊÕ³¬Ê±Ê±¼ä£¬0λûÓг¬Ê±Ê±¼ä
    dwt_rxenable(0);//´ò¿ª½ÓÊÕ
    
    
    
    start_time=HAL_LPTIM_ReadCounter(&hlptim1);
        end_time=start_time+((32768/1000)*2);
        if(end_time>=32768)
        {end_time-=32768;}
        current_time=HAL_LPTIM_ReadCounter(&hlptim1);
    while(current_time<end_time||current_time>end_time+15000)
        {
            current_time=HAL_LPTIM_ReadCounter(&hlptim1);
            while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))//²»¶Ï²éѯоƬ״ֱ̬µ½½ÓÊճɹ¦»òÕß³öÏÖ´íÎó
            { 
                        current_time=HAL_LPTIM_ReadCounter(&hlptim1);
                                 if(current_time>=end_time&&current_time<end_time+15000)
                                        break;
            };
    
    if (status_reg & SYS_STATUS_RXFCG && status_reg!=0xffffffff)//³É¹¦½ÓÊÕ
        {
            uint16_t tag_recv_interval;
            float temp_tagpos;
             
        dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);                        
        frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023;//»ñµÃ½ÓÊÕÊý¾Ý³¤¶È
        recpoll_len = frame_len;
        dwt_readrxdata(rx_buffer, frame_len, 0);//¶ÁÈ¡½ÓÊÕÊý¾Ý
        dwt_setrxtimeout(0);//DELAY_BETWEEN_TWO_FRAME_UUS*(nearbase_num+1-recbase_num)+10);//É趨½ÓÊÕ³¬Ê±Ê±¼ä£¬0λûÓг¬Ê±Ê±¼ä
        dwt_rxenable(0);
        memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2);
        //½«ÊÕµ½µÄtag_id·Ö±ðдÈë¸÷´ÎͨѶµÄ°üÖУ¬Îª¶à±êǩͨѶ·þÎñ£¬·ÀÖ¹Ò»´ÎͨѶÖнÓÊÕµ½²»Í¬ID±êÇ©µÄÊý¾Ý
        memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],2);
 
//        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
//        printf("ID %x\r\n",tag_id_recv);            
        switch(rx_buffer[MESSAGE_TYPE_IDX])
        {uint8_t target_tagi;
            case MBX_REG:
//                dwt_readdiagnostics(&d1);
//                LOS(&d1);
//                   usart_send[2]=0x14;
//                usart_send[3]=20;
//                memcpy(&usart_send[4],&dev_id,2);
//                memcpy(&usart_send[6],&tag_id_recv,2);
//                memcpy(&usart_send[8],&rx_buffer[10],9);
//                usart_send[17] = firstpath_power;
//                memcpy(&usart_send[18],&rx_buffer[19],2);
//                checksum = Checksum_u16(&usart_send[2],20);
//                memcpy(&usart_send[22],&checksum,2);
//                HexToAsciiSendUDP(usart_send,24);
                break;
            case MBX_POLL:
              if(group_id==rx_buffer[GROUP_ID_IDX])
              {   
//                printf("timec:%d timee:%d\r\n",current_time,end_time);                  
                dwt_readdiagnostics(&d1);
                LOS(&d1);
                if(firstpath_power<-100)
                {
                    break;
                }
                frame_seq_nb2 = rx_buffer[SEQUENCE_IDX];
                battary = rx_buffer[BATTARY_IDX];
                button = rx_buffer[BUTTON_IDX];
                rec_nearbase_num=rx_buffer[FZ_NEARBASENUM_INDEX];                
                memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],2);
                memcpy(&rec_ancidlist,&rx_buffer[FZ_NEARBASEID_INDEX],2*rec_nearbase_num);
                memcpy(&rec_ancdistlist,&rx_buffer[FZ_NEARBASEID_INDEX+2*rec_nearbase_num],2*rec_nearbase_num);
                memcpy(&rec_antdelay,&rx_buffer[FZ_NEARBASEID_INDEX+rec_nearbase_num*4],2);
                uwb_losttimer = 0;
//                wltag_state=RANGE;
                for(u8 i=0;i<rec_nearbase_num;i++)
                {
                    if(rec_ancidlist[i] == dev_id)
                    {   
                        for(kk=0;kk<Lora_tx_ancnum;kk++)
                        {
                          if(Lora_tx_ancid[kk]==tag_id_recv)
                          {
//                          Lora_tx_ancnum--;
                          Lora_tx_ancdist[kk]=rec_ancdistlist[i];
                              Lora_tx_anc_electricity[kk]=battary;
                              break_flag=1;
                              break;
                          }
                        }
                        if(break_flag)
                        {
                            break_flag=0;
                            break;
                        }
                        Lora_tx_ancid[Lora_tx_ancnum]=tag_id_recv;
                        Lora_tx_ancdist[Lora_tx_ancnum]=rec_ancdistlist[i];
                        Lora_tx_anc_electricity[Lora_tx_ancnum]=battary;
//                        printf("½ÓÊÕµ½uwb²â¾à:%d",Lora_tx_ancdist[0]);
                        Lora_tx_ancnum++;
                        RX_TIMEOUTs_UWB++;
                     }
                }
////            #ifdef USART_ALL_OUTPUT
//                    usart_send[2] = 0x12;//Õý³£Ä£Ê½ 
//                    usart_send[3] = 8+rec_nearbase_num*4;//Êý¾Ý¶Î³¤¶È
//                    memcpy(&usart_send[4],&tag_id_recv,2);
//                    usart_send[6] = frame_seq_nb2;//°üÐò
//                    power_and_key=battary<<1;//°Ñ°´¼ü¸úµçÁ¿2ºÏ1£¬·¢³öÈ¥
//                    power_and_key=power_and_key+button;
//                    memcpy(usart_send+7,&power_and_key,1);
//                    usart_send[8] = lora_yingda_flag;//loraµÈ´ýÓ¦´ðλ
//                    usart_send[9] = rec_nearbase_num;//loraµÈ´ýÓ¦´ðλ
////                    usart_send[9] = lora_yingda_flag;
//                    memcpy(&usart_send[10],&rec_ancidlist,2*rec_nearbase_num);                
//                    memcpy(&usart_send[10+rec_nearbase_num*2],&rec_ancdistlist,2*rec_nearbase_num);
//                    checksum = Checksum_u16(&usart_send[2],8+4*rec_nearbase_num);
//                    memcpy(&usart_send[10+4*rec_nearbase_num],&checksum,2);
////                    UART_PushFrame(usart_send,12+rec_nearbase_num*4);
////            #endif
 
                taglist_pos=CmpTagInList(tag_id_recv);
                if(taglist_num>=TAG_NUM_IN_SYS)
                    break;
                if(taglist_pos==taglist_num)
                {
                    taglist_pos=taglist_num;
                    tagid_list[taglist_num++]=tag_id_recv;
                    
                    new_tagid=1;
                }else{
                    new_tagid=0;
                }
                temp_tagpos=round((float)(sync_timer%g_com_map[COM_INTERVAL])/slottime);
                tagpos_rec[(uint8_t)temp_tagpos]=1;
                
            
                if(rec_nearbase_num>ancidlist_num)
                    {
                        ancidlist_num=rec_nearbase_num;
                        memcpy(ancidlist_rec,&rx_buffer[FZ_NEARBASEID_INDEX],rec_nearbase_num*2);
                    }
                    
                for(i=0;i<rec_nearbase_num;i++)
                {
                     memcpy(&tempid,&rx_buffer[FZ_NEARBASEID_INDEX+i*2],2);
                    if(tempid==dev_id)
                    {
                        seize_anchor=0;  //·ÇÇÀÕ¼¡£ÒÑ´æÔÚÁбíÖÐ
                        Anchor_RecNearPoll(i);    
                        break;
                    }                        
                }
                
                    if(i==rec_nearbase_num)
                    {
                        seize_anchor=1;   //ÇÀÕ¼anchor
                        Anchor_RecNearPoll(i);    
                    }
                }
                
                break;        
        }    
    
    }else{
//        printf("debug4 timec:%d timee:%d\r\n",current_time,end_time);
        dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);
        dwt_rxenable(0);
        }
    }
        dwt_forcetrxoff();
        dwt_entersleep();
 
}