ROSBuzz_MISTLab/src/testflockfev.bzz

191 lines
4.0 KiB
Plaintext
Raw Normal View History

2017-02-21 20:34:34 -04:00
# We need this for 2D vectors
# Make sure you pass the correct include path to "bzzc -I <path1:path2> ..."
2017-02-24 00:10:01 -04:00
include "/home/ubuntu/buzz/src/include/vec2.bzz"
2017-02-21 20:34:34 -04:00
####################################################################################################
# Updater related
# This should be here for the updater to work, changing position of code will crash the updater
####################################################################################################
updated="update_ack"
update_no=0
function updated_neigh(){
neighbors.broadcast(updated, update_no)
}
TARGET_ALTITUDE = 5.0
2017-02-22 18:22:12 -04:00
CURSTATE = "TURNEDOFF"
2017-02-21 20:34:34 -04:00
# Lennard-Jones parameters
2017-02-22 20:59:11 -04:00
TARGET = 10.0 #0.000001001
2017-04-02 22:51:39 -03:00
EPSILON = 18.0 #0.001
2017-02-21 20:34:34 -04:00
# Lennard-Jones interaction magnitude
function lj_magnitude(dist, target, epsilon) {
return -(epsilon / dist) * ((target / dist)^4 - (target / dist)^2)
}
# Neighbor data to LJ interaction vector
function lj_vector(rid, data) {
return math.vec2.newp(lj_magnitude(data.distance, TARGET, EPSILON), data.azimuth)
}
# Accumulator of neighbor LJ interactions
function lj_sum(rid, data, accum) {
return math.vec2.add(data, accum)
}
# Calculates and actuates the flocking interaction
function hexagon() {
statef=hexagon
2017-02-22 18:22:12 -04:00
CURSTATE = "HEXAGON"
2017-02-21 20:34:34 -04:00
# Calculate accumulator
var accum = neighbors.map(lj_vector).reduce(lj_sum, math.vec2.new(0.0, 0.0))
if(neighbors.count() > 0)
math.vec2.scale(accum, 1.0 / neighbors.count())
# Move according to vector
2017-02-22 03:23:24 -04:00
#print("Robot ", id, "must push ",accum.length, "; ", accum.angle)
2017-02-21 20:34:34 -04:00
uav_moveto(accum.x, accum.y)
}
########################################
#
# BARRIER-RELATED FUNCTIONS
#
########################################
#
# Constants
#
BARRIER_VSTIG = 1
2017-04-02 19:54:11 -03:00
# ROBOTS = 3 # number of robots in the swarm
2017-02-21 20:34:34 -04:00
#
# 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
#
2017-03-15 17:53:32 -03:00
WAIT_TIMEOUT = 500
2017-03-13 19:11:01 -03:00
timeW=0
2017-02-21 20:34:34 -04:00
function barrier_wait(threshold, transf) {
barrier.get(id)
2017-02-23 17:44:57 -04:00
CURSTATE = "BARRIERWAIT"
2017-02-21 20:34:34 -04:00
if(barrier.size() >= threshold) {
barrier = nil
transf()
2017-03-13 19:11:01 -03:00
} else if(timeW>=WAIT_TIMEOUT) {
barrier = nil
2017-03-15 17:53:32 -03:00
statef=land
2017-03-13 19:11:01 -03:00
timeW=0
2017-02-21 20:34:34 -04:00
}
2017-03-13 19:11:01 -03:00
timeW = timeW+1
2017-02-21 20:34:34 -04:00
}
# flight status
function idle() {
statef=idle
2017-02-22 18:22:12 -04:00
CURSTATE = "IDLE"
2017-02-21 20:34:34 -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
2017-02-24 00:09:04 -04:00
} else if(value==400) {
uav_arm()
} else if(value==401){
uav_disarm()
2017-02-23 22:14:42 -04:00
}
2017-02-21 20:34:34 -04:00
}
)
}
function takeoff() {
log("TakeOff: ", flight.status)
if( flight.status == 2 and position.altitude >= TARGET_ALTITUDE-TARGET_ALTITUDE/20.0) {
2017-02-22 18:02:40 -04:00
barrier_set(ROBOTS,hexagon)
barrier_ready()
#statef=hexagon
2017-02-21 20:34:34 -04:00
}
else if( flight.status !=3){
log("Altitude: ", TARGET_ALTITUDE)
neighbors.broadcast("cmd", 22)
uav_takeoff(TARGET_ALTITUDE)
}
}
function land() {
log("Land: ", flight.status)
if(flight.status == 2 or flight.status == 3){
neighbors.broadcast("cmd", 21)
uav_land()
}
2017-02-22 20:47:55 -04:00
else {
barrier = nil
2017-02-21 20:34:34 -04:00
statef=idle
2017-02-22 20:47:55 -04:00
}
2017-02-21 20:34:34 -04:00
}
2017-02-23 22:14:42 -04:00
2017-02-21 20:34:34 -04:00
# Executed once at init time.
function init() {
statef=idle
2017-02-22 18:22:12 -04:00
CURSTATE = "IDLE"
2017-02-21 20:34:34 -04:00
}
# Executed at each time step.
function step() {
if(flight.rc_cmd==22) {
log("cmd 22")
flight.rc_cmd=0
2017-02-22 18:14:27 -04:00
statef = takeoff
2017-02-22 18:22:12 -04:00
CURSTATE = "TAKEOFF"
2017-02-21 20:34:34 -04:00
neighbors.broadcast("cmd", 22)
} else if(flight.rc_cmd==21) {
log("cmd 21")
log("To land")
flight.rc_cmd=0
statef = land
2017-02-22 18:22:12 -04:00
CURSTATE = "LAND"
2017-02-21 20:34:34 -04:00
neighbors.broadcast("cmd", 21)
} else if(flight.rc_cmd==16) {
flight.rc_cmd=0
2017-03-15 17:53:32 -03:00
statef = idle
2017-02-21 20:34:34 -04:00
uav_goto()
2017-02-23 12:49:10 -04:00
} else if(flight.rc_cmd==400) {
flight.rc_cmd=0
2017-02-24 00:09:04 -04:00
uav_arm()
neighbors.broadcast("cmd", 400)
} else if (flight.rc_cmd==401){
flight.rc_cmd=0
2017-02-24 00:09:04 -04:00
uav_disarm()
neighbors.broadcast("cmd", 401)
}
2017-02-21 20:34:34 -04:00
statef()
2017-02-22 18:14:27 -04:00
log("Current state: ", CURSTATE)
2017-04-02 19:54:11 -03:00
log("Swarm size: ",ROBOTS)
2017-02-21 20:34:34 -04:00
}
# Executed once when the robot (or the simulator) is reset.
function reset() {
}
# Executed once at the end of experiment.
function destroy() {
}