ROSBuzz_MISTLab/buzz_scripts/include/vstigenv.bzz

122 lines
2.7 KiB
Plaintext

########################################
#
# FLEET V.STIGMERGY-RELATED FUNCTIONS
#
########################################
#
# Constants
#
STATUS_VSTIG = 20
GROUND_VSTIG = 21
WAIT4STEP = 5
vstig_buzzy = 0
#
# Init var
#
var v_status = {}
var v_ground = {}
vstig_counter = WAIT4STEP
function init_swarm() {
s = swarm.create(1)
s.join()
}
function init_stig() {
v_status = stigmergy.create(STATUS_VSTIG)
#v_ground = stigmergy.create(GROUND_VSTIG)
}
function uav_updatestig() {
# TODO: Push values on update only?
if(vstig_counter<=0) {
vstig_buzzy = 1
var ls = battery.capacity*10 + s2i(BVMSTATE)
#log("Pushing ", ls, "on vstig with id:", id)
v_status.put(id, ls)
vstig_counter=WAIT4STEP
} else if(vstig_counter==WAIT4STEP-1){ # ensure comm. delay between fetch and update stig
vstig_buzzy = 1
vstig_counter=vstig_counter-1
stattab_send()
} else {
vstig_buzzy = 0
vstig_counter=vstig_counter-1
}
}
function unpackstatus(recv_value,state_struct){
#state_struct.gp=(recv_value-recv_value%1000000)/1000000
#recv_value=recv_value-state_struct.gp*1000000
state_struct.ba=(recv_value-recv_value%10)/10
recv_value=recv_value-state_struct.ba*10
#state_struct.xb=(recv_value-recv_value%10)/10
#recv_value=recv_value-state_struct.xb*10
state_struct.st=recv_value
return state_struct
}
function checkusers() {
# Read a value from the structure
if(size(users)>0)
log("Got a user!")
# log(users)
# users_print(users.dataG)
# if(size(users.dataG)>0)
# vt.put("p", users.dataG)
# Get the number of keys in the structure
# log("The vstig has ", vt.size(), " elements")
# users_save(vt.get("p"))
# table_print(users.dataL)
}
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)
})
}
}
# printing the contents of a table: a custom function
function usertab_print(t) {
if(size(t)>0) {
foreach(t, function(u, tab) {
log("id: ",u," Range ", tab.r, "Bearing ", tab.b)
})
}
}
function stattab_print() {
if(v_status.size()>0) {
var state_struct = {.id = -1, .gp = 0, .ba = 0, .xb = 0, .st = 0}
neighbors.foreach(function(rid, data) {
var nei_state = v_status.get(rid)
if(nei_state!=nil){
state_struct.id=u
state_struct = unpackstatus(nei_state, state_struct)
table_print(state_struct)
}
u=u+1
} )
}
}
function stattab_send() {
if(v_status.size()>0) {
var state_struct = {.id = -1, .gp = -1, .ba = -1, .xb = -1, .st = 0}
neighbors.foreach(function(rid, data) {
var nei_state = v_status.get(rid)
if(nei_state!=nil){
state_struct.id=rid
state_struct = unpackstatus(nei_state,state_struct)
add_neighborStatus(state_struct)
}
} )
}
}