| | |
| | | #define FINAL_MSG_FINAL_TX_TS_IDX 18 |
| | | #define FINAL_MSG_TS_LEN 4 |
| | | |
| | | #define SYNC_SEQ_IDX 5 |
| | | |
| | | #define GROUP_ID_IDX 0 |
| | | #define ANCHOR_ID_IDX 1 |
| | | #define TAG_ID_IDX 5 |
| | | #define MESSAGE_TYPE_IDX 9 |
| | | #define DIST_IDX 10 |
| | | //Poll |
| | | #define ANC_TYPE_IDX 14 |
| | | #define BATTARY_IDX 15 |
| | | #define BUTTON_IDX 16 |
| | | #define SEQUENCE_IDX 17 |
| | | //respose |
| | | #define ANCTIMEMS 14 |
| | | #define ANCTIMEUS 16 |
| | | |
| | | #define POLL 0x01 |
| | | #define RESPONSE 0x02 |
| | | #define FINAL 0x03 |
| | | #define SYNC 0x04 |
| | | |
| | | /*------------------------------------ Variables ------------------------------------------*/ |
| | | /* Default communication configuration. We use here EVK1000's default mode (mode 3). */ |
| | |
| | | }; |
| | | |
| | | /* Frames used in the ranging process. See NOTE 2 below. */ |
| | | static uint8_t tx_poll_msg[19] = {0}; |
| | | static uint8_t tx_poll_msg[20] = {0}; |
| | | static uint8_t tx_sync_msg[14] = {0}; |
| | | //static uint8_t rx_resp_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'V', 'E', 'W', 'A', 0x10, 0x02, 0, 0, 0, 0}; |
| | | static uint8_t tx_final_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| | | |
| | | //static uint8_t rx_poll_msg[] = {0x00, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x21, 0, 0}; |
| | | static uint8_t tx_resp_msg[16] = {0}; |
| | | static uint8_t tx_resp_msg[20] = {0}; |
| | | //static uint8_t rx_final_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; |
| | | |
| | | /* Frame sequence number, incremented after each transmission. */ |
| | |
| | | |
| | | static double tof; |
| | | |
| | | uint32_t anchor_dist_last_frm[TAG_NUM_IN_SYS]; |
| | | uint32_t anchor_dist_last_frm[TAG_NUM_IN_SYS],his_dist[TAG_NUM_IN_SYS]; ; |
| | | uint32_t tag_id = 0; |
| | | uint32_t tag_id_recv = 0; |
| | | uint8_t random_delay_tim = 0; |
| | |
| | | float dis_after_filter; //å½åè·ç¦»å¼ |
| | | LPFilter_Frac* p_Dis_Filter; //æµè·ç¨çä½éæ»¤æ³¢å¨ |
| | | |
| | | uint16_t g_Tagdist[TOTAL_TAG_NUM]; |
| | | uint16_t g_Tagdist[TAG_NUM_IN_SYS]; |
| | | uint8_t g_flag_Taggetdist[256]; |
| | | /*------------------------------------ Functions ------------------------------------------*/ |
| | | |
| | |
| | | * For initialisation, DW1000 clocks must be temporarily set to crystal speed. After initialisation SPI rate can be increased for optimum |
| | | * performance. */ |
| | | Reset_DW1000();//éå¯DW1000 /* Target specific drive of RSTn line into DW1000 low for a period. */ |
| | | Spi_ChangePrescaler(SPI_BaudRatePrescaler_64); //è®¾ç½®ä¸ºå¿«éæ¨¡å¼ |
| | | dwt_initialise(DWT_LOADUCODE);//åå§åDW1000 |
| | | Spi_ChangePrescaler(SPIx_PRESCALER_FAST); //è®¾ç½®ä¸ºå¿«éæ¨¡å¼ |
| | | Spi_ChangePrescaler(SPI_BaudRatePrescaler_32); //è®¾ç½®ä¸ºå¿«éæ¨¡å¼ |
| | | |
| | | /* Configure DW1000. See NOTE 6 below. */ |
| | | dwt_configure(&config);//é
ç½®DW1000 |
| | |
| | | tx_poll_msg[MESSAGE_TYPE_IDX]=POLL; |
| | | tx_resp_msg[MESSAGE_TYPE_IDX]=RESPONSE; |
| | | tx_final_msg[MESSAGE_TYPE_IDX]=FINAL; |
| | | tx_sync_msg[MESSAGE_TYPE_IDX]=SYNC; |
| | | |
| | | memcpy(&tx_poll_msg[TAG_ID_IDX], &dev_id, 4); |
| | | memcpy(&tx_final_msg[TAG_ID_IDX], &dev_id, 4); |
| | | memcpy(&tx_resp_msg[ANCHOR_ID_IDX], &dev_id, 4); |
| | | |
| | | memcpy(&tx_sync_msg[ANCHOR_ID_IDX], &dev_id, 4); |
| | | } |
| | | uint16_t Checksum_u16(uint8_t* pdata, uint32_t len) |
| | | { |
| | |
| | | return sum; |
| | | } |
| | | |
| | | u16 tag_time_recv[TOTAL_TAG_NUM]; |
| | | u16 tag_time_recv[TAG_NUM_IN_SYS]; |
| | | u8 usart_send[25]; |
| | | u8 battary,button; |
| | | extern uint8_t g_pairstart; |
| | |
| | | dwt_configuresleep(0x940, 0x7); |
| | | dwt_entersleep(); |
| | | } |
| | | extern uint8_t g_start_send_flag; |
| | | u8 g_start_sync_flag; |
| | | void SyncPoll(u8 sync_seq) |
| | | { |
| | | g_start_sync_flag=1; |
| | | dwt_forcetrxoff(); |
| | | tx_sync_msg[SYNC_SEQ_IDX]=sync_seq; |
| | | dwt_writetxdata(sizeof(tx_sync_msg), tx_sync_msg, 0);//å°Pollå
æ°æ®ä¼ ç»DW1000ï¼å°å¨å¼å¯åéæ¶ä¼ åºå» |
| | | dwt_writetxfctrl(sizeof(tx_sync_msg), 0);//设置è¶
宽带åéæ°æ®é¿åº¦ |
| | | dwt_starttx(DWT_START_TX_IMMEDIATE); |
| | | } |
| | | uint16_t g_Resttimer; |
| | | uint8_t result; |
| | | u8 tag_succ_times=0; |
| | | u32 hex_dist; |
| | | int32_t hex_dist; |
| | | u16 checksum; |
| | | int8_t tag_delaytime; |
| | | extern uint16_t sync_timer; |
| | | u16 tmp_time; |
| | | void Tag_App(void)//å鿍¡å¼(TAGæ ç¾) |
| | | { |
| | | uint32_t frame_len; |
| | | uint32_t final_tx_time; |
| | | u32 start_poll; |
| | | u8 i; |
| | | u8 i,getsync_flag=0; |
| | | //LED0_ON; |
| | | dwt_forcetrxoff(); |
| | | g_Resttimer=0; |
| | |
| | | tag_succ_times = 0; |
| | | tx_poll_msg[BATTARY_IDX] = Get_Battary(); |
| | | tx_poll_msg[BUTTON_IDX] = !READ_KEY0; |
| | | |
| | | for(i=0;i<REPOET_ANC_NUM;i++) |
| | | tx_poll_msg[SEQUENCE_IDX] = frame_seq_nb++; |
| | | GPIO_WriteBit(GPIOA, GPIO_Pin_9, Bit_RESET); |
| | | for(i=0;i<g_com_map[MAX_REPORT_ANC_NUM];i++) |
| | | { |
| | | /* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */ |
| | | tx_poll_msg[ANC_TYPE_IDX] = i; |
| | |
| | | { if(time32_incr - start_poll>20) |
| | | NVIC_SystemReset(); |
| | | UART_CheckReceive(); |
| | | UART_CheckSend(); |
| | | |
| | | }; |
| | | |
| | | /* Increment frame sequence number after transmission of the poll message (modulo 256). */ |
| | |
| | | poll_tx_ts = get_tx_timestamp_u64(); //è·å¾POLLåéæ¶é´T1 |
| | | resp_rx_ts = get_rx_timestamp_u64(); //è·å¾RESPONSEæ¥æ¶æ¶é´T4 |
| | | |
| | | memcpy(&anchor_dist_last_frm[tag_id], &rx_buffer[DIST_IDX], 4); |
| | | if(getsync_flag==0) |
| | | { |
| | | getsync_flag=1; |
| | | memcpy(&sync_timer,&rx_buffer[ANCTIMEMS],2); |
| | | memcpy(&tmp_time,&rx_buffer[ANCTIMEUS],2); |
| | | tmp_time=tmp_time+450; |
| | | if(tmp_time>999) |
| | | { |
| | | tmp_time-=999; |
| | | sync_timer++; |
| | | if(sync_timer>=1010) |
| | | {sync_timer=0;} |
| | | } |
| | | TIM3->CNT=tmp_time; |
| | | } |
| | | memcpy(&anchor_dist_last_frm[0], &rx_buffer[DIST_IDX], 4); |
| | | memcpy(&tx_final_msg[ANCHOR_ID_IDX], &rx_buffer[ANCHOR_ID_IDX], 4); |
| | | /* Compute final message transmission time. See NOTE 9 below. */ |
| | | final_tx_time = (resp_rx_ts + (RESP_RX_TO_FINAL_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8;//计ç®finalå
åéæ¶é´ï¼T5=T4+Treply2 |
| | |
| | | result=dwt_starttx(DWT_START_TX_DELAYED);//设å®ä¸ºå»¶è¿åé |
| | | |
| | | tag_succ_times++; |
| | | #ifdef WORK_MODE_TAG |
| | | LED0_BLINK; |
| | | #endif |
| | | |
| | | LED0_BLINK; |
| | | |
| | | |
| | | usart_send[2] = 1;//æ£å¸¸æ¨¡å¼ |
| | | usart_send[3] = 17;//æ°æ®æ®µé¿åº¦ |
| | | usart_send[4] = frame_seq_nb;//æ°æ®æ®µé¿åº¦ |
| | | memcpy(&usart_send[5],&dev_id,2); |
| | | memcpy(&usart_send[7],&rx_buffer[ANCHOR_ID_IDX],2); |
| | | hex_dist = anchor_dist_last_frm[0]+(int16_t)g_com_map[DIST_OFFSET]; |
| | | memcpy(&usart_send[9],&hex_dist,4); |
| | | usart_send[13] = battary; |
| | | usart_send[14] = button; |
| | | checksum = Checksum_u16(&usart_send[2],17); |
| | | memcpy(&usart_send[19],&checksum,2); |
| | | UART_PushFrame(usart_send,21); |
| | | |
| | | /* Poll DW1000 until TX frame sent event set. See NOTE 8 below. */ |
| | | if(result==0) |
| | | {while (!(dwt_read32bitreg(SYS_STATUS_ID) & SYS_STATUS_TXFRS))//䏿æ¥è¯¢è¯çç¶æç´å°åé宿 |
| | |
| | | // deca_sleep(10); |
| | | } |
| | | // dwt_entersleep(); |
| | | if(tag_succ_times<REPOET_ANC_NUM_MIN) |
| | | if(tag_succ_times<g_com_map[MIN_REPORT_ANC_NUM]) |
| | | { |
| | | random_delay_tim =time32_incr&0x8f+7; |
| | | deca_sleep(random_delay_tim); |
| | | //poll_timer +=time32_incr&0x7+3; |
| | | } |
| | | //LED0_BLINK; |
| | | RTC_SET_ALARM(1); |
| | | |
| | | /* Execute a delay between ranging exchanges. */ |
| | | |
| | | } |
| | | extern uint8_t g_start_send_flag; |
| | | int8_t correction_time; |
| | | extern uint8_t sync_seq; |
| | | |
| | | void Anchor_App(void) |
| | | { |
| | | uint32_t frame_len; |
| | |
| | | dwt_rxenable(0);//æå¼æ¥æ¶ |
| | | |
| | | /* Poll for reception of a frame or error/timeout. See NOTE 7 below. */ |
| | | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))&&!g_start_send_flag)//䏿æ¥è¯¢è¯çç¶æç´å°æ¥æ¶æåæè
åºç°é误 |
| | | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))&&!g_start_send_flag&&!g_start_sync_flag)//䏿æ¥è¯¢è¯çç¶æç´å°æ¥æ¶æåæè
åºç°é误 |
| | | { |
| | | UART_CheckReceive(); |
| | | UART_CheckSend(); |
| | | IdleTask(); |
| | | g_Resttimer=0; |
| | | }; |
| | | |
| | |
| | | |
| | | if (rx_buffer[MESSAGE_TYPE_IDX] == POLL&&(anchor_type == rx_buffer[ANC_TYPE_IDX])) //夿æ¯å¦æ¯pollå
æ°æ® |
| | | { |
| | | tmp_time=TIM3->CNT; |
| | | memcpy(&tx_resp_msg[ANCTIMEMS],&sync_timer,2); |
| | | memcpy(&tx_resp_msg[ANCTIMEUS],&tmp_time,2); |
| | | |
| | | // if(correction_time>10) |
| | | // {correction_time++;} |
| | | |
| | | /* Retrieve poll reception timestamp. */ |
| | | poll_rx_ts = get_rx_timestamp_u64();//è·å¾Pollå
æ¥æ¶æ¶é´T2 |
| | | |
| | |
| | | dwt_setrxtimeout(FINAL_RX_TIMEOUT_UUS);//æ¥æ¶è¶
æ¶æ¶é´ |
| | | |
| | | /* Write and send the response message. See NOTE 9 below.*/ |
| | | if(tag_id_recv-TAG_ID_START<=TOTAL_TAG_NUM) |
| | | if(tag_id_recv-TAG_ID_START<=TAG_NUM_IN_SYS) |
| | | memcpy(&tx_resp_msg[DIST_IDX], &anchor_dist_last_frm[tag_id_recv-TAG_ID_START], 4); |
| | | |
| | | dwt_writetxdata(sizeof(tx_resp_msg), tx_resp_msg, 0);//åå
¥åéæ°æ® |
| | |
| | | |
| | | battary = rx_buffer[BATTARY_IDX]; |
| | | button = rx_buffer[BUTTON_IDX]; |
| | | frame_seq_nb = rx_buffer[SEQUENCE_IDX]; |
| | | /* We assume that the transmission is achieved correctly, now poll for reception of expected "final" frame or error/timeout. |
| | | * See NOTE 7 below. */ |
| | | if(result==0) |
| | |
| | | LED0_BLINK; //æ¯æå䏿¬¡é讯åéªç䏿¬¡ |
| | | g_UWB_com_interval = 0; |
| | | dis_after_filter=dist_cm; |
| | | // g_Tagdist[tag_id_recv-TAG_ID_START]=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; |
| | | if(tag_id_recv-TAG_ID_START<=TOTAL_TAG_NUM) |
| | | { |
| | | g_flag_Taggetdist[tag_id_recv-TAG_ID_START]=0; |
| | | anchor_dist_last_frm[tag_id_recv-TAG_ID_START] = dist_cm; |
| | | } |
| | | #ifdef HEX_OUTPUT |
| | | usart_send[2] = frame_seq_nb++; |
| | | //usart_send[6] = tag_id_recv; |
| | | //usart_send[8] = g_com_map[DEV_ID]; |
| | | memcpy(&usart_send[3],&tag_id_recv,4); |
| | | memcpy(&usart_send[7],&dev_id,4); |
| | | hex_dist = dist_cm; |
| | | memcpy(&usart_send[11],&hex_dist,4); |
| | | usart_send[15] = battary; |
| | | usart_send[16] = button; |
| | | checksum = Checksum_u16(&usart_send[2],19); |
| | | memcpy(&usart_send[21],&checksum,2); |
| | | UART_PushFrame(usart_send,23); |
| | | #else |
| | | printf("Anchor ID: %d, Tag ID: %d, Dist = %d cm\n", g_com_map[DEV_ID_L]|g_com_map[DEV_ID_L]<<8, tag_id_recv, (uint16_t)dist_cm); |
| | | #endif |
| | | hex_dist = (int16_t)(dist_cm+g_com_map[DIST_OFFSET]); |
| | | if(tag_id_recv-TAG_ID_START<=TAG_NUM_IN_SYS) |
| | | if(abs(hex_dist-his_dist[tag_id_recv-TAG_ID_START])<1000) |
| | | { |
| | | g_Tagdist[tag_id_recv-TAG_ID_START] = hex_dist; |
| | | anchor_dist_last_frm[tag_id_recv-TAG_ID_START] = hex_dist; |
| | | } |
| | | his_dist[tag_id_recv-TAG_ID_START]=hex_dist; |
| | | |
| | | usart_send[2] = 1;//æ£å¸¸æ¨¡å¼ |
| | | usart_send[3] = 17;//æ°æ®æ®µé¿åº¦ |
| | | usart_send[4] = frame_seq_nb;//æ°æ®æ®µé¿åº¦ |
| | | memcpy(&usart_send[5],&tag_id_recv,2); |
| | | memcpy(&usart_send[7],&dev_id,2); |
| | | |
| | | memcpy(&usart_send[9],&anchor_dist_last_frm[tag_id_recv-TAG_ID_START],4); |
| | | usart_send[13] = battary; |
| | | usart_send[14] = button; |
| | | checksum = Checksum_u16(&usart_send[2],17); |
| | | memcpy(&usart_send[19],&checksum,2); |
| | | UART_PushFrame(usart_send,21); |
| | | |
| | | |
| | | //dis_after_filter = LP_Frac_Update(p_Dis_Filter, dist_cm); |
| | | |
| | |
| | | /* Clear RX error events in the DW1000 status register. */ |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); |
| | | } |
| | | }else if(rx_buffer[MESSAGE_TYPE_IDX] == SYNC) |
| | | { |
| | | if(rx_buffer[SYNC_SEQ_IDX]<sync_seq) |
| | | // if(rx_buffer[SYNC_SEQ_IDX]==2) |
| | | { |
| | | sync_seq=rx_buffer[SYNC_SEQ_IDX]+1; |
| | | TIM3->CNT = sync_seq*325%1000+15; |
| | | sync_timer = sync_seq*325/1000; |
| | | SyncPoll(sync_seq); |
| | | } |
| | | } |
| | | } |
| | | else |