2017-08-30 18:22:37 -03:00
|
|
|
########################################
|
|
|
|
#
|
|
|
|
# FLEET V.STIGMERGY-RELATED FUNCTIONS
|
|
|
|
#
|
|
|
|
########################################
|
|
|
|
#
|
|
|
|
# Constants
|
|
|
|
#
|
|
|
|
STATUS_VSTIG = 20
|
|
|
|
GROUND_VSTIG = 21
|
2018-11-04 17:27:11 -04:00
|
|
|
WAIT4STEP = 5
|
|
|
|
vstig_buzzy = 0
|
2017-08-30 18:22:37 -03:00
|
|
|
|
|
|
|
#
|
|
|
|
# Init var
|
|
|
|
#
|
|
|
|
var v_status = {}
|
|
|
|
var v_ground = {}
|
2017-12-19 14:09:22 -04:00
|
|
|
vstig_counter = WAIT4STEP
|
2017-07-24 23:56:26 -03:00
|
|
|
|
2017-12-22 17:48:39 -04:00
|
|
|
|
|
|
|
function init_swarm() {
|
|
|
|
s = swarm.create(1)
|
|
|
|
s.join()
|
|
|
|
}
|
|
|
|
|
|
|
|
function init_stig() {
|
2017-07-24 23:56:26 -03:00
|
|
|
v_status = stigmergy.create(STATUS_VSTIG)
|
2018-11-04 17:27:11 -04:00
|
|
|
#v_ground = stigmergy.create(GROUND_VSTIG)
|
2017-07-24 23:56:26 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
function uav_updatestig() {
|
2018-11-04 17:27:11 -04:00
|
|
|
# TODO: Push values on update only?
|
2017-12-19 14:09:22 -04:00
|
|
|
if(vstig_counter<=0) {
|
2018-11-04 17:27:11 -04:00
|
|
|
vstig_buzzy = 1
|
|
|
|
var ls = battery.capacity*10 + s2i(BVMSTATE)
|
|
|
|
#log("Pushing ", ls, "on vstig with id:", id)
|
2017-07-24 23:56:26 -03:00
|
|
|
v_status.put(id, ls)
|
2017-12-19 14:09:22 -04:00
|
|
|
vstig_counter=WAIT4STEP
|
2018-11-04 17:27:11 -04:00
|
|
|
} else if(vstig_counter==WAIT4STEP-1){ # ensure comm. delay between fetch and update stig
|
|
|
|
vstig_buzzy = 1
|
|
|
|
vstig_counter=vstig_counter-1
|
|
|
|
stattab_send()
|
2017-07-26 11:10:15 -03:00
|
|
|
} else {
|
2018-11-04 17:27:11 -04:00
|
|
|
vstig_buzzy = 0
|
2017-12-19 14:09:22 -04:00
|
|
|
vstig_counter=vstig_counter-1
|
2017-07-26 11:10:15 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 17:27:11 -04:00
|
|
|
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
|
2017-07-24 23:56:26 -03:00
|
|
|
}
|
|
|
|
|
2017-06-19 17:15:18 -03:00
|
|
|
function checkusers() {
|
2017-07-26 11:10:15 -03:00
|
|
|
# Read a value from the structure
|
|
|
|
if(size(users)>0)
|
|
|
|
log("Got a user!")
|
2017-06-19 17:15:18 -03:00
|
|
|
|
2017-07-26 11:10:15 -03:00
|
|
|
# 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)
|
2017-06-19 17:15:18 -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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# printing the contents of a table: a custom function
|
2017-07-24 23:56:26 -03:00
|
|
|
function usertab_print(t) {
|
2017-06-19 17:15:18 -03:00
|
|
|
if(size(t)>0) {
|
|
|
|
foreach(t, function(u, tab) {
|
|
|
|
log("id: ",u," Range ", tab.r, "Bearing ", tab.b)
|
|
|
|
})
|
|
|
|
}
|
2017-07-24 23:56:26 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
function stattab_print() {
|
|
|
|
if(v_status.size()>0) {
|
2018-11-05 05:11:02 -04:00
|
|
|
var state_struct = {.id = -1, .gp = 0, .ba = 0, .xb = 0, .st = 0}
|
2018-11-04 17:27:11 -04:00
|
|
|
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)
|
|
|
|
}
|
2017-07-26 11:10:15 -03:00
|
|
|
u=u+1
|
2018-11-04 17:27:11 -04:00
|
|
|
} )
|
2017-07-24 23:56:26 -03:00
|
|
|
}
|
2017-08-09 20:23:42 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
function stattab_send() {
|
|
|
|
if(v_status.size()>0) {
|
2018-11-04 17:27:11 -04:00
|
|
|
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)
|
2017-08-09 20:23:42 -03:00
|
|
|
}
|
2018-11-04 17:27:11 -04:00
|
|
|
} )
|
2017-08-09 20:23:42 -03:00
|
|
|
}
|
2017-06-19 17:15:18 -03:00
|
|
|
}
|