ROSBuzz_MISTLab/script/testflockfev.bzz.old

243 lines
5.1 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-05-10 22:57:41 -03:00
#include "vec2.bzz"
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)
}
2017-05-10 22:57:41 -03:00
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-05-10 22:57:41 -03:00
TARGET = 12.0 #0.000001001
2017-05-10 23:04:04 -03:00
EPSILON = 3.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-05-03 16:07:36 -03:00
uav_moveto(accum.x,accum.y)
2017-04-03 20:50:09 -03:00
2017-05-03 16:07:36 -03:00
# if(timeW>=WAIT_TIMEOUT) { #FOR MOVETO TESTS
# timeW =0
# statef=land
2017-05-10 19:39:28 -03:00
# } else if(timeW>=WAIT_TIMEOUT/2) {
# timeW = timeW+1
# uav_moveto(0.06,0.0)
2017-05-03 16:07:36 -03:00
# } else {
# timeW = timeW+1
2017-05-10 19:39:28 -03:00
# uav_moveto(0.0,0.06)
2017-05-03 16:07:36 -03:00
# }
2017-02-21 20:34:34 -04:00
}
########################################
#
# 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-04-05 18:58:03 -03:00
WAIT_TIMEOUT = 200
2017-03-13 19:11:01 -03:00
timeW=0
2017-02-21 20:34:34 -04:00
function barrier_wait(threshold, transf) {
2017-05-15 16:08:36 -03:00
barrier.put(id, 1)
2017-02-21 20:34:34 -04:00
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) {
2017-05-15 16:08:36 -03:00
# barrier = nil
2017-02-21 20:34:34 -04:00
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
}
function takeoff() {
2017-04-02 23:18:16 -03:00
CURSTATE = "TAKEOFF"
statef=takeoff
2017-02-21 20:34:34 -04:00
log("TakeOff: ", flight.status)
2017-04-03 20:50:09 -03:00
log("Relative position: ", position.altitude)
2017-04-03 16:41:03 -03:00
2017-04-05 18:02:35 -03:00
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
}
2017-04-03 20:50:09 -03:00
else {
2017-02-21 20:34:34 -04:00
log("Altitude: ", TARGET_ALTITUDE)
neighbors.broadcast("cmd", 22)
2017-04-03 20:50:09 -03:00
uav_takeoff(TARGET_ALTITUDE)
2017-02-21 20:34:34 -04:00
}
}
function land() {
2017-04-02 23:18:16 -03:00
CURSTATE = "LAND"
statef=land
2017-02-21 20:34:34 -04:00
log("Land: ", flight.status)
2017-04-05 18:02:35 -03:00
if(flight.status == 2 or flight.status == 3){
2017-02-21 20:34:34 -04:00
neighbors.broadcast("cmd", 21)
uav_land()
}
2017-02-22 20:47:55 -04:00
else {
2017-04-05 18:58:03 -03:00
timeW=0
2017-02-22 20:47:55 -04:00
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-05-10 23:04:04 -03:00
function users_save(t) {
if(size(t)>0) {
foreach(t, function(id, tab) {
#log("id: ",id," Latitude ", tab.la, "Longitude ", tab.lo)
add_user_rb(id,tab.la,tab.lo)
})
}
}
function table_print(t) {
if(size(t)>0) {
foreach(t, function(u, tab) {
log("id: ",u," Range ", tab.r, "Bearing ", tab.b)
})
}
}
2017-02-21 20:34:34 -04:00
# Executed once at init time.
function init() {
2017-04-10 15:49:01 -03:00
s = swarm.create(1)
s.join()
2017-05-10 23:04:04 -03:00
vt = stigmergy.create(5)
t = {}
vt.put("p",t)
2017-02-21 20:34:34 -04:00
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-04-02 23:44:47 -03:00
neighbors.listen("cmd",
function(vid, value, rid) {
print("Got (", vid, ",", value, ") from robot #", rid)
2017-04-02 23:49:04 -03:00
if(value==22 and CURSTATE=="IDLE") {
2017-04-02 23:44:47 -03:00
statef=takeoff
} else if(value==21) {
statef=land
2017-04-02 23:49:04 -03:00
} else if(value==400 and CURSTATE=="IDLE") {
2017-04-02 23:44:47 -03:00
uav_arm()
2017-04-02 23:49:04 -03:00
} else if(value==401 and CURSTATE=="IDLE"){
2017-04-02 23:44:47 -03:00
uav_disarm()
}
}
)
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-05-10 23:04:04 -03:00
# Check local users and push to v.stig
if(size(users.dataG)>0)
vt.put("p", users.dataG)
# Save locally the users and print RG
users_save(vt.get("p"))
table_print(users.dataL)
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() {
2017-04-05 18:02:35 -03:00
}