chen
2024-09-12 7337e9b612f3d2709294a8f905a00539918f9674
keil/uwb_app.c
@@ -91,7 +91,7 @@
/* Length of the common part of the message */
#define MSG_COMMON_LEN 10
#define UWB_DELAY_TIME_US 496
#define UWB_DELAY_TIME_US 495
static uint8_t receive_flag=0;
struct mk_uwb_configure
{
@@ -102,7 +102,7 @@
/* Default communication configuration. */
static struct mk_uwb_configure config = {
    .phy_work_mode = (uint8_t)(PHY_TX | PHY_RX),
    .phy_cfg.ch_num = 9,                      /* Channel number.                           */
    .phy_cfg.ch_num = 5,                      /* Channel number.                           */
    .phy_cfg.code_index = 9,                  /* TX preamble code.                         */
    .phy_cfg.mean_prf = MEAN_PRF_64M,         /* Data rate 6.8M                            */
    .phy_cfg.data_bit_rate = DATA_BR_6M8,     /* data rate 6.8M.                           */
@@ -254,6 +254,7 @@
static int32_t tagdist_list[TAG_NUM_IN_SYS];
uint16_t random_time;
//anchor
int32_t hist_dist;
void PushAnchorDataArray(uint16_t ancid,int16_t dist,uint8_t battary)
{
    uint8_t i;
@@ -274,8 +275,8 @@
    anchordata_dist[i] = dist;
    anchordata_bat[i] = battary;
    tagofflinetime[i]=0;//不断更新当前TAG对应离线时间
    //if(dist>0)
    distance=dist;
    if(dist>0)
    distance=dist*0.5+distance*0.5;
}
uint16_t CmpTagInList(uint16_t tagid)
{   uint16_t i;
@@ -343,9 +344,11 @@
    uwb_tx(send_buffer, 40,1 ,resp_tx_en_start_u32);//立即发送测试size大小
    temp_count1=phy_timer_count_get();
    while(mac_is_busy());
    gpio_pin_clr(SCL_PIN);
//    gpio_pin_clr(SCL_PIN);
}
uint16_t uwb_searchcount;
uint16_t uwb_searchcount,uwb_timer_reload_value = 496;
uint8_t range_success;
uint32_t delay_time;
extern uint8_t g_start_send_flag,search_open_flag = 1,link_success_flag;
int UwbRange(void)
{
@@ -374,62 +377,87 @@
    // Register rx interrupt callback function
    mac_register_process_handler(tx_int_callback, rx_int_callback);
    range_success = 0;
    receive_flag = 0;
    temp_count3=phy_timer_count_get();
      #ifdef DEBUG_BOXING
    gpio_pin_set(SCL_PIN);
      #endif
    uwb_rx(0, 0, RX_SYNC_WIN_US);//开启接收
    while(mac_is_busy());
    temp_count2=phy_timer_count_get();
//      start_receive_count=phy_timer_count_get();
//   poll_timeout=rec_nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS)+US_TO_PHY_TIMER_COUNT(DELAY_DEFAULT);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss
//   end_receive_count=start_receive_count+poll_timeout;
//   if(end_receive_count>=UINT32_MAX)
//   {end_receive_count-=UINT32_MAX;}
//   current_count=phy_timer_count_get();
//      while(current_count<end_receive_count||current_count>end_receive_count+HALF_SECOND_TIME)//循环接受包体,若为124.8K则是+62400000
//      {
    if(receive_flag==1)//成功接收
    start_receive_count=phy_timer_count_get();
    poll_timeout=US_TO_PHY_TIMER_COUNT(5000);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss
    end_receive_count=start_receive_count+poll_timeout;
    if(end_receive_count>=UINT32_MAX)
    {
        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();
            frame_seq_nb2 = rx_buf[SEQUENCE_IDX];//获取包序
            battary = rx_buf[BATTARY_IDX];
            rec_nearbase_num=rx_buf[FZ_NEARBASENUM_INDEX];  //标签传过来的他与基站交互的基站id数目
            memcpy(&tag_id_recv,&rx_buf[TAG_ID_IDX],2);
            memcpy(rec_ancidlist,&rx_buf[FZ_NEARBASEID_INDEX],2*rec_nearbase_num);
            memcpy(rec_ancdistlist,&rx_buf[FZ_NEARBASEID_INDEX+2*rec_nearbase_num],2*rec_nearbase_num);
            memcpy(&rec_antdelay,&rx_buf[FZ_NEARBASEID_INDEX+rec_nearbase_num*4],2);
            for(i=0; i<rec_nearbase_num; i++)
        end_receive_count-=UINT32_MAX;
    }
    current_count=phy_timer_count_get();
    while(current_count<end_receive_count||current_count>end_receive_count+HALF_SECOND_TIME)//循环接受包体,若为124.8K则是+62400000
    {
        current_count=phy_timer_count_get();
        while(mac_is_busy())
        {
            current_count=phy_timer_count_get();
            if(current_count>end_receive_count&&current_count<end_receive_count+HALF_SECOND_TIME)
            {
                if(rec_ancidlist[i] == dev_id)
                {
                    rec_ancdistlist[i]+=(int16_t)g_com_map[DIST_OFFSET];
                    PushAnchorDataArray(tag_id_recv,rec_ancdistlist[i],battary);
                }
            }
            for(i=0; i<rec_nearbase_num; i++)
            {
                memcpy(&tempid,&rx_buf[FZ_NEARBASEID_INDEX+i*2],2);
                if(tempid==dev_id)//也是比id如果有就更新没有就抢占
                {
                    seize_anchor=0;  //非抢占。已存在列表中
                    Anchor_RecNearPoll(i);
                    break;
                }
            }
            if(i==rec_nearbase_num)
            {
                seize_anchor=1;   //抢占anchor
                Anchor_RecNearPoll(i);
            }
            LOG_INFO(TRACE_MODULE_APP,"标签ID:%X,距离: %d 信号强度:%d.\r\n",g_com_map[BIND_DEV_ID],distance,rssi);
        }
    } else {
        LOG_INFO(TRACE_MODULE_APP,"测距失败\r\n");
        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();
                frame_seq_nb2 = rx_buf[SEQUENCE_IDX];//获取包序
                battary = rx_buf[BATTARY_IDX];
                rec_nearbase_num=rx_buf[FZ_NEARBASENUM_INDEX];  //标签传过来的他与基站交互的基站id数目
                memcpy(&tag_id_recv,&rx_buf[TAG_ID_IDX],2);
                memcpy(rec_ancidlist,&rx_buf[FZ_NEARBASEID_INDEX],2*rec_nearbase_num);
                memcpy(rec_ancdistlist,&rx_buf[FZ_NEARBASEID_INDEX+2*rec_nearbase_num],2*rec_nearbase_num);
                memcpy(&rec_antdelay,&rx_buf[FZ_NEARBASEID_INDEX+rec_nearbase_num*4],2);
                for(i=0; i<rec_nearbase_num; i++)
                {
                    if(rec_ancidlist[i] == dev_id)
                    {
                        rec_ancdistlist[i]+=(int16_t)g_com_map[DIST_OFFSET];
                        PushAnchorDataArray(tag_id_recv,rec_ancdistlist[i],battary);
                    }
                }
                for(i=0; i<rec_nearbase_num; i++)
                {
                    memcpy(&tempid,&rx_buf[FZ_NEARBASEID_INDEX+i*2],2);
                    if(tempid==dev_id)//也是比id如果有就更新没有就抢占
                    {
                        seize_anchor=0;  //非抢占。已存在列表中
                        Anchor_RecNearPoll(i);
                        break;
                    }
                }
                if(i==rec_nearbase_num)
                {
                    seize_anchor=1;   //抢占anchor
                    Anchor_RecNearPoll(i);
                }
                range_success = 1;
                sleep_timer_start(__MS_TO_32K_CNT(uwb_timer_reload_value));//测试
                #ifdef DEBUG_BOXING
                 gpio_pin_clr(SCL_PIN);
                        #endif
                delay_time = (current_count-start_receive_count)>>7;
                LOG_INFO(TRACE_MODULE_APP,"标签ID:%X,距离: %d 信号强度:%d. 等待时间%d\r\n",g_com_map[BIND_DEV_ID],distance,rssi,delay_time);
                break;
            }
        } else {
                uwb_rx(0, 0, RX_SYNC_WIN_US);//再次开启接收
        }
    }
   if(!range_success)
   {
        sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US-4));//测试
        LOG_INFO(TRACE_MODULE_APP,"测距失败,计数:%d\r\n",recev_error_num);
        if(recev_error_num++>5)
        {
            recev_error_num=0;
@@ -437,9 +465,8 @@
            uwb_searchcount = 0;
            search_open_flag = 1;
        }
    }
    sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
    gpio_pin_clr(SCL_PIN);
   }
}
@@ -474,7 +501,9 @@
    mac_register_process_handler(tx_int_callback, rx_int_callback);
    receive_flag = 0;
    temp_count3=phy_timer_count_get();
      #ifdef DEBUG_BOXING
    gpio_pin_set(SCL_PIN);
      #endif
    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
@@ -483,11 +512,12 @@
    {
        end_receive_count-=UINT32_MAX;
    }
    current_count=phy_timer_count_get();
    while(current_count<end_receive_count||current_count>end_receive_count+HALF_SECOND_TIME)//循环接受包体,若为124.8K则是+62400000
    {
        while(mac_is_busy())
        {
            IdleTask();
            current_count=phy_timer_count_get();
            if(current_count>end_receive_count&&current_count<end_receive_count+HALF_SECOND_TIME)
            {
@@ -531,7 +561,9 @@
                    Anchor_RecNearPoll(i);
                }
                sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
                        #ifdef DEBUG_BOXING
                gpio_pin_clr(SCL_PIN);//测试
                        #endif
                return 1;//返回发送成功标志
            }
@@ -540,7 +572,10 @@
        uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//再次开启接收
    }
    // LOG_INFO(TRACE_MODULE_APP,"超时定时器:%x,%x,%x\r\n",start_receive_count,end_receive_count,current_count);
      #ifdef DEBUG_BOXING
    gpio_pin_clr(SCL_PIN);//测试
      #endif
    return 0;//返回绑定失败标志
}
//}
@@ -548,7 +583,6 @@
#define SEARCH_TIMESTEMP 120
void UWBPoll(void)
{
    LOG_INFO(TRACE_MODULE_APP,"UWB状态:%d\r\n",UWB_work_state);
    switch(UWB_work_state)
    {
    case LINK_SUCCESS:
@@ -571,6 +605,8 @@
            uwb_led_off();
            if(link_success_flag)
                UWB_work_state=LINK_SUCCESS;
        }else{
            LOG_INFO(TRACE_MODULE_APP,"等待进入搜索模式:%d\r\n",SEARCH_TIMESTEMP-uwb_searchcount);
        }
    }
@@ -600,6 +636,7 @@
        //link_error_count=0;
        update_led_power_state();//更新灯状态
    }
     LOG_INFO(TRACE_MODULE_APP,"UWB状态:%d\r\n",UWB_work_state);
}
uint8_t GetUWBBindState(void)