Commit e6a4e807 authored by Léo-Paul Géneau's avatar Léo-Paul Géneau 👾

Update state management

parent 89fbfc94
...@@ -75,19 +75,6 @@ ...@@ -75,19 +75,6 @@
} }
} }
function sendLandingMessage(drone) {
console.log("Landing");
drone.sendMsg(JSON.stringify(
{id: drone.id, inAir: false, state: "Landed", type: "state"}
));
}
function land(drone) {
console.log("Landing");
sendLandingMessage(drone);
exitOnFail(drone.land(), "Failed to land");
}
function toRad(angle) { function toRad(angle) {
return Math.PI * angle / 180; return Math.PI * angle / 180;
} }
...@@ -137,7 +124,8 @@ ...@@ -137,7 +124,8 @@
me.flightAltitude = BASE_ALTITUDE + me.id * ALTITUDE_DIFF; me.flightAltitude = BASE_ALTITUDE + me.id * ALTITUDE_DIFF;
me.following = false; me.following = false;
me.going_to_parachute = false; me.going_to_parachute = false;
me.id_list = Object.keys(me.getDroneDict()).map((x) => Number(x)); me.id_list = [me.id];
me.timestamp_list = [0];
me.id_list.sort(); me.id_list.sort();
me.landing = false; me.landing = false;
me.last_cmd_timestamp = 0; me.last_cmd_timestamp = 0;
...@@ -145,10 +133,6 @@ ...@@ -145,10 +133,6 @@
me.started = false; me.started = false;
me.stopped = false; me.stopped = false;
me.reverse = false; me.reverse = false;
me.sendMsg(JSON.stringify(
{id: me.id, inAir: true, state: "Ready", type: "state"}
));
}; };
me.onUpdate = function (timestamp) { me.onUpdate = function (timestamp) {
...@@ -174,40 +158,49 @@ ...@@ -174,40 +158,49 @@
position.timestamp - neighbor_position.timestamp position.timestamp - neighbor_position.timestamp
); );
} }
return; return me.sendMsg(JSON.stringify(
{id: me.id, okState: true, state: "Ready"}
));
} }
if (me.landing && me.isLanding()) { if (me.landing && me.isLanding()) {
return sendLandingMessage(me); return me.sendMsg(JSON.stringify(
{id: drone.id, okState: false, state: "Landing"}
));
} }
if (!me.started) { if (!me.started) {
if(me.isReadyToFly()) { if(me.isReadyToFly()) {
me.started = true; me.started = true;
} else { } else {
console.log("Taking off"); return me.sendMsg(JSON.stringify(
return me.sendMsg(JSON.stringify({id: me.id, timestamp: Date.now()})); {id: me.id, okState: true, state: "Taking Off", timestamp: Date.now()}
));
} }
} }
if (!me.landing) { if (!me.landing) {
if(!me.isReadyToFly()) { if(!me.isReadyToFly()) {
return console.log("emergency state"); return me.sendMsg(JSON.stringify(
{id: me.id, okState: false, state: "Emergency"}
));
} }
me.timestamp_list.forEach(function (last_timestamp, id) { JSON.parse(JSON.stringify(me.timestamp_list)).forEach(
if (me.id_list[id] !== me.id && Math.abs(timestamp - last_timestamp) >= TIMEOUT) { function (last_timestamp, id) {
console.log( if (me.id_list[id] !== me.id
"Lost drone", && Math.abs(timestamp - last_timestamp) >= TIMEOUT) {
me.id_list[id], console.log(
"timestamp difference", "Lost drone",
timestamp - last_timestamp me.id_list[id],
); "timestamp difference",
me.id_list.splice(me.id_list.indexOf(id), 1); timestamp - last_timestamp
me.timestamp_list.splice(me.id_list.indexOf(id), 1); );
return; me.id_list.splice(me.id_list.indexOf(id), 1);
me.timestamp_list.splice(me.id_list.indexOf(id), 1);
}
} }
}); );
console.log("leader id", leader_id, "me id", me.id); console.log("leader id", leader_id, "me id", me.id);
if (leader_id !== me.id) { if (leader_id !== me.id) {
...@@ -230,12 +223,11 @@ ...@@ -230,12 +223,11 @@
distanceToTakeOffPoint distanceToTakeOffPoint
); );
if (distanceToTakeOffPoint < 2 * TARGETED_DISTANCE) { if (distanceToTakeOffPoint < 2 * TARGETED_DISTANCE) {
return me.sendMsg(JSON.stringify({id: me.id, timestamp: Date.now()})); return me.sendMsg(JSON.stringify(
{id: me.id, okState: true, state: "Waiting", timestamp: Date.now()}
));
} }
me.following = true; me.following = true;
return me.sendMsg(JSON.stringify(
{id: me.id, inAir: true, state: "Flying", timestamp: Date.now(), type: "state"}
));
} }
distanceDiff = distance2d - TARGETED_DISTANCE; distanceDiff = distance2d - TARGETED_DISTANCE;
...@@ -253,7 +245,9 @@ ...@@ -253,7 +245,9 @@
); );
console.log("distance to leader", distance2d); console.log("distance to leader", distance2d);
return me.sendMsg(JSON.stringify({id: me.id, timestamp: Date.now()})); return me.sendMsg(JSON.stringify(
{id: me.id, okState: true, state: "Flying", timestamp: Date.now()}
));
} }
checkpointIndex = (!me.reverse) ? me.next_checkpoint checkpointIndex = (!me.reverse) ? me.next_checkpoint
...@@ -276,8 +270,9 @@ ...@@ -276,8 +270,9 @@
me.direction_set = false; me.direction_set = false;
return me.sendMsg(JSON.stringify({ return me.sendMsg(JSON.stringify({
id: me.id, id: me.id,
type: "checkpoint", okState: true,
next_checkpoint: me.next_checkpoint, next_checkpoint: me.next_checkpoint,
state: "Flying",
timestamp: Date.now() timestamp: Date.now()
})); }));
} }
...@@ -286,7 +281,7 @@ ...@@ -286,7 +281,7 @@
if (me.going_to_parachute && if (me.going_to_parachute &&
pointReached(me, PARACHUTE_POINT_ARRAY[me.id], EPSILON)) { pointReached(me, PARACHUTE_POINT_ARRAY[me.id], EPSILON)) {
me.going_to_parachute = false; me.going_to_parachute = false;
return land(me); return exitOnFail(drone.land(), "Failed to land");
} }
if (!me.landing || me.going_to_parachute) { if (!me.landing || me.going_to_parachute) {
...@@ -298,7 +293,9 @@ ...@@ -298,7 +293,9 @@
Date.now() Date.now()
); );
} }
return me.sendMsg(JSON.stringify({id: me.id, timestamp: Date.now()})); return me.sendMsg(JSON.stringify(
{id: me.id, okState: true, state: "Flying", timestamp: Date.now()}
));
}; };
me.onGetMsg = function (msg) { me.onGetMsg = function (msg) {
...@@ -306,18 +303,11 @@ ...@@ -306,18 +303,11 @@
me.path_planning = true; me.path_planning = true;
console.log("running"); console.log("running");
me.timestamp_list = Array.apply(null, Array(me.id_list.length)).map(function () { return Date.now() }); me.timestamp_list = Array.apply(null, Array(me.id_list.length)).map(function () { return Date.now() });
me.takeOff(); return me.takeOff();
return me.sendMsg(JSON.stringify(
{id: me.id, inAir: true, state: "Flying", type: "state"}
));
} }
var msgDict = JSON.parse(msg); var msgDict = JSON.parse(msg);
if(msgDict.hasOwnProperty("id") && msgDict.hasOwnProperty("timestamp")) {
me.timestamp_list[msgDict.id] = msgDict.timestamp;
}
if (msgDict.hasOwnProperty("status")) { if (msgDict.hasOwnProperty("status")) {
switch (msgDict.status) { switch (msgDict.status) {
...@@ -345,15 +335,18 @@ ...@@ -345,15 +335,18 @@
return; return;
} }
if (msgDict.hasOwnProperty("type")) { if (msgDict.hasOwnProperty("state")) {
switch (msgDict.type) { switch (msgDict.state) {
case "checkpoint": case "Ready":
me.next_checkpoint = msgDict.next_checkpoint; if (!me.id_list.includes(msgDict.id)) {
me.id_list[me.id_list.length - 1] = msgDict.id;
me.id_list.sort();
}
break; break;
case "state": case "Landing":
if (msgDict.state === "Landed" && me.id_list.includes(msgDict.id)) { if (me.id_list.includes(msgDict.id)) {
me.id_list.splice(me.id_list.indexOf(msgDict.id), 1); me.id_list.splice(me.id_list.indexOf(msgDict.id), 1);
me.timestamp_list.splice(me.id_list.indexOf(id), 1); me.timestamp_list.splice(me.id_list.indexOf(id), 1);
if (me.stopped && me.id === (!me.reverse? me.id_list[me.id_list.length - 1] : me.id_list[0])) { if (me.stopped && me.id === (!me.reverse? me.id_list[me.id_list.length - 1] : me.id_list[0])) {
...@@ -368,8 +361,16 @@ ...@@ -368,8 +361,16 @@
break; break;
default: default:
console.log("Unknown message type: " + msgDict.type); break;
} }
} }
if(msgDict.hasOwnProperty("timestamp")) {
me.timestamp_list[msgDict.id] = msgDict.timestamp;
}
if (msgDict.hasOwnProperty("next_checkpoint")) {
me.next_checkpoint = msgDict.next_checkpoint;
}
}; };
}(console, me)); }(console, me));
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment