| | |
| | | uint8_t Anchor_RecNearPoll(uint8_t ancrec_nearbasepos) //0 mainbase 1 first near_base |
| | | { |
| | | uint8_t motorstate; |
| | | memcpy(&send_buffer[ANCHOR_ID_IDX],&dev_id,2); |
| | | |
| | | memcpy(&send_buffer[ANCHOR_ID_IDX],&dev_id,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 |
| | | 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) ; |
| | | 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 |
| | | 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。 |
| | | |
| | | /* 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); |
| | | 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[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; |
| | | |
| | | |
| | | |
| | | dwt_writetxdata(38, send_buffer, 0);//写入发送数据 |
| | | dwt_writetxfctrl(38, 0);//设定发送长度 |
| | | result = dwt_starttx(DWT_START_TX_DELAYED); |
| | | send_buffer[MESSAGE_TYPE_IDX]=MBX_RESPONSE; |
| | | send_buffer[MOTORSTATE_INDEX]=motorstate;//(remotesend_state<<4)|motorstate; |
| | | send_buffer[MOTORSTATE_INDEX]&=0x0f; |
| | | |
| | | start_time=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | timeout=100; //单位0.1ms |
| | | end_time=start_time+(timeout<<2); |
| | | if(end_time>=32768) |
| | | {end_time-=32768;} |
| | | current_time=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & SYS_STATUS_TXFRS ))///不断查询芯片状态直到接收成功或者出现错误 |
| | | { |
| | | current_time=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | if(current_time>=end_time&¤t_time<end_time+15000) |
| | | break; |
| | | }; |
| | | tagofflinetime[taglist_pos] = 0; |
| | | |
| | | |
| | | dwt_writetxdata(38, send_buffer, 0);//写入发送数据 |
| | | dwt_writetxfctrl(38, 0);//设定发送长度 |
| | | result = dwt_starttx(DWT_START_TX_DELAYED); |
| | | |
| | | start_time=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | timeout=100; //单位0.1ms |
| | | end_time=start_time+(timeout<<2); |
| | | if(end_time>=32768) |
| | | { |
| | | end_time-=32768; |
| | | } |
| | | current_time=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & SYS_STATUS_TXFRS ))///不断查询芯片状态直到接收成功或者出现错误 |
| | | { |
| | | current_time=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | if(current_time>=end_time&¤t_time<end_time+15000) |
| | | break; |
| | | }; |
| | | tagofflinetime[taglist_pos] = 0; |
| | | // printf("测距成功,序号%d,距离%d\r\n",frame_seq_nb2,tagdist_list[taglist_pos]); |
| | | // delay_ms(10); |
| | | } |
| | |
| | | uint16_t report_ancdist[ANC_MAX_NUM],report_ancid[ANC_MAX_NUM]; |
| | | void TagListUpdate(void) |
| | | { |
| | | uint16_t i,j=0,k=0,temp[TAG_NUM_IN_SYS]; |
| | | for(i=0;i<taglist_num;i++) |
| | | { |
| | | uint16_t i,j=0,k=0,temp[TAG_NUM_IN_SYS]; |
| | | for(i=0; i<taglist_num; i++) |
| | | { |
| | | if(tagofflinetime[i]++<REPORT_TAG_KEEPTIMES) |
| | | { |
| | | report_ancid[k]=tagid_list[i]; |
| | | report_ancdist[k++]=tagdist_list[i]; |
| | | } |
| | | if(tagofflinetime[i]++<TAG_KEEPTIMES) |
| | | { |
| | | tagid_list[j]=tagid_list[i]; |
| | | { |
| | | report_ancid[k]=tagid_list[i]; |
| | | report_ancdist[k++]=tagdist_list[i]; |
| | | } |
| | | if(tagofflinetime[i]++<TAG_KEEPTIMES) |
| | | { |
| | | tagid_list[j]=tagid_list[i]; |
| | | tagdist_list[j] = tagdist_list[i]; |
| | | tagofflinetime[j++]=tagofflinetime[i]; |
| | | } |
| | | } |
| | | tagofflinetime[j++]=tagofflinetime[i]; |
| | | } |
| | | } |
| | | report_ancnum = k; |
| | | taglist_num=j; |
| | | taglist_num=j; |
| | | } |
| | | uint16_t CmpTagInList(uint16_t tagid) |
| | | {uint16_t i; |
| | | for(i=0;i<taglist_num;i++) |
| | | { |
| | | if(memcmp(&tagid,&tagid_list[i],2)==0) |
| | | break; |
| | | } |
| | | return i; |
| | | { uint16_t i; |
| | | for(i=0; i<taglist_num; i++) |
| | | { |
| | | if(memcmp(&tagid,&tagid_list[i],2)==0) |
| | | break; |
| | | } |
| | | return i; |
| | | } |
| | | uint32_t temp231; |
| | | void MbxTagUwbRec(void) |
| | | { |
| | | |
| | | u8 send_len,i; |
| | | u16 tempid; |
| | | uint32_t rec_syncid,status_reg; |
| | | uint16_t checksum; |
| | | |
| | | u8 send_len,i; |
| | | u16 tempid; |
| | | uint32_t rec_syncid,status_reg; |
| | | uint16_t checksum; |
| | | uint8_t kk; |
| | | |
| | | |
| | | dwt_setrxtimeout(3000);//设定接收超时时间,0位没有超时时间 |
| | | dwt_rxenable(0);//打开接收 |
| | | dwt_rxenable(0);//打开接收 |
| | | HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET); |
| | | temp231 = dwt_read32bitreg(CHAN_CTRL_ID) ; |
| | | temp231 = dwt_read32bitreg(CHAN_CTRL_ID) ; |
| | | start_time=HAL_LPTIM_ReadCounter(&hlptim1); |
| | | timeout=100; //单位0.1ms |
| | | end_time=start_time+(timeout<<2); |
| | | if(end_time>=32768) |
| | | {end_time-=32768;} |
| | | { |
| | | end_time-=32768; |
| | | } |
| | | 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<end_time+15000) |
| | | break; |
| | | }; |
| | | |
| | | if (status_reg & SYS_STATUS_RXFCG && status_reg!=0xffffffff)//成功接收 |
| | | { |
| | | uint16_t tag_recv_interval; |
| | | float temp_tagpos; |
| | | |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS); |
| | | frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023;//获得接收数据长度 |
| | | |
| | | dwt_readrxdata(rx_buffer, frame_len, 0);//读取接收数据 |
| | | memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2); |
| | | //将收到的tag_id分别写入各次通讯的包中,为多标签通讯服务,防止一次通讯中接收到不同ID标签的数据 |
| | | memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],2); |
| | | switch(rx_buffer[MESSAGE_TYPE_IDX]) |
| | | {uint8_t target_tagi; |
| | | case MBX_REG: |
| | | break; |
| | | case MBX_POLL: |
| | | if(group_id==rx_buffer[GROUP_ID_IDX]) |
| | | { |
| | | |
| | | if (status_reg & SYS_STATUS_RXFCG && status_reg!=0xffffffff)//成功接收 |
| | | { |
| | | uint16_t tag_recv_interval; |
| | | float temp_tagpos; |
| | | |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS); |
| | | frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFL_MASK_1023;//获得接收数据长度 |
| | | |
| | | dwt_readrxdata(rx_buffer, frame_len, 0);//读取接收数据 |
| | | memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2); |
| | | //将收到的tag_id分别写入各次通讯的包中,为多标签通讯服务,防止一次通讯中接收到不同ID标签的数据 |
| | | memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],2); |
| | | switch(rx_buffer[MESSAGE_TYPE_IDX]) |
| | | { uint8_t target_tagi; |
| | | case MBX_REG: |
| | | break; |
| | | case MBX_POLL: |
| | | if(group_id==rx_buffer[GROUP_ID_IDX]) |
| | | { |
| | | // dwt_readdiagnostics(&d1); |
| | | // LOS(&d1); |
| | | // if(firstpath_power<-100) |
| | |
| | | frame_seq_nb2 = rx_buffer[SEQUENCE_IDX]; |
| | | battary = rx_buffer[BATTARY_IDX]; |
| | | button = rx_buffer[BUTTON_IDX]; |
| | | rec_nearbase_num=rx_buffer[FZ_NEARBASENUM_INDEX]; |
| | | memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],2); |
| | | rec_nearbase_num=rx_buffer[FZ_NEARBASENUM_INDEX]; |
| | | memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],2); |
| | | memcpy(&rec_ancidlist,&rx_buffer[FZ_NEARBASEID_INDEX],2*rec_nearbase_num); |
| | | memcpy(&rec_ancdistlist,&rx_buffer[FZ_NEARBASEID_INDEX+2*rec_nearbase_num],2*rec_nearbase_num); |
| | | // memcpy(&rec_antdelay,&rx_buffer[FZ_NEARBASEID_INDEX+rec_nearbase_num*4],2); |
| | | // memcpy(&rec_antdelay,&rx_buffer[FZ_NEARBASEID_INDEX+rec_nearbase_num*4],2); |
| | | uwb_losttimer = 0; |
| | | |
| | | |
| | | taglist_pos=CmpTagInList(tag_id_recv); |
| | | if(taglist_num>=ANC_MAX_NUM) |
| | | break; |
| | | |
| | | for(i=0;i<rec_nearbase_num;i++) |
| | | { |
| | | memcpy(&tempid,&rx_buffer[FZ_NEARBASEID_INDEX+i*2],2); |
| | | if(tempid==dev_id) |
| | | { |
| | | Anchor_RecNearPoll(i); |
| | | |
| | | for(i=0; i<rec_nearbase_num; i++) |
| | | { |
| | | memcpy(&tempid,&rx_buffer[FZ_NEARBASEID_INDEX+i*2],2); |
| | | if(tempid==dev_id) |
| | | { |
| | | Anchor_RecNearPoll(i); |
| | | tagdist_list[taglist_pos]=rec_ancdistlist[i]; |
| | | if(taglist_pos==taglist_num)// taglist_pos==taglist_num 说明这个基站不在当前列表中 |
| | | { //tempid==dev_id 说明基站下发测距报文有这个标签的信息 |
| | | { //tempid==dev_id 说明基站下发测距报文有这个标签的信息 |
| | | taglist_num++; //满足上述两种情况才会添加基站ID进入列表中,否则会出现标签不在基站列表中,标签也不响应的情况 |
| | | tagid_list[taglist_pos] = tag_id_recv; |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | if(i==rec_nearbase_num&&taglist_pos==taglist_num)//当基站不在标签列表中,标签也不在基站列表中时,随机找个时间片回复基站。 |
| | | { |
| | | //Anchor_RecNearPoll(rec_nearbase_num+start_time%2); |
| | | Anchor_RecNearPoll(rec_nearbase_num); |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | |
| | | break; |
| | | } |
| | | |
| | | }else{ |
| | | // printf("debug4 timec:%d timee:%d\r\n",current_time,end_time); |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); |
| | | // dwt_rxenable(0); |
| | | if(i==rec_nearbase_num&&taglist_pos==taglist_num)//当基站不在标签列表中,标签也不在基站列表中时,随机找个时间片回复基站。 |
| | | { |
| | | //Anchor_RecNearPoll(rec_nearbase_num+start_time%2); |
| | | Anchor_RecNearPoll(rec_nearbase_num); |
| | | } |
| | | } |
| | | |
| | | break; |
| | | } |
| | | |
| | | dwt_forcetrxoff(); |
| | | dwt_entersleep(); |
| | | |
| | | } else { |
| | | // printf("debug4 timec:%d timee:%d\r\n",current_time,end_time); |
| | | dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); |
| | | // dwt_rxenable(0); |
| | | } |
| | | |
| | | dwt_forcetrxoff(); |
| | | dwt_entersleep(); |
| | | |
| | | } |
| | | wg_state_enum wg_state = WG_Lost; |
| | |
| | | extern u8 wg_lost_count; |
| | | uint16_t wg_report_freq,wg_report_id; |
| | | #define WG_LOST_SWITCH_THRES 3 |
| | | #define WG_LOST_NOUWB_COUNT 30 |
| | | u8 closeuwb_flag; |
| | | #define WG_LOST_NOUWB_COUNT 60 |
| | | #define DEFAULT_WG_ID 0xFFFF |
| | | u8 lora_jianting_flag = 1; |
| | | void LoraReportFreqPoll(void) |
| | | { |
| | | if(wg_lost_count++>WG_LOST_NOUWB_COUNT) |
| | | { |
| | | closeuwb_flag = 1; |
| | | }else{ |
| | | closeuwb_flag = 0; |
| | | lora_jianting_flag = 0; |
| | | } else { |
| | | lora_jianting_flag = 1; |
| | | } |
| | | if(wg_state==WG_Lost) |
| | | { |
| | | wg_report_id = 0xffff; |
| | | wg_report_id = DEFAULT_WG_ID; |
| | | wg_report_freq = REPORT_MANGE_CHANNEL_FRQ; //如果丢失链接就进入WG管理信道。 |
| | | }else{ |
| | | if(wg_lost_count>WG_LOST_SWITCH_THRES) |
| | | { |
| | | wg_state = WG_Lost; |
| | | } |
| | | } else { |
| | | if(wg_lost_count>WG_LOST_SWITCH_THRES) |
| | | { |
| | | wg_state = WG_Lost; |
| | | } |
| | | } |
| | | } |
| | | static u16 checksum; |
| | |
| | | extern float Height; |
| | | void LoraReportPoll(void) |
| | | { |
| | | // delay_ms(100); |
| | | // delay_ms(100); |
| | | #ifdef _USE_BAR |
| | | GetPressAndHeight(); |
| | | GetPressAndHeight(); |
| | | intheight = Height*100; |
| | | #endif |
| | | #ifdef _SMT_TEST |
| | | printf("气压值:%d",intheight); |
| | | #endif |
| | | |
| | | if(sendcount++>0) |
| | | #endif |
| | | #ifdef _SMT_TEST |
| | | printf("气压值:%d",intheight); |
| | | #endif |
| | | |
| | | TagListUpdate(); |
| | | |
| | | |
| | | LoraReportFreqPoll(); |
| | | flag_getwgresp = 0; |
| | | // wg_report_freq = REPORT_MANGE_CHANNEL_FRQ; |
| | | // wg_report_freq = REPORT_MANGE_CHANNEL_FRQ; |
| | | SwitchLoraSettings(wg_report_freq,REPORT_CHANNEL_SF,g_com_map[LORA_POWER]); |
| | | if(heatbeat_count++>HEATBEAT_UPDATE_TIME && WG_Connected == wg_state) //如果心跳包到达上传时间,并且网关处于链接状态,就上传心跳包 |
| | | if(heatbeat_count++>HEATBEAT_UPDATE_TIME && WG_Connected == wg_state) //如果心跳包到达上传时间,并且网关处于链接状态,就上传心跳包 |
| | | { |
| | | heatbeat_count = 0; |
| | | LoraHeartBeartPoll(); |
| | |
| | | lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_TAGMSGTOWG; |
| | | lora_sendbuffer[MSG_LENGTH] = 4*report_ancnum+ANCID_IDX; |
| | | #endif |
| | | |
| | | memcpy(&lora_sendbuffer[SOURCE_ID_IDX],&g_com_map[DEV_ID],2); |
| | | memcpy(&lora_sendbuffer[DEST_ID_IDX],&wg_report_id,2); |
| | | lora_sendbuffer[SEQNUM_IDX] = seq_num++; |
| | |
| | | lora_sendbuffer[GPS_SPOWER_IDX] = gps_signalpower; |
| | | lora_sendbuffer[GPS_CHAFENLINGQI] = gps_chafenlingqi; |
| | | #endif |
| | | for(u16 i=0; i<report_ancnum-1; i++) |
| | | { |
| | | for(u16 j=0; j<report_ancnum-1-i; j++) |
| | | { |
| | | if(report_ancdist[j]>report_ancdist[j+1]) |
| | | { |
| | | u16 id,dist; |
| | | u8 bat; |
| | | id = report_ancid[j]; |
| | | dist = report_ancdist[j]; |
| | | report_ancid[j] = report_ancid[j+1]; |
| | | report_ancdist[j] = report_ancdist[j+1]; |
| | | report_ancid[j+1] = id; |
| | | report_ancdist[j+1] = dist; |
| | | } |
| | | } |
| | | } |
| | | memcpy(&lora_sendbuffer[BAR_HEIGHT_IDX],&bar_height,2); |
| | | lora_sendbuffer[ANCNUM_IDX] = report_ancnum; |
| | | memcpy(&lora_sendbuffer[ANCID_IDX],report_ancid,report_ancnum*2); |
| | |
| | | checksum = Checksum_u16(lora_sendbuffer,4*report_ancnum+ANCID_IDX); |
| | | memcpy(&lora_sendbuffer[ANCID_IDX+report_ancnum*4],&checksum,2); |
| | | Radio.Send(lora_sendbuffer,ANCID_IDX+report_ancnum*4+2); |
| | | |
| | | LED_TB_ON; |
| | | bat_percent=Get_Battary(); |
| | | |
| | | bat_percent=Get_Battary(); |
| | | |
| | | } |
| | | uint8_t Lora_send_flag; |
| | | void LedTask(void) |
| | | { |
| | | if(report_ancnum>0) |
| | | { |
| | | LED2_TB_ON; |
| | | #ifdef _SMT_TEST |
| | | // tflag1 = 1; |
| | | printf("测距值%d\r\n",tagdist_list[0]); |
| | | #endif |
| | | } |
| | | if(wg_lost_count<=1) |
| | | { |
| | | #ifdef _SMT_TEST |
| | | // tflag2 = 1; |
| | | printf("网关通讯正常\r\n"); |
| | | #endif |
| | | LED_TB_ON; |
| | | } |
| | | if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin)) |
| | | { |
| | | if(bat_percent<15) |
| | | { |
| | | LED_TR_ON; |
| | | } else { |
| | | LED2_TB_ON; |
| | | } |
| | | } |
| | | |
| | | } |
| | | u16 lora_send_count; |
| | | void Lora_Send_Poll(void) |
| | | { |
| | | if(Lora_send_flag) |
| | | { |
| | | if(lora_send_count++%10==0||lora_jianting_flag) |
| | | { |
| | | Lora_send_flag=0; |
| | | LedTask(); |
| | | LoraReportPoll(); |
| | | LED_TB_OFF; |
| | | LED_TR_OFF; |
| | | LED2_TB_OFF; |
| | | LED2_TR_OFF; |
| | | } |
| | | } |
| | | } |