| | |
| | | #include "beep.h" |
| | | #include "modbus.h" |
| | | //#define DEBUG_OUTPUT |
| | | #define TDFILTER |
| | | #define CONSTANT_FILTER |
| | | /*------------------------------------ Marcos ------------------------------------------*/ |
| | | /* Inter-ranging delay period, in milliseconds. */ |
| | | #define RNG_DELAY_MS 100 |
| | |
| | | * frame length of approximately 2.66 ms with above configuration. */ |
| | | #define RESP_RX_TO_FINAL_TX_DLY_UUS 400 |
| | | /* Receive response timeout. See NOTE 5 below. */ |
| | | #define RESP_RX_TIMEOUT_UUS 600 |
| | | #define RESP_RX_TIMEOUT_UUS 9600 |
| | | |
| | | #define POLL_RX_TO_RESP_TX_DLY_UUS 420 |
| | | /* This is the delay from the end of the frame transmission to the enable of the receiver, as programmed for the DW1000's wait for response feature. */ |
| | |
| | | #define ANCTIMEUS 16 |
| | | #define ANCSEND_INTERVAL 18 |
| | | #define SIGNALPOWER 20 |
| | | //DATA POLL |
| | | #define CURENTPACKNUM_IDX 7 |
| | | #define TOTALPACKNUM_IDX 8 |
| | | #define DATA_IDX 10 |
| | | |
| | | |
| | | #define POLL 0x01 |
| | | #define RESPONSE 0x02 |
| | | #define FINAL 0x03 |
| | | #define SYNC 0x04 |
| | | |
| | | #define DATA_POLL 0x21 |
| | | #define DATA_RESPONSE 0x22 |
| | | /*------------------------------------ Variables ------------------------------------------*/ |
| | | /* Default communication configuration. We use here EVK1000's default mode (mode 3). */ |
| | | static dwt_config_t config = { |
| | |
| | | |
| | | /* Frames used in the ranging process. See NOTE 2 below. */ |
| | | static uint8_t tx_poll_msg[20] = {0}; |
| | | static uint8_t tx_sync_msg[14] = {0}; |
| | | static uint8_t tx_sync_msg[140] = {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[24] = {0}; |
| | | |
| | |
| | | |
| | | /* Buffer to store received response message. |
| | | * Its size is adjusted to longest frame that this example code is supposed to handle. */ |
| | | #define RX_BUF_LEN 24 |
| | | #define RX_BUF_LEN 224 |
| | | static uint8_t rx_buffer[RX_BUF_LEN]; |
| | | |
| | | /* Time-stamps of frames transmission/reception, expressed in device time units. |
| | |
| | | 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); |
| | | memcpy(&tx_sync_msg[TAG_ID_IDX], &dev_id, 4); |
| | | |
| | | memcpy(&tx_resp_msg[ANCSEND_INTERVAL], &g_com_map[COM_INTERVAL], 2); |
| | | } |
| | |
| | | // min_power = - 10 * log10((F1 *F1 + F2 * F2 + F3 * F3) / (C *B)); |
| | | return min_power; |
| | | } |
| | | #define CONSTANT_LEN 50 |
| | | extern u16 dist_threshold; |
| | | int32_t ConstantFilter(int32_t currentdist,u8 channel) |
| | | { |
| | | static int32_t cs_lastdist[10],cs_lastvalid[10]; |
| | | static u8 cfstart_flag[10] = {1}; |
| | | static u8 constant_count[10] = {100}; |
| | | if(cfstart_flag[channel]) |
| | | { |
| | | cfstart_flag[channel] = 0; |
| | | cs_lastdist[channel] = currentdist; |
| | | } |
| | | if( abs(currentdist - cs_lastdist[channel])<100) |
| | | { |
| | | if(constant_count[channel]<CONSTANT_LEN) |
| | | constant_count[channel]++; |
| | | }else{ |
| | | constant_count[channel] = 0; |
| | | } |
| | | if(constant_count[channel] == CONSTANT_LEN) |
| | | { |
| | | cs_lastvalid[channel] = currentdist; |
| | | } |
| | | cs_lastdist[channel] = currentdist; |
| | | return cs_lastvalid[channel]; |
| | | } |
| | | |
| | | uint16_t g_Resttimer; |
| | | uint8_t result; |
| | | u8 tag_succ_times=0; |
| | |
| | | u16 checksum; |
| | | int8_t tag_delaytime; |
| | | extern uint16_t sync_timer; |
| | | u16 tmp_time; |
| | | u16 tmp_time; |
| | | u32 start_poll; |
| | | extern float dw_vbat; |
| | | extern u16 slottime,max_slotnum,current_slotpos,tyncpoll_time; |
| | | void Tag_App(void)//å鿍¡å¼(TAGæ ç¾) |
| | | { |
| | | uint32_t frame_len; |
| | | uint32_t final_tx_time; |
| | | u32 start_poll; |
| | | |
| | | u8 i,getsync_flag=0; |
| | | u8 bat_percent; |
| | | //LED0_ON; |
| | |
| | | |
| | | int8_t correction_time; |
| | | extern uint8_t sync_seq; |
| | | #define TDFILTER |
| | | |
| | | //#define CHECK_UID |
| | | extern uint8_t UID_ERROR; |
| | | extern u16 dist_threshold; |
| | | |
| | | u8 misdist_num[TAG_NUM_IN_SYS]; |
| | | void Anchor_App(void) |
| | | { |
| | |
| | | #else |
| | | filter_dist=hex_dist/10; |
| | | #endif |
| | | |
| | | #ifdef CONSTANT_FILTER |
| | | filter_dist = ConstantFilter(filter_dist,tag_id_recv-TAG_ID_START); |
| | | #endif |
| | | anchor_dist_last_frm[tag_id_recv-TAG_ID_START]=filter_dist; |
| | | g_Tagdist[tag_id_recv]= filter_dist; |
| | | |
| | | |
| | | |
| | | his_dist[tag_id_recv-TAG_ID_START]=hex_dist; |
| | | g_flag_Taggetdist[tag_id_recv]=0; |
| | | if(!g_com_map[MODBUS_MODE]) |
| | |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); |
| | | } |
| | | } |
| | | u8 frame_len; |
| | | u8 rec_gpsdata[1000]; |
| | | extern u8 RTCMdata[2000]; |
| | | u16 recgpsdata_i,recdata_len; |
| | | void UWBSendOnePackData(u8* data_addr,u8 len) |
| | | { |
| | | g_Resttimer = 0; |
| | | LED0_BLINK; |
| | | g_start_sync_flag=1; |
| | | dwt_forcetrxoff(); |
| | | dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS); //设置åéåå¼å¯æ¥æ¶ï¼å¹¶è®¾å®å»¶è¿æ¶é´ |
| | | dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS); |
| | | tx_sync_msg[MESSAGE_TYPE_IDX]=DATA_POLL; |
| | | memcpy(&tx_sync_msg[DATA_IDX],data_addr,len); |
| | | dwt_writetxdata(len+14, tx_sync_msg, 0);//å°Pollå
æ°æ®ä¼ ç»DW1000ï¼å°å¨å¼å¯åéæ¶ä¼ åºå» |
| | | dwt_writetxfctrl(len+14, 0);//设置è¶
宽带åéæ°æ®é¿åº¦ |
| | | dwt_starttx(DWT_START_TX_IMMEDIATE| DWT_RESPONSE_EXPECTED); |
| | | start_poll = time32_incr; |
| | | recgpsdata_i = 0; |
| | | while(time32_incr - start_poll<100) |
| | | { |
| | | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))//䏿æ¥è¯¢è¯çç¶æç´å°æåæ¥æ¶æè
åçé误 |
| | | { |
| | | }; |
| | | if(status_reg==0xffffffff) |
| | | { |
| | | NVIC_SystemReset(); |
| | | } |
| | | |
| | | if (status_reg & SYS_STATUS_RXFCG)//妿æåæ¥æ¶ |
| | | { |
| | | /* Clear good RX frame event and TX frame sent in the DW1000 status register. */ |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);//æ¸
æ¥å¯å卿 å¿ä½ |
| | | |
| | | /* A frame has been received, read it into the local buffer. */ |
| | | frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFLEN_MASK; //è·å¾æ¥æ¶å°çæ°æ®é¿åº¦ |
| | | |
| | | dwt_readrxdata(rx_buffer, frame_len, 0); //è¯»åæ¥æ¶æ°æ® |
| | | |
| | | dwt_rxenable(0); |
| | | /* 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] == DATA_RESPONSE ) //å¤ææ¥æ¶å°çæ°æ®æ¯å¦æ¯responseæ°æ® |
| | | { |
| | | recdata_len = frame_len-14; |
| | | memcpy(&rec_gpsdata[recgpsdata_i],&rx_buffer[DATA_IDX],recdata_len); |
| | | recgpsdata_i+=recdata_len; |
| | | if(rx_buffer[CURENTPACKNUM_IDX]==1) |
| | | { |
| | | USART_puts(rec_gpsdata,recgpsdata_i); |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | extern u8 gpsdataready_flag; |
| | | extern u16 gps_packlen; |
| | | u8 totalpack_num,currentpack_num; |
| | | void RecOnePackData(void) |
| | | { |
| | | dwt_setrxtimeout(0);//è®¾å®æ¥æ¶è¶
æ¶æ¶é´ï¼0使²¡æè¶
æ¶æ¶é´ |
| | | |
| | | /* Activate reception immediately. */ |
| | | 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&&!g_start_sync_flag)//䏿æ¥è¯¢è¯çç¶æç´å°æ¥æ¶æåæè
åºç°é误 |
| | | { |
| | | IdleTask(); |
| | | }; |
| | | |
| | | if (status_reg & SYS_STATUS_RXFCG)//æåæ¥æ¶ |
| | | { |
| | | u16 tag_recv_interval,send_i=0,remain_i=0; |
| | | /* Clear good RX frame event in the DW1000 status register. */ |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG);//æ¸
餿 å¿ä½ |
| | | |
| | | /* A frame has been received, read it into the local buffer. */ |
| | | frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023;//è·å¾æ¥æ¶æ°æ®é¿åº¦ |
| | | g_Resttimer = 0; |
| | | dwt_readrxdata(rx_buffer, frame_len, 0);//è¯»åæ¥æ¶æ°æ® |
| | | if (rx_buffer[MESSAGE_TYPE_IDX] == DATA_POLL ) //å¤ææ¥æ¶å°çæ°æ®æ¯å¦æ¯responseæ°æ® |
| | | { |
| | | recdata_len = frame_len-14; |
| | | memcpy(rec_gpsdata,&rx_buffer[DATA_IDX],recdata_len); |
| | | if(gpsdataready_flag) |
| | | { |
| | | gpsdataready_flag = 0; |
| | | tx_sync_msg[MESSAGE_TYPE_IDX]=DATA_RESPONSE; |
| | | remain_i = gps_packlen; |
| | | currentpack_num = 0; |
| | | totalpack_num = gps_packlen/110; |
| | | while(remain_i>0) |
| | | { |
| | | if(remain_i>=110) |
| | | { |
| | | tx_sync_msg[CURENTPACKNUM_IDX] = 0; |
| | | memcpy(&tx_sync_msg[DATA_IDX],&RTCMdata[send_i],110); |
| | | send_i+=110; |
| | | remain_i-=110; |
| | | dwt_writetxdata(110+14, tx_sync_msg, 0);//å°Pollå
æ°æ®ä¼ ç»DW1000ï¼å°å¨å¼å¯åéæ¶ä¼ åºå» |
| | | dwt_writetxfctrl(110+14, 0);//设置è¶
宽带åéæ°æ®é¿åº¦ |
| | | dwt_starttx(DWT_START_TX_IMMEDIATE); |
| | | }else{ |
| | | tx_sync_msg[CURENTPACKNUM_IDX] = 1; |
| | | memcpy(&tx_sync_msg[DATA_IDX],&RTCMdata[send_i],remain_i); |
| | | dwt_writetxdata(remain_i+14, tx_sync_msg, 0);//å°Pollå
æ°æ®ä¼ ç»DW1000ï¼å°å¨å¼å¯åéæ¶ä¼ åºå» |
| | | dwt_writetxfctrl(remain_i+14, 0);//设置è¶
宽带åéæ°æ®é¿åº¦ |
| | | dwt_starttx(DWT_START_TX_IMMEDIATE); |
| | | remain_i = 0; |
| | | } |
| | | delay_us(1000); |
| | | } |
| | | } |
| | | LED0_BLINK; |
| | | USART_puts(rec_gpsdata,recdata_len); |
| | | } |
| | | }else{ |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); |
| | | } |
| | | |
| | | } |