From a268b9fa1c215ae353de26fb137665232381debe Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期四, 20 九月 2018 21:16:18 +0800
Subject: [PATCH] 上海调试完成,可通讯,可设ID

---
 源码/核心板/Src/application/dw_app.c |   50 +++++++++++++++++++++++++++++++-------------------
 1 files changed, 31 insertions(+), 19 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 84ee3d2..c5c6329 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"
@@ -32,6 +32,7 @@
 #include "filters.h"
 
 
+
 /*------------------------------------ Marcos ------------------------------------------*/
 /* Inter-ranging delay period, in milliseconds. */
 #define RNG_DELAY_MS 100
@@ -69,8 +70,10 @@
 #define FINAL_MSG_TS_LEN 4
 
 #define GROUP_ID_IDX   				0
-#define SOURCE_ID_IDX    			1
-#define MESSAGE_TYPE_IDX 			3
+#define ANCHOR_ID_IDX    			1
+#define TAG_ID_IDX    				3
+#define MESSAGE_TYPE_IDX 			5
+#define DIST_IDX 							6
 
 #define POLL     					0x01
 #define RESPONSE 					0x02
@@ -240,7 +243,16 @@
     dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);			//设置发送后开启接收,并设定延迟时间
     dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);						//设置接收超时时间
 }
-
+void Dw1000_App_Init(void)
+{
+	tx_poll_msg[MESSAGE_TYPE_IDX]=POLL;
+	tx_resp_msg[MESSAGE_TYPE_IDX]=RESPONSE;
+	tx_final_msg[MESSAGE_TYPE_IDX]=FINAL;
+	memcpy(&tx_poll_msg[TAG_ID_IDX], &g_com_map[DEV_ID], 2);
+	memcpy(&tx_final_msg[TAG_ID_IDX], &g_com_map[DEV_ID], 2);
+	memcpy(&tx_resp_msg[ANCHOR_ID_IDX], &g_com_map[DEV_ID], 2);
+	
+}	
 void tag_sleep_configuraion(void)
 {
 	dwt_configuresleep(0x940, 0x7);
@@ -257,7 +269,7 @@
 	GPIO_SetBits(SPIx_GPIO, SPIx_CS);
 	
 	/* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */
-	tx_poll_msg[ALL_MSG_SN_IDX] = frame_seq_nb;
+
 	dwt_writetxdata(sizeof(tx_poll_msg), tx_poll_msg, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
 	dwt_writetxfctrl(sizeof(tx_poll_msg), 0);//设置超宽带发送数据长度
 
@@ -285,15 +297,15 @@
 
 		/* Check that the frame is the expected response from the companion "DS TWR responder" example.
 		 * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */
-		rx_buffer[ALL_MSG_SN_IDX] = 0;
-		if (rx_buffer[9] == 0x10) //判断接收到的数据是否是response数据
+
+		if (rx_buffer[MESSAGE_TYPE_IDX] == RESPONSE) //判断接收到的数据是否是response数据
 		{
 			/* Retrieve poll transmission and response reception timestamp. */
 			poll_tx_ts = get_tx_timestamp_u64();										//获得POLL发送时间T1
 			resp_rx_ts = get_rx_timestamp_u64();										//获得RESPONSE接收时间T4
-
-			memcpy(&anchor_dist_last_frm[tag_id], &rx_buffer[11], 2);
-
+			
+			memcpy(&anchor_dist_last_frm[tag_id], &rx_buffer[DIST_IDX], 2);
+			memcpy(&tx_final_msg[ANCHOR_ID_IDX], &rx_buffer[ANCHOR_ID_IDX], 2);
 			/* Compute final message transmission time. See NOTE 9 below. */
 			final_tx_time = (resp_rx_ts + (RESP_RX_TO_FINAL_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8;//计算final包发送时间,T5=T4+Treply2
 			dwt_setdelayedtrxtime(final_tx_time);//设置final包发送时间T5
@@ -307,7 +319,7 @@
 			final_msg_set_ts(&tx_final_msg[FINAL_MSG_FINAL_TX_TS_IDX], final_tx_ts);
 
 			/* Write and send final message. See NOTE 7 below. */
-			tx_final_msg[ALL_MSG_SN_IDX] = frame_seq_nb;
+		
 			dwt_writetxdata(sizeof(tx_final_msg), tx_final_msg, 0);//将发送数据写入DW1000
 			dwt_writetxfctrl(sizeof(tx_final_msg), 0);//设定发送数据长度
 			dwt_starttx(DWT_START_TX_DELAYED);//设定为延迟发送
@@ -371,14 +383,14 @@
 
 		/* Check that the frame is a poll sent by "DS TWR initiator" example.
 		 * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */
-		rx_buffer[ALL_MSG_SN_IDX] = 0;
+	
 		
 		//将收到的tag_id分别写入各次通讯的包中,为多标签通讯服务,防止一次通讯中接收到不同ID标签的数据
-		tag_id_recv = rx_buffer[5];
-		tx_resp_msg[5] = tag_id_recv;
+		tag_id_recv = rx_buffer[TAG_ID_IDX];
+		tx_resp_msg[TAG_ID_IDX] = tag_id_recv;
 
 		
-		if (rx_buffer[9] == 0x21) //判断是否是poll包数据
+		if (rx_buffer[MESSAGE_TYPE_IDX] == POLL) //判断是否是poll包数据
 		{
 			/* Retrieve poll reception timestamp. */
 			poll_rx_ts = get_rx_timestamp_u64();//获得Poll包接收时间T2
@@ -392,8 +404,8 @@
 			dwt_setrxtimeout(FINAL_RX_TIMEOUT_UUS);//接收超时时间
 
 			/* Write and send the response message. See NOTE 9 below.*/
-			memcpy(&tx_resp_msg[11], &anchor_dist_last_frm[tag_id_recv], 2);
-			tx_resp_msg[ALL_MSG_SN_IDX] = frame_seq_nb;
+			memcpy(&tx_resp_msg[DIST_IDX], &anchor_dist_last_frm[tag_id_recv], 2);
+			
 			dwt_writetxdata(sizeof(tx_resp_msg), tx_resp_msg, 0);//写入发送数据
 			dwt_writetxfctrl(sizeof(tx_resp_msg), 0);//设定发送长度
 			dwt_starttx(DWT_START_TX_DELAYED | DWT_RESPONSE_EXPECTED);//延迟发送,等待接收
@@ -419,8 +431,7 @@
 
 				/* Check that the frame is a final message sent by "DS TWR initiator" example.
 				 * As the sequence number field of the frame is not used in this example, it can be zeroed to ease the validation of the frame. */
-				rx_buffer[ALL_MSG_SN_IDX] = 0;
-				if (rx_buffer[9] == 0x23) //判断是否为Final包
+				if (rx_buffer[MESSAGE_TYPE_IDX] == FINAL&&rx_buffer[TAG_ID_IDX]==tag_id_recv&&rx_buffer[ANCHOR_ID_IDX]==g_com_map[DEV_ID]) //判断是否为Final包
 				{
 					uint32_t poll_tx_ts, resp_rx_ts, final_tx_ts;
 					uint32_t poll_rx_ts_32, resp_tx_ts_32, final_rx_ts_32;
@@ -456,7 +467,8 @@
 					/*--------------------------以下为非测距逻辑------------------------*/
 					LED0_BLINK; //每成功一次通讯则闪烁一次
 					g_UWB_com_interval = 0;
-					dis_after_filter = LP_Frac_Update(p_Dis_Filter, dist_cm);
+					dis_after_filter=dist_cm;
+					//dis_after_filter = LP_Frac_Update(p_Dis_Filter, dist_cm);
 
 				}
 			}

--
Gitblit v1.9.3