From 13fa2c6eaab3b3ee51d447d2549f6e09c821dc60 Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期一, 20 十二月 2021 12:14:04 +0800
Subject: [PATCH] 1

---
 Src/application/dw_app.c |   88 ++++++++++++++++++++++----------------------
 1 files changed, 44 insertions(+), 44 deletions(-)

diff --git a/Src/application/dw_app.c b/Src/application/dw_app.c
index ca2fc8c..f19f2c4 100644
--- a/Src/application/dw_app.c
+++ b/Src/application/dw_app.c
@@ -81,7 +81,7 @@
     SINGLEPOLL,
 }tag_state=STARTPOLL;
 static dwt_config_t config = {
-	5,               /* Channel number. */
+	2,               /* Channel number. */
 	DWT_PRF_64M,     /* Pulse repetition frequency. */
 	DWT_PLEN_64,    /* Preamble length. */
 	DWT_PAC8,        /* Preamble acquisition chunk size. Used in RX only. */
@@ -176,7 +176,7 @@
     Reset_DW1000();//重启DW1000 /* Target specific drive of RSTn line into DW1000 low for a period. */
 	Spi_ChangePrescaler(SPIx_PRESCALER_SLOW);	//设置为快速模式
     dwt_initialise(DWT_LOADUCODE);//初始化DW1000
-	Spi_ChangePrescaler(SPIx_PRESCALER_FAST);	//设置为快速模式
+	//Spi_ChangePrescaler(SPIx_PRESCALER_FAST);	//设置为快速模式
 
     /* Configure DW1000. See NOTE 6 below. */
     dwt_configure(&config);//配置DW1000
@@ -624,21 +624,25 @@
 //	mainbase_id=minid;
 //	mainbase_id = 0x4;
 }
-
+int16_t target_offsettime = 850;
+//#define TAGET_OFFSETTIME 940
 #define MAX_NEARBASE_ANCNUM 11
 u16 rec_maxrangelen,rec_anc_signalpower[MAX_NEARBASE_ANCNUM];
 u8 outrange_times;
 extern u32 synctimer;
 u32 target_time=100;
 u8 pd_i;
-u32 temptime,delaytime,expect_anctime,error_time;
+u32 temptime,delaytime,expect_anctime;
+int32_t error_time;
 u16 delaycount;
-int16_t poll_offsettime=-6900;
+int16_t poll_offsettime=-6900,clockoffset;
 extern u16 slotpos_intoatl, slotpos;
 void NextPollDelay(u32 anchor_time)
 {
-		tagslotpos = 0;
-	//	error_time = anchor_time-expect_anctime;
+		//tagslotpos = 0;
+		error_time = anchor_time-expect_anctime;
+	if(error_time<2000&&error_time>0)
+		clockoffset += (target_offsettime-error_time)*0.1;
     for(pd_i=0;pd_i<tag_frequency;pd_i++)
     {
         slotpos_intoatl = pd_i*bigslot_num+tagslotpos;
@@ -646,18 +650,18 @@
         if(anchor_time<temptime-5000)
         { 
 					current_slotnum = pd_i;
-            delaytime = temptime-anchor_time+poll_offsettime;  
-		//				expect_anctime = temptime;
+            delaytime = temptime-anchor_time+poll_offsettime+clockoffset;  
+						expect_anctime = temptime;
             return ;
         }
     }
     temptime = (tagslotpos*SLOTTIME_MS+100+10000)*100;
-	//	expect_anctime = temptime-1000000;
-    delaytime = temptime-anchor_time+poll_offsettime;
+		expect_anctime = temptime-1000000;
+    delaytime = temptime-anchor_time+poll_offsettime+clockoffset;
    return ;
 }
 u16 lpcount,poll_startcount,last_lpcount;
-u16 waketopolltimeus = 4000;
+u16 waketopolltimeus = 3200;
 #define BASENUM_COMTIME 244
 u32 last_anchor_time;
 void SetLPTimer(u32 anchor_time)
@@ -677,22 +681,14 @@
     __HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
 		last_lpcount = lpcount;
 	}
-void DelayOneSlot(void)
-{
-			lpcount+=166;
-			if(lpcount>=32768)
-		{
-		lpcount -=32768;
-		}
-		__HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
-}
-void NextSlotDelayMs(u16 delayms)
+
+void NextSlotDelayMs(int16_t delayms)
 {
 	current_slotnum++;
 	if(current_slotnum>=tag_frequency)
 	{	
 		current_slotnum = 0;		
-		lpcount = last_lpcount+31457/tag_frequency+1311+5;//(float)(delayms+40)*32.767 ;//(200/tag_frequency+delayms)*4.8*1000/LPTIMER_LSB
+		lpcount = last_lpcount+31457/tag_frequency+1311+32.767*delayms;//(float)(delayms+40)*32.767 ;//(200/tag_frequency+delayms)*4.8*1000/LPTIMER_LSB
 	}else{
 		lpcount = last_lpcount+31457/tag_frequency+32.767*delayms;//(float)(200/tag_frequency*48+delayms*10)*3.3 ;
 	}
@@ -714,7 +710,7 @@
 {
 	u32 start_poll,frame_len;
     u8 nearmsg_i=0;
-	DelayOneSlot();
+	NextSlotDelayMs(0);
     for(nearmsg_i=0;nearmsg_i<20;nearmsg_i++)
     {
         nearbase_distlist[nearmsg_i] = 0x1ffff;
@@ -770,7 +766,7 @@
 //			tyncpoll_time = (tagslotpos--%max_slotpos)*slottime;
 	}
 }
-
+uint8_t pack_length = 0,pack_index = 0,pack_msgtype = 0;
 void NearPoll(void)
 {
 	
@@ -798,11 +794,11 @@
 	tx_near_msg[BATTARY_IDX] = bat_percent;
 	tx_near_msg[BUTTON_IDX] = userkey_state|stationary_flag<<1|gotosleep_flag<<2;
 	tx_near_msg[SEQUENCE_IDX] = frame_seq_nb;
-    tx_near_msg[SEQUENCEH_IDX] = (frame_seq_nb++)>>8;
+  tx_near_msg[SEQUENCEH_IDX] = (frame_seq_nb++)>>8;
 	tx_near_msg[NEARBASENUM_INDEX] = nearbase_num;
 	memcpy(&tx_near_msg[NEARBASEID_INDEX],&nearbaseid_list,nearbase_num*2);
-    memcpy(&tx_near_msg[NEARBASEID_INDEX+nearbase_num*2],&nearbase_distlist,nearbase_num*4+4);
-    memcpy(&tx_near_msg[NEARBASEID_INDEX+nearbase_num*6+4],&rec_anc_signalpower,nearbase_num*2+2);
+  memcpy(&tx_near_msg[NEARBASEID_INDEX+nearbase_num*2],&nearbase_distlist,nearbase_num*4+4);
+  memcpy(&tx_near_msg[NEARBASEID_INDEX+nearbase_num*6+4],&rec_anc_signalpower,nearbase_num*2+2);
 
     
     if(intheight!=0)
@@ -818,6 +814,7 @@
 	dwt_writetxdata(29+8*nearbase_num, tx_near_msg, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
 	dwt_writetxfctrl(29+8*nearbase_num, 0);//设置超宽带发送数据长度
 				current_count=HAL_LPTIM_ReadCounter(&hlptim1);
+		
 		while(current_count<poll_startcount||current_count>poll_startcount+16384)
 		{
 			current_count=HAL_LPTIM_ReadCounter(&hlptim1);
@@ -827,7 +824,7 @@
 			}
 		}
 	dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);//开启发送,发送完成后等待一段时间开启接收,等待时间在dwt_setrxaftertxdelay中设置
-	HAL_GPIO_WritePin(LED1_G_GPIO_Port, LED1_G_Pin, GPIO_PIN_SET);
+//	HAL_GPIO_WritePin(LED1_G_GPIO_Port, LED1_G_Pin, GPIO_PIN_RESET);
 //    for(i=0;i<nearbase_num+1;i++)
 //    {
 //        nearbase_distlist[i] = 0x1ffff;
@@ -863,11 +860,11 @@
 					if (status_reg & SYS_STATUS_RXFCG)//如果成功接收
 						{
 							dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);//清楚寄存器标志位
-							dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);//设定接收超时时间,0位没有超时时间
-							dwt_rxenable(0);//打开接收
 							frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFLEN_MASK;	//获得接收到的数据长度
 							dwt_readrxdata(rx_buffer, frame_len, 0);   //读取接收数据
-
+							dwt_setrxtimeout(0);//设定接收超时时间,0位没有超时时间
+							dwt_rxenable(0);//打开接收
+							//HAL_GPIO_WritePin(LED1_G_GPIO_Port, LED1_G_Pin, GPIO_PIN_SET);
 							if (rx_buffer[MESSAGE_TYPE_IDX] == NEAR_RESPONSE&&!memcmp(&rx_buffer[TAG_ID_IDX],&dev_id,2)) //判断接收到的数据是否是response数据
 							{	u16 rec_nearbaseid,rec_nearbasepos;
 								poll_tx_ts = get_tx_timestamp_u64();										//获得POLL发送时间T1
@@ -887,7 +884,7 @@
 									final_msg_set_ts(&tx_near_msg[FINAL_MSG_RESP_RX_NEARBASE_IDX+nearbase_num*4], resp_rx_ts);
 									final_msg_set_ts(&tx_near_msg[FINAL_MSG_FINAL_TX_TS_IDX], final_tx_ts);
 									tx_near_msg[MESSAGE_TYPE_IDX]=NEAR_FINAL;
-							
+									rec_remotepara_state=rx_buffer[MOTORSTATE_INDEX]>>4;
 								if(rec_remotepara_state&&!memcmp(&rx_buffer[TAG_ID_IDX],&dev_id,2))
 									{
 										para_update = 1;
@@ -922,7 +919,7 @@
                                          }
                                      }
                                  
-									rec_remotepara_state=rx_buffer[MOTORSTATE_INDEX]>>4;
+									
 									
 									mainbase_lost_count=0;
 									flag_finalsend=1;
@@ -943,7 +940,8 @@
 									SetLPTimer(ancsync_time);
 									if(result==0)
 											{while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))//不断查询芯片状态直到发送完成
-											{ };}								
+											{ };}			
+//HAL_GPIO_WritePin(LED1_G_GPIO_Port, LED1_G_Pin, GPIO_PIN_RESET);											
 										break;
 								}else{
 									
@@ -971,6 +969,7 @@
 
 						}								
 							}
+			//				HAL_GPIO_WritePin(LED1_G_GPIO_Port, LED1_G_Pin, GPIO_PIN_RESET);
 						}else{
 						dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_RXFCG| SYS_STATUS_ALL_RX_ERR);
 						if(recbase_num!=nearbase_num+1)
@@ -985,8 +984,8 @@
             if(flag_finalsend!=1)
             {flag_finalsend = 2;}
 			dwt_forcetrxoff();
-dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_RXFCG| SYS_STATUS_ALL_RX_ERR);
-HAL_GPIO_WritePin(LED1_G_GPIO_Port, LED1_G_Pin, GPIO_PIN_RESET);
+			dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_RXFCG| SYS_STATUS_ALL_RX_ERR);
+//			HAL_GPIO_WritePin(LED1_G_GPIO_Port, LED1_G_Pin, GPIO_PIN_SET);
 			if(mainbase_lost_count==0)
 			{
 				#ifdef SWITCHBASE_DIST
@@ -1005,7 +1004,7 @@
 			#endif
 			}else if(mainbase_lost_count>5*tag_frequency)
 			{
-			//	tag_state = STARTPOLL;
+				tag_state = STARTPOLL;
 			}	
 			if(mainbase_lost_count!=0)
 			{
@@ -1018,7 +1017,7 @@
 
 			if(para_update)
 			{
-			uint8_t pack_length = 0,pack_index = 0,pack_msgtype = 0;
+			
 				pack_msgtype = rec_remotepara[0];
 				pack_index = rec_remotepara[1];
 				pack_length = rec_remotepara[2];
@@ -1055,7 +1054,6 @@
 
 }
 extern uint8_t module_power,imu_enable,motor_enable;
-
 void Registor_Poll(void)
 {
 	static u8 regpoll_count=0;
@@ -1069,7 +1067,7 @@
 				tagslotpos=TOTAL_SLOTNUM;
 		}
 		//SetNextPollTime(tagslotpos);
-		DelayOneSlot();
+		NextSlotDelayMs(-2);
 		
 		dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);			//设置发送后开启接收,并设定延迟时间
     dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);		
@@ -1081,7 +1079,7 @@
 		memcpy(&tx_near_msg[REGP_VERSION_INDEX],&g_com_map[VERSION],2);	
 		memcpy(&tx_near_msg[REGP_IMUTHRES_INDEX],&g_com_map[IMU_THRES],2);
 		memcpy(&tx_near_msg[REGP_NOMOVESLEEPTIME_INDEX],&g_com_map[NOMOVESLEEP_TIME],2);
-        memcpy(&tx_near_msg[REGP_HEIGHTOFFSET_INDEX],&g_com_map[HEIGHTOFFEST_INDEX],2);
+    memcpy(&tx_near_msg[REGP_HEIGHTOFFSET_INDEX],&g_com_map[HEIGHTOFFEST_INDEX],2);
 		dwt_writetxdata(23, tx_near_msg, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
 		dwt_writetxfctrl(23, 0);//设置超宽带发送数据长度
 		dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);//开启发送,发送完成后等待一段时间开启接收,等待时间在dwt_setrxaftertxdelay中设置
@@ -1149,8 +1147,6 @@
 	//LED0_ON;
 	SPIx_CS_GPIO->BRR = SPIx_CS;
 	delay_us(700);
-   // GetPressAndHeight();
-   // intheight = Height*100;
 	SPIx_CS_GPIO->BSRR = SPIx_CS;
 	id =  dwt_readdevid() ;
 	    while (DWT_DEVICE_ID != id) 
@@ -1164,6 +1160,8 @@
             SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
         }
     }
+
+		
     error_times = 0;
 	switch(tag_state)
 	{
@@ -1189,6 +1187,8 @@
 			break;
 		case NEARPOLL:	
 			NearPoll();
+			//GetPressAndHeight();
+			//intheight = Height*100;
 			break;
         case SINGLEPOLL:
             Poll();

--
Gitblit v1.9.3