From 4864dd7a1bf69e16045c18bc19220b3ce9f55c72 Mon Sep 17 00:00:00 2001 From: zhyinch <zhyinch@gmail.com> Date: 星期三, 17 六月 2020 23:49:37 +0800 Subject: [PATCH] V1.18华星测试基站防撞版本 --- 源码/核心板/Src/application/dw_app.c | 163 ++++++++++++++++++++++++++++++------------------------ 1 files changed, 91 insertions(+), 72 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 1b499a2..ac79717 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" @@ -251,11 +251,43 @@ // change_base_count=0; // } //} +uint8_t GetRandomValue(void) +{ + uint8_t random_value=0,temp_adc,i; + for(i=0;i<8;i++) + { + temp_adc=Get_ADC_Value(); + random_value=random_value|((temp_adc&0x01)<<i); + } + return random_value; +} +u8 GetRandomSlotPos(uint32_t emptyslot) +{ + u8 i,temp_value; + temp_value = GetRandomValue(); + for(i=temp_value%32;i<max_slotpos;i++) + { + if(((emptyslot>>i)&0x1)==0) + { + return i; + } + } + + for(i=1;i<max_slotpos;i++) + { + if(((emptyslot>>i)&0x1)==0) + { + return i; + } + } +return max_slotpos-1; +} u8 test=0; u8 anclost_times=0; -u8 exsistbase_list[MAX_NEARBASE_NUM],report_num,get_newbase=0;; +u8 exsistbase_list[MAX_NEARBASE_NUM],report_num,get_newbase=0; u16 temp_sync_timer1,temp_sync_timer2; -void NearPoll(void) +u32 rec_tagpos_binary; + void NearPoll(void) { static u8 mainbase_lost_count=0,flag_finalsend,flag_rxon; uint32_t temp1,temp2,dw_systime; @@ -264,7 +296,6 @@ u32 start_poll; u8 i,j,getsync_flag=0,timeout; // printf("%d",sync_timer); - dwt_forcetrxoff(); dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS); //设置发送后开启接收,并设定延迟时间 dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS); tag_succ_times = 0; @@ -347,11 +378,15 @@ temp_sync_timer2=sync_timer; memcpy(&temp_sync_timer1,&rx_buffer[ANCTIMEMS],2); memcpy(&tmp_time,&rx_buffer[ANCTIMEUS],2); - sync_timer=temp_sync_timer1; exsistbase_list[0]=KEEP_TIMES; + + if(g_com_map[DEV_ROLE]!=0) + { + sync_timer=temp_sync_timer1; //// memcpy(&tagslotpos,&rx_buffer[TAGSLOTPOS],2); // - tmp_time=tmp_time+650; + + tmp_time=tmp_time+650; if(tmp_time>999) { tmp_time-=999; @@ -360,13 +395,13 @@ {sync_timer=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]=10; + exsistbase_list[rec_nearbasepos]=KEEP_TIMES; memcpy(&temp_dist,&rx_buffer[DIST_IDX],4); nearbase_distlist[rec_nearbasepos]=temp_dist; @@ -383,23 +418,10 @@ tx_nearfinal_msg[MESSAGE_TYPE_IDX]=NEAR_FINAL; dwt_writetxdata(28+nearbase_num*4, tx_nearfinal_msg, 0);//将发送数据写入DW1000 dwt_writetxfctrl(28+nearbase_num*4, 0);//设定发送数据长度 - if(temp_dist!=0x1ffff) - { - #ifndef USART_INTEGRATE_OUTPUT - usart_send[2] = 1;//正常模式 - usart_send[3] = 17;//数据段长度 - usart_send[4] = frame_seq_nb;//数据段长度 - memcpy(&usart_send[5],&dev_id,2); - memcpy(&usart_send[7],&rec_nearbaseid,2); - memcpy(&usart_send[9],&rx_buffer[DIST_IDX],4); - usart_send[13] = battary; - usart_send[14] = button; - checksum = Checksum_u16(&usart_send[2],17); - memcpy(&usart_send[19],&checksum,2); - UART_PushFrame(usart_send,21); - #endif - } - //result=dwt_starttx(DWT_START_TX_DELAYED);//设定为延迟发送 + memcpy(&rec_tagpos_binary,&rx_buffer[NEARMSG_EMPTYSLOTPOS_INDEX],4); + + + }else{ rec_nearbasepos=FindNearBasePos(rec_nearbaseid); if(rec_nearbasepos>=last_nearbase_num) //发现新的基站 @@ -418,6 +440,8 @@ dwt_writetxdata(28+nearbase_num*4, tx_nearfinal_msg, 0);//将发送数据写入DW1000 dwt_writetxfctrl(28+nearbase_num*4, 0);//设定发送数据长度 LED0_BLINK; +// memcpy(&rec_tagpos_binary,&rx_buffer[NEARMSG_EMPTYSLOTPOS_INDEX],4); +// tagslotpos=GetRandomSlotPos(rec_tagpos_binary); if(temp_dist!=0x1ffff) { #ifndef USART_INTEGRATE_OUTPUT @@ -460,22 +484,22 @@ 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); - } - tyncpoll_time=(random_value%max_slotpos)*slottime; +// 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]; @@ -526,13 +550,13 @@ } // printf("%d,%d",temp_sync_timer2,temp_sync_timer1); - #ifdef USART_INTEGRATE_OUTPUT - 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); - UART_PushFrame(usart_send,6+report_num*6); - #endif +// #ifdef USART_INTEGRATE_OUTPUT +// 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); +// UART_PushFrame(usart_send,6+report_num*6); +// #endif // if(mainbase_lost_count>5) // { // //tag_state=DISCPOLL; @@ -549,12 +573,9 @@ // anclost_times=0; // } - dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); - - + dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR| SYS_STATUS_TXFRS |SYS_STATUS_RXFCG); } -u8 nearmsg_mainbase=0; -u32 rec_tagpos_binary; +u8 nearmsg_mainbase=0,rec_tagpos_emptylist[32]; void GetNearMsg(void) { u32 start_poll,frame_len; @@ -591,18 +612,22 @@ { next_nearbase_num=rx_buffer[NEARBASENUM_INDEX]; memcpy(&rec_tagpos_binary,&rx_buffer[NEARMSG_EMPTYSLOTPOS_INDEX],4); - tagslotpos=rx_buffer[TAGSLOTPOS]; + tagslotpos=GetRandomSlotPos(rec_tagpos_binary); + //tagslotpos=rx_buffer[TAGSLOTPOS]; memcpy(nearbaseid_list,&rx_buffer[NEARBASEID_INDEX],next_nearbase_num*2); slottime=ceil((nearbase_num+2)*0.3)+1; tyncpoll_time=tagslotpos*slottime; tag_state=NEARPOLL; } + }else{ + tyncpoll_time = (tagslotpos--%max_slotpos)*slottime; } } void Tag_App(void)//发送模式(TAG标签) { //LED0_ON; + dwt_forcetrxoff(); g_Resttimer=0; switch(tag_state) { @@ -610,13 +635,11 @@ break; case GETNEARMSG: - GetNearMsg(); + GetNearMsg(); break; case NEARPOLL: NearPoll(); break; - - } } @@ -813,6 +836,7 @@ misdist_num[taglist_pos]=0; tagdist_list[taglist_pos] = hex_dist; his_dist[taglist_pos]=hex_dist; + g_Tagdist[taglist_pos]=hex_dist; #ifndef USART_INTEGRATE_OUTPUT usart_send[2] = 1;//正常模式 usart_send[3] = 17;//数据段长度 @@ -836,12 +860,10 @@ } } } - }else{ //printf("%x/n",status_reg); dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); } - } uint32_t current_syncid=0xffffffff,synclost_timer; extern u8 flag_syncbase; @@ -862,25 +884,26 @@ 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; }; if (status_reg & SYS_STATUS_RXFCG)//成功接收 { u16 tag_recv_interval; float temp_tagpos; + + g_Resttimer=0; dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG);//清除标志位 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标签的数据 - //tag_id_recv = rx_buffer[TAG_ID_IDX]; + tag_id_recv = rx_buffer[TAG_ID_IDX]; switch(rx_buffer[MESSAGE_TYPE_IDX]) { // case POLL: // if (anchor_type == rx_buffer[ANC_TYPE_IDX]) // Anchor_RecPoll(); // break; - case SYNC: - + case SYNC: memcpy(&rec_syncid,&rx_buffer[ANCHOR_ID_IDX],4); if(rec_syncid<current_syncid) { @@ -921,10 +944,8 @@ } break; - case NEAR_POLL: - + case NEAR_POLL: memcpy(&tag_id_recv,&rx_buffer[TAG_ID_IDX],2); - memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2); taglist_pos=CmpTagInList(tag_id_recv); if(taglist_pos==taglist_num) { @@ -936,10 +957,16 @@ new_tagid=0; } tagofflinetime[taglist_pos]=0; - temp_tagpos=round((float)(sync_timer%g_com_map[COM_INTERVAL])/slottime); + temp_tagpos=round((float)(sync_timer%g_com_map[COM_INTERVAL])/slottime); tagpos_rec[(u8)temp_tagpos]=1; rec_nearbase_num=rx_buffer[NEARBASENUM_INDEX]; + if(rec_nearbase_num>ancidlist_num) + { + ancidlist_num=rec_nearbase_num; + memcpy(ancidlist_rec,&rx_buffer[NEARBASEID_INDEX],rec_nearbase_num*2); + } + for(i=0;i<rec_nearbase_num;i++) { memcpy(&tempid,&rx_buffer[NEARBASEID_INDEX+i*2],2); @@ -955,19 +982,11 @@ seize_anchor=1; //抢占anchor Anchor_RecNearPoll(i); } - if(rec_nearbase_num>ancidlist_num) - { - ancidlist_num=rec_nearbase_num; - memcpy(ancidlist_rec,&rx_buffer[NEARBASEID_INDEX],rec_nearbase_num*2); - } + - break; - - } - - } - else - { + break; + } + }else{ dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR); } } -- Gitblit v1.9.3