From 3203b700d2d66b316843f310b135c916a74fafe6 Mon Sep 17 00:00:00 2001
From: yincheng.zhong <634916154@qq.com>
Date: 星期二, 28 三月 2023 10:26:43 +0800
Subject: [PATCH] 1

---
 Src/application/dw_app.c |  121 +++++++++++++++++++++++-----------------
 1 files changed, 70 insertions(+), 51 deletions(-)

diff --git a/Src/application/dw_app.c b/Src/application/dw_app.c
index edf3fe6..fd3aa5c 100644
--- a/Src/application/dw_app.c
+++ b/Src/application/dw_app.c
@@ -29,7 +29,7 @@
 #include <stdio.h>
 #include "beep.h"
 #include "modbus.h"
-
+#include "CRC.h"
 /*------------------------------------ Marcos ------------------------------------------*/
 /* Inter-ranging delay period, in milliseconds. */
 #define RNG_DELAY_MS 100
@@ -120,7 +120,7 @@
 uint16_t current_slotpos = 0;
 
 uint8_t rec_anc_sync_sec,tagpoll_sync_sec,current_sync_sec,flag_tagnewpos;
-
+void GetNearMsg(void);
 static uint64_t get_tx_timestamp_u64(void)
 {
     uint8_t ts_tab[5];
@@ -471,7 +471,7 @@
 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,temptimer;
-u8 motor_state,rec_remotepara_state,rec_remotepara[80];
+static u8 motor_state,rec_remotepara_state,rec_remotepara[80];
 
 
 #define SINGLEPOLL_BASENUM 5
@@ -615,6 +615,7 @@
 			trygetnearmsg_times = 0;
             tag_state = GETNEARMSG;
             mainbase_id = mindist_ancid;   
+            GetNearMsg();
     }
    
 	if(getsync_flag==0)
@@ -703,7 +704,8 @@
 				last_lpcount = lpcount;
 }
 uint16_t rec_tagperiod;
-u8 nearmsg_mainbase=0,rec_tagpos_emptylist[32],mainbase_type;;
+u8 nearmsg_mainbase=0,rec_tagpos_emptylist[32],mainbase_type;
+int8_t notenoughdist_count=0;
 void GetNearMsg(void)
 {
 	u32 start_poll,frame_len;
@@ -714,6 +716,7 @@
         nearbase_distlist[nearmsg_i] = 0x1ffff;
     }
 //mainbase_id = 0x9818;
+    notenoughdist_count = -2;
 	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] = POS_MSG;
@@ -760,6 +763,7 @@
             tagpoll_sync_sec = current_slotpos/200;
             tagslotpos = current_slotpos%200;
             max_slotpos = rec_tagperiod*20;
+            tag_frequency = 10/rec_tagperiod;
 			tag_state=NEARPOLL;
 		}
 	}else{
@@ -795,11 +799,14 @@
 uint8_t pack_length = 0,pack_index = 0,pack_msgtype = 0;
 uint8_t motor_flag;
 uint16_t rec_ancpos;
+uint8_t dist_noutenough_count;
+uint8_t ancdist_hist[11];
+uint16_t calCRC;
 void NearPoll(void)
 {
 	
 	uint32_t temp1,temp2,dw_systime,mindist_slavebaseid;
-    static u8 notenoughdist_count=0;
+    
 	uint32_t final_tx_time;
 	u32 start_poll;
 	u8 i,j,getsync_flag=0,timeout,get_newdist,pos_nochange=1;
@@ -819,12 +826,7 @@
 
 	recbase_num=0;
 //	motor_state=0;
-    if(motor_state!=0&&motor_state!=3)
-    {
-        motor_flag = 1;
-    }else{
-        motor_flag = 0;
-    }
+
 	
 	tx_near_msg[BATTARY_IDX] = bat_percent;
 	tx_near_msg[BUTTON_IDX] = userkey_state|stationary_flag<<1|gotosleep_flag<<2|motor_flag<<5;
@@ -962,8 +964,7 @@
 									mainbase_dist=temp_dist;
                                     nearbase_distlist[0] = temp_dist;
                                     base_mindist = temp_dist;
-									if((rx_buffer[MOTORSTATE_INDEX]&0xf)!=3)
-										motor_state=rx_buffer[MOTORSTATE_INDEX]&0xf;
+
                                     if(frame_len==28)
                                      {
                                          memcpy(&rec_maxrangelen,&rx_buffer[MAXRANGE_DISTANCE],2);
@@ -979,9 +980,7 @@
                                              outrange_times = 0;
                                          }
                                      }
-                                 
-									
-									
+                                 									
 									mainbase_lost_count=0;
 									flag_finalsend=1;
 									memcpy(&mainbase_dist,&rx_buffer[DIST_IDX],4);
@@ -989,7 +988,7 @@
 
 									flag_getresponse=1;								
 									//时间同步
-								tmp_time=tmp_time+450;
+                                    tmp_time=tmp_time+450;
 									if(tmp_time>999)
 									{
 										tmp_time-=999;
@@ -1036,6 +1035,7 @@
                                             mainbase_id = rec_nearbaseid;
                                             tag_state = GETNEARMSG;
 											trygetnearmsg_times = 0;
+                                            GetNearMsg();
                                         }
                                     #endif
 
@@ -1066,9 +1066,10 @@
 					changemainbase_count++;
 					if(changemainbase_count>tag_frequency*2)
 					{
-										mainbase_id = mindist_slavebaseid;
-										tag_state = GETNEARMSG;
-										trygetnearmsg_times = 0;
+                        mainbase_id = mindist_slavebaseid;
+                        tag_state = GETNEARMSG;
+                        trygetnearmsg_times = 0;
+                        GetNearMsg();
 					}
 				}else{
 					changemainbase_count = 0;
@@ -1092,37 +1093,45 @@
 			}	
 
 			if(para_update)
-			{
-			
-				pack_msgtype = rec_remotepara[0];
-				pack_index = rec_remotepara[1];
-				pack_length = rec_remotepara[2];
-				if(pack_msgtype==2)
-				{
-					if( pack_index == MOTOR_ONTIME_INDEX)
-					{
-                       // if(motor_keeptime==0)
-                            motor_keeptime = rec_remotepara[3];
-					}else{
-                        if(pack_index<200)
+			{   
+               
+                calCRC = CRC_Compute(rec_remotepara,5);
+                if(memcmp(&calCRC,&rec_remotepara[5],2)==0)
+                {
+                    pack_msgtype = rec_remotepara[0];
+                    pack_index = rec_remotepara[1];
+                    pack_length = rec_remotepara[2];
+                    if(pack_msgtype==2)
+                    {
+                        if( pack_index == MOTOR_ONTIME_INDEX)
                         {
-                            memcpy((uint8_t*)&g_com_map + pack_index, &rec_remotepara[3], pack_length);
-                            //返回一个error状态
-                            //SendComMap(pack_datalen,pack_index);
-                            save_com_map_to_flash();
-                            delay_ms(100);
-                            NVIC_SystemReset();
+                           // if(motor_keeptime==0)
+                                motor_keeptime = rec_remotepara[3];
+                        }else{
+                            if(pack_index<200)
+                            {
+                                memcpy((uint8_t*)&g_com_map + pack_index, &rec_remotepara[3], pack_length);
+                                //返回一个error状态
+                                //SendComMap(pack_datalen,pack_index);
+                                save_com_map_to_flash();
+                                delay_ms(100);
+                                NVIC_SystemReset();
+                            }
                         }
-					}
-				}
+                    }
+                }
 			}
-            if(get_newdist>2)
+            if(get_newdist>=2)
             {
                 notenoughdist_count = 0;
             }else{
                 if(notenoughdist_count++>3)
                 {
                     notenoughdist_count = 0;
+                    current_slotpos = start_count%max_slotpos;  //如果丢失连接超过5次就随机换个时间片
+                    tagpoll_sync_sec = current_slotpos/200;
+                    tagslotpos = current_slotpos%200;
+                    flag_tagnewpos = 0;
              //       tag_state = DISCPOLL;
                 }
             }
@@ -1131,8 +1140,9 @@
                 if(nearbaseid_list[i]!=nearbaseid_list2[i])
                 {
                     tag_state = GETNEARMSG;
-									trygetnearmsg_times = 0;
-									nearbaseid_list0[i]=1;
+                    trygetnearmsg_times = 0;
+                    nearbaseid_list0[i]=1;
+                    GetNearMsg();
                 }
 			}
             if(pos_nochange)
@@ -1150,7 +1160,7 @@
 {
 	static u8 regpoll_count=0;
 	mainbase_lost_count = 0;
-		tag_frequency = REGISTER_FREQUENCY;
+		//tag_frequency = REGISTER_FREQUENCY;
 		bigslot_num = TOTAL_SLOTNUM/tag_frequency;
 		regpoll_count++;
 		if(regpoll_count%2)
@@ -1218,17 +1228,17 @@
 								}
 							}
 }
+extern void FZNearPoll(void);
 u32 id,error_times=0;
-
+u8 ttt = 1;
 extern float Height;
-void Tag_App(void)//发送模式(TAG标签)
+void UWBWkWp(void)
 {
-	static uint8_t diccount = 0;
-	//LED0_ON;
 	SPIx_CS_GPIO->BRR = SPIx_CS;
 	delay_us(700);
 	SPIx_CS_GPIO->BSRR = SPIx_CS;
 	id =  dwt_readdevid() ;
+    
 	    while (DWT_DEVICE_ID != id) 
     {
 		//	Dw1000_Init();
@@ -1241,6 +1251,13 @@
         }
     }
     error_times = 0;
+}
+
+void Tag_App(void)//发送模式(TAG标签)
+{
+	static uint8_t diccount = 0,diccount2;
+	//LED0_ON;
+    UWBWkWp();
 	switch(tag_state)
 	{
 		case REGPOLL:
@@ -1284,7 +1301,9 @@
         case SINGLEPOLL:
           //  Poll();
             break;
-	}
-     userkey_state = !GET_USERKEY;
+	}       
+    FZNearPoll();
+    dwt_forcetrxoff();
+    userkey_state = !GET_USERKEY;
 	dwt_entersleep();
 }

--
Gitblit v1.9.3