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

check for anormal flight state and communication lost

parent 8f76574a
...@@ -57,7 +57,8 @@ ...@@ -57,7 +57,8 @@
DEFAULT_SPEED = 5, DEFAULT_SPEED = 5,
MAX_SPEED = 6, MAX_SPEED = 6,
R = 6371e3, R = 6371e3,
SPEED_FACTOR = 0.2; SPEED_FACTOR = 0.2,
TIMEOUT = 1000;
function exitOnFail(ret, msg) { function exitOnFail(ret, msg) {
if (ret) { if (ret) {
...@@ -154,11 +155,11 @@ ...@@ -154,11 +155,11 @@
newSpeed, newSpeed,
position = me.getCurrentPosition(); position = me.getCurrentPosition();
if (me.isLanding()) { if (leader_id !== me.id) {
return sendLandingMessage(me); neighbor_position = me.getDroneDict()[neighbor_id];
} }
if (!me.started) { if (!me.path_planning){
if (leader_id !== me.id) { if (leader_id !== me.id) {
console.log( console.log(
"timestamp difference", "timestamp difference",
...@@ -168,14 +169,40 @@ ...@@ -168,14 +169,40 @@
return; return;
} }
if (me.landing && me.isLanding()) {
return sendLandingMessage(me);
}
if (!me.started) {
if(me.isReadyToFly()) {
me.started = true;
} else {
console.log("Taking off");
return me.sendMsg(JSON.stringify({id: me.id, timestamp: Date.now()}));
}
}
if (!me.landing) { if (!me.landing) {
if (!me.isReadyToFly()) { if(!me.isReadyToFly()) {
return console.log("Taking off"); return console.log("emergency state");
} }
if (leader_id !== me.id) { me.timestamp_list.forEach(function (last_timestamp, id) {
neighbor_position = me.getDroneDict()[neighbor_id]; if (me.id_list[id] !== me.id && Math.abs(timestamp - last_timestamp) >= TIMEOUT) {
console.log(
"Lost drone",
me.id_list[id],
"timestamp difference",
timestamp - last_timestamp
);
me.id_list.splice(me.id_list.indexOf(id), 1);
me.timestamp_list.splice(me.id_list.indexOf(id), 1);
return;
}
});
console.log("leader id", leader_id, "me id", me.id);
if (leader_id !== me.id) {
distance2d = distance( distance2d = distance(
position.latitude, position.latitude,
position.longitude, position.longitude,
...@@ -195,24 +222,14 @@ ...@@ -195,24 +222,14 @@
distanceToTakeOffPoint distanceToTakeOffPoint
); );
if (distanceToTakeOffPoint < 2 * TARGETED_DISTANCE) { if (distanceToTakeOffPoint < 2 * TARGETED_DISTANCE) {
return; return me.sendMsg(JSON.stringify({id: me.id, timestamp: Date.now()}));
} }
me.following = true; me.following = true;
me.sendMsg(JSON.stringify( return me.sendMsg(JSON.stringify(
{id: me.id, inAir: true, state: "Flying", type: "state"} {id: me.id, inAir: true, state: "Flying", timestamp: Date.now(), type: "state"}
)); ));
} }
if (Math.abs(
position.timestamp - neighbor_position.timestamp
) >= 1000) {
console.log(
"timestamp difference",
position.timestamp - neighbor_position.timestamp
);
return;
}
distanceDiff = distance2d - TARGETED_DISTANCE; distanceDiff = distance2d - TARGETED_DISTANCE;
newSpeed = Math.max( newSpeed = Math.max(
Math.min(distanceDiff * SPEED_FACTOR + DEFAULT_SPEED, MAX_SPEED), Math.min(distanceDiff * SPEED_FACTOR + DEFAULT_SPEED, MAX_SPEED),
...@@ -227,7 +244,8 @@ ...@@ -227,7 +244,8 @@
timestamp timestamp
); );
return console.log("distance to leader", distance2d); console.log("distance to leader", distance2d);
return me.sendMsg(JSON.stringify({id: me.id, timestamp: Date.now()}));
} }
checkpointIndex = (!me.reverse) ? me.next_checkpoint checkpointIndex = (!me.reverse) ? me.next_checkpoint
...@@ -248,9 +266,11 @@ ...@@ -248,9 +266,11 @@
me.next_checkpoint += 1; me.next_checkpoint += 1;
me.next_checkpoint %= CHECKPOINT_LIST.length; me.next_checkpoint %= CHECKPOINT_LIST.length;
me.direction_set = false; me.direction_set = false;
me.sendMsg(JSON.stringify({ return me.sendMsg(JSON.stringify({
id: me.id,
type: "checkpoint", type: "checkpoint",
next_checkpoint: me.next_checkpoint next_checkpoint: me.next_checkpoint,
timestamp: Date.now()
})); }));
} }
} }
...@@ -267,15 +287,17 @@ ...@@ -267,15 +287,17 @@
me.next_point.latitude, me.next_point.latitude,
me.next_point.longitude, me.next_point.longitude,
DEFAULT_SPEED, DEFAULT_SPEED,
timestamp Date.now()
); );
} }
return me.sendMsg(JSON.stringify({id: me.id, timestamp: Date.now()}));
}; };
me.onGetMsg = function (msg) { me.onGetMsg = function (msg) {
if (msg.hasOwnProperty("path_planning")) { if (msg.hasOwnProperty("path_planning")) {
me.started = 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.takeOff(); me.takeOff();
return me.sendMsg(JSON.stringify( return me.sendMsg(JSON.stringify(
{id: me.id, inAir: true, state: "Flying", type: "state"} {id: me.id, inAir: true, state: "Flying", type: "state"}
...@@ -284,12 +306,16 @@ ...@@ -284,12 +306,16 @@
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) {
case "stopped": case "stopped":
me.stopped = true; me.stopped = true;
if (me.id === me.id_list[0]) { if (me.id === (!me.reverse? me.id_list[me.id_list.length - 1] : me.id_list[0])) {
me.landing = true; me.landing = true;
me.going_to_parachute = true; me.going_to_parachute = true;
me.next_point = { me.next_point = {
...@@ -311,6 +337,7 @@ ...@@ -311,6 +337,7 @@
return; return;
} }
if (msgDict.hasOwnProperty("type")) {
switch (msgDict.type) { switch (msgDict.type) {
case "checkpoint": case "checkpoint":
...@@ -320,7 +347,8 @@ ...@@ -320,7 +347,8 @@
case "state": case "state":
if (msgDict.state === "Landed" && me.id_list.includes(msgDict.id)) { if (msgDict.state === "Landed" && 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);
if (me.stopped && me.id === me.id_list[0]) { 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])) {
me.landing = true; me.landing = true;
me.going_to_parachute = true; me.going_to_parachute = true;
me.next_point = { me.next_point = {
...@@ -334,5 +362,6 @@ ...@@ -334,5 +362,6 @@
default: default:
console.log("Unknown message type: " + msgDict.type); console.log("Unknown message type: " + msgDict.type);
} }
}
}; };
}(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