From 0017519b68e7c8f47ccc1df3d92eaf07a2b21050 Mon Sep 17 00:00:00 2001
From: yincheng.zhong <634916154@qq.com>
Date: 星期五, 26 五月 2023 13:59:19 +0800
Subject: [PATCH] 修改标签测距0bug

---
 Src/main.c |  318 +++++++++++++++++++++++-----------------------------
 1 files changed, 143 insertions(+), 175 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index a247a73..6b735d7 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -33,7 +33,7 @@
 #define NSH1 0x0001
 #define GP   0x0002
 #define FLASH_HARDWARE_VERSION_MAP		(uint32_t)0x08004F00    //硬件版本号和批次信息位置
-//#define DEBUG_MODE
+#define DEBUG_MODE
 /* USER CODE END Includes */
 
 /* Private typedef -----------------------------------------------------------*/
@@ -75,7 +75,7 @@
 static void MX_LPTIM1_Init(void);
 static void MX_USART1_UART_Init(void);
 static void MX_SPI1_Init(void);
-static void MX_ADC_Init(void);
+void MX_ADC_Init(void);
 static void MX_DMA_Init(void);
 static void MX_TIM2_Init(void);
 /* USER CODE BEGIN PFP */
@@ -115,80 +115,17 @@
 float motor_keeptime;
 void IdleTask(void)
 {u16 current_time;
-	
-			UART_CheckReceive();
-		UART_CheckSend();
-
-if(huart1.Instance->ISR&USART_ISR_FE)
-	{
-	SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
-	}	
-//if(!HAL_GPIO_ReadPin(PWR_ON_GPIO_Port,PWR_ON_Pin))
-//{
-//	LED_TR_ON;
-//	LED_TB_OFF;
-//}else{
-//	LED_TR_OFF;
-	current_time=GetLPTime();
-	//motor_state=2;
-//		if(g_com_map[MOTOR_ONTIME]>0)
+//if(huart1.Instance->ISR&USART_ISR_FE)
 //	{
-//		g_com_map[MOTOR_ONTIME]--;
-//		motor_state = 1;
-//	}
-if(g_com_map[MOTOR_ENABLE])
-{
-    
-
-	switch(motor_state)
-		{case 0:
-			if(hardware_type==NSH1)
-			{MOTOR_OFF;
-			}else if(hardware_type==GP)
-			{
-			HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
-			}
-			break;
-		case 1:
-			if(current_time<MOTOR_ONTIME)
-			{
-				if(hardware_type==NSH1)
-				{MOTOR_ON;
-				}else if(hardware_type==GP)
-				{
-				HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
-				}	
-			}else{
-				if(hardware_type==NSH1)
-				{MOTOR_OFF;
-				}else if(hardware_type==GP)
-				{
-				HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
-				}	
-			}				
-			break;
-		case 2:
-			if(hardware_type==NSH1)
-				{MOTOR_ON;
-				}else if(hardware_type==GP)
-				{
-				HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
-				}	
-			break;
-		case 3:
-			if(hardware_type==NSH1)
-				{MOTOR_OFF;
-				}else if(hardware_type==GP)
-				{
-				HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
-				}	
-			break;	
-	}
-}
+//	SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+//	}	
 	if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
 	{
+        	
+        UART_CheckReceive();
+		UART_CheckSend();
 		nomove_count = 0;
-        bat_percent=Get_Battary();
+       // bat_percent=Get_Battary();
 		if(state5v==0)
 		{
 			state5v=1;
@@ -208,41 +145,9 @@
 		LED_LG_OFF;
 //	}
 }
-	if(g_com_map[CNT_UPDATE]==1)
-	{
-	uint32_t result = 0;
-		uint16_t tmp = 0xAAAA;
-	__disable_irq();
-	result = FLASH_Prepare(0x8004A38, 128);
-	if(result)
-		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   
-	}
-			if(g_com_map[CNT_REBOOT]==1)
+        if(g_com_map[MAP_SIGN_INDEX]!=0x55AA||g_com_map[COM_INTERVAL]==0)
 		{
-			g_com_map[CNT_REBOOT]=0;
-			g_com_map[MAP_SIGN_INDEX]=0;
-			save_com_map_to_flash();
-			delay_ms(100);
-			SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
-		}
-		if(g_com_map[CNT_RESTART]==1)
-		{
-			g_com_map[CNT_RESTART]=0;
-			save_com_map_to_flash();
-			delay_ms(100);
-				SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
-		}
-			if(g_com_map[MAP_SIGN_INDEX]!=0x55AA||g_com_map[COM_INTERVAL]==0)
-		{
-				SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
 		}
 
 	}
@@ -250,45 +155,51 @@
 
 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+32768/tag_frequency-time*LPTIMER_LSB;
+    while(lpcount>LPTIMER_1S_COUNT)
+    {
+        lpcount-=LPTIMER_1S_COUNT;
+    }
+    last_lpcount = lpcount;
 	__HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
-}
+
 }
 
 void HardWareTypeDiffConfig(void)
 {
-GPIO_InitTypeDef GPIO_InitStruct = {0};
-//hardware_type=2;
-if(hardware_type == 0)
-    hardware_type = 1;
+    GPIO_InitTypeDef GPIO_InitStruct = {0};
+    //hardware_type=2;
+    if(hardware_type == 0)
+        hardware_type = 1;
 	switch(hardware_type)
 		{
         case 1:
 	  
 		 HAL_GPIO_WritePin(GPIOB, MOTOR_Pin, GPIO_PIN_SET);
-  GPIO_InitStruct.Pin = MOTOR_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
-  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+          GPIO_InitStruct.Pin = MOTOR_Pin;
+          GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+          GPIO_InitStruct.Pull = GPIO_NOPULL;
+          GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
+          HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 			break;
 		case 2:
 			MX_TIM2_Init();
@@ -307,9 +218,19 @@
 	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] = (1<<8)|29;
+	g_com_map[VERSION] = (1<<8)|55;
+    
+    #ifdef DEBUG_MODE
+   // g_com_map[GROUP_ID]=21;
+//    		g_com_map[DEV_ID]=4;
+    //	g_com_map[COM_INTERVAL]=100;
+    //	g_com_map[MAX_REPORT_ANC_NUM]=1;
+    //	g_com_map[NOMOVESLEEP_TIME]=10;
+    #endif
+    g_com_map[COM_INTERVAL] = 1000;
     active_flag = g_com_map[ACTIVE_INDEX];
-    	module_power = g_com_map[POWER];
+    module_power = g_com_map[POWER];
+    g_com_map[IMU_ENABLE] = 0;
 	imu_enable=g_com_map[IMU_ENABLE];
 	motor_enable=g_com_map[MOTOR_ENABLE];
 	
@@ -328,13 +249,8 @@
 		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
+
+ //   g_com_map[COM_INTERVAL]=100;
 		if(g_com_map[COM_INTERVAL]==0)
 	{
 	  g_com_map[COM_INTERVAL]=100;
@@ -351,11 +267,8 @@
     Error_Handler();
   }
 //g_com_map[NOMOVESLEEP_TIME]=5;
-	printf("标签ID: %d .\r\n",dev_id);
-	printf("通讯间隔: %d ms.\r\n",g_com_map[COM_INTERVAL]);
-	printf("单次通讯基站数量: %d个.\r\n",g_com_map[MAX_REPORT_ANC_NUM]);
-	
-
+    printf("固件版本: 免布线基站 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
+	memcpy(g_com_map2,g_com_map,COM_MAP_SIZE);
 //	printf("DEVICE PAIRID: %d .\r\n",g_com_map[PAIR_ID]);
 //	printf("DEVICE ALARM DISTANCE: 1.%d 2.%d 3.%d .\r\n",g_com_map[ALARM_DISTANCE1],g_com_map[ALARM_DISTANCE2],g_com_map[ALARM_DISTANCE3]);
 
@@ -390,6 +303,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 */
@@ -418,25 +364,24 @@
    MX_DMA_Init();
   MX_USART1_UART_Init();
   MX_SPI1_Init();
-  MX_ADC_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;;
 	DW_DISABLE;
 	 if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
 	{
 	 Error_Handler();
 	}
-//	HAL_Delay(2000);
+	HAL_Delay(2000);
 //	mcu_sleep();
 	//LED0_BLINK;		
 //	SystemPower_Config();
@@ -450,7 +395,7 @@
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */
-		if(g_start_send_flag&&active_flag)
+		if(g_start_send_flag)
 	{ static uint16_t blink_count=0;
 		SystemClock_Config();
 		g_start_send_flag = 0;	
@@ -463,7 +408,7 @@
 //		}
 //		LED_TR_BLINK;s
 		
-			bat_percent=Get_Battary();
+			
 		if(bat_percent>15)
 		{	LED_TB_ON;
 			Tag_App();
@@ -479,8 +424,12 @@
 		{
 			waitusart_timer--;
 		}
-			if(m_bEUARTTxEn==0&&waitusart_timer==0&&(motor_state==0||motor_state==3))
-			{	
+			if(m_bEUARTTxEn==0&&waitusart_timer==0)
+			{
+             if(memcmp(g_com_map2,g_com_map,200)!=0)
+             {
+                SCB->AIRCR = 0X05FA0000|(unsigned int)0x04;
+             }
 #ifndef DEBUG_MODE	
 if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))				
 				HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
@@ -559,7 +508,12 @@
   * @param None
   * @retval None
   */
-static void MX_ADC_Init(void)
+void MX_ADC_DeInit(void)
+{
+    HAL_ADC_DeInit(&hadc);
+    HAL_ADCEx_DisableVREFINT();
+}
+void MX_ADC_Init(void)
 {
 
   /* USER CODE BEGIN ADC_Init 0 */
@@ -596,14 +550,15 @@
   }
   /** Configure for the selected ADC regular channel to be converted.
   */
-  sConfig.Channel = ADC_CHANNEL_1;
+  sConfig.Channel = ADC_CHANNEL_VREFINT;
   sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
   if (HAL_ADC_ConfigChannel(&hadc, &sConfig) != HAL_OK)
   {
     Error_Handler();
   }
   /* USER CODE BEGIN ADC_Init 2 */
-HAL_ADCEx_Calibration_Start(&hadc,ADC_SINGLE_ENDED);
+    HAL_ADCEx_EnableVREFINT();
+    HAL_ADCEx_Calibration_Start(&hadc,ADC_SINGLE_ENDED);
   /* USER CODE END ADC_Init 2 */
 
 }
@@ -926,21 +881,34 @@
 }
 extern uint8_t tx_near_msg[80],stationary_flag;
 float key_keeptime;
+extern float freqlost_count,range_lost_time;
 void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
 {
 	static uint8_t i=0;
 	i++;
 	g_start_send_flag=1;
 	current_slotnum++;
-	SetNextPollTime(tyncpoll_time);
-	nomove_count+=(float)g_com_map[COM_INTERVAL]/1000;
+    freqlost_count+=1/(float)tag_frequency;
+    range_lost_time +=1/(float)tag_frequency;
+    
+    if(freqlost_count>FREQ_LOST_TIME)
+    {
+        tag_frequency = NOTAG_FREQ;
+    }
+    if(range_lost_time<1||freqlost_count>FREQ_LOST_TIME)
+    {
+        SetNextPollTime(0);
+    }else{
+        SetNextPollTime(5);
+    }
+	nomove_count+=1/(float)tag_frequency;
     if(motor_keeptime>0)
 	{	
-	motor_keeptime-=(float)g_com_map[COM_INTERVAL]/1000;
+        nomove_count+=1/(float)tag_frequency;
 	}
 	if(!GET_USERKEY)
 	{
-		key_keeptime+=(float)g_com_map[COM_INTERVAL]/1000;
+		key_keeptime+=1/(float)tag_frequency;
 		if(key_keeptime>=KEY_KEEPRESET_TIME)
 		{
             g_com_map[ACTIVE_INDEX]=!active_flag;
@@ -952,20 +920,20 @@
 	}else{
 		key_keeptime=0;
 	}
-	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();
-	}
+//	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