From 1ee47e9f40e3a7c1681d61a89a9367c80a561a0f Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期四, 04 三月 2021 10:35:31 +0800
Subject: [PATCH] V1.41 修改到ch3,preamble 改成64

---
 源码/核心板/Src/application/dw_app.c |   70 ++++++++++++++++++++++++----------
 1 files changed, 49 insertions(+), 21 deletions(-)

diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c"
index 3d4a60d..28fa6e4 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c"
@@ -28,7 +28,7 @@
 #include <stdio.h>
 #include "beep.h"
 #include "modbus.h"
-
+//#define DEBUG_OUTPUT
 /*------------------------------------ Marcos ------------------------------------------*/
 /* Inter-ranging delay period, in milliseconds. */
 #define RNG_DELAY_MS 100
@@ -90,16 +90,16 @@
 /*------------------------------------ Variables ------------------------------------------*/
 /* Default communication configuration. We use here EVK1000's default mode (mode 3). */
 static dwt_config_t config = {
-	2,               /* Channel number. */
+	3,               /* Channel number. */
 	DWT_PRF_64M,     /* Pulse repetition frequency. */
-	DWT_PLEN_128,    /* Preamble length. */
+	DWT_PLEN_64,    /* Preamble length. */
 	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. */
 	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. */
+	(65 + 8 - 8)    /* SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only. */
 };
 
 /* Frames used in the ranging process. See NOTE 2 below. */
@@ -237,12 +237,14 @@
 	if(clear_judge_cnt++>1000)  //设定1S分频,每秒进一次。判断标志位大于等于2,2s没收到数据就把数据变成0xffff,不触发警报。
 	{
 		clear_judge_cnt=0;
-		for(i=0;i<255;i++)
+		for(i=0;i<100;i++)
 		{
 			g_flag_Taggetdist[i]++;
 			if(g_flag_Taggetdist[i]>=2)
 			{
 				g_Tagdist[i]=0xffff;
+				Modbus_HoldReg[i*2]=1;
+				Modbus_HoldReg[i*2+1]=0xffff;
 			}
 		}
 	}
@@ -327,6 +329,7 @@
 extern uint16_t sync_timer;
 u16 tmp_time;
 extern float dw_vbat;
+extern u16 slottime,max_slotnum,current_slotpos,tyncpoll_time;
 void Tag_App(void)//发送模式(TAG标签)
 {
 	uint32_t frame_len;
@@ -336,7 +339,6 @@
 	u8 bat_percent;
 	//LED0_ON;
 	dwt_forcetrxoff();
-	g_Resttimer=0;
     dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);			//设置发送后开启接收,并设定延迟时间
     dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);		
 	tag_succ_times = 0;
@@ -359,6 +361,9 @@
 	 * set by dwt_setrxaftertxdelay() has elapsed. */
 	dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);//开启发送,发送完成后等待一段时间开启接收,等待时间在dwt_setrxaftertxdelay中设置
 	start_poll = time32_incr;
+               #ifdef DEBUG_OUTPUT
+        printf("P包发送,基站ID: %d .\r\n",i);
+            #endif
 	/* We assume that the transmission is achieved correctly, poll for reception of a frame or error/timeout. See NOTE 8 below. */
 	while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))//不断查询芯片状态直到成功接收或者发生错误
 	{ if(time32_incr - start_poll>20)
@@ -437,16 +442,20 @@
 			dwt_writetxfctrl(sizeof(tx_final_msg), 0);//设定发送数据长度
 			result=dwt_starttx(DWT_START_TX_DELAYED);//设定为延迟发送
 			
+             #ifdef DEBUG_OUTPUT
+            printf("F包发送,基站ID: %d .\r\n",i);
+            #endif
+            
 			tag_succ_times++;
 			
 				LED0_BLINK;
-				
+				g_Resttimer=0;
 					memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2);
 					if(hex_dist2!=0xffff)
 					{	
 					g_Tagdist[anc_id_recv]=	hex_dist2;
 					g_flag_Taggetdist[anc_id_recv]=0;
-					
+
 					if(!g_com_map[MODBUS_MODE])
 					{
 					hex_dist2 = hex_dist2;		
@@ -467,7 +476,9 @@
 		//			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))//不断查询芯片状态直到发送完成
+			{
+			
+				while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))//不断查询芯片状态直到发送完成
 			{ };
 		}
 			/* Clear TXFRS event. */
@@ -484,6 +495,9 @@
 	}
 	else
 	{
+          #ifdef DEBUG_OUTPUT
+            printf("R包失败错误信息: %x .\r\n",status_reg);
+            #endif
 		/* Clear RX error events in the DW1000 status register. */
 		dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);
 		random_delay_tim = DFT_RAND_DLY_TIM_MS;
@@ -491,10 +505,10 @@
 //	deca_sleep(10);
 }
 //	dwt_entersleep();
-//	if(tag_succ_times<g_com_map[MIN_REPORT_ANC_NUM])
-//	{
-//	//poll_timer +=time32_incr&0x7+3;
-//	}
+	if(tag_succ_times<1)
+	{
+		tyncpoll_time=(current_slotpos--%max_slotnum)*slottime;
+	}
 
 	/* Execute a delay between ranging exchanges. */
 	
@@ -504,11 +518,12 @@
 #define TDFILTER
 //#define CHECK_UID
 extern uint8_t UID_ERROR;
+extern u16 dist_threshold;
+u8 misdist_num[TAG_NUM_IN_SYS];
 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位没有超时时间
 
@@ -519,7 +534,6 @@
 	while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))&&!g_start_send_flag&&!g_start_sync_flag)//不断查询芯片状态直到接收成功或者出现错误
 	{ 
 		IdleTask();
-		g_Resttimer=0;
 	};
 
 	if (status_reg & SYS_STATUS_RXFCG)//成功接收
@@ -581,6 +595,9 @@
 			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. */
+            #ifdef DEBUG_OUTPUT
+            printf("收到POLL包,标签ID: %d .\r\n",tag_id_recv);
+            #endif
 			if(result==0)
 			{
 				while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))///不断查询芯片状态直到接收成功或者出现错误
@@ -640,15 +657,19 @@
 //					dist[TAG_ID] = LP(dis, TAG_ID); //LP 为低通滤波器,让数据更稳定
 					
 					/*--------------------------以下为非测距逻辑------------------------*/
+             #ifdef DEBUG_OUTPUT
+            printf("收到FINAL包,标签ID: %d .\r\n",tag_id_recv);
+            #endif
 					LED0_BLINK; //每成功一次通讯则闪烁一次
 					g_UWB_com_interval = 0;
-					
+					g_Resttimer=0;
 					hex_dist = dist_cm+(int16_t)g_com_map[DIST_OFFSET]*10;
 					if(tag_id_recv-TAG_ID_START<=TAG_NUM_IN_SYS)
 					{
-					if(hex_dist-his_dist[tag_id_recv-TAG_ID_START]<15000||misdist_num>4)
-					{int32_t filter_dist;
-						misdist_num=0;
+					if(abs(hex_dist-his_dist[tag_id_recv-TAG_ID_START])<dist_threshold||misdist_num[tag_id_recv-TAG_ID_START]>4)
+					{
+						int32_t filter_dist;
+						misdist_num[tag_id_recv-TAG_ID_START]=0;
 					if(hex_dist<1000000&&hex_dist>-10000)
 					{		
 						#ifdef TDFILTER
@@ -683,13 +704,17 @@
 					//dis_after_filter = LP_Frac_Update(p_Dis_Filter, dist_cm);
 						}
 					
-					}else{
-						misdist_num++;
+					}
+					else{
+						misdist_num[tag_id_recv-TAG_ID_START]++;
 					}
 				}
 				}
 			}else{
 				/* Clear RX error events in the DW1000 status register. */
+            #ifdef DEBUG_OUTPUT
+            printf("F包失败错误信息: %x .\r\n",status_reg);
+            #endif
 				dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);
 			}
 		}else if(rx_buffer[MESSAGE_TYPE_IDX] == SYNC)
@@ -705,6 +730,9 @@
 	}
 	else
 	{
+         #ifdef DEBUG_OUTPUT
+            printf("P包失败错误信息: %x .\r\n",status_reg);
+            #endif
 		/* Clear RX error events in the DW1000 status register. */
 		dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);
 	}

--
Gitblit v1.9.3