From db36f74f33fca1df7b2ac72e54178db4d14b00a2 Mon Sep 17 00:00:00 2001
From: WXK <287788329@qq.com>
Date: 星期五, 07 二月 2025 10:12:46 +0800
Subject: [PATCH] 2.11,修改重启bug当周围防撞标签多的时候

---
 Src/application/dw_app.c |  111 ++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 72 insertions(+), 39 deletions(-)

diff --git a/Src/application/dw_app.c b/Src/application/dw_app.c
index 5c0f4c3..b460ef3 100644
--- a/Src/application/dw_app.c
+++ b/Src/application/dw_app.c
@@ -80,8 +80,8 @@
 	NEARPOLL,
     SINGLEPOLL,
 }tag_state=STARTPOLL;
-static dwt_config_t config = {
-	2,               /* Channel number. */
+ dwt_config_t config = {
+	5,               /* Channel number. */
 	DWT_PRF_64M,     /* Pulse repetition frequency. */
 	DWT_PLEN_128,    /* Preamble length. */
 	DWT_PAC8,        /* Preamble acquisition chunk size. Used in RX only. */
@@ -118,7 +118,7 @@
 LPFilter_Frac* p_Dis_Filter;		//测距用的低通滤波器
 
 
-
+void GetNearMsg(void);
 static uint64_t get_tx_timestamp_u64(void)
 {
     uint8_t ts_tab[5];
@@ -189,6 +189,29 @@
     /* Set expected response's delay and timeout. See NOTE 4 and 5 below.
      * As this example only handles one incoming frame with always the same delay and timeout, those values can be set here once for all. */
 				//设置接收超时时间
+}
+uint8_t uwb_onflag=1;
+void UWB_RXON()
+{
+    if(!g_com_map[MODBUS_MODE])
+    {
+    Dw1000_Init();
+    uwb_onflag=1;
+    g_com_map[UWB_ONFLAG]=uwb_onflag;
+    save_com_map_to_flash();    
+    }
+}
+void UWB_RXOFF()
+{
+    if(!g_com_map[MODBUS_MODE])
+    {
+    dwt_setinterrupt(  DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 0);
+    dwt_forcetrxoff();
+    dwt_entersleep();
+    uwb_onflag=0;
+    g_com_map[UWB_ONFLAG]=uwb_onflag;
+    save_com_map_to_flash();    
+    }
 }
 void Dw1000_App_Init(void)
 {
@@ -610,8 +633,9 @@
     if(minddist!=0x1ffff&&minddist!=0)
     {
 			trygetnearmsg_times = 0;
-            tag_state = GETNEARMSG;
+            tag_state = GETNEARMSG;            
             mainbase_id = mindist_ancid;   
+            GetNearMsg();
     }
    
 	if(getsync_flag==0)
@@ -1055,18 +1079,18 @@
                                     }
 									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&&nearbase_switchdistlist[rec_nearbasepos]==1)
+										if(temp_dist<salvebase_mindist&&nearbase_switchdistlist[rec_nearbasepos]!=0&&(nearbase_switchdistlist[rec_nearbasepos]==1||temp_dist<nearbase_switchdistlist[rec_nearbasepos]))
 									{
 										salvebase_mindist = temp_dist;
 										mindist_slavebaseid = rec_nearbaseid;
 									}
 									#ifdef SWITCHBASE_ZHUANDIAN
-                                       if(temp_dist<nearbase_switchdistlist[rec_nearbasepos]&&nearbase_switchdistlist[rec_nearbasepos]>1&&temp_dist>0)
-									  {
-                                            mainbase_id = rec_nearbaseid;
-                                            tag_state = GETNEARMSG;
-											trygetnearmsg_times = 0;
-                                        }
+//                                       if(temp_dist<nearbase_switchdistlist[rec_nearbasepos]&&nearbase_switchdistlist[rec_nearbasepos]>1&&temp_dist>0)
+//									  {
+//                                            mainbase_id = rec_nearbaseid;
+//                                            tag_state = GETNEARMSG;
+//											trygetnearmsg_times = 0;
+//                                        }
                                     #endif
 
 						}								
@@ -1094,23 +1118,27 @@
 				if(salvebase_mindist<mainbase_dist- THRESHOLD_CHANGE_MAINBASE_DIST )
 				{
 					changemainbase_count++;
-					if(changemainbase_count>tag_frequency*2)
+					if(changemainbase_count>2)
 					{
-										mainbase_id = mindist_slavebaseid;
-										tag_state = GETNEARMSG;
-										trygetnearmsg_times = 0;
+                        changemainbase_count = 0;
+                        mainbase_id = mindist_slavebaseid;
+                        tag_state = GETNEARMSG;                    
+                        trygetnearmsg_times = 0;
+                        GetNearMsg();
 					}
 				}else{
 					changemainbase_count = 0;
 				}
 			#endif
-			}else if(mainbase_lost_count>5*tag_frequency)
+			}else if(mainbase_lost_count>10)
 			{
+                mainbase_lost_count = 0;
 				tag_state = DISCPOLL;
+                DiscPoll();
 			}	
 			if(mainbase_lost_count!=0)
 			{
-				if(mainbase_lost_count<=tag_frequency*1)
+				if(mainbase_lost_count<=5)
 				{NextSlotDelayMs(0);
 				}else{
 					NextSlotDelayMs(0);
@@ -1167,6 +1195,7 @@
                 if(nearbaseid_list[i]!=nearbaseid_list2[i])
                 {
                     tag_state = GETNEARMSG;
+                    GetNearMsg();
 									trygetnearmsg_times = 0;
 									nearbaseid_list0[i]=1;
                 }
@@ -1212,10 +1241,10 @@
 		IdleTask();
 		
 	};
-		if(status_reg==0xffffffff)
-					{
-						NVIC_SystemReset();
-					}
+//		if(status_reg==0xffffffff)
+//					{
+//						NVIC_SystemReset();
+//					}
 					if (status_reg & SYS_STATUS_RXFCG)//如果成功接收
 						{
 							dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);//清楚寄存器标志位				
@@ -1263,27 +1292,27 @@
 							}
 }
 u32 id,error_times=0;
-
 extern float Height;
 void Tag_App(void)//发送模式(TAG标签)
 {
 	
-	//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();
-			id =  dwt_readdevid() ;
-        IdleTask();	
-        if(error_times++>20)
-        {
-            printf("DW ID ERROR.\r\n");
-            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
-        }
-    }
+//	//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();
+//			id =  dwt_readdevid() ;
+//        IdleTask();	
+//        if(error_times++>20)
+//        {
+//            printf("DW ID ERROR.\r\n");
+//            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+//        }
+//    }
+    Dw1000_Init();
     error_times = 0;
 	switch(tag_state)
 	{
@@ -1317,5 +1346,9 @@
             break;
 	}
      userkey_state = !GET_USERKEY;
-	dwt_entersleep();
+           dwt_forcetrxoff();
+       dwt_setinterrupt(  DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 1);
+		dwt_setrxtimeout(0);//设定接收超时时间,0位没有超时时间
+        dwt_rxenable(0);
+	//dwt_entersleep();
 }

--
Gitblit v1.9.3