WXK
2023-11-03 03c3a1878df2cb6a72fdffa393c9e453f6e1b6ac
Src/main.c
@@ -31,7 +31,7 @@
#define NSH1 0x0001
#define GP   0x0002
//#define DEBUG_MODE
#define DEBUG_MODE
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
@@ -94,6 +94,7 @@
extern float nomove_count;
float motor_keeptime;
uint8_t imu_enable,motor_enable;   
uint8_t chongqi_num;
u16 GetLPTime(void)
{
   u16 count=HAL_LPTIM_ReadCounter(&hlptim1);
@@ -101,7 +102,7 @@
}   
void UsartInit(void)
{
   waitusart_timer = tag_frequency*USART_KEEPWAKE_TIME;
//   waitusart_timer = tag_frequency*USART_KEEPWAKE_TIME;
    MX_DMA_Init();
   MX_USART1_UART_Init();
 if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
@@ -130,60 +131,8 @@
//   LED_TR_OFF;
   current_time=GetLPTime();
   //motor_state=2;
if(g_com_map[MOTOR_ENABLE])
{
      if(motor_keeptime>0)
      {
         motor_state = 2;
      }else{
         motor_state = 0;
      }
   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;
   }
}
    bat_percent=Get_Battary();
   if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
   {
      nomove_count = 0;
@@ -192,7 +141,7 @@
         state5v=1;
         UsartInit();
      }
      if(bat_percent>99)
      if(HAL_GPIO_ReadPin(PWR_ON_GPIO_Port,PWR_ON_Pin))
      {
         LED_LR_OFF;
         LED_LG_ON;
@@ -252,40 +201,9 @@
#define FIXSLOTPOS  4
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) //时间片逻辑:上来默认计算一个时间,时间同步之后,就近选择时间片。SetNextPollTime会调整下次发送
{
#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;
   //if(target_time<90000)
   {
      lptime=target_time-offsettimeus+1200;
      lpcount = lptime/LPTIMER_LSB;
   if(lpcount>LPTIMER_1S_COUNT)
      lpcount-=LPTIMER_1S_COUNT;
   if(lpcount<0)
   {
   lpcount+=LPTIMER_1S_COUNT;
   }
   __HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
}
}
void ErrorConfig_Handler(void)
{
      if(g_com_map[COM_INTERVAL]==0||g_com_map[COM_INTERVAL]>1000)
@@ -294,12 +212,19 @@
   }
   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;}
    if(g_com_map[POWER]>MAX_RFPOWER)
        {g_com_map[POWER]=MAX_RFPOWER;}
    if(g_com_map[POWER]<0)
        {g_com_map[POWER]=0;}
    if(g_com_map[STATIONARY_TIME]==0)
        {
        g_com_map[STATIONARY_TIME]=10;
        }
}
uint8_t uwb_active_flag = 0;
u8 active_flag=0;
u16 nomovesleep_time=0,interval;
void Program_Init(void)
{   float temp;
   uint16_t temp2;
@@ -310,13 +235,14 @@
   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] = 0x0217;
   g_com_map[DEV_ID]=0x6;
   g_com_map[VERSION] = (2<<8)|11;
//    g_com_map[COM_INTERVAL]=200;
//   g_com_map[DEV_ID]=0x7;
#ifdef DEBUG_MODE
//   g_com_map[DEV_ROLE]=1;
      g_com_map[DEV_ID]=0x4008;
    g_com_map[ACTIVE_INDEX]=1;
//   g_com_map[COM_INTERVAL]=100;
   //   g_com_map[DEV_ID]=0x4009;
//    g_com_map[ACTIVE_INDEX]=1;
//   g_com_map[COM_INTERVAL]=50;
//   g_com_map[MAX_REPORT_ANC_NUM]=1;
//   g_com_map[NOMOVESLEEP_TIME]=10;
//         g_com_map[IMU_ENABLE]=0;
@@ -324,35 +250,41 @@
#endif
//g_com_map[NOMOVESLEEP_TIME]=10;
//   g_com_map[IMU_ENABLE]=0;
//    g_com_map[COM_INTERVAL]=1000;
//    g_com_map[COM_INTERVAL]=50;
   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;
   }
//g_com_map[HEIGHTOFFEST_INDEX]=g_com_map[MAX_REPORT_ANC_NUM];
   module_power = g_com_map[POWER];
   imu_enable=g_com_map[IMU_ENABLE];
   motor_enable=g_com_map[MOTOR_ENABLE];
   uwb_active_flag = g_com_map[UWB_ACTIVE_FLAG_IDX];
   group_id=g_com_map[GROUP_ID];
   dev_id = g_com_map[DEV_ID];
   //slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*SLOT_SCALE)+3;
      slottime = 5;
   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];
   bigslot_num = TOTAL_SLOTNUM/tag_frequency;
   current_slotnum=1;
   current_time=GetLPTime();
    nomovesleep_time = g_com_map[NOMOVESLEEP_TIME];
    interval = g_com_map[COM_INTERVAL];
//    if(active_flag==0)
//   {
//        interval=1000;
//      imu_enable=1;
//      nomovesleep_time = 10;
//      motor_enable=0;
//   }
    tag_frequency = 1000/interval;
      if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, slot_startcount) != HAL_OK)  //system time is 1010ms   1 puls=30.518us
  {
    Error_Handler();
  }
   printf("固件版本:SS双路定位标签-防撞基站 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
//g_com_map[NOMOVESLEEP_TIME]=5;
//   printf("标签ID: %d .\r\n",dev_id);
//   printf("通讯间隔: %d ms.\r\n",g_com_map[COM_INTERVAL]);
@@ -379,31 +311,52 @@
void HardWareTypeDiffConfig(void)
{
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);
         break;
      case 2:
         MX_TIM2_Init();
      //BarInit();
         break;
      }
//   GPIO_InitTypeDef GPIO_InitStruct = {0};
//   if(g_com_map[MOTORBEEPER_INDEX]!=1&&g_com_map[MOTORBEEPER_INDEX]!=2)
//   {
//         g_com_map[MOTORBEEPER_INDEX] = 2;
//   }
//   hardware_type   = g_com_map[MOTORBEEPER_INDEX];
//   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);
//         break;
//      case 2:
//         MX_TIM2_Init();
//         break;
//      }
}
void DoubleClickProcess(void)
{ char temp[50];
  uint8_t len;
if(uwb_active_flag)
{
    uwb_active_flag = 0;
    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
    HAL_Delay(3000);
    HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
}else{
    uwb_active_flag = 1;
    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
    HAL_Delay(1000);
    HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
}
   g_com_map[UWB_ACTIVE_FLAG_IDX]= uwb_active_flag;
save_com_map_to_flash();
}
void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
      //SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
}
uint8_t i;
uint8_t uwb_tx[300]={0,};
/* USER CODE END 0 */
/**
@@ -435,26 +388,36 @@
  /* 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();
  //MX_TIM2_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */
   LED_LG_ON;
   Program_Init();
   LIS3DH_Data_Init();
   Accelerometer_Init();
   // BarInit();
   Dw1000_Init();
   Dw1000_App_Init();
   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;;
//   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;;
    dwt_setinterrupt(  DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 1);
    dwt_setrxtimeout(0);//设定接收超时时间,0位没有
    dwt_rxenable(0);
   DW_DISABLE;
    if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
   {
    Error_Handler();
   }
   for(i=0;i<255;i++)
    {
    uwb_tx[i]=i;
    }
//   HAL_Delay(2000);
//   mcu_sleep();
   //LED0_BLINK;      
@@ -473,9 +436,15 @@
//      {
//      MODBUS_Poll();
//      }
      if(g_start_send_flag&&active_flag)
if(m_bEUARTTxEn==0&&(motor_state==0||motor_state==3))
         {
#ifndef DEBUG_MODE
            if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
            HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
#endif
         }
      if(g_start_send_flag)
   { static uint16_t blink_count=0;
      SystemClock_Config();
      g_start_send_flag = 0;   
   //   GetPressAndTemp();
//      if(current_count>slot_startcount&&current_count<slot_startcount+interval_count)
@@ -485,8 +454,8 @@
//      HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_RESET);
//      }
//      LED_TR_BLINK;
         bat_percent=Get_Battary();
            chongqi_num=0;
         __disable_irq();
      if(bat_percent>15)
      {   LED_TB_ON;
         Tag_App();
@@ -496,21 +465,14 @@
         Tag_App();
         LED_TR_OFF;      
      }
         __enable_irq();
      //LED0_BLINK;      
      IdleTask();   
      if(waitusart_timer>0)
      {
         waitusart_timer--;
      }
         if(m_bEUARTTxEn==0&&waitusart_timer==0&&(motor_state==0||motor_state==3))
         {
#ifndef DEBUG_MODE
            if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
            HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
#endif
         }
   }else{
   IdleTask();   
   }
@@ -917,9 +879,18 @@
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(PWR_ON_GPIO_Port, &GPIO_InitStruct);
  /*Configure GPIO pin : PB8 */
  GPIO_InitStruct.Pin = GPIO_PIN_8;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  /* EXTI interrupt init*/
  HAL_NVIC_SetPriority(EXTI0_1_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);
}
@@ -939,6 +910,8 @@
   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
   
   HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
   HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);  
  /*Clear all related wakeup flags*/
@@ -951,19 +924,52 @@
extern u16 tagslotpos;
extern uint8_t tx_near_msg[80],stationary_flag,gotosleep_flag;
float key_keeptime;
extern float motor_ontime;
extern float motor_ontime,button_delay;
uint32_t tagdisterror_list[TAG_NUM_IN_SYS];
extern uint16_t taglist_num;
extern int32_t tagdist_list[TAG_NUM_IN_SYS];
extern uint16_t tagid_list[TAG_NUM_IN_SYS];
void Up_tagdisterror_list_Poll()
{
    static uint8_t r=0;
    static uint8_t t=0;
    for(r=0;r<taglist_num;r++)
    {
        tagdisterror_list[r]++;
        if(tagdisterror_list[r]>10)
        {
            for(t=r;t<taglist_num;t++)
            {
            tagdist_list[t]=tagdist_list[t+1];
            tagid_list[t]=tagid_list[t+1];
            tagdisterror_list[t]=tagdisterror_list[t+1];
            taglist_num=taglist_num-1;
            }
        }
    }
}
void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
{
   static uint8_t i=0;
   i++;
   SystemClock_Config();
   g_start_send_flag=1;
   current_slotnum++;
   SetNextPollTime(tagslotpos);
   nomove_count+=(float)g_com_map[COM_INTERVAL]/1000;
    chongqi_num++;
    if(chongqi_num>20)
    {NVIC_SystemReset();}
//   current_slotnum++;
//   SetNextPollTime(tagslotpos);
    if(button_delay<1)
    button_delay+=1/(float)tag_frequency;
   nomove_count+=1/(float)tag_frequency;
//Up_tagdisterror_list_Poll();
//    memset(tagdist_list,0,taglist_num);
//    memset(tagid_list,0,taglist_num);
//    taglist_num=0;
    TagListUpdate();
     bubble_sort(tagdist_list,taglist_num);
   if(motor_keeptime>0)
   {   
   motor_keeptime-=(float)g_com_map[COM_INTERVAL]/1000;
        motor_keeptime-=1/(float)tag_frequency;
   }
    if(motor_keeptime<0)
    {motor_keeptime = 0;}
@@ -972,7 +978,7 @@
   if(!GET_USERKEY)
   {
      LED_LG_ON;
         key_keeptime+=(float)g_com_map[COM_INTERVAL]/1000;
         key_keeptime+=1/(float)tag_frequency;
      if(key_keeptime>3)
      {
         u8 i=250,keystate=1;
@@ -1004,7 +1010,7 @@
   if(!GET_USERKEY)
   {
      key_keeptime+=(float)g_com_map[COM_INTERVAL]/1000;
      key_keeptime+=1/(float)tag_frequency;
      if(key_keeptime>=KEY_KEEPRESET_TIME)
      {
         SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; 
@@ -1018,16 +1024,18 @@
   }else{
      stationary_flag = 0;
   }
if(nomove_count>=g_com_map[NOMOVESLEEP_TIME]-1&&imu_enable)
if(nomove_count>=nomovesleep_time-1&&imu_enable)
   {
      gotosleep_flag=1;
   }else{
      gotosleep_flag=0;
   }
#ifndef DEBUG_MODE
   if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&imu_enable)
   {
      mcu_sleep();
   }
#endif
}
/* USER CODE END 4 */