From aaf7e4ef5c1aa0535940531f20e4846cbcd39b69 Mon Sep 17 00:00:00 2001 From: zhyinch <zhyinch@gmail.com> Date: 星期一, 04 十一月 2019 19:48:15 +0800 Subject: [PATCH] Merge branch '基站测距' into ALPHAV1 --- 源码/核心板/Src/application/dw_app.c | 96 ++++++++++++++++++++++++++++++++++-------------- 1 files changed, 68 insertions(+), 28 deletions(-) diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c" index 643eb8b..6825d03 100644 --- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c" +++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c" @@ -64,6 +64,8 @@ #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 @@ -73,13 +75,15 @@ #define ANC_TYPE_IDX 14 #define BATTARY_IDX 15 #define BUTTON_IDX 16 - +#define SEQUENCE_IDX 17 //respose -#define TIMECORRE 14 +#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). */ @@ -97,12 +101,13 @@ }; /* 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[18] = {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. */ @@ -246,6 +251,7 @@ * 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(SPIx_PRESCALER_SLOW); //设置为快速模式 dwt_initialise(DWT_LOADUCODE);//初始化DW1000 Spi_ChangePrescaler(SPIx_PRESCALER_FAST); //设置为快速模式 @@ -268,10 +274,12 @@ 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) { @@ -292,18 +300,31 @@ 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; @@ -312,7 +333,8 @@ tag_succ_times = 0; tx_poll_msg[BATTARY_IDX] = Get_Battary(); tx_poll_msg[BUTTON_IDX] = !READ_KEY0; - + 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. */ @@ -359,15 +381,21 @@ poll_tx_ts = get_tx_timestamp_u64(); //获得POLL发送时间T1 resp_rx_ts = get_rx_timestamp_u64(); //获得RESPONSE接收时间T4 - if(i==0) - { - tag_delaytime=rx_buffer[TIMECORRE]; - if(tag_delaytime>g_com_map[COM_INTERVAL]/2) - { - tag_delaytime=tag_delaytime-g_com_map[COM_INTERVAL]; - } - } - + 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. */ @@ -395,10 +423,10 @@ usart_send[2] = 1;//正常模式 usart_send[3] = 17;//数据段长度 - usart_send[4] = frame_seq_nb++;//数据段长度 + 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];; + 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; @@ -441,7 +469,8 @@ } int8_t correction_time; -extern uint8_t g_start_send_flag; +extern uint8_t sync_seq; + void Anchor_App(void) { uint32_t frame_len; @@ -454,7 +483,7 @@ 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)//不断查询芯片状态直到接收成功或者出现错误 { IdleTask(); g_Resttimer=0; @@ -488,10 +517,10 @@ if (rx_buffer[MESSAGE_TYPE_IDX] == POLL&&(anchor_type == rx_buffer[ANC_TYPE_IDX])) //判断是否是poll包数据 { - correction_time=tag_timer-(tag_id_recv%GROUP_TAG_NUM)*3; - if(correction_time == g_com_map[COM_INTERVAL]) - correction_time = 0; - tx_resp_msg[TIMECORRE] = correction_time; + 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++;} @@ -516,6 +545,7 @@ 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) @@ -587,11 +617,11 @@ usart_send[2] = 1;//正常模式 usart_send[3] = 17;//数据段长度 - usart_send[4] = frame_seq_nb++;//数据段长度 + usart_send[4] = frame_seq_nb;//数据段长度 memcpy(&usart_send[5],&tag_id_recv,2); memcpy(&usart_send[7],&dev_id,2); - hex_dist = dist_cm; - memcpy(&usart_send[9],&hex_dist,4); + + 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); @@ -606,6 +636,16 @@ /* 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 -- Gitblit v1.9.3