#include //#include "stm32f4xx_hal.h" #include "filters.h" #include "ADC.h" #include "deca_device_api.h" #include "deca_regs.h" #include "dw_driver.h" #include "Spi.h" #include "led.h" #include "serial_at_cmd_app.h" #include "Usart.h" #include "global_param.h" #include "filters.h" #include #include "beep.h" #include #include "TrackingDiff.h" //#include "UDPClient.h" //#include "Uart.h" //#include "DBG.h" #include "CRC.h" #define DBGMSG_OUTPUT #define SENDANCPARA_LEN 102 #define SPEED_OF_LIGHT 299702547 #define UUS_TO_DWT_TIME 65536 #define POLL_TX_TO_RESP_RX_DLY_UUS 150 #define RESP_RX_TO_FINAL_TX_DLY_UUS 410 #define RESP_RX_TIMEOUT_UUS 2000 #define DELAY_BETWEEN_TWO_FRAME_UUS 400 #define POLL_RX_TO_RESP_TX_DLY_UUS 2000 #define RESP_TX_TO_FINAL_RX_DLY_UUS 10 #define FINAL_RX_TIMEOUT_UUS 4300 #define WHITELIST1_START 0x25 #define WHITELIST1_END 0x26 #define MODBUS_ID1 0x11 #define FZ_NEARBASENUM_INDEX 10 #define FZ_NEARBASEID_INDEX 11 #define RESP_MSG_POLL_RX_TS_IDX 26 #define RESP_MSG_RESP_TX_TS_IDX 30 #define RESP_MSG_ANC_DISTOFFSET 34 #define MBX_ANC_NUM_IN_SYS 50 #define QUIT_SLOT_TIME 5 uint8_t lora_yingda_flag; static uint64_t resp_tx_ts; static uint64_t final_rx_ts; static uint32_t status_reg,resp_tx_time,tagpos_binary; static uint8_t frame_len,recpoll_len,current_syncid,new_tagid,seize_anchor,result,g_start_sync_flag; static uint8_t rx_buffer[150]; static uint16_t anc_id_recv,tag_id_recv; static uint8_t flag_syncbase,sync_seq,synclost_timer,temp_nearbaspos; static uint16_t sync_timer; static uint8_t send_buffer[150]; static uint8_t frame_seq_nb2,battary,button,ancidlist_num; uint8_t rec_nearbase_num; static uint16_t ancidlist_rec[TAG_NUM_IN_SYS],ancidlist_send[TAG_NUM_IN_SYS],rec_ancidlist[TAG_NUM_IN_SYS],rec_ancdistlist[TAG_NUM_IN_SYS]; static int16_t rec_antdelay; static uint16_t taglist_num=0,taglist_pos,tmp_time; static uint16_t tagid_list[TAG_NUM_IN_SYS]; uint8_t tagbattary_list[TAG_NUM_IN_SYS]; static uint8_t tagofflinetime[TAG_NUM_IN_SYS]; static int32_t tagdist_list[TAG_NUM_IN_SYS]; static uint8_t tagpos_rec[50]; static uint64_t poll_rx_ts; static uint8_t g_flag_Taggetdist[TAG_NUM_IN_SYS],flag_tag_distsmooth[TAG_NUM_IN_SYS],misdist_num[TAG_NUM_IN_SYS]; static uint32_t poll_tx_ts, resp_rx_ts, final_tx_ts; static uint32_t poll_rx_ts_32, resp_tx_ts_32, final_rx_ts_32; static double Ra, Rb, Da, Db; static int64_t tof_dtu; static double tof; static double distance, dist_no_bias, dist_cm; extern dwt_config_t config; static double firstpath_power, rx_power,rec_firstpath_power; static double f1, f2, r1, r2; static uint16_t F1,F2,F3,N,C; const double B = 131072; const double A = 121.74; double min_power; static dwt_rxdiag_t d1; static int32_t hex_dist,his_dist[TAG_NUM_IN_SYS]; extern uint16_t g_Resttimer; static int32_t filter_dist,g_Tagdist[TAG_NUM_IN_SYS]; double LOS(dwt_rxdiag_t *dia) { F1 = dia->firstPathAmp1; F2 = dia->firstPathAmp2; F3 = dia->firstPathAmp3; N = dia->rxPreamCount; C = dia->maxGrowthCIR; firstpath_power=10* log10((F1*F1+F2*F2+F3*F3)/(N*N))-A; rx_power=10*log10(C*B/(N*N))-A; // min_power = - 10 * log10((F1 *F1 + F2 * F2 + F3 * F3) / (C *B)); return min_power; } static uint64_t get_tx_timestamp_u64(void) { uint8_t ts_tab[5]; uint64_t ts = 0; int i; dwt_readtxtimestamp(ts_tab); for (i = 4; i >= 0; i--) { ts <<= 8; ts |= ts_tab[i]; } return ts; } static uint64_t get_rx_timestamp_u64(void) { uint8_t ts_tab[5]; uint64_t ts = 0; int i; dwt_readrxtimestamp(ts_tab); for (i = 4; i >= 0; i--) { ts <<= 8; ts |= ts_tab[i]; } return ts; } static void final_msg_set_ts(uint8_t *ts_field, uint64_t ts) { int i; for (i = 0; i < FINAL_MSG_TS_LEN; i++) { ts_field[i] = (uint8_t) ts; ts >>= 8; } } static void final_msg_get_ts(const uint8_t *ts_field, uint32_t *ts) { int i; *ts = 0; for (i = 0; i < FINAL_MSG_TS_LEN; i++) { *ts += ts_field[i] << (i * 8); } } void TagListUpdate(void) { uint16_t i,j=0,temp[TAG_NUM_IN_SYS]; for(i=0;i> (i * 8)) & 0xFF; } } extern uint16_t current_time,start_time,end_time; uint32_t uwb_timeout; extern uint32_t lp_time; //extern u8 uwb_timeout; u16 basesystimer,uwb_losttimer; uint8_t remotesend_state,remotesend_index; uint16_t random_time; uint8_t Anchor_RecNearPoll(uint8_t ancrec_nearbasepos) //0 mainbase 1 first near_base { uint8_t motorstate; // tmp_time=TIM3->CNT; temp_nearbaspos = ancrec_nearbasepos; memcpy(&send_buffer[ANCHOR_ID_IDX],&dev_id,2); memcpy(&send_buffer[ANCTIMEMS],&basesystimer,2); // memcpy(&send_buffer[ANCTIMEUS],&tmp_time,2); send_buffer[TAGFREQ_IDX] = tag_frequency; memcpy(&send_buffer[TAG_ID_IDX],&tag_id_recv,2); poll_rx_ts = get_rx_timestamp_u64();//»ñµÃPoll°ü½ÓÊÕʱ¼äT2 if(rec_nearbase_num == ancrec_nearbasepos)//ÇÀռģʽ { random_time = (GetLPTime()%100); resp_tx_time = (poll_rx_ts+((random_time+rec_nearbase_num*20 + POLL_RX_TO_RESP_TX_DLY_UUS+ancrec_nearbasepos*DELAY_BETWEEN_TWO_FRAME_UUS) * UUS_TO_DWT_TIME)) >> 8;//¼ÆËãResponse·¢ËÍʱ¼äT3¡£ }else{ resp_tx_time = (poll_rx_ts + ((rec_nearbase_num*20+POLL_RX_TO_RESP_TX_DLY_UUS+ancrec_nearbasepos*DELAY_BETWEEN_TWO_FRAME_UUS) * UUS_TO_DWT_TIME)) >> 8;//¼ÆËãResponse·¢ËÍʱ¼äT3¡£ } dwt_setdelayedtrxtime(resp_tx_time);//ÉèÖÃResponse·¢ËÍʱ¼äT3 //dwt_setrxaftertxdelay(RESP_TX_TO_FINAL_RX_DLY_UUS+(rec_nearbase_num+1-ancrec_nearbasepos)*DELAY_BETWEEN_TWO_FRAME_UUS);//ÉèÖ÷¢ËÍÍê³Éºó¿ªÆô½ÓÊÕÑÓ³Ùʱ¼ä //dwt_setrxtimeout(FINAL_RX_TIMEOUT_UUS);//½ÓÊÕ³¬Ê±Ê±¼ä resp_tx_ts = (((uint64_t)(resp_tx_time & 0xFFFFFFFEUL)) << 8) ; /* Write all timestamps in the final message. See NOTE 8 below. */ resp_msg_set_ts(&send_buffer[RESP_MSG_POLL_RX_TS_IDX], poll_rx_ts); resp_msg_set_ts(&send_buffer[RESP_MSG_RESP_TX_TS_IDX], resp_tx_ts); memcpy(&send_buffer[RESP_MSG_ANC_DISTOFFSET],&g_com_map[DIST_OFFSET],2); send_buffer[GROUP_ID_IDX] = group_id; // send_buffer[MAINBASE_INDEX]=flag_syncbase; send_buffer[MESSAGE_TYPE_IDX]=MBX_RESPONSE; send_buffer[MOTORSTATE_INDEX]=motorstate;//(remotesend_state<<4)|motorstate; send_buffer[MOTORSTATE_INDEX]&=0x0f; remotesend_state = 0; dwt_forcetrxoff(); dwt_writetxdata(38, send_buffer, 0);//дÈë·¢ËÍÊý¾Ý dwt_writetxfctrl(38, 0);//É趨·¢Ëͳ¤¶È result = dwt_starttx(DWT_START_TX_DELAYED); // if(remotesend_state==0) // { // dwt_writetxdata(38, send_buffer, 0);//дÈë·¢ËÍÊý¾Ý // dwt_writetxfctrl(38, 0);//É趨·¢Ëͳ¤¶È // result = dwt_starttx(DWT_START_TX_DELAYED); //// uwb_timeout = 0; // if(result == 0) uwb_timeout=lp_time; while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_TXFRS | SYS_STATUS_ALL_RX_ERR)))///²»¶Ï²éѯоƬ״ֱ̬µ½½ÓÊճɹ¦»òÕß³öÏÖ´íÎó { current_time=HAL_LPTIM_ReadCounter(&hlptim1); if(current_time>end_time/*||current_time2) // break; }; dwt_rxenable(0); } uint8_t power_and_key=0; uint16_t target_tagid[12] = {0x1316,0x1394,0x1265,0x1318,0x1306,0x1304,0x1376,0x1312,0x1277,0x1388,0x1140,0x1392}; void Anchor_App(void) { u8 send_len,i; u16 tempid; uint32_t rec_syncid; uint16_t checksum; dwt_setrxtimeout(0);//É趨½ÓÊÕ³¬Ê±Ê±¼ä£¬0λûÓг¬Ê±Ê±¼ä dwt_rxenable(0);//´ò¿ª½ÓÊÕ start_time=HAL_LPTIM_ReadCounter(&hlptim1); end_time=start_time+((32768/1000)*20); if(end_time>=32768) {end_time-=32768;} current_time=HAL_LPTIM_ReadCounter(&hlptim1); while(current_timeend_time+15000) { current_time=HAL_LPTIM_ReadCounter(&hlptim1); while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))//²»¶Ï²éѯоƬ״ֱ̬µ½½ÓÊճɹ¦»òÕß³öÏÖ´íÎó { current_time=HAL_LPTIM_ReadCounter(&hlptim1); if(current_time>=end_time&¤t_time=TAG_NUM_IN_SYS) break; if(taglist_pos==taglist_num) { taglist_pos=taglist_num; tagid_list[taglist_num++]=tag_id_recv; new_tagid=1; }else{ new_tagid=0; } temp_tagpos=round((float)(sync_timer%g_com_map[COM_INTERVAL])/slottime); tagpos_rec[(uint8_t)temp_tagpos]=1; if(rec_nearbase_num>ancidlist_num) { ancidlist_num=rec_nearbase_num; memcpy(ancidlist_rec,&rx_buffer[FZ_NEARBASEID_INDEX],rec_nearbase_num*2); } for(i=0;i