2017-06-16 20:31:54 -03:00
|
|
|
########################################
|
|
|
|
#
|
|
|
|
# BARRIER-RELATED FUNCTIONS
|
|
|
|
#
|
|
|
|
########################################
|
|
|
|
|
|
|
|
#
|
|
|
|
# Constants
|
|
|
|
#
|
2018-09-06 13:47:38 -03:00
|
|
|
BARRIER_TIMEOUT = 200 # in steps
|
2017-09-06 00:48:50 -03:00
|
|
|
BARRIER_VSTIG = 80
|
2017-09-05 01:34:59 -03:00
|
|
|
timeW = 0
|
2017-09-06 00:48:50 -03:00
|
|
|
barrier = nil
|
2018-09-11 01:02:20 -03:00
|
|
|
hvs = 0;
|
|
|
|
|
2017-06-16 20:31:54 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Sets a barrier
|
|
|
|
#
|
2017-09-06 00:48:50 -03:00
|
|
|
function barrier_create() {
|
2017-09-05 01:34:59 -03:00
|
|
|
# reset
|
|
|
|
timeW = 0
|
|
|
|
# create barrier vstig
|
2017-09-06 00:48:50 -03:00
|
|
|
#log("---> Prev. br. ", barrier, " ", BARRIER_VSTIG)
|
|
|
|
if(barrier!=nil) {
|
|
|
|
barrier=nil
|
2018-09-11 01:02:20 -03:00
|
|
|
if(hvs) {
|
|
|
|
BARRIER_VSTIG = BARRIER_VSTIG -1
|
|
|
|
hvs = 0
|
|
|
|
}else{
|
|
|
|
BARRIER_VSTIG = BARRIER_VSTIG +1
|
|
|
|
hvs = 1
|
|
|
|
}
|
2017-09-06 00:48:50 -03:00
|
|
|
}
|
|
|
|
#log("---> New. br. ", barrier, " ", BARRIER_VSTIG)
|
|
|
|
barrier = stigmergy.create(BARRIER_VSTIG)
|
2017-09-05 01:34:59 -03:00
|
|
|
}
|
|
|
|
|
2018-09-06 13:47:38 -03:00
|
|
|
function barrier_set(threshold, transf, resumef, bc) {
|
2017-06-16 20:31:54 -03:00
|
|
|
statef = function() {
|
2018-09-06 13:47:38 -03:00
|
|
|
barrier_wait(threshold, transf, resumef, bc);
|
2017-06-16 20:31:54 -03:00
|
|
|
}
|
2017-12-20 15:33:23 -04:00
|
|
|
BVMSTATE = "BARRIERWAIT"
|
2017-09-06 00:48:50 -03:00
|
|
|
barrier_create()
|
2017-06-16 20:31:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Make yourself ready
|
|
|
|
#
|
2018-09-06 13:47:38 -03:00
|
|
|
function barrier_ready(bc) {
|
2017-11-27 23:55:32 -04:00
|
|
|
#log("BARRIER READY -------")
|
2018-09-06 13:47:38 -03:00
|
|
|
barrier.put(id, bc)
|
2017-09-05 01:34:59 -03:00
|
|
|
barrier.put("d", 0)
|
2017-06-16 20:31:54 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Executes the barrier
|
|
|
|
#
|
2018-09-06 13:47:38 -03:00
|
|
|
function barrier_wait(threshold, transf, resumef, bc) {
|
2018-09-08 20:15:46 -03:00
|
|
|
if(barrier==nil) #failsafe
|
|
|
|
barrier_create()
|
|
|
|
|
2018-09-06 13:47:38 -03:00
|
|
|
barrier.put(id, bc)
|
2017-08-30 18:22:37 -03:00
|
|
|
barrier.get(id)
|
2018-09-06 13:47:38 -03:00
|
|
|
log("--->BS: ", barrier.size(), " / ", threshold, " (", BARRIER_VSTIG, " - ", barrier.get("d"), ") t= ", timeW)
|
2018-09-08 20:15:46 -03:00
|
|
|
if(barrier.size()-1 >= threshold or barrier.get("d") == 1) {
|
|
|
|
if(barrier_allgood(barrier,bc)) {
|
|
|
|
barrier.put("d", 1)
|
|
|
|
timeW = 0
|
|
|
|
BVMSTATE = transf
|
|
|
|
} else
|
|
|
|
barrier.put("d", 0)
|
2018-09-06 13:47:38 -03:00
|
|
|
}
|
2018-09-08 20:15:46 -03:00
|
|
|
|
|
|
|
if(timeW >= BARRIER_TIMEOUT) {
|
2017-09-06 00:48:50 -03:00
|
|
|
log("------> Barrier Timeout !!!!")
|
2018-09-06 13:47:38 -03:00
|
|
|
barrier = nil
|
2017-09-05 01:34:59 -03:00
|
|
|
timeW = 0
|
2017-12-22 17:48:39 -04:00
|
|
|
BVMSTATE = resumef
|
2018-09-06 13:47:38 -03:00
|
|
|
} else if(timeW % 100 == 0 and bc > 0)
|
|
|
|
neighbors.broadcast("cmd", bc)
|
2017-09-06 00:48:50 -03:00
|
|
|
|
2017-06-16 20:31:54 -03:00
|
|
|
timeW = timeW+1
|
2017-08-09 20:23:42 -03:00
|
|
|
}
|
2018-09-06 13:47:38 -03:00
|
|
|
|
2018-09-08 20:15:46 -03:00
|
|
|
# Check all members state
|
2018-09-06 13:47:38 -03:00
|
|
|
function barrier_allgood(barrier, bc) {
|
|
|
|
barriergood = 1
|
|
|
|
barrier.foreach(
|
|
|
|
function(key, value, robot){
|
|
|
|
#log("VS entry : ", key, " ", value, " ", robot)
|
|
|
|
if(value != bc and key != "d"){
|
|
|
|
barriergood = 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return barriergood
|
|
|
|
}
|