From 3a1d007b19b92f62bbbb8f14cbbdb26a61ac05f5 Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期五, 25 十二月 2020 13:47:54 +0800
Subject: [PATCH] V2.19 下发震动功能

---
 Src/main.c |  189 ++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 144 insertions(+), 45 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 5eb9090..2b5cc59 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -87,6 +87,8 @@
 uint16_t slottime,max_slotpos;
 uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency,lastpoll_time,current_time;
 extern uint8_t module_power;
+extern float nomove_count;
+float motor_keeptime;
 uint8_t imu_enable,motor_enable;	
 u16 GetLPTime(void)
 {
@@ -103,16 +105,17 @@
 	 Error_Handler();
 	}
 }
+u16 current_time;
 uint8_t state5v;
 void IdleTask(void)
-{u16 current_time;
+{
 	
 			UART_CheckReceive();
 		UART_CheckSend();
 
 if(huart1.Instance->ISR&USART_ISR_FE)
 	{
-	SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+//	SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
 	}	
 //if(!HAL_GPIO_ReadPin(PWR_ON_GPIO_Port,PWR_ON_Pin))
 //{
@@ -122,11 +125,14 @@
 //	LED_TR_OFF;
 	current_time=GetLPTime();
 	//motor_state=2;
-		if(g_com_map[MOTOR_ONTIME]>0)
-	{
-		g_com_map[MOTOR_ONTIME]--;
-		motor_state = 1;
-	}
+if(g_com_map[MOTOR_ENABLE])
+{
+		if(motor_keeptime>0)
+		{
+			motor_state = 2;
+		}else{
+			motor_state = 0;
+		}
 	switch(motor_state)
 		{case 0:
 			MOTOR_OFF;
@@ -137,8 +143,7 @@
 				MOTOR_ON;		
 			}else{
 				MOTOR_OFF;	
-			}	
-				
+			}				
 			break;
 		case 2:
 			MOTOR_ON;	
@@ -147,8 +152,10 @@
 			MOTOR_OFF;
 			break;	
 	}
+}
 	if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
 	{
+		nomove_count = 0;
 		if(state5v==0)
 		{
 			state5v=1;
@@ -200,24 +207,44 @@
 			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   
+		}
 
 	}
 
 
-u16 total_slotnum,current_slotnum;
+u16 current_slotnum;
 extern int32_t offsettimeus;
-
-void SetNextPollTime(u16 time)
+//#define FIXSLOT
+#define FIXSLOTPOS  2
+u16 slotpos_intoatl;
+uint16_t bigslot_num;
+uint8_t fix_slotpos =FIXSLOTPOS; 
+int32_t lpcount,lptime,target_time;
+extern u16 tagslotpos;
+void SetNextPollTime(u16 slotpos)
 {
-  int32_t lpcount,lptime,target_time;
 
-	if(current_slotnum>=total_slotnum)
-		current_slotnum-=total_slotnum;
+#ifdef FIXSLOT
+//	fix_slotpos =FIXSLOTPOS;
+	fix_slotpos = g_com_map[ALARM_DISTANCE1];
+	slotpos = fix_slotpos;
+	tagslotpos = fix_slotpos;
+#endif
+
+	while(current_slotnum>=tag_frequency)
+	{	current_slotnum-=tag_frequency;}
+	slotpos_intoatl = current_slotnum*bigslot_num+slotpos;
+
+	if(current_slotnum==19)
+	{slotpos_intoatl = current_slotnum*bigslot_num+slotpos;}
+	target_time=(slotpos_intoatl*SLOTTIME_MS+100)*100;
 	
-	target_time=((current_slotnum*g_com_map[COM_INTERVAL])+time)*1000;
 	//if(target_time<90000)
 	{
-		lptime=target_time-offsettimeus;
+		lptime=target_time-offsettimeus+1200;
 		lpcount = lptime/LPTIMER_LSB;
 	if(lpcount>LPTIMER_1S_COUNT)
 		lpcount-=LPTIMER_1S_COUNT;
@@ -228,6 +255,7 @@
 	__HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
 }
 }
+u8 active_flag=0;
 void Program_Init(void)
 {	float temp;
 	uint16_t temp2;
@@ -235,29 +263,51 @@
 	Usart1ParseDataCallback = UsartParseDataHandler;
 	parameter_init();
 	//deca_sleep(1000);
-	g_com_map[VERSION] = 0x010f;
+	g_com_map[VERSION] = 0x0213;
+//	g_com_map[DEV_ID]=0x11;
 #ifdef DEBUG_MODE
 //	g_com_map[DEV_ROLE]=1;
-//		g_com_map[DEV_ID]=1;
-	g_com_map[COM_INTERVAL]=100;
+	//	g_com_map[DEV_ID]=0x11;
+//	g_com_map[COM_INTERVAL]=100;
 //	g_com_map[MAX_REPORT_ANC_NUM]=1;
-	g_com_map[NOMOVESLEEP_TIME]=10;
+//	g_com_map[NOMOVESLEEP_TIME]=10;
+			g_com_map[IMU_ENABLE]=0;
+	//g_com_map[COM_INTERVAL]=50;
 #endif
-		
-	
+//g_com_map[NOMOVESLEEP_TIME]=10;
+//	g_com_map[IMU_ENABLE]=0;
+//	 g_com_map[COM_INTERVAL]=1000;
+	active_flag = g_com_map[ACTIVE_INDEX];
+	if(active_flag==0)
+	{ g_com_map[COM_INTERVAL]=1000;
+		g_com_map[IMU_ENABLE]=1;
+		g_com_map[NOMOVESLEEP_TIME]=10;
+		g_com_map[MOTOR_ENABLE]=0;
+	}
+		if(g_com_map[COM_INTERVAL]==0)
+	{
+	  g_com_map[COM_INTERVAL]=100;
+	}
+	if(g_com_map[IMU_THRES]>10)
+	{g_com_map[IMU_THRES]=2;}
+	 if(g_com_map[POWER]>MAX_RFPOWER)
+		{g_com_map[POWER]=MAX_RFPOWER;}
+		if(g_com_map[POWER]<0)
+		{g_com_map[POWER]=0;}
 	module_power = g_com_map[POWER];
 	imu_enable=g_com_map[IMU_ENABLE];
 	motor_enable=g_com_map[MOTOR_ENABLE];
 	
 	group_id=g_com_map[GROUP_ID];
 	dev_id = g_com_map[DEV_ID];
-	slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*0.4)+2;
+	//slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*SLOT_SCALE)+3;
+		slottime = 5;
 	max_slotpos=g_com_map[COM_INTERVAL]/slottime;
 	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];
-	total_slotnum = 1000/g_com_map[COM_INTERVAL];
+	bigslot_num = TOTAL_SLOTNUM/tag_frequency;
 	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
@@ -265,9 +315,9 @@
     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("标签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("DEVICE PAIRID: %d .\r\n",g_com_map[PAIR_ID]);
@@ -330,7 +380,7 @@
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
   MX_LPTIM1_Init();
-	 MX_DMA_Init();
+	MX_DMA_Init();
   MX_USART1_UART_Init();
   MX_SPI1_Init();
   MX_ADC_Init();
@@ -363,7 +413,11 @@
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */
-		if(g_start_send_flag)
+//				while(1)
+//		{
+//		MODBUS_Poll();
+//		}
+		if(g_start_send_flag&&active_flag)
 	{ static uint16_t blink_count=0;
 		SystemClock_Config();
 		g_start_send_flag = 0;	
@@ -392,9 +446,10 @@
 		{
 			waitusart_timer--;
 		}
-			if(m_bEUARTTxEn==0&&waitusart_timer==0)
+			if(m_bEUARTTxEn==0&&waitusart_timer==0&&(motor_state==0||motor_state==3))
 			{	
-#ifndef DEBUG_MODE				
+#ifndef DEBUG_MODE	
+				if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
 				HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
 #endif
 			}
@@ -625,12 +680,12 @@
     Error_Handler();
   }
   /* USER CODE BEGIN USART1_Init 2 */
-	    GPIO_InitStruct.Pin = GPIO_PIN_10;
-    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
-    GPIO_InitStruct.Pull = GPIO_PULLUP;
-    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
-    GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
-    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+//	    GPIO_InitStruct.Pin = GPIO_PIN_9;
+//    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+//    GPIO_InitStruct.Pull = GPIO_PULLUP;
+//    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
+//    GPIO_InitStruct.Alternate = GPIO_AF4_USART1;
+//    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 //  if(HAL_UART_Receive_IT(&huart1, (uint8_t *)aRxBuffer, 1) != HAL_OK)
 //  {
 //    Error_Handler();
@@ -675,7 +730,7 @@
   HAL_GPIO_WritePin(GPIOA, SCL_Pin|SDA_Pin|SPI_CS_Pin, GPIO_PIN_SET);
 
   /*Configure GPIO pin Output Level */
-  HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|MOTOR_Pin, GPIO_PIN_SET);
+  HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|MOTOR_Pin|PWR_CTL_Pin, GPIO_PIN_SET);
 
   /*Configure GPIO pin Output Level */
   HAL_GPIO_WritePin(GPIOB, LED1_G_Pin|LED1_R_Pin|LED2_R_Pin, GPIO_PIN_RESET);
@@ -722,8 +777,10 @@
   GPIO_InitStruct.Pull = GPIO_PULLUP;
   HAL_GPIO_Init(USER_KEY_GPIO_Port, &GPIO_InitStruct);
 
-  /*Configure GPIO pins : MOTOR_Pin LED1_G_Pin LED1_R_Pin LED2_R_Pin */
-  GPIO_InitStruct.Pin = MOTOR_Pin|LED1_G_Pin|LED1_R_Pin|LED2_R_Pin;
+  /*Configure GPIO pins : MOTOR_Pin LED1_G_Pin LED1_R_Pin LED2_R_Pin 
+                           PWR_CTL_Pin */
+  GPIO_InitStruct.Pin = MOTOR_Pin|LED1_G_Pin|LED1_R_Pin|LED2_R_Pin 
+                          |PWR_CTL_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
@@ -779,17 +836,57 @@
   /*Enter the Standby mode*/
   HAL_PWR_EnterSTANDBYMode();	
 }
-extern uint8_t tx_near_msg[80],stationary_flag;
-extern float nomove_count;
+extern u16 tagslotpos;
+extern uint8_t tx_near_msg[80],stationary_flag,gotosleep_flag;
 float key_keeptime;
+extern float motor_ontime;
 void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
 {
 	static uint8_t i=0;
 	i++;
 	g_start_send_flag=1;
 	current_slotnum++;
-	SetNextPollTime(tyncpoll_time);
+	SetNextPollTime(tagslotpos);
 	nomove_count+=(float)g_com_map[COM_INTERVAL]/1000;
+	if(motor_keeptime>0)
+	{	
+	motor_keeptime-=(float)g_com_map[COM_INTERVAL]/1000;
+	}
+	if(active_flag==0)
+	{
+	if(!GET_USERKEY)
+	{
+		LED_LG_ON;
+			key_keeptime+=(float)g_com_map[COM_INTERVAL]/1000;
+		if(key_keeptime>3)
+		{
+			u8 i=250,keystate=1;
+			while(i--)
+			{
+				if(i%10==0)
+				{
+				LED_LG_BLINK;
+				}
+				if(GET_USERKEY)
+				{keystate = 0;}
+			if(keystate==0&&!GET_USERKEY)
+			{
+				parameter_init();
+				g_com_map[ACTIVE_INDEX] = 1;
+				save_com_map_to_flash();
+				delay_ms(100);
+				SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+			}
+			delay_ms(10);
+			}		
+		}
+	}else{
+		LED_LG_OFF;
+		key_keeptime=0;
+	}
+		
+	}
+
 	if(!GET_USERKEY)
 	{
 		key_keeptime+=(float)g_com_map[COM_INTERVAL]/1000;
@@ -808,7 +905,9 @@
 	}
 if(nomove_count>=g_com_map[NOMOVESLEEP_TIME]-1&&imu_enable)
 	{
-		tx_near_msg[TAGSTATE] = tx_near_msg[TAGSTATE]|GOINGSLEEPBIT;
+		gotosleep_flag=1;
+	}else{
+		gotosleep_flag=0;
 	}
 	if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&imu_enable)
 	{

--
Gitblit v1.9.3