| | |
| | | #include "Usart.h" |
| | | #include "global_param.h" |
| | | #include "filters.h" |
| | | |
| | | #include <stdio.h> |
| | | #include "beep.h" |
| | | |
| | | |
| | | /*------------------------------------ Marcos ------------------------------------------*/ |
| | |
| | | #define TAG_ID_IDX 3 |
| | | #define MESSAGE_TYPE_IDX 5 |
| | | #define DIST_IDX 6 |
| | | #define ANC_TYPE_IDX 7 |
| | | |
| | | #define POLL 0x01 |
| | | #define RESPONSE 0x02 |
| | |
| | | /*------------------------------------ Variables ------------------------------------------*/ |
| | | /* Default communication configuration. We use here EVK1000's default mode (mode 3). */ |
| | | static dwt_config_t config = { |
| | | 5, /* Channel number. */ |
| | | 2, /* Channel number. */ |
| | | DWT_PRF_64M, /* Pulse repetition frequency. */ |
| | | DWT_PLEN_128, /* Preamble length. */ |
| | | DWT_PAC8, /* Preamble acquisition chunk size. Used in RX only. */ |
| | |
| | | float dis_after_filter; //å½åè·ç¦»å¼ |
| | | LPFilter_Frac* p_Dis_Filter; //æµè·ç¨çä½éæ»¤æ³¢å¨ |
| | | |
| | | uint16_t g_Tagdist[256]; |
| | | uint8_t g_flag_Taggetdist[256]; |
| | | /*------------------------------------ Functions ------------------------------------------*/ |
| | | |
| | | |
| | |
| | | *ts += ts_field[i] << (i * 8); |
| | | } |
| | | } |
| | | |
| | | void TagDistClear(void) |
| | | { |
| | | static uint16_t clear_judge_cnt; |
| | | uint16_t i; |
| | | if(clear_judge_cnt++>1000) //设å®1Såé¢ï¼æ¯ç§è¿ä¸æ¬¡ã夿æ å¿ä½å¤§äºçäº2ï¼2s没æ¶å°æ°æ®å°±ææ°æ®åæ0xffffï¼ä¸è§¦åè¦æ¥ã |
| | | { |
| | | clear_judge_cnt=0; |
| | | for(i=0;i<255;i++) |
| | | { |
| | | g_flag_Taggetdist[i]++; |
| | | if(g_flag_Taggetdist[i]>=2) |
| | | { |
| | | g_Tagdist[i]=0xffff; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | void Dw1000_Init(void) |
| | | { |
| | | /* Reset and initialise DW1000. |
| | |
| | | } |
| | | void Dw1000_App_Init(void) |
| | | { |
| | | //g_com_map[DEV_ID] = 0x0b; |
| | | tx_poll_msg[MESSAGE_TYPE_IDX]=POLL; |
| | | tx_resp_msg[MESSAGE_TYPE_IDX]=RESPONSE; |
| | | tx_final_msg[MESSAGE_TYPE_IDX]=FINAL; |
| | |
| | | dwt_configuresleep(0x940, 0x7); |
| | | dwt_entersleep(); |
| | | } |
| | | |
| | | uint16_t g_Resttimer; |
| | | uint8_t result; |
| | | u8 tag_succ_times=0; |
| | | void Tag_App(void)//å鿍¡å¼(TAGæ ç¾) |
| | | { |
| | | uint32_t frame_len; |
| | | uint32_t final_tx_time; |
| | | u32 start_poll; |
| | | u8 i; |
| | | |
| | | g_Resttimer=0; |
| | | UART_CheckReceive(); |
| | | GPIO_ResetBits(SPIx_GPIO, SPIx_CS); |
| | | delay_us(2000); |
| | | delay_us(2500); |
| | | GPIO_SetBits(SPIx_GPIO, SPIx_CS); |
| | | |
| | | tag_succ_times = 0; |
| | | for(i=0;i<REPOET_ANC_NUM;i++) |
| | | { |
| | | /* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */ |
| | | |
| | | tx_poll_msg[ALL_MSG_SN_IDX] = frame_seq_nb; |
| | | tx_poll_msg[ANC_TYPE_IDX] = i; |
| | | |
| | | dwt_writetxdata(sizeof(tx_poll_msg), tx_poll_msg, 0);//å°Pollå
æ°æ®ä¼ ç»DW1000ï¼å°å¨å¼å¯åéæ¶ä¼ åºå» |
| | | dwt_writetxfctrl(sizeof(tx_poll_msg), 0);//设置è¶
宽带åéæ°æ®é¿åº¦ |
| | | |
| | | /* Start transmission, indicating that a response is expected so that reception is enabled automatically after the frame is sent and the delay |
| | | * set by dwt_setrxaftertxdelay() has elapsed. */ |
| | | dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);//å¼å¯åéï¼åé宿åçå¾
䏿®µæ¶é´å¼å¯æ¥æ¶ï¼çå¾
æ¶é´å¨dwt_setrxaftertxdelayä¸è®¾ç½® |
| | | |
| | | start_poll = time32_incr; |
| | | /* We assume that the transmission is achieved correctly, poll for reception of a frame or error/timeout. See NOTE 8 below. */ |
| | | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))//䏿æ¥è¯¢è¯çç¶æç´å°æåæ¥æ¶æè
åçé误 |
| | | { }; |
| | | { if(time32_incr - start_poll>20) |
| | | NVIC_SystemReset(); |
| | | |
| | | }; |
| | | |
| | | /* Increment frame sequence number after transmission of the poll message (modulo 256). */ |
| | | frame_seq_nb++; |
| | | if(status_reg==0xffffffff) |
| | | { |
| | | NVIC_SystemReset(); |
| | | } |
| | | |
| | | if (status_reg & SYS_STATUS_RXFCG)//妿æåæ¥æ¶ |
| | | { |
| | |
| | | |
| | | /* Check that the frame is the expected response from the companion "DS TWR responder" example. |
| | | * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */ |
| | | |
| | | if (rx_buffer[MESSAGE_TYPE_IDX] == RESPONSE) //å¤ææ¥æ¶å°çæ°æ®æ¯å¦æ¯responseæ°æ® |
| | | rx_buffer[ALL_MSG_SN_IDX] = 0; |
| | | if (rx_buffer[MESSAGE_TYPE_IDX] == RESPONSE&&rx_buffer[TAG_ID_IDX]==g_com_map[DEV_ID]) //å¤ææ¥æ¶å°çæ°æ®æ¯å¦æ¯responseæ°æ® |
| | | { |
| | | /* Retrieve poll transmission and response reception timestamp. */ |
| | | poll_tx_ts = get_tx_timestamp_u64(); //è·å¾POLLåéæ¶é´T1 |
| | |
| | | final_msg_set_ts(&tx_final_msg[FINAL_MSG_FINAL_TX_TS_IDX], final_tx_ts); |
| | | |
| | | /* Write and send final message. See NOTE 7 below. */ |
| | | |
| | | tx_final_msg[ALL_MSG_SN_IDX] = frame_seq_nb; |
| | | dwt_writetxdata(sizeof(tx_final_msg), tx_final_msg, 0);//å°åéæ°æ®åå
¥DW1000 |
| | | dwt_writetxfctrl(sizeof(tx_final_msg), 0);//设å®åéæ°æ®é¿åº¦ |
| | | dwt_starttx(DWT_START_TX_DELAYED);//设å®ä¸ºå»¶è¿åé |
| | | result=dwt_starttx(DWT_START_TX_DELAYED);//设å®ä¸ºå»¶è¿åé |
| | | |
| | | tag_succ_times++; |
| | | |
| | | /* Poll DW1000 until TX frame sent event set. See NOTE 8 below. */ |
| | | while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))//䏿æ¥è¯¢è¯çç¶æç´å°åé宿 |
| | | if(result==0) |
| | | {while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))//䏿æ¥è¯¢è¯çç¶æç´å°åé宿 |
| | | { }; |
| | | |
| | | } |
| | | /* Clear TXFRS event. */ |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS);//æ¸
餿 å¿ä½ |
| | | |
| | | /* Increment frame sequence number after transmission of the final message (modulo 256). */ |
| | | frame_seq_nb++; |
| | | frame_seq_nb++; |
| | | random_delay_tim = 0; |
| | | } |
| | | else |
| | |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); |
| | | random_delay_tim = DFT_RAND_DLY_TIM_MS; |
| | | } |
| | | // deca_sleep(10); |
| | | } |
| | | if(tag_succ_times!=REPOET_ANC_NUM) |
| | | { |
| | | random_delay_tim =g_com_map[DEV_ID]*13+7; |
| | | }else{ |
| | | random_delay_tim=0; |
| | | } |
| | | LED0_BLINK; |
| | | deca_sleep(random_delay_tim); |
| | | RTC_SET_ALARM(1); |
| | | /* Execute a delay between ranging exchanges. */ |
| | | dwt_entersleep(); |
| | | |
| | | } |
| | | uint16_t Checksum_u16(uint8_t* pdata, uint32_t len) |
| | | { |
| | | uint16_t sum = 0; |
| | | uint32_t i; |
| | | for(i=0; i<len; i++) |
| | | sum += pdata[i]; |
| | | sum = ~sum; |
| | | return sum; |
| | | } |
| | | |
| | | u16 tag_time_recv[TOTAL_TAG_NUM]; |
| | | u8 usart_send[16]; |
| | | extern uint8_t g_pairstart; |
| | | void Anchor_App(void) |
| | | { |
| | | uint32_t frame_len; |
| | |
| | | { |
| | | UART_CheckReceive(); |
| | | UART_CheckSend(); |
| | | g_Resttimer=0; |
| | | }; |
| | | |
| | | if (status_reg & SYS_STATUS_RXFCG)//æåæ¥æ¶ |
| | | { |
| | | { u16 tag_recv_interval; |
| | | /* Clear good RX frame event in the DW1000 status register. */ |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG);//æ¸
餿 å¿ä½ |
| | | |
| | |
| | | |
| | | /* Check that the frame is a poll sent by "DS TWR initiator" example. |
| | | * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */ |
| | | |
| | | rx_buffer[ALL_MSG_SN_IDX] = 0; |
| | | |
| | | //å°æ¶å°çtag_idåå«åå
¥å次é讯çå
ä¸ï¼ä¸ºå¤æ ç¾é讯æå¡ï¼é²æ¢ä¸æ¬¡éè®¯ä¸æ¥æ¶å°ä¸åIDæ ç¾çæ°æ® |
| | | tag_id_recv = rx_buffer[TAG_ID_IDX]; |
| | | tx_resp_msg[TAG_ID_IDX] = tag_id_recv; |
| | | |
| | | if(tag_recv_timer>tag_time_recv[tag_id_recv]) |
| | | { tag_recv_interval = tag_recv_timer - tag_time_recv[tag_id_recv]; |
| | | }else{ |
| | | tag_recv_interval = tag_recv_timer + 65535 - tag_time_recv[tag_id_recv]; |
| | | } |
| | | |
| | | if (rx_buffer[MESSAGE_TYPE_IDX] == POLL) //夿æ¯å¦æ¯pollå
æ°æ® |
| | | if (rx_buffer[MESSAGE_TYPE_IDX] == POLL&&tag_id_recv!= g_com_map[PAIR_ID]&&(tag_recv_interval>g_com_map[COM_INTERVAL]/2)&&(g_com_map[DEV_ID]%REPOET_ANC_NUM == rx_buffer[ANC_TYPE_IDX])) //夿æ¯å¦æ¯pollå
æ°æ® |
| | | { |
| | | /* Retrieve poll reception timestamp. */ |
| | | poll_rx_ts = get_rx_timestamp_u64();//è·å¾Pollå
æ¥æ¶æ¶é´T2 |
| | |
| | | |
| | | /* Write and send the response message. See NOTE 9 below.*/ |
| | | memcpy(&tx_resp_msg[DIST_IDX], &anchor_dist_last_frm[tag_id_recv], 2); |
| | | |
| | | tx_resp_msg[ALL_MSG_SN_IDX] = frame_seq_nb; |
| | | dwt_writetxdata(sizeof(tx_resp_msg), tx_resp_msg, 0);//åå
¥åéæ°æ® |
| | | dwt_writetxfctrl(sizeof(tx_resp_msg), 0);//设å®åéé¿åº¦ |
| | | dwt_starttx(DWT_START_TX_DELAYED | DWT_RESPONSE_EXPECTED);//å»¶è¿åéï¼çå¾
æ¥æ¶ |
| | | result = dwt_starttx(DWT_START_TX_DELAYED | DWT_RESPONSE_EXPECTED);//å»¶è¿åéï¼çå¾
æ¥æ¶ |
| | | |
| | | /* We assume that the transmission is achieved correctly, now poll for reception of expected "final" frame or error/timeout. |
| | | * See NOTE 7 below. */ |
| | | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))///䏿æ¥è¯¢è¯çç¶æç´å°æ¥æ¶æåæè
åºç°é误 |
| | | if(result==0) |
| | | { |
| | | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))///䏿æ¥è¯¢è¯çç¶æç´å°æ¥æ¶æåæè
åºç°é误 |
| | | { }; |
| | | |
| | | } |
| | | /* Increment frame sequence number after transmission of the response message (modulo 256). */ |
| | | frame_seq_nb++; |
| | | |
| | |
| | | |
| | | /* Check that the frame is a final message sent by "DS TWR initiator" example. |
| | | * As the sequence number field of the frame is not used in this example, it can be zeroed to ease the validation of the frame. */ |
| | | rx_buffer[ALL_MSG_SN_IDX] = 0; |
| | | if (rx_buffer[MESSAGE_TYPE_IDX] == FINAL&&rx_buffer[TAG_ID_IDX]==tag_id_recv&&rx_buffer[ANCHOR_ID_IDX]==g_com_map[DEV_ID]) //夿æ¯å¦ä¸ºFinalå
|
| | | { |
| | | uint32_t poll_tx_ts, resp_rx_ts, final_tx_ts; |
| | | uint32_t poll_rx_ts_32, resp_tx_ts_32, final_rx_ts_32; |
| | | double Ra, Rb, Da, Db; |
| | | int64_t tof_dtu; |
| | | |
| | | u32 hex_dist; |
| | | u16 checksum; |
| | | /* Retrieve response transmission and final reception timestamps. */ |
| | | resp_tx_ts = get_tx_timestamp_u64();//è·å¾responseåéæ¶é´T3 |
| | | final_rx_ts = get_rx_timestamp_u64();//è·å¾finalæ¥æ¶æ¶é´T6 |
| | |
| | | LED0_BLINK; //æ¯æå䏿¬¡é讯åéªç䏿¬¡ |
| | | g_UWB_com_interval = 0; |
| | | dis_after_filter=dist_cm; |
| | | g_Tagdist[tag_id_recv]=dist_cm; |
| | | // if(g_pairstart==1&&dist_cm<20) |
| | | // { |
| | | // g_pairstart=0; |
| | | // g_com_map[PAIR_ID]=tag_id_recv; |
| | | // save_com_map_to_flash(); |
| | | // BEEP2_ON; |
| | | // delay_ms(1000); |
| | | // printf("Pair Finish PairID: %d. \r\n",g_com_map[PAIR_ID]); |
| | | // } |
| | | tag_time_recv[tag_id_recv] = tag_recv_timer; |
| | | g_flag_Taggetdist[tag_id_recv]=0; |
| | | #ifdef HEX_OUTPUT |
| | | usart_send[2] = frame_seq_nb; |
| | | usart_send[6] = tag_id_recv; |
| | | usart_send[8] = g_com_map[DEV_ID]; |
| | | hex_dist = dist_cm; |
| | | memcpy(&usart_send[10],&hex_dist,4); |
| | | checksum = Checksum_u16(&usart_send[2],12); |
| | | memcpy(&usart_send[14],&checksum,2); |
| | | UART_PushFrame(usart_send,16); |
| | | #else |
| | | printf("Anchor ID: %d, Tag ID: %d, Dist = %d cm\n", g_com_map[DEV_ID], tag_id_recv, (uint16_t)dis_after_filter); |
| | | #endif |
| | | //dis_after_filter = LP_Frac_Update(p_Dis_Filter, dist_cm); |
| | | |
| | | } |
| | | } |
| | | else |
| | | { |
| | | }else{ |
| | | /* Clear RX error events in the DW1000 status register. */ |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); |
| | | } |