广播命令根据数据量自动计算发送数据的窗口时间
This commit is contained in:
@@ -280,3 +280,5 @@
|
|||||||
添加广播命令的解析和回复指令
|
添加广播命令的解析和回复指令
|
||||||
2023.12.5
|
2023.12.5
|
||||||
使用定时器来精确控制数据发送的时间间隙,保证和其他小板不冲突
|
使用定时器来精确控制数据发送的时间间隙,保证和其他小板不冲突
|
||||||
|
2023.12.7
|
||||||
|
广播命令根据数据量自动计算发送数据的窗口时间
|
||||||
|
@@ -201,6 +201,16 @@ array_def *protu_encode2(protu_def *p,array_def *data)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 计算在第n为之前,bit为1的个数
|
||||||
|
static int calc_bit_num(int bits,int n)
|
||||||
|
{
|
||||||
|
int num=0;
|
||||||
|
for(int i=0;i<n;i++){
|
||||||
|
if(bits&(1<<i))
|
||||||
|
num++;
|
||||||
|
}
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
|
||||||
// 小板协议解码
|
// 小板协议解码
|
||||||
array_def *protm_decode(protu_def *p,array_def *data)
|
array_def *protm_decode(protu_def *p,array_def *data)
|
||||||
@@ -256,6 +266,8 @@ array_def *protm_decode(protu_def *p,array_def *data)
|
|||||||
if((dst_addrs&(1<<(self_addr-1)))!=0){
|
if((dst_addrs&(1<<(self_addr-1)))!=0){
|
||||||
p->silent=1;
|
p->silent=1;
|
||||||
p->timer->write(p->timer,0);
|
p->timer->write(p->timer,0);
|
||||||
|
p->rank=calc_bit_num(dst_addrs,self_addr-1);
|
||||||
|
p->num=calc_bit_num(dst_addrs,32);
|
||||||
arr_remove(r,0,3);
|
arr_remove(r,0,3);
|
||||||
}else{
|
}else{
|
||||||
arr_delete(r);
|
arr_delete(r);
|
||||||
@@ -263,6 +275,8 @@ array_def *protm_decode(protu_def *p,array_def *data)
|
|||||||
str_set(p->str_err,"bordcast without this.");
|
str_set(p->str_err,"bordcast without this.");
|
||||||
}
|
}
|
||||||
}else{
|
}else{
|
||||||
|
p->rank=dst-1;
|
||||||
|
p->num=20;// 最多支持20个设备
|
||||||
p->silent=0;
|
p->silent=0;
|
||||||
}
|
}
|
||||||
return r;
|
return r;
|
||||||
@@ -274,7 +288,7 @@ array_def *protm_encode(protu_def *p,array_def *data)
|
|||||||
{
|
{
|
||||||
array_def *t=arr_creat();
|
array_def *t=arr_creat();
|
||||||
param_check(t);
|
param_check(t);
|
||||||
if(p->silent==0){
|
// if(p->silent==0){
|
||||||
uint16_t len=arr_length(data)+10;
|
uint16_t len=arr_length(data)+10;
|
||||||
arr_append(t,'Y');
|
arr_append(t,'Y');
|
||||||
arr_append(t,'e');
|
arr_append(t,'e');
|
||||||
@@ -287,10 +301,10 @@ array_def *protm_encode(protu_def *p,array_def *data)
|
|||||||
arr_append(t,p->cmd_no>>8);
|
arr_append(t,p->cmd_no>>8);
|
||||||
arr_appends_from(t,data);
|
arr_appends_from(t,data);
|
||||||
arr_append(t,crc_crc8(arr_data(t),arr_length(t)));
|
arr_append(t,crc_crc8(arr_data(t),arr_length(t)));
|
||||||
}else{
|
// }else{
|
||||||
// 广播命令只需回复本机地址
|
// // 广播命令只需回复本机地址
|
||||||
arr_append(t,elec_local_addr());
|
// arr_append(t,elec_local_addr());
|
||||||
}
|
// }
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -299,14 +299,23 @@ static send_pkt_def g_send_pkt;
|
|||||||
static void protu_send_ontime(protu_def *p,send_pkt_def *s)
|
static void protu_send_ontime(protu_def *p,send_pkt_def *s)
|
||||||
{
|
{
|
||||||
uint32_t tick=p->timer->read(p->timer);
|
uint32_t tick=p->timer->read(p->timer);
|
||||||
int delay=tick%100;
|
// 根据返回的数据长度计算发送需要的时间,添加1ms的余量
|
||||||
int gap=5*(elec_local_addr()-1);
|
// 根据协议,每个指令从机的返回数据长度等长,所以需要的时间窗口也相等
|
||||||
|
int wnd_tick=((arr_length(s->t)+15)/16+1);
|
||||||
|
int delay=tick%(wnd_tick*(p->num));
|
||||||
|
int gap=p->rank*wnd_tick;
|
||||||
if(delay<=gap){
|
if(delay<=gap){
|
||||||
delay=gap-delay;
|
delay=gap-delay;
|
||||||
}else{
|
}else{
|
||||||
delay=gap+100-delay;
|
delay=gap+(wnd_tick*(p->num))-delay;
|
||||||
|
}
|
||||||
|
if(p->silent!=0){
|
||||||
|
// 广播命令在指定时间窗口发送
|
||||||
|
later_execute(protu_send_later,s,delay);
|
||||||
|
}else{
|
||||||
|
// 单播命令直接发送
|
||||||
|
protu_send_later(s);
|
||||||
}
|
}
|
||||||
later_execute(protu_send_later,s,delay);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@@ -34,7 +34,9 @@ struct _protu_def{
|
|||||||
uint8_t cmd;
|
uint8_t cmd;
|
||||||
int is_big_data;
|
int is_big_data;
|
||||||
codec_item *codec;
|
codec_item *codec;
|
||||||
int silent;// 此项为1,不返回数据
|
int silent;// 此项为1则是广播命令
|
||||||
|
int rank;// 本机在广播命令中的排行,从0开始
|
||||||
|
int num;// 本次广播命令包含的从机数量
|
||||||
timer_def *timer;
|
timer_def *timer;
|
||||||
};
|
};
|
||||||
typedef struct _protu_def protu_def;
|
typedef struct _protu_def protu_def;
|
||||||
|
@@ -166,7 +166,7 @@ static void check_submit_later(void *t)
|
|||||||
array_def *ret=elec_check_result();
|
array_def *ret=elec_check_result();
|
||||||
if(ret!=0){
|
if(ret!=0){
|
||||||
emit tran_send_signal(s->tran,s->cmd,ret);
|
emit tran_send_signal(s->tran,s->cmd,ret);
|
||||||
s->submit_delay+=elec_local_addr()*3;
|
// s->submit_delay=150;
|
||||||
}else{
|
}else{
|
||||||
//while(1);
|
//while(1);
|
||||||
}
|
}
|
||||||
@@ -187,7 +187,7 @@ static void check_later(void *t)
|
|||||||
{
|
{
|
||||||
s->submit_times=10;
|
s->submit_times=10;
|
||||||
s->submit_running=1;
|
s->submit_running=1;
|
||||||
s->submit_delay=elec_local_addr()*10+200;
|
s->submit_delay=150;
|
||||||
later_execute(check_submit_later,s,s->submit_delay);
|
later_execute(check_submit_later,s,s->submit_delay);
|
||||||
}else{
|
}else{
|
||||||
s->cmd=0;
|
s->cmd=0;
|
||||||
|
Reference in New Issue
Block a user