From 503efc0a0d2ea34213ec87aea5bf63c52c2eb93a Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期二, 12 十一月 2019 19:29:18 +0800
Subject: [PATCH] 实现标签时间隙动态更新

---
 源码/核心板/Src/application/dw_app.h |    4 +
 源码/核心板/Src/main.c               |   36 +++++++++--
 源码/核心板/Src/stm32f10x_it.c       |   40 +++++--------
 源码/核心板/Src/application/dw_app.c |   71 ++++++++++++++++-------
 4 files changed, 97 insertions(+), 54 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 58345cd..82c7da6 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"
@@ -79,6 +79,7 @@
 //respose
 #define ANCTIMEMS             14
 #define ANCTIMEUS             16
+#define TAGSLOTPOS						18
 
 #define POLL     					0x01
 #define RESPONSE 					0x02
@@ -107,7 +108,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[20] = {0};
+static uint8_t tx_resp_msg[22] = {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. */
@@ -319,6 +320,7 @@
 extern uint16_t sync_timer;
 u16 tmp_time;
 int32_t temp_dist;
+u16 tagslotpos;
 void Tag_App(void)//发送模式(TAG标签)
 {
 	uint32_t frame_len;
@@ -386,6 +388,7 @@
 				getsync_flag=1;
 			memcpy(&sync_timer,&rx_buffer[ANCTIMEMS],2);
 			memcpy(&tmp_time,&rx_buffer[ANCTIMEUS],2);
+			memcpy(&tagslotpos,&rx_buffer[TAGSLOTPOS],2);		
 			tmp_time=tmp_time+450;
 			if(tmp_time>999)
 			{
@@ -395,6 +398,9 @@
 					{sync_timer=0;}
 			}
 			TIM3->CNT=tmp_time;
+			if(tagslotpos>max_slotpos)
+				tagslotpos=tagslotpos%(max_slotpos+1);
+				tyncpoll_time=(tagslotpos-1)*slottime;
 		}
 			memcpy(&anchor_dist_last_frm[0], &rx_buffer[DIST_IDX], 4);
 			memcpy(&tx_final_msg[ANCHOR_ID_IDX], &rx_buffer[ANCHOR_ID_IDX], 4);
@@ -464,19 +470,47 @@
 		dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);
 		random_delay_tim = DFT_RAND_DLY_TIM_MS;
 	}
-//	deca_sleep(10);
 }
-//	dwt_entersleep();
-	if(tag_succ_times<g_com_map[MIN_REPORT_ANC_NUM])
-	{
-	//poll_timer +=time32_incr&0x7+3;
-	}
 
-	/* Execute a delay between ranging exchanges. */
+if(getsync_flag==0)
+{
+tagslotpos--;
+		if(tagslotpos==0||tagslotpos>max_slotpos)
+	{		
+		tagslotpos=max_slotpos;	
+	}
+	tyncpoll_time=(tagslotpos-1)*slottime;
+}
 	
 }
+
 int8_t correction_time;
 extern uint8_t sync_seq;
+u16 taglist_num=0,taglist_pos;
+u16 tagid_list[TAG_NUM_IN_SYS];
+u8 tagofflinetime[TAG_NUM_IN_SYS];
+void TagListUpdate(void)
+{
+	u16 i,j=0,temp[TAG_NUM_IN_SYS];
+	for(i=0;i<taglist_num;i++)
+	{
+		if(tagofflinetime[i]++<QUIT_SLOT_TIME)
+		{
+			tagid_list[j]=tagid_list[i];
+			tagofflinetime[j++]=tagofflinetime[i];
+		}
+	}
+	taglist_num=j;
+}
+u16 CmpTagInList(u16 tagid)
+{u16 i;
+	for(i=0;i<taglist_num;i++)
+	{
+		if(memcmp(&tagid,&tagid_list[i],2)==0)
+			return i+1;
+	}
+	return 0;
+}
 
 void Anchor_App(void)
 {
@@ -515,19 +549,19 @@
 		//tag_id_recv = rx_buffer[TAG_ID_IDX];
 		memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],4);
 		memcpy(&tx_resp_msg[TAG_ID_IDX],&tag_id_recv,4);
-		//tx_resp_msg[TAG_ID_IDX] = tag_id_recv;
-//		if(tag_recv_timer>tag_time_recv[tag_id_recv-TAG_ID_START])
-//		{	tag_recv_interval =  tag_recv_timer - tag_time_recv[tag_id_recv];
-//		}else{
-//			tag_recv_interval = tag_recv_timer + 65535 - tag_time_recv[tag_id_recv];
-//		}
-		
+		taglist_pos=CmpTagInList(tag_id_recv);
+		if(taglist_pos==0)
+		{
+			tagid_list[taglist_num++]=tag_id_recv;
+			taglist_pos=taglist_num;
+		}
+		tagofflinetime[taglist_pos-1]=0;
 		if (rx_buffer[MESSAGE_TYPE_IDX] == POLL&&(anchor_type == rx_buffer[ANC_TYPE_IDX])) //判断是否是poll包数据
 		{
 			tmp_time=TIM3->CNT;
 			memcpy(&tx_resp_msg[ANCTIMEMS],&sync_timer,2);
 			memcpy(&tx_resp_msg[ANCTIMEUS],&tmp_time,2);
-
+			memcpy(&tx_resp_msg[TAGSLOTPOS],&taglist_pos,2);
 //							if(correction_time>10)
 //							{correction_time++;}
 							
@@ -613,7 +647,6 @@
 					//dist_cm=33000;
 					
 					LED0_BLINK; //每成功一次通讯则闪烁一次
-					g_UWB_com_interval = 0;
 					dis_after_filter=dist_cm;
 					hex_dist = dist_cm+(int16_t)g_com_map[DIST_OFFSET];
 						if(tag_id_recv-TAG_ID_START<=TAG_NUM_IN_SYS)
@@ -636,10 +669,6 @@
 					checksum = Checksum_u16(&usart_send[2],17);
 					memcpy(&usart_send[19],&checksum,2);
 					UART_PushFrame(usart_send,21);
-
-					
-					//dis_after_filter = LP_Frac_Update(p_Dis_Filter, dist_cm);
-
 				}
 			}else{
 				/* Clear RX error events in the DW1000 status register. */
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.h" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.h"
index e3388ff..8d79013 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.h"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.h"
@@ -19,6 +19,8 @@
 #define GROUP_TAG_NUM 2
 #define TAG_NUM_IN_SYS				256
 #define DFT_RAND_DLY_TIM_MS			1
+#define QUIT_SLOT_TIME    5
+extern u16 slottime,tyncpoll_time,max_slotpos;
 extern u16 poll_timer,tag_timer;
 extern int8_t tag_delaytime;
 extern u32 dev_id;
@@ -41,4 +43,6 @@
 extern uint8_t g_flag_Taggetdist[256];
 extern void TagDistClear(void);
 void Dw1000_App_Init(void);
+void TagListUpdate(void);
+void SyncPoll(u8 sync_seq);
 #endif
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/main.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/main.c"
index 9a8ec1c..f775c2a 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/main.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/main.c"
@@ -11,6 +11,7 @@
 #include "global_param.h"
 #include "ADC.h"
 
+#define DEBUG_MODE
 
 void Device_Init(void)
 {
@@ -44,18 +45,34 @@
 	memcpy(&hbsend[14],&checksum,2);
 }
 u16 tyncpoll_time;
+u16 slottime,max_slotpos;
 void Program_Init(void)
-{uint16_t i;
+{	float temp;
+	u16 temp2;
+	uint16_t i;
 	Usart1ParseDataCallback = UsartParseDataHandler;
 	parameter_init();
 	//deca_sleep(1000);
 	HeartBeatInit();
-	dev_id = g_com_map[DEV_ID];
-//	g_com_map[DEV_ROLE]=1;
-//		g_com_map[DEV_ID]=1;
-//	g_com_map[COM_INTERVAL]=10;
+#ifdef DEBUG_MODE
+	g_com_map[DEV_ROLE]=1;
+		g_com_map[DEV_ID]=4;
+	g_com_map[COM_INTERVAL]=100;
+	g_com_map[MAX_REPORT_ANC_NUM]=3;
+#endif
 	OUT485_ENABLE;
-	g_com_map[VERSION] = 0x0105;		
+	g_com_map[VERSION] = 0x0106;	
+	dev_id = g_com_map[DEV_ID];
+	temp=(float)g_com_map[MAX_REPORT_ANC_NUM]*4/3;	
+	temp2=g_com_map[MAX_REPORT_ANC_NUM]*4/3;
+	if(temp2<temp)
+	{
+		temp2++;
+	}
+	slottime=temp2;
+	max_slotpos=g_com_map[COM_INTERVAL]/slottime;
+	tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;	
+	
 	if(g_com_map[DEV_ROLE])
 	{
 		tyncpoll_time=g_com_map[DEV_ID]*g_com_map[MAX_REPORT_ANC_NUM]*4/3;
@@ -117,6 +134,9 @@
 		result = FLASH_Write(0x8004A38, (const uint8_t*)&tmp, 2); 
 	__enable_irq();
 		printf("进入升级模式\r\n");
+	g_com_map[CNT_UPDATE]=0;
+	save_com_map_to_flash();
+	delay_ms(100);
 		//	STMFLASH_Write_NoCheck(0x8004A38,0xAAAA);
 		//	Delay_ms(100);
 			SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
@@ -138,13 +158,13 @@
 	
 	while(1)
 	{
-		IdleTask();		
+	
 	if(g_start_send_flag)
 	{
 		g_start_send_flag = 0;		
 		Tag_App();
 	}
-	
+		IdleTask();		
 if(g_com_map[DEV_ROLE]==0)
 		Anchor_App();
 	}
diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/stm32f10x_it.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/stm32f10x_it.c"
index 96ad665..d00482c 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/stm32f10x_it.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/stm32f10x_it.c"
@@ -44,20 +44,21 @@
 	if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)  //??TIM3????????
 		{
 		TIM_ClearITPendingBit(TIM3, TIM_IT_Update  );  //??TIMx?????? 
-			 time32_incr++;
-	g_UWB_com_interval++;
+			
+	time32_incr++;
 	heartbeat_timer++;
-	poll_timer++;
-	
-	if(tag_timer++>=g_com_map[COM_INTERVAL])
-	{tag_timer=0;}
+	sync_timer++;	
+			
+	if(sync_timer>=1010)
+		{sync_timer=0;}
+		
 	if(g_Resttimer++>2000)
 	{
 		NVIC_SystemReset();
 	}
 
 
-	sync_timer++;
+
 	
 	if(!g_com_map[DEV_ROLE])  //基站
 	{
@@ -65,33 +66,22 @@
 		TagDistClear();
 		if(g_com_map[ALARM_DEV]!=0)
 			main_logic();
-		if(sync_timer==10)
-		{
-			GPIO_WriteBit(GPIOA, GPIO_Pin_9, Bit_SET);
-		}else{
-			GPIO_WriteBit(GPIOA, GPIO_Pin_9, Bit_RESET);
-		}
+
 		if(sync_timer>500&&dev_id!=0)
 		{
 		 sync_seq=0xff;
 		}
-		if(sync_timer>=1010)
-	{
-		sync_timer=0;	
-		if(dev_id==0)
+
+		if(sync_timer==0)
 		{
-			SyncPoll(0);
+			TagListUpdate();
+			if(dev_id==0)
+				SyncPoll(0);
 		}
-	}
 	}else{		//标签同步处理
-		if(sync_timer>=1010)
-		{sync_timer=0;}
-				if((sync_timer-10)%g_com_map[COM_INTERVAL]==tyncpoll_time)
+				if((sync_timer-10)%g_com_map[COM_INTERVAL]==tyncpoll_time&&sync_timer>=10)
 		{
 			GPIO_WriteBit(GPIOA, GPIO_Pin_9, Bit_SET);
-		}
-		if((sync_timer-10)%g_com_map[COM_INTERVAL]==tyncpoll_time)
-		{
 		g_start_send_flag=1;
 		}
 		

--
Gitblit v1.9.3