chen
2024-09-03 11163a8d87777cd4d47f6bc477fd7b75eccdf8a1
keil/uwb_app.c
@@ -50,7 +50,7 @@
uint32_t temp_count2=0;
uint32_t temp_count3=0;
uint32_t temp_internal=0;
int temp_flag,distance;
int32_t distance;
uint8_t taglist_num;
extern uint8_t recev_error_num;
@@ -87,7 +87,7 @@
#define FINAL_MSG_RESP_RX_TS_IDX 14
#define FINAL_MSG_FINAL_TX_TS_IDX 18
#define DELAY_DEFAULT 1000
#define HALF_SECOND_TIME 62400000
#define HALF_SECOND_TIME 624000000
/* Length of the common part of the message */
#define MSG_COMMON_LEN 10
@@ -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.                           */
@@ -143,9 +143,9 @@
static uint32_t resp_tx_timeout;
int64_t temp_resp_i64;
/* 41 bits timestamps of frames transmission/reception. */
 int64_t poll_rx_ts_i64;
 int64_t resp_tx_ts_i64;
 int64_t final_rx_ts_i64;
int64_t poll_rx_ts_i64;
int64_t resp_tx_ts_i64;
int64_t final_rx_ts_i64;
/* Frame sequence number, incremented after each transmission. */
static uint8_t frame_seq_nb = 0;
@@ -196,6 +196,7 @@
}
/* RX done process handler. */
int8_t rssi;
static void rx_int_callback(struct MAC_HW_REPORT_T *rx_report)
{
    // Power off radio
@@ -213,6 +214,7 @@
        poll_rx_en_start_u32 = rx_rpt.timestamp - phy_shr_duration();
        poll_rx_ts_i64 = ranging_rx_time_correct(&rx_rpt);
        poll_rx_num++;
        rssi = rx_report->rssi;
        receive_flag=1;
        recev_error_num=0;
    }
@@ -224,9 +226,10 @@
        /* UWB_BD_ERR      Preamble detection error  */
        /* UWB_TO_ERR      Receive timeout           */
        /* UWB_STS_ERR     STS error                 */
                 temp_count= phy_timer_count_get();
        memcpy(&rx_rpt, rx_report, sizeof(struct MAC_HW_REPORT_T));
        rx_length = 0;
    }
}
@@ -252,6 +255,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;
@@ -272,7 +276,8 @@
    anchordata_dist[i] = dist;
    anchordata_bat[i] = battary;
    tagofflinetime[i]=0;//不断更新当前TAG对应离线时间
    distance=dist;
    if(dist>0)
    distance=dist*0.5+distance*0.5;
}
uint16_t CmpTagInList(uint16_t tagid)
{   uint16_t i;
@@ -337,11 +342,13 @@
    memcpy(&send_buffer[RESP_MSG_ANC_DISTOFFSET],&disoffset,2);//差个修正offset,修正有符号但是这个com表为无符号的,传过去直接赋给Int16_t相当于还原了
    temp_resp_i64=resp_tx_ts_i64;
    temp_count3= phy_timer_count_get();
    temp_flag=uwb_tx(send_buffer, 40,1 ,resp_tx_en_start_u32);//立即发送测试size大小
    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);
}
uint16_t uwb_searchcount;
extern uint8_t g_start_send_flag,search_open_flag = 1,link_success_flag;
int UwbRange(void)
{
    uint8_t i;
@@ -400,6 +407,7 @@
            {
                if(rec_ancidlist[i] == dev_id)
                {
                    rec_ancdistlist[i]+=(int16_t)g_com_map[DIST_OFFSET];
                    PushAnchorDataArray(tag_id_recv,rec_ancdistlist[i],battary);
                }
            }
@@ -418,19 +426,23 @@
            {
                seize_anchor=1;   //抢占anchor
                Anchor_RecNearPoll(i);
            }
        }
    }else{
        if(recev_error_num++>20)
            {
                recev_error_num=0;
                UWB_work_state=SEARCH_DEV;
            }
    }
    sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
            sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
            LOG_INFO(TRACE_MODULE_APP,"标签ID:%X,距离: %d 信号强度:%d.\r\n",g_com_map[BIND_DEV_ID],distance,rssi);
        }
    } else {
        sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
        LOG_INFO(TRACE_MODULE_APP,"测距失败,计数:%d\r\n",recev_error_num);
        if(recev_error_num++>5)
        {
            recev_error_num=0;
            UWB_work_state=SEARCH_DEV;
            uwb_searchcount = 0;
            search_open_flag = 1;
        }
    }
    gpio_pin_clr(SCL_PIN);
}
@@ -438,7 +450,7 @@
//主函数绑定接受逻辑
int UwbSearch(void)
{
    uint8_t i;
    uint16_t tempid;
    // The following peripherals will be initialized in the uwb_open function
@@ -475,18 +487,19 @@
    {
        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)
            {
                    break;
            }
        }
        temp_count2=phy_timer_count_get();
        if(receive_flag==1)//成功接收
        {
@@ -523,46 +536,48 @@
                    Anchor_RecNearPoll(i);
                }
                sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
                 gpio_pin_clr(SCL_PIN);//测试
            return 1;//返回发送成功标志
                gpio_pin_clr(SCL_PIN);//测试
                return 1;//返回发送成功标志
            }
        }
      uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//再次开启接收
            temp_count3=phy_timer_count_get();
        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);
    gpio_pin_clr(SCL_PIN);//测试
    return 0;//返回绑定失败标志
}
//}
#define SEARCH_TIMESTEMP 120
uint16_t uwb_searchcount;
extern uint8_t g_start_send_flag,search_open_flag = 1,link_success_flag;
#define SEARCH_TIMESTEMP 20
void UWBPoll(void)
{
    switch(UWB_work_state)
        {
    switch(UWB_work_state)
    {
    case LINK_SUCCESS:
    {   //连接成功进行轮询测距
            uwb_led_on();
            UwbRange();
            uwb_led_off();
        uwb_led_on();
        UwbRange();
        uwb_led_off();
    }
    break;
    case SEARCH_DEV:
    {   //接包不成功或者通讯失败进入搜索模式
        if(search_open_flag)
            {
            first_search_flag=0;
        if(search_open_flag)
        {
            LOG_INFO(TRACE_MODULE_APP,"开启搜索\r\n");
            search_open_flag=0;
            uwb_led_on();
            link_success_flag=UwbSearch();//第一次开启接收2s绑定失败后则进行下面1分钟后重新开启2s绑定流程
            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);
        }
    }
@@ -574,24 +589,25 @@
    break;
    }
    if(UWB_work_state==LINK_SUCCESS)
        { //成功时清0错误计数
        uwb_searchcount=0;
    {   //成功时清0错误计数
        uwb_searchcount=0;
    }
    if(UWB_work_state==SEARCH_DEV)
        {
    {
        if(!search_open_flag)
        {
        {
            if(uwb_searchcount++>SEARCH_TIMESTEMP)
                {
                    uwb_searchcount=0;
                    search_open_flag=1;
                }
            {
                uwb_searchcount=0;
                search_open_flag=1;
            }
        }
        //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();//更新灯状态
    }
     LOG_INFO(TRACE_MODULE_APP,"UWB状态:%d\r\n",UWB_work_state);
}
uint8_t GetUWBBindState(void)