From 7b8a137768f244173209f115738366efa7878b2f Mon Sep 17 00:00:00 2001
From: yincheng.zhong <634916154@qq.com>
Date: 星期五, 28 十月 2022 10:32:55 +0800
Subject: [PATCH] V1.36 1.增加UWB芯片电量检测 2.解决基站轮流丢包的问题。

---
 Src/main.c |  168 +++++++++++++++++++++++++++++++++++++-------------------
 1 files changed, 111 insertions(+), 57 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index ac12441..21dc8d0 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -136,7 +136,7 @@
 //		g_com_map[MOTOR_ONTIME]--;
 //		motor_state = 1;
 //	}
-if(g_com_map[MOTOR_ENABLE])
+if(0)//g_com_map[MOTOR_ENABLE])
 {
     
 
@@ -188,6 +188,7 @@
 	if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
 	{
 		nomove_count = 0;
+       // bat_percent=Get_Battary();
 		if(state5v==0)
 		{
 			state5v=1;
@@ -249,27 +250,33 @@
 
 u16 total_slotnum,current_slotnum;
 extern int32_t offsettimeus;
-
+u16 last_lpcount = 0;
 void SetNextPollTime(u16 time)
 {
   int32_t lpcount,lptime,target_time;
 
-	if(current_slotnum>=total_slotnum)
-		current_slotnum-=total_slotnum;
-	
-	target_time=((current_slotnum*g_com_map[COM_INTERVAL])+time)*1000;
-	//if(target_time<90000)
-	{
-		lptime=target_time-offsettimeus;
-		lpcount = lptime/LPTIMER_LSB;
-	if(lpcount>LPTIMER_1S_COUNT)
-		lpcount-=LPTIMER_1S_COUNT;
-	if(lpcount<0)
-	{
-	lpcount+=LPTIMER_1S_COUNT;
-	}
+//	if(current_slotnum>=total_slotnum)
+//		current_slotnum-=total_slotnum;
+//	
+//	target_time=((current_slotnum*g_com_map[COM_INTERVAL])+time)*1000;
+//	//if(target_time<90000)
+//	{
+//		lptime=target_time-offsettimeus;
+//		lpcount = lptime/LPTIMER_LSB;
+//	if(lpcount>LPTIMER_1S_COUNT)
+//		lpcount-=LPTIMER_1S_COUNT;
+//	if(lpcount<0)
+//	{
+//	lpcount+=LPTIMER_1S_COUNT;
+//	}
+    lpcount = last_lpcount+g_com_map[COM_INTERVAL]*32.768;
+    while(lpcount>LPTIMER_1S_COUNT)
+    {
+        lpcount-=LPTIMER_1S_COUNT;
+    }
+    last_lpcount = lpcount;
 	__HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
-}
+
 }
 
 void HardWareTypeDiffConfig(void)
@@ -295,6 +302,7 @@
 			break;
 		}
 }
+u8 active_flag,nomovesleeptime;
 void Program_Init(void)
 {	float temp;
 	uint16_t temp2;
@@ -305,23 +313,9 @@
 	hardware_pici = STMFLASH_ReadHalfWord(FLASH_HARDWARE_VERSION_MAP+2);
 	hardware_type = STMFLASH_ReadHalfWord(FLASH_HARDWARE_VERSION_MAP+4);
 	//deca_sleep(1000);
-	g_com_map[VERSION] = 0x0116;
-#ifdef DEBUG_MODE
-//	g_com_map[DEV_ROLE]=1;
-//		g_com_map[DEV_ID]=1;
-	g_com_map[COM_INTERVAL]=100;
-//	g_com_map[MAX_REPORT_ANC_NUM]=1;
-	g_com_map[NOMOVESLEEP_TIME]=10;
-#endif
-		if(g_com_map[COM_INTERVAL]==0)
-	{
-	  g_com_map[COM_INTERVAL]=100;
-	}
-	 if(module_power>67)
-		{module_power=67;}
-		if(module_power<0)
-		{module_power=0;}
-	module_power = g_com_map[POWER];
+	g_com_map[VERSION] = (1<<8)|36;
+    active_flag = g_com_map[ACTIVE_INDEX];
+    	module_power = g_com_map[POWER];
 	imu_enable=g_com_map[IMU_ENABLE];
 	motor_enable=g_com_map[MOTOR_ENABLE];
 	
@@ -332,8 +326,30 @@
 	tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;	
 	slot_startcount=tyncpoll_time*1000/LPTIMER_LSB;
 	lastpoll_time=tyncpoll_time;
-  tag_frequency = 1000/g_com_map[COM_INTERVAL];
+    tag_frequency = 1000/g_com_map[COM_INTERVAL];
 	total_slotnum = 1000/g_com_map[COM_INTERVAL];
+    nomovesleeptime = g_com_map[NOMOVESLEEP_TIME];
+	if(active_flag==0)
+	{   
+		imu_enable=1;
+		nomovesleeptime =10;		
+	}
+#ifdef DEBUG_MODE
+//	g_com_map[DEV_ROLE]=1;
+//		g_com_map[DEV_ID]=1;
+//	g_com_map[COM_INTERVAL]=100;
+//	g_com_map[MAX_REPORT_ANC_NUM]=1;
+//	g_com_map[NOMOVESLEEP_TIME]=10;
+#endif
+		if(g_com_map[COM_INTERVAL]==0)
+	{
+	  g_com_map[COM_INTERVAL]=100;
+	}
+	 if(module_power>67)
+		{module_power=67;}
+		if(module_power<0)
+		{module_power=0;}
+
 	current_slotnum=1;
 	current_time=GetLPTime();
 		if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, slot_startcount) != HAL_OK)  //system time is 1010ms   1 puls=30.518us
@@ -380,6 +396,39 @@
   * @brief  The application entry point.
   * @retval int
   */
+uint16_t temp2; 
+float dw_vbat,last_vbat;
+uint8_t Get_Battary_UWB(void)
+{
+    static uint8_t first = 1;
+    static float last_value = 100;   
+    static uint16_t bat_count = 0;
+    //if(bat_count++%600==0)
+    {
+	temp2=dwt_readtempvbat(1);
+	dw_vbat=(float)((temp2&0xff)-173)/173+3.3;
+    if(fabs(dw_vbat-dw_vbat)>0.2)
+    {
+        last_vbat = dw_vbat;
+        return last_value;
+    }
+    last_vbat = dw_vbat;      
+	if(dw_vbat>=3.0)
+	{
+		if(first)
+		{
+            first = 0;			
+			last_value =(dw_vbat-3.0)*167;//(bat_volt-3.5)/0.6*100
+		}
+		last_value = 0.9*last_value + (dw_vbat-3.0)*16.7;
+	if(last_value>100)
+		last_value = 100;
+	if(last_value<0)
+		last_value = 0;
+    }
+    }
+	return last_value;	       
+}
 int main(void)
 {
   /* USER CODE BEGIN 1 */
@@ -405,18 +454,19 @@
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
   MX_LPTIM1_Init();
+   MX_DMA_Init();
   MX_USART1_UART_Init();
   MX_SPI1_Init();
-  MX_ADC_Init();
-  MX_DMA_Init();
+ // MX_ADC_Init();
+ 
 //  MX_TIM2_Init();
   /* USER CODE BEGIN 2 */
 	LED_LG_ON;
 	Program_Init();
-	LIS3DH_Data_Init();	
+//	LIS3DH_Data_Init();	
 	Dw1000_Init();
 	Dw1000_App_Init();
-	HardWareTypeDiffConfig();
+//	HardWareTypeDiffConfig();
 	dwt_configuresleep(DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_CS | DWT_WAKE_WK| DWT_SLP_EN);
 	dwt_entersleep();
 	waitusart_timer=tag_frequency*USART_KEEPWAKE_TIME;;
@@ -439,7 +489,7 @@
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */
-		if(g_start_send_flag)
+		if(g_start_send_flag&&active_flag)
 	{ static uint16_t blink_count=0;
 		SystemClock_Config();
 		g_start_send_flag = 0;	
@@ -450,9 +500,9 @@
 //		}else{
 //		HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_RESET);
 //		}
-//		LED_TR_BLINK;
+//		LED_TR_BLINK;s
 		
-			bat_percent=Get_Battary();
+			
 		if(bat_percent>15)
 		{	LED_TB_ON;
 			Tag_App();
@@ -468,7 +518,7 @@
 		{
 			waitusart_timer--;
 		}
-			if(m_bEUARTTxEn==0&&waitusart_timer==0&&(motor_state==0||motor_state==3))
+			if(m_bEUARTTxEn==0&&waitusart_timer==0)
 			{	
 #ifndef DEBUG_MODE	
 if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))				
@@ -932,25 +982,29 @@
 		key_keeptime+=(float)g_com_map[COM_INTERVAL]/1000;
 		if(key_keeptime>=KEY_KEEPRESET_TIME)
 		{
+            g_com_map[ACTIVE_INDEX]=!active_flag;
+            save_com_map_to_flash();
+            MOTOR_ON;
+            HAL_Delay(2000);
 			SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; 
 		}
 	}else{
 		key_keeptime=0;
 	}
-	if(nomove_count>STATIONARY_TIME)
-	{
-		stationary_flag = 1;
-	}else{
-		stationary_flag = 0;
-	}
-if(nomove_count>=g_com_map[NOMOVESLEEP_TIME]-1&&imu_enable)
-	{
-		tx_near_msg[TAGSTATE] = tx_near_msg[TAGSTATE]|GOINGSLEEPBIT;
-	}
-	if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&imu_enable)
-	{
-		mcu_sleep();
-	}
+//	if(nomove_count>STATIONARY_TIME)
+//	{
+//		stationary_flag = 1;
+//	}else{
+//		stationary_flag = 0;
+//	}
+//if(nomove_count>=nomovesleeptime-1&&imu_enable)
+//	{
+//		tx_near_msg[TAGSTATE] = tx_near_msg[TAGSTATE]|GOINGSLEEPBIT;
+//	}
+//	if(nomove_count>nomovesleeptime&&imu_enable)
+//	{
+//		mcu_sleep();
+//	}
 }
 /* USER CODE END 4 */
 

--
Gitblit v1.9.3