From 20ef2d42f15f2763a5079929fc7c375e9c94b697 Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期六, 09 十一月 2019 14:16:39 +0800
Subject: [PATCH] V1.5 修改数据溢出bug

---
 源码/核心板/Src/application/dw_app.c |   68 ++++++++++++++++++++++------------
 1 files changed, 44 insertions(+), 24 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 2491416..58345cd 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"
@@ -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
@@ -106,7 +107,7 @@
 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. */
@@ -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;
@@ -315,12 +316,15 @@
 int32_t hex_dist;
 u16 checksum;
 int8_t tag_delaytime;
+extern uint16_t sync_timer;
+u16 tmp_time;
+int32_t temp_dist;
 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 +334,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. */
@@ -377,15 +381,21 @@
 			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];
-							}
-								}
-								
+			if(getsync_flag==0)
+			{
+				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(&anchor_dist_last_frm[0], &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. */
@@ -409,14 +419,21 @@
 			tag_succ_times++;
 			
 				LED0_BLINK;
-			
+//				temp_dist=anchor_dist_last_frm[0];
+//				if(anchor_dist_last_frm[0]>1000000)
+//				{
+//					temp_dist=anchor_dist_last_frm[0]-0xffffffff;
+//					temp_dist+=0xffff;
+//					anchor_dist_last_frm[0]=temp_dist;
+//				}
+
 					
 					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];;
+					hex_dist = anchor_dist_last_frm[0]+(int16_t)g_com_map[DIST_OFFSET];
 					memcpy(&usart_send[9],&hex_dist,4);
 					usart_send[13] = battary;
 					usart_send[14] = button;
@@ -460,7 +477,7 @@
 }
 int8_t correction_time;
 extern uint8_t sync_seq;
-extern uint16_t sync_timer;
+
 void Anchor_App(void)
 {
 	uint32_t frame_len;
@@ -507,10 +524,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++;}
 							
@@ -593,10 +610,12 @@
 //					dist[TAG_ID] = LP(dis, TAG_ID); //LP 为低通滤波器,让数据更稳定
 					
 					/*--------------------------以下为非测距逻辑------------------------*/
+					//dist_cm=33000;
+					
 					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(tag_id_recv-TAG_ID_START<=TAG_NUM_IN_SYS)
 					if(abs(hex_dist-his_dist[tag_id_recv-TAG_ID_START])<1000)
 					{
@@ -629,10 +648,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);
 			}
 		}

--
Gitblit v1.9.3