ROSBuzz_MISTLab/buzz_scripts/include/act/barrier.bzz

111 lines
2.3 KiB
Plaintext
Raw Normal View History

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
BARRIER_VSTIG = 80
timeW = 1
barrier = nil
hvs = 0;
2017-06-16 20:31:54 -03:00
#
# Sets a barrier
#
function barrier_create() {
# reset
timeW = 1
# create barrier vstig
#log("---> Prev. br. ", barrier, " ", BARRIER_VSTIG)
#if(barrier!=nil) {
# barrier=nil
#if(hvs) {
# BARRIER_VSTIG = BARRIER_VSTIG -1
# hvs = 0
#}else{
# BARRIER_VSTIG = BARRIER_VSTIG +1
# hvs = 1
#}
#}
#log("---> New. br. ", barrier, " ", BARRIER_VSTIG)
if(barrier==nil)
barrier = stigmergy.create(BARRIER_VSTIG)
}
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"
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) {
#log("BARRIER READY -------")
2018-09-06 13:47:38 -03:00
barrier.put(id, bc)
barrier.put("d", 0)
barrier.put("n", 1)
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)
if(barrier.get("n")<threshold)
barrier.put("n", threshold)
if(threshold>1) {
neighbors.foreach(function (nei,data){
barrier.get(nei)
})
}
# Not to miss any RC inputs, for waypoints, potential and deploy states
check_rc_wp()
#log("--->BS: ", barrier.size(), " / ", threshold, " (", BARRIER_VSTIG, " - ", barrier.get("d"), ") t= ", timeW)
if(barrier.size()-2 >= barrier.get("n")) {
2018-09-08 20:15:46 -03:00
if(barrier_allgood(barrier,bc)) {
barrier.put("d", 1)
timeW = 0
BVMSTATE = transf
} else
barrier.put("d", 0)
}
2018-09-08 20:15:46 -03:00
if(timeW >= BARRIER_TIMEOUT) {
log("------> Barrier Timeout !!!!")
#barrier = nil
timeW = 0
2017-12-22 17:48:39 -04:00
BVMSTATE = resumef
} else if(timeW % 10 == 0 and bc > 0)
2018-09-06 13:47:38 -03:00
neighbors.broadcast("cmd", bc)
2017-06-16 20:31:54 -03:00
timeW = timeW+1
}
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
2018-11-06 05:01:16 -04:00
barrier.foreach(function(key, value, robot){
barrier.get(key)
#log("VS entry : ", key, " ", value, " ", robot)
2018-11-06 05:01:16 -04:00
if(key == "d"){
if(value == 1)
return 1
} else if(key!="n" and value != bc)
2018-09-06 13:47:38 -03:00
barriergood = 0
2018-11-06 05:01:16 -04:00
})
2018-09-06 13:47:38 -03:00
return barriergood
}