2016-12-23 15:21:52 -04:00
|
|
|
|
|
|
|
########################################
|
|
|
|
#
|
|
|
|
# BARRIER-RELATED FUNCTIONS
|
|
|
|
#
|
|
|
|
########################################
|
|
|
|
|
|
|
|
#
|
|
|
|
# Constants
|
|
|
|
#
|
|
|
|
BARRIER_VSTIG = 1
|
|
|
|
ROBOTS = 2 # number of robots in the swarm
|
|
|
|
|
|
|
|
#
|
|
|
|
# Sets a barrier
|
|
|
|
#
|
|
|
|
function barrier_set(threshold, transf) {
|
|
|
|
statef = function() {
|
|
|
|
barrier_wait(threshold, transf);
|
|
|
|
}
|
|
|
|
barrier = stigmergy.create(BARRIER_VSTIG)
|
|
|
|
statestr = "barrier"
|
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Make yourself ready
|
|
|
|
#
|
|
|
|
function barrier_ready() {
|
2016-12-23 17:36:31 -04:00
|
|
|
|
2016-12-23 15:21:52 -04:00
|
|
|
barrier.put(id, 1)
|
2016-12-23 17:36:31 -04:00
|
|
|
print ("before put")
|
|
|
|
barrier.get(id)
|
|
|
|
print("barrier put : ")
|
2016-12-23 15:21:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#
|
|
|
|
# Executes the barrier
|
|
|
|
#
|
2016-12-23 17:36:31 -04:00
|
|
|
function table_print(t) {
|
|
|
|
foreach(t, function(key, value) {
|
|
|
|
log(key, " -> ", value)
|
|
|
|
})
|
|
|
|
}
|
2016-12-23 15:21:52 -04:00
|
|
|
function barrier_wait(threshold, transf) {
|
2016-12-23 18:48:01 -04:00
|
|
|
#table_print(barrier)
|
2016-12-23 15:21:52 -04:00
|
|
|
barrier.get(id)
|
|
|
|
extradbg = barrier.size()
|
|
|
|
if(barrier.size() >= threshold) {
|
|
|
|
barrier = nil
|
|
|
|
transf()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# flight status
|
|
|
|
|
|
|
|
function idle() {
|
2016-12-23 18:48:01 -04:00
|
|
|
statef=idle
|
2016-12-23 17:36:31 -04:00
|
|
|
neighbors.listen("cmd",
|
|
|
|
function(vid, value, rid) {
|
|
|
|
print("Got (", vid, ",", value, ") from robot #", rid)
|
|
|
|
if(value==22) {
|
|
|
|
statef=takeoff
|
|
|
|
} else if(value==21) {
|
|
|
|
statef=land
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2016-12-23 15:21:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function takeoff() {
|
|
|
|
if( flight.status == 2) {
|
|
|
|
barrier_set(ROBOTS,transition_to_land)
|
|
|
|
barrier_ready()
|
|
|
|
}
|
2016-12-24 21:59:00 -04:00
|
|
|
else if(flight.status!=3)
|
2016-12-23 15:21:52 -04:00
|
|
|
uav_takeoff()
|
|
|
|
}
|
|
|
|
function land() {
|
|
|
|
if( flight.status == 1) {
|
|
|
|
barrier_set(ROBOTS,idle)
|
|
|
|
barrier_ready()
|
|
|
|
}
|
2016-12-24 21:59:00 -04:00
|
|
|
else if(flight.status!=0 and flight.status!=4)
|
2016-12-23 15:21:52 -04:00
|
|
|
uav_land()
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function transition_to_land() {
|
2016-12-23 18:48:01 -04:00
|
|
|
statef=transition_to_land
|
2016-12-24 21:59:00 -04:00
|
|
|
if(battery.capacity<50){
|
|
|
|
print("Low battery! Landing the fleet")
|
2016-12-23 15:21:52 -04:00
|
|
|
statef=land
|
2016-12-24 21:59:00 -04:00
|
|
|
neighbors.broadcast("cmd", 21)
|
2016-12-23 15:21:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 21:35:33 -04:00
|
|
|
# Executed once at init time.
|
|
|
|
function init() {
|
|
|
|
i = 0
|
2016-12-05 18:57:20 -04:00
|
|
|
a = 0
|
|
|
|
val = 0
|
2016-12-06 13:02:39 -04:00
|
|
|
oldcmd = 0
|
2016-12-23 15:21:52 -04:00
|
|
|
tim=0
|
|
|
|
statef=idle
|
2016-11-28 21:35:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Executed at each time step.
|
|
|
|
function step() {
|
|
|
|
|
2016-12-23 18:48:01 -04:00
|
|
|
#if(flight.rc_cmd!=oldcmd) {
|
2016-12-23 15:21:52 -04:00
|
|
|
|
2016-12-23 18:48:01 -04:00
|
|
|
if(flight.rc_cmd==22 and flight.status==1) {
|
|
|
|
flight.rc_cmd=0
|
2016-12-23 15:21:52 -04:00
|
|
|
statef=takeoff
|
2016-12-06 13:02:39 -04:00
|
|
|
neighbors.broadcast("cmd", 22)
|
2016-12-23 18:48:01 -04:00
|
|
|
} else if(flight.rc_cmd==21 and flight.status==2) {
|
|
|
|
flight.rc_cmd=0
|
2016-12-23 15:21:52 -04:00
|
|
|
statef=land
|
2016-12-06 13:02:39 -04:00
|
|
|
neighbors.broadcast("cmd", 21)
|
2016-12-05 18:57:20 -04:00
|
|
|
}
|
2016-12-23 18:48:01 -04:00
|
|
|
# oldcmd=flight.rc_cmd
|
|
|
|
#}
|
2016-11-28 21:35:33 -04:00
|
|
|
|
2016-12-23 15:21:52 -04:00
|
|
|
statef()
|
2016-11-28 21:35:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Executed once when the robot (or the simulator) is reset.
|
|
|
|
function reset() {
|
|
|
|
}
|
|
|
|
|
|
|
|
# Executed once at the end of experiment.
|
|
|
|
function destroy() {
|
|
|
|
}
|