adapted sync algo to robots
This commit is contained in:
parent
9f75bd989e
commit
7ab97b4fce
|
@ -67,7 +67,7 @@ function barrier_wait(threshold, transf, resumef, bc) {
|
|||
barrier = nil
|
||||
timeW = 0
|
||||
BVMSTATE = resumef
|
||||
} else if(timeW % 10 == 0 and bc > 0)
|
||||
} else if(timeW % 50 == 0 and bc > 0)
|
||||
neighbors.broadcast("cmd", bc)
|
||||
|
||||
timeW = timeW+1
|
||||
|
|
|
@ -5,7 +5,8 @@ logical_time = 0
|
|||
# sync algo. constants
|
||||
TIME_JUMP_THR = 5
|
||||
TIME_TO_FORGET = 20
|
||||
TIME_TO_SYNC = 100
|
||||
TIME_TO_SYNC = 200
|
||||
COM_DELAY = 3
|
||||
# table to store neighbor time data
|
||||
time_nei_table = {}
|
||||
# Algo. global parameters
|
||||
|
@ -18,20 +19,23 @@ sync_timer = 0
|
|||
function init_time_sync(){
|
||||
neighbors.listen("time_sync",
|
||||
function(vid, value, rid) {
|
||||
log(" TIME SYNC Got (", vid, ",", value, ") #", rid)
|
||||
var msg = string.split(value,",")
|
||||
var msg_time = string.toint(msg[0])
|
||||
var msg_max = string.toint(msg[1])
|
||||
#log("msg: 1: ", msg_time, " 2: ", msg_max )
|
||||
diffMaxLogical = math.max(diffMaxLogical,msg_max-logical_time)
|
||||
var time_offset = msg_time - logical_time
|
||||
if(math.abs(time_offset) > math.abs(syncError)) syncError = time_offset
|
||||
if(time_offset > TIME_JUMP_THR){
|
||||
logical_time = logical_time + time_offset
|
||||
diffMaxLogical = math.max(diffMaxLogical-time_offset,0)
|
||||
jumped = 1
|
||||
if(value != nil){
|
||||
log(" TIME SYNC Got (", vid, ",", value.time , " , ", value.max, ") #", rid)
|
||||
var msg_time = value.time
|
||||
var msg_max = value.max
|
||||
#log("msg: 1: ", msg_time, " 2: ", msg_max )
|
||||
if(msg_time != nil and msg_max != nil){
|
||||
diffMaxLogical = math.max(diffMaxLogical,msg_max-logical_time)
|
||||
var time_offset = msg_time - logical_time
|
||||
if(math.abs(time_offset) > math.abs(syncError)) syncError = time_offset
|
||||
if(time_offset > TIME_JUMP_THR){
|
||||
logical_time = logical_time + time_offset
|
||||
diffMaxLogical = math.max(diffMaxLogical-time_offset,0)
|
||||
jumped = 1
|
||||
}
|
||||
time_nei_table[rid] = { .time=msg_time, .age=0, .max=msg_max}
|
||||
}
|
||||
}
|
||||
time_nei_table[rid] = { .time=msg_time, .age=0, .max=msg_max}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -44,36 +48,39 @@ function step_time_sync(){
|
|||
log(" SYNC ALGO ACTIVE time:", sync_timer)
|
||||
cnt = 0
|
||||
avg_offset = 0
|
||||
foreach(time_nei_table, function(key, value) {
|
||||
if(value != nil){
|
||||
#log("ForEach neigh : id ", key, " time ", value.time, " , age ", value.age, " , diffmax ", value.max)
|
||||
var local_offset = value.time - logical_time - value.age
|
||||
if(local_offset > 0){
|
||||
avg_offset = avg_offset + 1 * local_offset
|
||||
cnt = cnt + 1
|
||||
}
|
||||
else{
|
||||
if(math.abs(local_offset)<TIME_JUMP_THR){
|
||||
avg_offset = avg_offset + local_offset
|
||||
cnt = cnt + 1
|
||||
}
|
||||
}
|
||||
if(size(time_nei_table) > 0){
|
||||
foreach(time_nei_table, function(key, value) {
|
||||
if(value.time != 0){
|
||||
#log("ForEach neigh : id ", key, " time ", value.time, " , age ", value.age, " , diffmax ", value.max)
|
||||
var local_offset = value.time - logical_time - value.age
|
||||
if(local_offset > 0){
|
||||
avg_offset = avg_offset + 1 * local_offset
|
||||
cnt = cnt + 1
|
||||
}
|
||||
else{
|
||||
if(math.abs(local_offset)<TIME_JUMP_THR){
|
||||
avg_offset = avg_offset + local_offset
|
||||
cnt = cnt + 1
|
||||
}
|
||||
}
|
||||
|
||||
if(value.age > TIME_TO_FORGET)
|
||||
time_nei_table[key] = nil
|
||||
value.age = value.age + 1
|
||||
if(value.age > TIME_TO_FORGET)
|
||||
value.time = 0
|
||||
|
||||
value.age = value.age + 1
|
||||
}
|
||||
})
|
||||
if(cnt > 0 and jumped != 1){
|
||||
var correction = math.ceil(avg_offset / (cnt + 1) )
|
||||
if(math.abs(correction) < TIME_JUMP_THR){
|
||||
logical_time = logical_time + correction
|
||||
}
|
||||
}
|
||||
})
|
||||
if(cnt > 0 and jumped != 1){
|
||||
var correction = math.ceil(avg_offset / (cnt + 1) )
|
||||
if(math.abs(correction) < TIME_JUMP_THR){
|
||||
logical_time = logical_time + correction
|
||||
}
|
||||
}
|
||||
|
||||
jumped = 0
|
||||
syncError=0
|
||||
var mstr = string.concat(string.tostring(logical_time + 1),",",string.tostring(logical_time + 1 + diffMaxLogical))
|
||||
var mstr = {.time = (logical_time + COM_DELAY) , .max = (logical_time + COM_DELAY + diffMaxLogical) }
|
||||
#string.concat(string.tostring(logical_time + 1),",",string.tostring(logical_time + 1 + diffMaxLogical))
|
||||
neighbors.broadcast("time_sync",mstr)
|
||||
}
|
||||
log("Logical time now ", logical_time)
|
||||
|
|
Loading…
Reference in New Issue