keil/uwb_app.c
@@ -8,11 +8,14 @@
#include "dw_app_anchor.h"
#include "global_param.h"
#include "board.h"
#include "HIDO_TypeDef.h"
extern int simple_main(void);
extern int temp_main(void);
extern void Air780E_4G_POLL(void);
void TagListUpdate(void);
void Calibration_Time(void);
extern void IO_LED_control_change(uint8_t data);
extern void IO_control_init(void);
@@ -35,7 +38,7 @@
extern uint16_t tag_frequency;
extern uint16_t disoffset;
static uint8_t frame_len,recpoll_len,current_syncid,new_tagid,seize_anchor,result,g_start_sync_flag;
extern uint8_t gps_power_state,motor_power_state,uwb_state,air780_power_state,gps_success_state,chaging_state,changed_state,air780_success_state;
extern uint8_t gps_power_state,motor_power_state,uwb_state,air780_power_state,gps_success_state,red_charge_state,green_charge_state,air780_success_state;
int poll_rx_num,resp_tx_num;
//resp函数
void PushAnchorDataArray(uint16_t ancid,int16_t dist,uint8_t battary);//找到自己的id信息对应位置更新自己的交互信息
@@ -79,7 +82,7 @@
/* RX window open in advance */
#define RX_WIN_IN_ADVANCE_US (150)
#define DELAY_BETWEEN_TWO_FRAME_UUS 400
#define DELAY_BETWEEN_TWO_FRAME_UUS 600 //yuan1400
/* Field index in frame */
#define MSG_SEQ_NUM_IDX 2
@@ -263,6 +266,8 @@
    }
}
uint32_t start_receive_count,end_receive_count,poll_timeout,current_count,temp_resp;
uint32_t start_receive_count_calibration;
uint32_t current_count_calibration;
static uint16_t anchordata_id[TAG_NUM_IN_SYS],anchordata_dist[TAG_NUM_IN_SYS];
static uint8_t anchordata_bat[TAG_NUM_IN_SYS];
uint8_t anchordata_num = 0;
@@ -270,6 +275,8 @@
uint16_t random_time;
//anchor
int32_t hist_dist;
int16_t temp_recdist_before_offset;
int16_t dist_temp;
void PushAnchorDataArray(uint16_t ancid,int16_t dist,uint8_t battary)
{
    uint8_t i;
@@ -290,8 +297,14 @@
    anchordata_dist[i] = dist;
    anchordata_bat[i] = battary;
    tagofflinetime[i]=0;//不断更新当前TAG对应离线时间
      dist_temp=dist;
//    if(dist>0)
      if(temp_recdist_before_offset!=0&&distance!=0x1ffff)
      {
    distance=dist*0.5+distance*0.5;
      }else{
         distance=dist;
      }
}
uint16_t CmpTagInList(uint16_t tagid)
{   uint16_t i;
@@ -344,11 +357,11 @@
        random_time = (phy_timer_count_get()%100);//多基站抢占一个位置时避免一个也收不到的情况就每一个随机错开几十uS
        //resp_tx_time = (poll_rx_ts+((random_time+rec_nearbase_num*20 + POLL_RX_TO_RESP_TX_DLY_UUS+ancrec_nearbasepos*DELAY_BETWEEN_TWO_FRAME_UUS) * UUS_TO_DWT_TIME)) >> 8;//计算Response发送时间T3。
        // (resp_tx_en_start_u32) is the moment when TX enable
        resp_tx_en_start_u32 = random_time+poll_rx_en_start_u32+US_TO_PHY_TIMER_COUNT(20*rec_nearbase_num)+US_TO_PHY_TIMER_COUNT(POLL_RX_TO_RESP_TX_DLY_US)+rec_nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS);//后面的需要根据已有基站数量进行更改,如果是抢占自己最后一个回复,要有底数
        resp_tx_en_start_u32 = poll_rx_en_start_u32+US_TO_PHY_TIMER_COUNT(20*rec_nearbase_num+POLL_RX_TO_RESP_TX_DLY_US+rec_nearbase_num*DELAY_BETWEEN_TWO_FRAME_UUS);//后面的需要根据已有基站数量进行更改,如果是抢占自己最后一个回复,要有底数
            
      } else {
        //resp_tx_time = (poll_rx_ts + ((rec_nearbase_num*20+POLL_RX_TO_RESP_TX_DLY_UUS+ancrec_nearbasepos*DELAY_BETWEEN_TWO_FRAME_UUS) * UUS_TO_DWT_TIME)) >> 8;//计算Response发送时间T3。
        resp_tx_en_start_u32 = poll_rx_en_start_u32+US_TO_PHY_TIMER_COUNT(20*rec_nearbase_num)+US_TO_PHY_TIMER_COUNT(POLL_RX_TO_RESP_TX_DLY_US)+rec_nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS);//后面的需要根据已有基站数量进行更改,自己非抢占
       resp_tx_en_start_u32 = poll_rx_en_start_u32+US_TO_PHY_TIMER_COUNT(20*rec_nearbase_num+POLL_RX_TO_RESP_TX_DLY_US+rec_nearbase_num*DELAY_BETWEEN_TWO_FRAME_UUS);//后面的需要根据已有基站数量进行更改,如果是抢占自己最后一个回复,要有底数
            //这里应该有问题问一下钟工
      }//此处设置绝对时间将poll u32改为phy_timer_count_get()
@@ -362,11 +375,18 @@
    uwb_tx(send_buffer, 40,1 ,resp_tx_en_start_u32);//立即发送测试size大小
    temp_count1=phy_timer_count_get();
    while(mac_is_busy());
      #ifdef BO_XING
    gpio_pin_clr(SCL_PIN);
      #endif
}
uint16_t uwb_searchcount,range_timeout_us = 5000;
uint32_t range_timeout_us = 5000;
uint16_t uwb_searchcount;
uint8_t flag_recsuccess;
extern uint8_t g_start_send_flag,search_open_flag = 1,link_success_flag;
uint8_t secondtask_search_count,secondtask_search_flag;
uint8_t flag_temp2,flag_temp1;
extern HIDO_UINT32 u32CurTick;
extern float nomove_count;
extern uint8_t g_start_send_flag,search_open_flag = 1,link_success_flag,flag_secondtask;
int UwbRange(void)
{
    uint8_t i;
@@ -398,9 +418,12 @@
    receive_flag = 0;
      flag_recsuccess = 0;
    temp_count3=phy_timer_count_get();
      #ifdef BO_XING
    gpio_pin_set(SCL_PIN);
    uwb_rx(0, 0, range_timeout_us);//开启接收
  //  while(mac_is_busy());
      #endif
    flag_temp1=uwb_rx(0, 0, range_timeout_us);//开启接收
   // while(mac_is_busy());
  //  temp_count2=phy_timer_count_get();
      start_receive_count=phy_timer_count_get();
   poll_timeout=US_TO_PHY_TIMER_COUNT(range_timeout_us);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss
@@ -412,19 +435,25 @@
      {
         current_count=phy_timer_count_get();
         while(mac_is_busy())
        {
        {
//                  MotorPoll();
            IdleTask();
                  UserKeyTask();
                  IMUTask();
            current_count=phy_timer_count_get();
            if(current_count>end_receive_count&&current_count<end_receive_count+HALF_SECOND_TIME)
            {
                    break;
            }
        }
    if(receive_flag==1)//成功接收
    {
        if(group_id==rx_buf[GROUP_ID_IDX]&&rx_buf[MESSAGE_TYPE_IDX] == MBX_POLL&&!memcmp(&rx_buf[TAG_ID_IDX],&g_com_map[BIND_DEV_ID],2))//判断是否是和自己是同一组通讯的且为poll包
        {   flag_recsuccess = 1;
        {    flag_recsuccess = 1;
                sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
                 temp_count2=phy_timer_count_get();
            frame_seq_nb2 = rx_buf[SEQUENCE_IDX];//获取包序
            battary = rx_buf[BATTARY_IDX];
@@ -436,7 +465,7 @@
            for(i=0; i<rec_nearbase_num; i++)
            {
                if(rec_ancidlist[i] == dev_id)
                {
                {      temp_recdist_before_offset=rec_ancdistlist[i];
                    rec_ancdistlist[i]+=(int16_t)g_com_map[DIST_OFFSET];
                    PushAnchorDataArray(tag_id_recv,rec_ancdistlist[i],battary);
                }
@@ -456,17 +485,21 @@
            {
                seize_anchor=1;   //抢占anchor
                Anchor_RecNearPoll(i);
            }
                  recev_error_num=0;
            sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
              range_timeout_us=5000;//恢复为5000进入range后
            LOG_INFO(TRACE_MODULE_APP,"标签ID:%X,距离: %d 信号强度:%d.\r\n",g_com_map[BIND_DEV_ID],distance,rssi);
                  #ifdef BO_XING
                  gpio_pin_clr(SCL_PIN);
                  #endif
        break;
            }
    } else {
         uwb_rx(0, 0, range_timeout_us);
    }
    }
      //失败或者接受被高发射机打断都会再次开启接收
         flag_temp1=uwb_rx(0, 0, range_timeout_us);
   }
      delay_us(1);
      uwb_rx_force_off(1);
      if(!flag_recsuccess)
      {
         sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US-3));//测试
@@ -479,9 +512,45 @@
            search_open_flag = 1;
        }
         }
    gpio_pin_clr(SCL_PIN);
         return 0;
}
void Calibration_Time(void)
{
   current_count_calibration=phy_timer_count_get();
   if((current_count_calibration-start_receive_count_calibration>=MS_TO_PHY_TIMER_COUNT(500)||current_count_calibration+(UINT32_MAX-start_receive_count_calibration)>=MS_TO_PHY_TIMER_COUNT(500))
      &&!((end_receive_count-current_count_calibration<=MS_TO_PHY_TIMER_COUNT(200))||(end_receive_count+(UINT32_MAX-current_count_calibration)<=MS_TO_PHY_TIMER_COUNT(200))))
   {
      start_receive_count_calibration=current_count_calibration;//更新开始时间
      MotorPoll();
      if(secondtask_search_count++%2==0)
    {
        secondtask_search_flag = 1;
    }else{
        secondtask_search_flag = 0;
    }
      if(secondtask_search_flag)//更新S时间TICK
      {
      HIDO_TimerTick();
      TagListUpdate();
    GPS_Poll();
      if(nomove_count<=g_com_map[NOMOVESLEEP_TIME])//防止溢出
    nomove_count++;
      else{
      nomove_count=g_com_map[NOMOVESLEEP_TIME]+1;
      }
      }
      update_led_power_state();//更新等状态防止震动卡死在搜索
   }
//   if(current_count_calibration-start_receive_count_calibration>=MS_TO_PHY_TIMER_COUNT(1000)||current_count_calibration+(UINT32_MAX-start_receive_count_calibration)>=MS_TO_PHY_TIMER_COUNT(1000))
//   {
//      HIDO_TimerTick();
//   }
}
//主函数绑定接受逻辑
int UwbSearch(void)
@@ -512,12 +581,16 @@
    // Register rx interrupt callback function
    mac_register_process_handler(tx_int_callback, rx_int_callback);
    receive_flag = 0;
    temp_count3=phy_timer_count_get();
      #ifdef BO_XING
    gpio_pin_set(SCL_PIN);
    uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//开启接收
      #endif
    flag_temp2=uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//开启接收
    start_receive_count=phy_timer_count_get();
    poll_timeout=US_TO_PHY_TIMER_COUNT(2000000);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss
      start_receive_count_calibration=start_receive_count;
    poll_timeout=US_TO_PHY_TIMER_COUNT(1000000);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss
    end_receive_count=start_receive_count+poll_timeout;
    if(end_receive_count>=UINT32_MAX)
    {
@@ -527,8 +600,10 @@
    while(current_count<end_receive_count||current_count>end_receive_count+HALF_SECOND_TIME)//循环接受包体,若为124.8K则是+62400000
    {
        while(mac_is_busy())
        {
        {      Calibration_Time();
            IdleTask();
                  UserKeyTask();
                  IMUTask();
            current_count=phy_timer_count_get();
            if(current_count>end_receive_count&&current_count<end_receive_count+HALF_SECOND_TIME)
            {
@@ -540,7 +615,10 @@
        if(receive_flag==1)//成功接收
        {
            if(group_id==rx_buf[GROUP_ID_IDX]&&rx_buf[MESSAGE_TYPE_IDX] == MBX_POLL&&!memcmp(&rx_buf[TAG_ID_IDX],&g_com_map[BIND_DEV_ID],2))//判断是否是和自己是同一组通讯的且为poll包
            {   temp_count2=phy_timer_count_get();
            {
                       sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US-13));//测试
                        range_timeout_us=20000;//将下次range开启后超时时间设为20ms之后再range里恢复
                        temp_count2=phy_timer_count_get();
                frame_seq_nb2 = rx_buf[SEQUENCE_IDX];//获取包序
                battary = rx_buf[BATTARY_IDX];
                rec_nearbase_num=rx_buf[FZ_NEARBASENUM_INDEX];  //标签传过来的他与基站交互的基站id数目
@@ -552,6 +630,8 @@
                {
                    if(rec_ancidlist[i] == dev_id)
                    {
                                    temp_recdist_before_offset=rec_ancdistlist[i];
                                    rec_ancdistlist[i]+=(int16_t)g_com_map[DIST_OFFSET];
                        PushAnchorDataArray(tag_id_recv,rec_ancdistlist[i],battary);
                    }
                }
@@ -571,18 +651,23 @@
                    seize_anchor=1;   //抢占anchor
                    Anchor_RecNearPoll(i);
                }
                sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
                #ifdef BO_XING
                gpio_pin_clr(SCL_PIN);//测试
                        #endif
                        LOG_INFO(TRACE_MODULE_APP,"标签ID:%X,距离: %d 信号强度:%d.\r\n",g_com_map[BIND_DEV_ID],distance,rssi);
                return 1;//返回发送成功标志
            }
        }
            temp_count3=phy_timer_count_get();
        uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//再次开启接收
        flag_temp2=uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//再次开启接收
    }
      delay_us(2);
      uwb_rx_force_off(1);
    // LOG_INFO(TRACE_MODULE_APP,"超时定时器:%x,%x,%x\r\n",start_receive_count,end_receive_count,current_count);
      #ifdef BO_XING
    gpio_pin_clr(SCL_PIN);//测试
      #endif
    return 0;//返回绑定失败标志
}
//}
@@ -609,6 +694,7 @@
            search_open_flag=0;
            uwb_led_on();
            link_success_flag=UwbSearch();//第一次开启接收2s绑定失败后则进行下面1分钟后重新开启2s绑定流程
                  Calibration_Time();
            uwb_led_off();
            if(link_success_flag)
                UWB_work_state=LINK_SUCCESS;
@@ -641,7 +727,7 @@
        //link_error_count+=g_com_map[COM_INTERVAL];
        //if(link_error_count>=g_com_map[4G_INTERNAL])
        //link_error_count=0;
        update_led_power_state();//更新灯状态
       // update_led_power_state();//更新灯状态
    }
     LOG_INFO(TRACE_MODULE_APP,"UWB状态:%d\r\n",UWB_work_state);
}