From fa5a89bd9f87ef619619194d00cd495d165e1f12 Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期六, 27 六月 2020 19:33:44 +0800
Subject: [PATCH] V1.19 测试10对10 200ms防撞。

---
 源码/核心板/Src/application/dw_app.c |  100 +++++++++++++++++++++++++++++++------------------
 1 files changed, 63 insertions(+), 37 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 ff3a407..53978d4 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"
@@ -208,7 +208,31 @@
 u16 nearbaseid_list[MAX_NEARBASE_NUM],mainbase_id,true_nearbase_idlist[MAX_NEARBASE_NUM];
 int32_t mainbase_dist,nearbase_distlist[MAX_NEARBASE_NUM],true_nearbase_distlist[MAX_NEARBASE_NUM],true_exsistbase_list[MAX_NEARBASE_NUM];
 uint8_t trygetnearmsg_times;
+u16 current_slotnum,total_slotnum,target_time,last_time;
+u32 rec_tagpos_binary;
+extern uint32_t tagpos_binary;
+void SetNextPollTime(u16 time)
+{
+	current_slotnum++;
+	if(current_slotnum==total_slotnum)
+		current_slotnum-=total_slotnum;
+	//time=5;
+	target_time=((current_slotnum*g_com_map[COM_INTERVAL])+time);
 
+	last_time=target_time;
+	if(target_time>=990&&target_time<992)
+	{
+		current_slotnum++;
+	target_time+=g_com_map[COM_INTERVAL];
+	tagslotpos=GetRandomSlotPos(rec_tagpos_binary|tagpos_binary);
+	tyncpoll_time = (tagslotpos--%max_slotpos)*slottime;	
+	}	
+	if(target_time>=1000)
+	{target_time-=1000;}
+	if(target_time<0)
+	{target_time+=1000;}
+
+}
 u8 FindNearBasePos(u16 baseid)
 {
 	u8 i;
@@ -265,7 +289,7 @@
 {
 	u8 i,temp_value;
 	temp_value = GetRandomValue();
-	for(i=temp_value%32;i<max_slotpos;i++)
+	for(i=temp_value%max_slotpos;i<max_slotpos;i++)
 	{
 		if(((emptyslot>>i)&0x1)==0)
 		{
@@ -284,10 +308,10 @@
 }
 u8 test=0;
 u8 anclost_times=0;
-u8 exsistbase_list[MAX_NEARBASE_NUM],report_num,get_newbase=0;;
+u8 exsistbase_list[MAX_NEARBASE_NUM],report_num,get_newbase=0;
 u16 temp_sync_timer1,temp_sync_timer2;
-u32 rec_tagpos_binary;
-void NearPoll(void)
+
+ void NearPoll(void)
 {
 	static u8 mainbase_lost_count=0,flag_finalsend,flag_rxon;
 	uint32_t temp1,temp2,dw_systime;
@@ -296,7 +320,6 @@
 	u32 start_poll;
 	u8 i,j,getsync_flag=0,timeout;
 //	printf("%d",sync_timer);
-		dwt_forcetrxoff();
     dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);			//设置发送后开启接收,并设定延迟时间
     dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);		
 	tag_succ_times = 0;
@@ -324,7 +347,7 @@
 	flag_rxon=1;
 	neartimout_timer=0;
 	get_newbase=0;
-	timeout=ceil((float)nearbase_num/4)+2;
+	timeout=ceil((float)nearbase_num*0.4)+3;
 	//timeout=5;
 	mainbase_dist=100000;
 	mainbase_lost_count++;
@@ -379,11 +402,15 @@
 									temp_sync_timer2=sync_timer;
 									memcpy(&temp_sync_timer1,&rx_buffer[ANCTIMEMS],2);
 									memcpy(&tmp_time,&rx_buffer[ANCTIMEUS],2);
-									sync_timer=temp_sync_timer1;
 									exsistbase_list[0]=KEEP_TIMES;
+									
+									if(g_com_map[DEV_ROLE]!=0)
+									{
+									sync_timer=temp_sync_timer1;
 ////									memcpy(&tagslotpos,&rx_buffer[TAGSLOTPOS],2);		
 //									
-									tmp_time=tmp_time+650;
+									
+										tmp_time=tmp_time+650;
 									if(tmp_time>999)
 									{
 										tmp_time-=999;
@@ -392,13 +419,14 @@
 											{sync_timer=0;}
 									}
 									TIM3->CNT=tmp_time;
-									
+								}
+									current_slotnum=temp_sync_timer1/g_com_map[COM_INTERVAL];
 //									if(tagslotpos>max_slotpos)
 //										tagslotpos=tagslotpos%(max_slotpos+1);
 //										tyncpoll_time=(tagslotpos-1)*slottime;
 									////////////////////////////
 									rec_nearbasepos=0;
-									exsistbase_list[rec_nearbasepos]=10;
+									exsistbase_list[rec_nearbasepos]=KEEP_TIMES;
 									memcpy(&temp_dist,&rx_buffer[DIST_IDX],4);
 									nearbase_distlist[rec_nearbasepos]=temp_dist;
 									
@@ -415,25 +443,10 @@
 									tx_nearfinal_msg[MESSAGE_TYPE_IDX]=NEAR_FINAL;
 									dwt_writetxdata(28+nearbase_num*4, tx_nearfinal_msg, 0);//将发送数据写入DW1000
 									dwt_writetxfctrl(28+nearbase_num*4, 0);//设定发送数据长度
-
+									memcpy(&rec_tagpos_binary,&rx_buffer[NEARMSG_EMPTYSLOTPOS_INDEX],4);
 									
-									if(temp_dist!=0x1ffff)
-									{
-									#ifndef USART_INTEGRATE_OUTPUT
-										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],&rec_nearbaseid,2);				
-										memcpy(&usart_send[9],&rx_buffer[DIST_IDX],4);
-										usart_send[13] = battary;
-										usart_send[14] = button;
-										checksum = Checksum_u16(&usart_send[2],17);
-										memcpy(&usart_send[19],&checksum,2);
-										UART_PushFrame(usart_send,21);
-									#endif
-									}
-									//result=dwt_starttx(DWT_START_TX_DELAYED);//设定为延迟发送			
+		
+									
 								}else{
 									rec_nearbasepos=FindNearBasePos(rec_nearbaseid);
 									if(rec_nearbasepos>=last_nearbase_num)          //发现新的基站
@@ -503,7 +516,9 @@
 //					temp_adc=Get_ADC_Value();
 //					random_value=random_value|((temp_adc&0x01)<<i);
 //				}
-				tyncpoll_time = (tagslotpos%max_slotpos)*slottime;	
+				tagslotpos=GetRandomSlotPos(rec_tagpos_binary|tagpos_binary);
+				tyncpoll_time = (tagslotpos--%max_slotpos)*slottime;
+		tag_state=GETNEARMSG;		
 			}
 		//	tyncpoll_time=0;
 			next_nearbase_num=0;
@@ -548,7 +563,7 @@
 			{
 				nearbaseid_list[i]=true_nearbase_idlist[i];
 				nearbase_distlist[i]=true_nearbase_distlist[i];
-				if(nearbase_distlist[i]!=0x1ffff&&true_exsistbase_list[i]==0xa)
+				if(nearbase_distlist[i]!=0x1ffff&&true_exsistbase_list[i]==KEEP_TIMES)
 				{
 					memcpy(&usart_send[4+6*report_num],&nearbaseid_list[i],2);
 					memcpy(&usart_send[6+6*report_num],&nearbase_distlist[i],4);
@@ -583,10 +598,8 @@
 //			}else{
 //				anclost_times=0;
 //			}
-
-	dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);
-
-
+dwt_forcetrxoff();
+	dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR| SYS_STATUS_TXFRS |SYS_STATUS_RXFCG);
 }
 u8 nearmsg_mainbase=0,rec_tagpos_emptylist[32];
 void GetNearMsg(void)
@@ -628,16 +641,24 @@
 			tagslotpos=GetRandomSlotPos(rec_tagpos_binary);
 			//tagslotpos=rx_buffer[TAGSLOTPOS];
 			memcpy(nearbaseid_list,&rx_buffer[NEARBASEID_INDEX],next_nearbase_num*2);
+			//slottime=ceil((nearbase_num+2)*0.4)+2;
 			tyncpoll_time=tagslotpos*slottime;
 			tag_state=NEARPOLL;
+		}else{
+				tyncpoll_time = (tagslotpos--%max_slotpos)*slottime;
 		}
+	}else{
+			tyncpoll_time = (tagslotpos--%max_slotpos)*slottime;
 	}
+	
 }
 
 void Tag_App(void)//发送模式(TAG标签)
 {
 	//LED0_ON;
+	dwt_forcetrxoff();
 	g_Resttimer=0;
+		GPIO_WriteBit(GPIOA, GPIO_Pin_10, Bit_SET);	
 	switch(tag_state)
 	{
 		case DISCPOLL:
@@ -651,6 +672,9 @@
 			break;
 	}
 
+	SetNextPollTime(tyncpoll_time);
+	g_start_send_flag = 0;		
+	GPIO_WriteBit(GPIOA, GPIO_Pin_10, Bit_RESET);	
 }
 
 int8_t correction_time,new_tagid=0;
@@ -845,6 +869,7 @@
 						misdist_num[taglist_pos]=0;
 					tagdist_list[taglist_pos] = hex_dist;	
 					his_dist[taglist_pos]=hex_dist;	
+						g_Tagdist[taglist_pos]=hex_dist;	
 				#ifndef USART_INTEGRATE_OUTPUT
 					usart_send[2] = 1;//正常模式
 					usart_send[3] = 17;//数据段长度
@@ -875,7 +900,6 @@
 }
 uint32_t current_syncid=0xffffffff,synclost_timer;
 extern u8 flag_syncbase;
-extern uint32_t tagpos_binary;
 u8 tagpos_rec[50],tagpos_send[50],ancidlist_num;
 u16 ancidlist_rec[20],ancidlist_send[20];
 void Anchor_App(void)
@@ -885,15 +909,17 @@
 	u16 tempid;
 	uint32_t rec_syncid;
 	
+	dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR| SYS_STATUS_TXFRS |SYS_STATUS_RXFCG);
 	g_start_sync_flag=0;
+
 	dwt_setrxtimeout(0);//设定接收超时时间,0位没有超时时间
 	dwt_rxenable(0);//打开接收
-
+//	GPIO_WriteBit(GPIOA, GPIO_Pin_10, Bit_SET);	
 	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();
 	};
-
+//GPIO_WriteBit(GPIOA, GPIO_Pin_10, Bit_RESET);	
 	if (status_reg & SYS_STATUS_RXFCG)//成功接收
 	{ u16 tag_recv_interval;
 		float temp_tagpos;

--
Gitblit v1.9.3