¶Ô±ÈÐÂÎļþ |
| | |
| | | #include <string.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 <stdio.h> |
| | | |
| | | #include <math.h> |
| | | #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 670 |
| | | #define RESP_TX_TO_FINAL_RX_DLY_UUS 10 |
| | | #define FINAL_RX_TIMEOUT_UUS 4300 |
| | | static u8 tag_succ_times=0; |
| | | static u16 checksum; |
| | | static int8_t tag_delaytime; |
| | | extern uint16_t sync_timer; |
| | | static int32_t temp_dist; |
| | | static u16 tagslotpos; |
| | | static u8 recbase_num=0; |
| | | static int32_t mainbase_dist,nearbase_switchdistlist[MAX_NEARBASE_NUM],nearbase_distlist[MAX_NEARBASE_NUM],true_nearbase_distlist[MAX_NEARBASE_NUM],true_exsistbase_list[MAX_NEARBASE_NUM],ancsync_time,nextpoll_delaytime,offsettimeus; |
| | | static u8 anclost_times=0 , mainbase_lost_count=0; |
| | | static u8 exsistbase_list[MAX_NEARBASE_NUM],report_num,get_newbase=0; |
| | | static u8 flag_finalsend,flag_getresponse,flag_rxon; |
| | | static uint16_t current_count,start_count,end_count,lastsync_timer; |
| | | extern u16 bigslot_num; |
| | | static u8 nearbase_num=0,last_nearbase_num,next_nearbase_num,last_slotnum, para_update,para_len; |
| | | static u32 rec_tagpos_binary; |
| | | static int16_t offset=4700,temptimer; |
| | | static u8 rec_remotepara_state,rec_remotepara[80],neartimout_timer; |
| | | static u8 frame_seq_nb; |
| | | static u16 mainbase_id,true_nearbase_idlist[MAX_NEARBASE_NUM],nearbaseid_list[MAX_NEARBASE_NUM],nearbaseid_list2[MAX_NEARBASE_NUM]; |
| | | |
| | | static u8 FindNearBasePos(u16 baseid) |
| | | { |
| | | u8 i; |
| | | for(i=0; i<nearbase_num; i++) |
| | | { |
| | | if(baseid==nearbaseid_list[i]) |
| | | return i; |
| | | } |
| | | } |
| | | static uint8_t rx_buffer[100]; |
| | | static uint64_t poll_tx_ts; |
| | | static uint64_t resp_rx_ts; |
| | | static uint64_t final_tx_ts; |
| | | static uint64_t poll_rx_ts; |
| | | static uint64_t resp_tx_ts; |
| | | static uint64_t final_rx_ts; |
| | | static uint32_t status_reg = 0; |
| | | extern uint8_t tx_near_msg[80]; |
| | | extern uint8_t motor_state; |
| | | static u8 result; |
| | | 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); |
| | | } |
| | | } |
| | | u8 send_buffer[100],outside_count; |
| | | #define FZ_NEARBASENUM_INDEX 10 |
| | | #define FZ_NEARBASEID_INDEX 11 |
| | | void FZNearPoll(void) |
| | | { |
| | | uint32_t temp1,temp2,dw_systime; |
| | | uint32_t frame_len; |
| | | uint32_t final_tx_time; |
| | | u32 start_poll; |
| | | u8 i,j,getsync_flag=0,timeout; |
| | | dwt_setrxaftertxdelay(10); //设置åéåå¼å¯æ¥æ¶ï¼å¹¶è®¾å®å»¶è¿æ¶é´ |
| | | dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS); |
| | | tag_succ_times = 0; |
| | | //GPIO_WriteBit(GPIOA, GPIO_Pin_10, Bit_SET); |
| | | if(next_nearbase_num>=MAX_NEARBASE_NUM) |
| | | { |
| | | next_nearbase_num = MAX_NEARBASE_NUM-1; |
| | | } |
| | | HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_SET); |
| | | last_nearbase_num=next_nearbase_num; |
| | | nearbase_num=next_nearbase_num; |
| | | recbase_num=0; |
| | | motor_state=0; |
| | | |
| | | memcpy(&send_buffer[TAG_ID_IDX], &dev_id, 2); |
| | | send_buffer[GROUP_ID_IDX] = group_id; |
| | | send_buffer[BATTARY_IDX] = bat_percent; |
| | | //tx_near_msg[BUTTON_IDX] = !GET_USERKEY|stationary_flag<<1; |
| | | send_buffer[SEQUENCE_IDX] = frame_seq_nb++; |
| | | send_buffer[FZ_NEARBASENUM_INDEX] = nearbase_num; |
| | | memcpy(&send_buffer[FZ_NEARBASEID_INDEX],&nearbaseid_list,nearbase_num*2); |
| | | send_buffer[MESSAGE_TYPE_IDX] = NEAR_POLL; |
| | | memcpy(&send_buffer[ANCHOR_ID_IDX],&mainbase_id,2); |
| | | dwt_writetxdata(35, send_buffer, 0);//å°Pollå
æ°æ®ä¼ ç»DW1000ï¼å°å¨å¼å¯åéæ¶ä¼ åºå» |
| | | dwt_writetxfctrl(35, 0);//设置è¶
宽带åéæ°æ®é¿åº¦ |
| | | dwt_starttx(DWT_START_TX_IMMEDIATE| DWT_RESPONSE_EXPECTED);//å¼å¯åéï¼åé宿åçå¾
䏿®µæ¶é´å¼å¯æ¥æ¶ï¼çå¾
æ¶é´å¨dwt_setrxaftertxdelayä¸è®¾ç½® |
| | | |
| | | tx_near_msg[TAGCONFIGSUCCESS_INDEX] =0; |
| | | para_update = 0; |
| | | flag_finalsend=0; |
| | | flag_rxon=1; |
| | | flag_getresponse=0; |
| | | |
| | | neartimout_timer = 0; |
| | | recbase_num=0; |
| | | start_count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | timeout=ceil((float)nearbase_num*SLOT_SCALE)+3; |
| | | current_count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | end_count=start_count+(timeout<<5); |
| | | if(end_count>=32768) |
| | | { |
| | | end_count-=32768; |
| | | } |
| | | while(current_count<end_count||current_count>end_count+15000) |
| | | { |
| | | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR)))//䏿æ¥è¯¢è¯çç¶æç´å°æåæ¥æ¶æè
åçé误 |
| | | { |
| | | if(flag_finalsend) |
| | | { |
| | | dw_systime=dwt_readsystimestamphi32(); |
| | | if(dw_systime>temp1&&dw_systime<temp2) |
| | | { |
| | | |
| | | dwt_forcetrxoff(); |
| | | flag_rxon=0; |
| | | dwt_setdelayedtrxtime(final_tx_time); |
| | | result=dwt_starttx(DWT_START_TX_DELAYED);//设å®ä¸ºå»¶è¿åé |
| | | flag_finalsend=0; |
| | | break; |
| | | } |
| | | } |
| | | |
| | | current_count=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | if(current_count>=end_count&¤t_count<end_count+15000) |
| | | break; |
| | | }; |
| | | if(status_reg==0xffffffff) |
| | | { |
| | | NVIC_SystemReset(); |
| | | } |
| | | if (status_reg & SYS_STATUS_RXFCG)//妿æåæ¥æ¶ |
| | | { |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);//æ¸
æ¥å¯å卿 å¿ä½ |
| | | frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFLEN_MASK; //è·å¾æ¥æ¶å°çæ°æ®é¿åº¦ |
| | | dwt_readrxdata(rx_buffer, frame_len, 0); //è¯»åæ¥æ¶æ°æ® |
| | | dwt_setrxtimeout(0);//DELAY_BETWEEN_TWO_FRAME_UUS*(nearbase_num+1-recbase_num)+10);//è®¾å®æ¥æ¶è¶
æ¶æ¶é´ï¼0使²¡æè¶
æ¶æ¶é´ |
| | | dwt_rxenable(0);//æå¼æ¥æ¶ |
| | | if (rx_buffer[MESSAGE_TYPE_IDX] == NEAR_RESPONSE&&!memcmp(&rx_buffer[TAG_ID_IDX],&dev_id,2)&&rx_buffer[GROUP_ID_IDX]==group_id) //å¤ææ¥æ¶å°çæ°æ®æ¯å¦æ¯responseæ°æ® |
| | | { u16 rec_nearbaseid,rec_nearbasepos; |
| | | poll_tx_ts = get_tx_timestamp_u64(); //è·å¾POLLåéæ¶é´T1 |
| | | resp_rx_ts = get_rx_timestamp_u64(); //è·å¾RESPONSEæ¥æ¶æ¶é´T4 |
| | | recbase_num++; |
| | | memcpy(&rec_nearbaseid,&rx_buffer[ANCHOR_ID_IDX],2); |
| | | if(last_nearbase_num==0) |
| | | { |
| | | get_newbase=1; |
| | | nearbaseid_list[0]=rec_nearbaseid; |
| | | nearbase_num=1; |
| | | memcpy(&tx_near_msg[ANCHOR_ID_IDX],&rec_nearbaseid,2); |
| | | } |
| | | if(rec_nearbaseid==nearbaseid_list[0]) |
| | | { |
| | | //////////////////////////////////æ¶é´åæ¥ |
| | | |
| | | // TIM3->CNT=tmp_time; |
| | | // if(tagslotpos>max_slotpos) |
| | | // tagslotpos=tagslotpos%(max_slotpos+1); |
| | | // tyncpoll_time=(tagslotpos-1)*slottime; |
| | | //////////////////////////// |
| | | rec_nearbasepos=0; |
| | | exsistbase_list[rec_nearbasepos]=KEEP_TIMES; |
| | | memcpy(&temp_dist,&rx_buffer[DIST_IDX],4); |
| | | nearbase_distlist[rec_nearbasepos]=temp_dist; |
| | | if((rx_buffer[MOTORSTATE_INDEX]&0xf)!=3) |
| | | motor_state=rx_buffer[MOTORSTATE_INDEX]&0xf; |
| | | rec_remotepara_state=rx_buffer[MOTORSTATE_INDEX]>>4; |
| | | |
| | | mainbase_lost_count=0; |
| | | flag_finalsend=1; |
| | | memcpy(&mainbase_dist,&rx_buffer[DIST_IDX],4); |
| | | final_tx_time = (resp_rx_ts + ((RESP_RX_TO_FINAL_TX_DLY_UUS+DELAY_BETWEEN_TWO_FRAME_UUS*nearbase_num+500) * UUS_TO_DWT_TIME)) >> 8; |
| | | temp1=final_tx_time-((850*UUS_TO_DWT_TIME)>>8); |
| | | temp2=final_tx_time+((100*UUS_TO_DWT_TIME)>>8); |
| | | // dwt_setdelayedtrxtime(final_tx_time);//设置finalå
åéæ¶é´T5 |
| | | final_tx_ts = (((uint64_t)(final_tx_time & 0xFFFFFFFE)) << 8) ;//finalå
å®é
åéæ¶é´æ¯è®¡ç®æ¶é´å ä¸åé天线delay |
| | | final_msg_set_ts(&tx_near_msg[FINAL_MSG_POLL_TX_TS_IDX], poll_tx_ts);//å°T1ï¼T4ï¼T5åå
¥åéæ°æ® |
| | | final_msg_set_ts(&tx_near_msg[FINAL_MSG_RESP_RX_NEARBASE_IDX], resp_rx_ts); |
| | | final_msg_set_ts(&tx_near_msg[FINAL_MSG_FINAL_TX_TS_IDX], final_tx_ts); |
| | | tx_near_msg[MESSAGE_TYPE_IDX]=NEAR_FINAL; |
| | | tx_near_msg[GROUP_ID_IDX] = group_id; |
| | | |
| | | dwt_writetxdata(28+nearbase_num*4, tx_near_msg, 0);//å°åéæ°æ®åå
¥DW1000 |
| | | dwt_writetxfctrl(28+nearbase_num*4, 0);//设å®åéæ°æ®é¿åº¦ |
| | | flag_getresponse=1; |
| | | |
| | | } else { |
| | | rec_nearbasepos=FindNearBasePos(rec_nearbaseid); |
| | | |
| | | if(rec_nearbasepos>=last_nearbase_num) //åç°æ°çåºç« |
| | | { |
| | | get_newbase=1; |
| | | nearbase_num++; |
| | | nearbaseid_list[rec_nearbasepos] = rec_nearbaseid; |
| | | memcpy(&tx_near_msg[ANCHOR_ID_IDX],&rec_nearbaseid,2); |
| | | } |
| | | |
| | | exsistbase_list[rec_nearbasepos]=KEEP_TIMES; |
| | | memcpy(&temp_dist,&rx_buffer[DIST_IDX],4); |
| | | nearbase_distlist[rec_nearbasepos]=temp_dist; |
| | | |
| | | if(motor_state<(rx_buffer[MOTORSTATE_INDEX]&0xf)&&(rx_buffer[MOTORSTATE_INDEX]&0xf)!=3) |
| | | { |
| | | motor_state=rx_buffer[MOTORSTATE_INDEX]; |
| | | } |
| | | final_msg_set_ts(&tx_near_msg[FINAL_MSG_RESP_RX_NEARBASE_IDX+(rec_nearbasepos)*4], resp_rx_ts); |
| | | dwt_writetxdata(28+nearbase_num*4, tx_near_msg, 0);//å°åéæ°æ®åå
¥DW1000 |
| | | dwt_writetxfctrl(28+nearbase_num*4, 0);//设å®åéæ°æ®é¿åº¦ |
| | | |
| | | |
| | | //dwt_setdelayedtrxtime(final_tx_time);//设置finalå
åéæ¶é´T5 |
| | | // result=dwt_starttx(DWT_START_TX_DELAYED);//设å®ä¸ºå»¶è¿åé |
| | | //dwt_writetxdata(4,&tx_near_msg[FINAL_MSG_RESP_RX_NEARBASE_IDX+(rec_nearbasepos+1)*4], FINAL_MSG_RESP_RX_NEARBASE_IDX+(rec_nearbasepos+1)*4);//å°åéæ°æ®åå
¥DW1000 |
| | | } |
| | | } |
| | | } else { |
| | | dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_RXFCG| SYS_STATUS_ALL_RX_ERR); |
| | | if(flag_rxon) |
| | | { dwt_rxenable(0); |
| | | } |
| | | } |
| | | // dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_RXFCG| SYS_STATUS_ALL_RX_ERR); |
| | | } |
| | | dwt_write32bitreg(SYS_STATUS_ID,SYS_STATUS_RXFCG| SYS_STATUS_ALL_RX_ERR); |
| | | nearbase_num=recbase_num; |
| | | j=0; |
| | | if(exsistbase_list[0]==0) |
| | | { |
| | | // u8 temp_adc,random_value; |
| | | // random_value=0; |
| | | // for(i=0;i<8;i++) |
| | | // { |
| | | // temp_adc=Get_ADC_Value(); |
| | | // random_value=random_value|((temp_adc&0x01)<<i); |
| | | // } |
| | | tagslotpos=GetRandomSlotPos(rec_tagpos_binary); |
| | | tyncpoll_time = (tagslotpos--%max_slotpos)*slottime; |
| | | } |
| | | // tyncpoll_time=0; |
| | | next_nearbase_num=0; |
| | | for(i=0; i<last_nearbase_num+get_newbase; i++) |
| | | { |
| | | if(exsistbase_list[i]>0) |
| | | { |
| | | next_nearbase_num++; |
| | | true_exsistbase_list[j]=exsistbase_list[i]; |
| | | true_nearbase_idlist[j]=nearbaseid_list[i]; |
| | | true_nearbase_distlist[j++]=nearbase_distlist[i]; |
| | | exsistbase_list[i]--; |
| | | } |
| | | } |
| | | |
| | | if(recbase_num<3) |
| | | { |
| | | next_nearbase_num=next_nearbase_num; |
| | | } |
| | | last_nearbase_num = next_nearbase_num; |
| | | for(i=0; i<last_nearbase_num-1; i++) |
| | | { |
| | | for(j=0; j<last_nearbase_num-1; j++) |
| | | { |
| | | if(true_nearbase_distlist[j]>true_nearbase_distlist[j+1]) |
| | | { |
| | | u32 temp_dist,temp_id,temp_exsis; |
| | | temp_dist=true_nearbase_distlist[j]; |
| | | temp_id = true_nearbase_idlist[j]; |
| | | temp_exsis=true_exsistbase_list[i]; |
| | | true_nearbase_distlist[j]=true_nearbase_distlist[j+1]; |
| | | true_nearbase_idlist[j]=true_nearbase_idlist[j+1]; |
| | | true_exsistbase_list[j]=true_exsistbase_list[j+1]; |
| | | |
| | | true_nearbase_distlist[j+1]=temp_dist; |
| | | true_nearbase_idlist[j+1]=temp_id; |
| | | true_exsistbase_list[j+1]=temp_exsis; |
| | | } |
| | | } |
| | | } |
| | | |
| | | report_num=0; |
| | | for (i=0; i<last_nearbase_num; i++) |
| | | { |
| | | nearbaseid_list[i]=true_nearbase_idlist[i]; |
| | | nearbase_distlist[i]=true_nearbase_distlist[i]; |
| | | // if(nearbase_distlist[i]!=0x1ffff&&true_exsistbase_list[i]==KEEP_TIMES) |
| | | // { |
| | | // memcpy(&usart_send[4+6*report_num],&nearbaseid_list[i],2); |
| | | // memcpy(&usart_send[6+6*report_num],&nearbase_distlist[i],4); |
| | | // report_num++; |
| | | // } |
| | | } |
| | | #ifdef USART_INTEGRATE_OUTPUT |
| | | usart_send[0] = 0x55; |
| | | usart_send[1] = 0xAA; |
| | | usart_send[2] = 4;//æ£å¸¸æ¨¡å¼ |
| | | usart_send[3] = report_num*6+2;//æ£å¸¸æ¨¡å¼ |
| | | checksum = Checksum_u16(&usart_send[2],report_num*6+2); |
| | | memcpy(&usart_send[4+report_num*6],&checksum,2); |
| | | USART_puts(usart_send,6+report_num*6); |
| | | #endif |
| | | if(motor_state==2) |
| | | { outside_count=0; |
| | | } else { |
| | | outside_count++; |
| | | } |
| | | for(i=0; i<MAX_NEARBASE_NUM; i++) |
| | | { |
| | | nearbase_distlist[i]=0x1ffff; |
| | | } |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR| SYS_STATUS_TXFRS |SYS_STATUS_RXFCG); |
| | | |
| | | } |