ROSBuzz_MISTLab/src/test1.bzz

103 lines
1.6 KiB
Plaintext
Raw Normal View History

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)
}
#
# Make yourself ready
#
function barrier_ready() {
barrier.put(id, 1)
}
#
# Executes the barrier
#
function barrier_wait(threshold, transf) {
barrier.get(id)
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) {
2017-01-09 18:15:33 -04:00
barrier_set(ROBOTS,idle)
2016-12-23 15:21:52 -04:00
barrier_ready()
}
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()
}
else if(flight.status!=0 and flight.status!=4)
2016-12-23 15:21:52 -04:00
uav_land()
}
# Executed once at init time.
function init() {
2017-01-09 18:15:33 -04:00
statef=idle
}
2017-01-09 18:15:33 -04:00
# Executed at each time step.
function step() {
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 15:21:52 -04:00
statef()
}
# Executed once when the robot (or the simulator) is reset.
function reset() {
}
# Executed once at the end of experiment.
function destroy() {
}