| | |
| | | NEARPOLL, |
| | | SINGLEPOLL, |
| | | }tag_state=STARTPOLL; |
| | | static dwt_config_t config = { |
| | | 2, /* Channel number. */ |
| | | dwt_config_t config = { |
| | | 5, /* Channel number. */ |
| | | DWT_PRF_64M, /* Pulse repetition frequency. */ |
| | | DWT_PLEN_128, /* Preamble length. */ |
| | | DWT_PAC8, /* Preamble acquisition chunk size. Used in RX only. */ |
| | |
| | | LPFilter_Frac* p_Dis_Filter; //测距用的低通滤波器 |
| | | |
| | | |
| | | |
| | | void GetNearMsg(void); |
| | | static uint64_t get_tx_timestamp_u64(void) |
| | | { |
| | | uint8_t ts_tab[5]; |
| | |
| | | *ts += ts_field[i] << (i * 8); |
| | | } |
| | | } |
| | | |
| | | extern u32 id,error_times; |
| | | void Dw1000_Init(void) |
| | | { |
| | | /* Reset and initialise DW1000. |
| | |
| | | Reset_DW1000();//重启DW1000 /* Target specific drive of RSTn line into DW1000 low for a period. */ |
| | | Spi_ChangePrescaler(SPIx_PRESCALER_SLOW); //设置为快速模式 |
| | | dwt_initialise(DWT_LOADUCODE);//初始化DW1000 |
| | | id = dwt_readdevid() ; |
| | | Spi_ChangePrescaler(SPIx_PRESCALER_FAST); //设置为快速模式 |
| | | |
| | | id = dwt_readdevid() ; |
| | | /* Configure DW1000. See NOTE 6 below. */ |
| | | dwt_configure(&config);//配置DW1000 |
| | | |
| | | |
| | | id = dwt_readdevid() ; |
| | | |
| | | /* Apply default antenna delay value. See NOTE 1 below. */ |
| | | dwt_setrxantennadelay(RX_ANT_DLY); //设置接收天线延迟 |
| | | dwt_settxantennadelay(TX_ANT_DLY); //设置发射天线延迟 |
| | | |
| | | id = dwt_readdevid() ; |
| | | /* Set expected response's delay and timeout. See NOTE 4 and 5 below. |
| | | * As this example only handles one incoming frame with always the same delay and timeout, those values can be set here once for all. */ |
| | | //设置接收超时时间 |
| | | } |
| | | uint8_t uwb_onflag=1; |
| | | void UWB_RXON() |
| | | { |
| | | if(!g_com_map[MODBUS_MODE]) |
| | | { |
| | | Dw1000_Init(); |
| | | uwb_onflag=1; |
| | | g_com_map[UWB_ONFLAG]=uwb_onflag; |
| | | save_com_map_to_flash(); |
| | | } |
| | | } |
| | | void UWB_RXOFF() |
| | | { |
| | | if(!g_com_map[MODBUS_MODE]) |
| | | { |
| | | 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), 0); |
| | | dwt_forcetrxoff(); |
| | | dwt_entersleep(); |
| | | uwb_onflag=0; |
| | | g_com_map[UWB_ONFLAG]=uwb_onflag; |
| | | save_com_map_to_flash(); |
| | | } |
| | | } |
| | | void Dw1000_App_Init(void) |
| | | { |
| | |
| | | |
| | | tag_succ_times++; |
| | | |
| | | LED0_BLINK; |
| | | // LED0_BLINK; |
| | | |
| | | memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2); |
| | | // if(hex_dist2!=0xffff) |
| | |
| | | if(minddist!=0x1ffff&&minddist!=0) |
| | | { |
| | | trygetnearmsg_times = 0; |
| | | tag_state = GETNEARMSG; |
| | | tag_state = GETNEARMSG; |
| | | mainbase_id = mindist_ancid; |
| | | GetNearMsg(); |
| | | } |
| | | |
| | | if(getsync_flag==0) |
| | |
| | | //tyncpoll_time=tagslotpos*slottime; |
| | | //tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime; |
| | | tag_state=NEARPOLL; |
| | | outrange_times=0; |
| | | } |
| | | }else{ |
| | | // tyncpoll_time = (tagslotpos--%max_slotpos)*slottime; |
| | |
| | | extern int32_t dwt_readcarrierintegrator(void) ; |
| | | void CalculateDists(void) |
| | | { |
| | | for(int i=0;i<11;i++) |
| | | for(int i=0; i<11; i++) |
| | | { |
| | | rec_anc_signalpower[i] = exsistbase_list[i]; |
| | | if(exsistbase_list[i]>0) |
| | | { |
| | | // rec_anc_signalpower[i] = exsistbase_list[i]; |
| | | if(exsistbase_list[i]==KEEP_TIMES) |
| | | { |
| | | |
| | | exsistbase_list[i]--; |
| | | clockOffsetRatio = anc_clockoffset[i] * (FREQ_OFFSET_MULTIPLIER * HERTZ_TO_PPM_MULTIPLIER_CHAN_2 / 1.0e6) ; |
| | | rtd_init = tag_resprx[i] - poll_tx_ts; |
| | | rtd_resp = anc_resptx[i] - anc_pollrx[i]; |
| | | tof = ((rtd_init - rtd_resp * (1 - clockOffsetRatio)) / 2.0) * DWT_TIME_UNITS; |
| | | distance = tof * SPEED_OF_LIGHT; |
| | | if(distance>-1000&&distance<100000) |
| | | nearbase_distlist[i] = distance*100+anc_distoffset[i]; |
| | | }else{ |
| | | nearbase_distlist[i] = 0x1ffff; |
| | | } |
| | | |
| | | exsistbase_list[i]--; |
| | | #ifdef _UWB_4G |
| | | clockOffsetRatio = anc_clockoffset[i] * (FREQ_OFFSET_MULTIPLIER * HERTZ_TO_PPM_MULTIPLIER_CHAN_2 / 1.0e6) ; |
| | | #else |
| | | clockOffsetRatio = anc_clockoffset[i] * (FREQ_OFFSET_MULTIPLIER * HERTZ_TO_PPM_MULTIPLIER_CHAN_5 / 1.0e6) ; |
| | | #endif |
| | | rtd_init = tag_resprx[i] - poll_tx_ts&0xffffffff; |
| | | rtd_resp = anc_resptx[i] - anc_pollrx[i]; |
| | | tof = ((rtd_init - rtd_resp * (1 - clockOffsetRatio)) / 2.0) * DWT_TIME_UNITS; |
| | | distance = tof * SPEED_OF_LIGHT; |
| | | if(distance>-10&&distance<1000) |
| | | { |
| | | nearbase_distlist[i] = distance*100+anc_distoffset[i]; |
| | | } else { |
| | | nearbase_distlist[i] = 0x1ffff; |
| | | } |
| | | } else { |
| | | nearbase_distlist[i] = 0x1ffff; |
| | | } |
| | | |
| | | } |
| | | } |
| | | void NearPoll(void) |
| | |
| | | } |
| | | nearbase_distlist[rec_nearbasepos+1]=temp_dist; // nearbase_distlist[1]对应 rec_nearbaseid[0]的距离 |
| | | final_msg_set_ts(&tx_near_msg[FINAL_MSG_RESP_RX_NEARBASE_IDX+(rec_nearbasepos)*4], resp_rx_ts); |
| | | if(temp_dist<salvebase_mindist&&nearbase_switchdistlist[rec_nearbasepos]==1) |
| | | if(temp_dist<salvebase_mindist&&nearbase_switchdistlist[rec_nearbasepos]!=0&&(nearbase_switchdistlist[rec_nearbasepos]==1||temp_dist<nearbase_switchdistlist[rec_nearbasepos])) |
| | | { |
| | | salvebase_mindist = temp_dist; |
| | | mindist_slavebaseid = rec_nearbaseid; |
| | | } |
| | | #ifdef SWITCHBASE_ZHUANDIAN |
| | | if(temp_dist<nearbase_switchdistlist[rec_nearbasepos]&&nearbase_switchdistlist[rec_nearbasepos]>1&&temp_dist>0) |
| | | { |
| | | mainbase_id = rec_nearbaseid; |
| | | tag_state = GETNEARMSG; |
| | | trygetnearmsg_times = 0; |
| | | } |
| | | // if(temp_dist<nearbase_switchdistlist[rec_nearbasepos]&&nearbase_switchdistlist[rec_nearbasepos]>1&&temp_dist>0) |
| | | // { |
| | | // mainbase_id = rec_nearbaseid; |
| | | // tag_state = GETNEARMSG; |
| | | // trygetnearmsg_times = 0; |
| | | // } |
| | | #endif |
| | | |
| | | } |
| | |
| | | if(salvebase_mindist<mainbase_dist- THRESHOLD_CHANGE_MAINBASE_DIST ) |
| | | { |
| | | changemainbase_count++; |
| | | if(changemainbase_count>tag_frequency*2) |
| | | if(changemainbase_count>2) |
| | | { |
| | | mainbase_id = mindist_slavebaseid; |
| | | tag_state = GETNEARMSG; |
| | | trygetnearmsg_times = 0; |
| | | changemainbase_count = 0; |
| | | mainbase_id = mindist_slavebaseid; |
| | | tag_state = GETNEARMSG; |
| | | trygetnearmsg_times = 0; |
| | | GetNearMsg(); |
| | | } |
| | | }else{ |
| | | changemainbase_count = 0; |
| | | } |
| | | #endif |
| | | }else if(mainbase_lost_count>5*tag_frequency) |
| | | }else if(mainbase_lost_count>10) |
| | | { |
| | | mainbase_lost_count = 0; |
| | | tag_state = DISCPOLL; |
| | | DiscPoll(); |
| | | } |
| | | if(mainbase_lost_count!=0) |
| | | { |
| | | if(mainbase_lost_count<=tag_frequency*1) |
| | | if(mainbase_lost_count<=5) |
| | | {NextSlotDelayMs(0); |
| | | }else{ |
| | | NextSlotDelayMs(0); |
| | |
| | | if(nearbaseid_list[i]!=nearbaseid_list2[i]) |
| | | { |
| | | tag_state = GETNEARMSG; |
| | | GetNearMsg(); |
| | | trygetnearmsg_times = 0; |
| | | nearbaseid_list0[i]=1; |
| | | } |
| | |
| | | IdleTask(); |
| | | |
| | | }; |
| | | if(status_reg==0xffffffff) |
| | | { |
| | | NVIC_SystemReset(); |
| | | } |
| | | // if(status_reg==0xffffffff) |
| | | // { |
| | | // NVIC_SystemReset(); |
| | | // } |
| | | if (status_reg & SYS_STATUS_RXFCG)//如果成功接收 |
| | | { |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);//清楚寄存器标志位 |
| | |
| | | } |
| | | } |
| | | u32 id,error_times=0; |
| | | |
| | | extern float Height; |
| | | extern IWDG_HandleTypeDef hiwdg; |
| | | void Tag_App(void)//发送模式(TAG标签) |
| | | { |
| | | |
| | | //LED0_ON; |
| | | SPIx_CS_GPIO->BRR = SPIx_CS; |
| | | delay_us(700); |
| | | SPIx_CS_GPIO->BSRR = SPIx_CS; |
| | | id = dwt_readdevid() ; |
| | | while (DWT_DEVICE_ID != id) |
| | | { |
| | | // Dw1000_Init(); |
| | | id = dwt_readdevid() ; |
| | | IdleTask(); |
| | | if(error_times++>20) |
| | | { |
| | | printf("DW ID ERROR.\r\n"); |
| | | SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | } |
| | | } |
| | | // //LED0_ON; |
| | | // SPIx_CS_GPIO->BRR = SPIx_CS; |
| | | // delay_us(700); |
| | | // SPIx_CS_GPIO->BSRR = SPIx_CS; |
| | | // id = dwt_readdevid() ; |
| | | // while (DWT_DEVICE_ID != id) |
| | | // { |
| | | // // Dw1000_Init(); |
| | | // id = dwt_readdevid() ; |
| | | // IdleTask(); |
| | | // if(error_times++>20) |
| | | // { |
| | | // printf("DW ID ERROR.\r\n"); |
| | | // SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader |
| | | // } |
| | | // } |
| | | // Dw1000_Init(); |
| | | dwt_forcetrxoff(); |
| | | 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), 0); |
| | | error_times = 0; |
| | | HAL_IWDG_Refresh(&hiwdg); |
| | | switch(tag_state) |
| | | { |
| | | case REGPOLL: |
| | | LED_LG_ON; |
| | | // LED_LG_ON; |
| | | Registor_Poll(); |
| | | LED_LG_OFF; |
| | | // LED_LG_OFF; |
| | | break; |
| | | case DISCPOLL: |
| | | LED_LG_ON; |
| | | // LED_LG_ON; |
| | | DiscPoll(); |
| | | LED_LG_OFF; |
| | | // LED_LG_OFF; |
| | | break; |
| | | case GETNEARMSG: |
| | | trygetnearmsg_times++; |
| | | LED_LG_ON; |
| | | // LED_LG_ON; |
| | | GetNearMsg(); |
| | | LED_LG_OFF; |
| | | // LED_LG_OFF; |
| | | if(trygetnearmsg_times>5) |
| | | { |
| | | tag_state = STARTPOLL; |
| | |
| | | break; |
| | | } |
| | | userkey_state = !GET_USERKEY; |
| | | dwt_entersleep(); |
| | | dwt_forcetrxoff(); |
| | | 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); |
| | | } |