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

Comply to ERP5 JsLint

parent 8ca353c0
......@@ -93,10 +93,11 @@
function altitudeReached(altitude, target_altitude) {
console.log(
`[DEMO] Waiting for altitude... (${altitude} , ${target_altitude})`
);
return Math.abs(altitude - target_altitude) < EPSILON_ALTITUDE;
}
"[DEMO] Waiting for altitude... ("
+ altitude + " , " + target_altitude + ")"
);
return Math.abs(altitude - target_altitude) < EPSILON_ALTITUDE;
}
function distance(lat1, lon1, lat2, lon2) {
var R = 6371e3, // meters
......@@ -109,32 +110,32 @@
h = haversine_phi + Math.cos(la1) * Math.cos(la2) * sin_lon * sin_lon;
return 2 * R * Math.asin(Math.sqrt(h));
}
function exit_on_fail(ret, msg) {
if (ret) {
console.log(msg);
me.exit(1);
}
}
function mustWait(timestamp) {
if (me.timestamp === 0) {
me.timestamp = timestamp;
}
return timestamp - me.timestamp < me.must_wait;
}
function groundLevel(drone) {
return drone.getAltitudeAbs() - drone.getCurrentPosition().z;
}
me.onStart = function () {
me.direction_set = false;
me.landing_alt_reached = false;
me.next_checkpoint = 0;
me.parachute_triggered = false;
};
me.onUpdate = function (timestamp) {
if (me.must_wait > 0) {
if (!mustWait(timestamp)) {
......@@ -154,7 +155,7 @@
),
"Failed to set checkpoint coordinates"
);
console.log(`[DEMO] Going to Checkpoint ${me.next_checkpoint}\n`);
console.log("[DEMO] Going to Checkpoint " + me.next_checkpoint + "\n");
} else {
me.loiter();
console.log("[DEMO] Going to landing altitude...\n");
......@@ -167,7 +168,7 @@
me.direction_set = true;
return;
}
if (me.next_checkpoint < CHECKPOINT_LIST.length) {
me.current_position = me.getCurrentPosition();
me.distance = distance(
......@@ -178,9 +179,10 @@
);
if (me.distance > EPSILON) {
console.log(
`Waiting for drone to get to destination (${me.distance} m)`);
"Waiting for drone to get to destination (" + me.distance + " m)"
);
} else {
console.log(`[DEMO] Reached Checkpoint ${me.next_checkpoint}\n`);
console.log("[DEMO] Reached Checkpoint " + me.next_checkpoint + "\n");
me.next_checkpoint += 1;
me.direction_set = false;
}
......
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, me*/
(function (console, me) {
"use strict";
function displayMessage(message) {
console.log(message);
return 0;
}
var help = ["altitude(altitude)", "exit",
"goto(latitude, longitude, altitude)", "help", "land", "loiter",
"positions", "speed(speed)"].join("\n"),
wrongParameters = displayMessage.bind(null, "Wrong parameters");
function checkNumber(value, toExecute) {
return (
Number.isNaN(value)
? wrongParameters
: toExecute.bind(null, value)
);
}
function displayDronePositions() {
Object.entries(me.droneDict).forEach(function ([id, drone]) {
console.log(
id,
drone.latitude,
drone.longitude,
drone.altitudeAbs,
drone.altitudeRel
);
});
return 0;
}
function exit() {
try {
me.f.close();
} catch (error) {
console.error(error);
}
me.exit(0);
}
me.onStart = function () {
me.f = me.fdopen(me.in, "r");
console.log(help);
};
me.onUpdate = function () {
var altitude,
latitude,
longitude,
speed,
user_input,
undefined_cmd = false,
cmd,
ret;
console.log("> ");
user_input = me.f.getline();
switch (user_input) {
case "altitude":
console.log("Altitude: ");
altitude = parseFloat(me.f.getline());
cmd = checkNumber(altitude, me.setAltitude);
break;
case "exit":
cmd = exit;
break;
case "goto":
console.log("Latitude: ");
latitude = parseFloat(me.f.getline());
console.log("Longitude: ");
longitude = parseFloat(me.f.getline());
console.log("Altitude: ");
altitude = parseFloat(me.f.getline());
cmd = checkNumber(
altitude,
checkNumber(longitude, checkNumber(latitude, me.setTargetCoordinates))
);
break;
case "help":
cmd = displayMessage.bind(null, help);
break;
case "land":
cmd = me.triggerParachute;
break;
case "loiter":
cmd = me.loiter;
break;
case "positions":
cmd = displayDronePositions;
break;
case "speed":
console.log("Speed: ");
speed = parseFloat(me.f.getline());
cmd = checkNumber(speed, me.setAirspeed);
break;
default:
undefined_cmd = true;
cmd = displayMessage.bind(null, " Undefined command");
}
ret = cmd();
if (ret) {
console.log(" [ERROR] function:\n", cmd, "\nreturn value:", ret);
} else if (user_input !== "help" && !undefined_cmd) {
console.log(" Command successful");
}
};
}(console, me));
(function (console, me) {
"use strict";
function displayMessage(message) {
console.log(message);
return 0;
}
var help = ["altitude(altitude)", "exit",
"goto(latitude, longitude, altitude)", "help", "land", "loiter",
"positions", "speed(speed)"].join("\n"),
wrongParameters = displayMessage.bind(null, "Wrong parameters");
function checkNumber(value, toExecute) {
return (
Number.isNaN(value)
? wrongParameters
: toExecute.bind(null, value)
);
}
function displayDronePositions() {
Object.entries(me.droneDict).forEach(function ([id, drone]) {
console.log(
id,
drone.latitude,
drone.longitude,
drone.altitudeAbs,
drone.altitudeRel
);
});
return 0;
}
function exit() {
try {
me.f.close();
} catch (error) {
console.error(error);
}
me.exit(0);
}
me.onStart = function () {
me.f = me.fdopen(me.in, "r");
console.log(help);
};
me.onUpdate = function () {
var altitude,
latitude,
longitude,
speed,
user_input,
undefined_cmd = false,
cmd,
ret;
console.log("> ");
user_input = me.f.getline();
switch (user_input) {
case "altitude":
console.log("Altitude: ");
altitude = parseFloat(me.f.getline());
cmd = checkNumber(altitude, me.setAltitude);
break;
case "exit":
cmd = exit;
break;
case "goto":
console.log("Latitude: ");
latitude = parseFloat(me.f.getline());
console.log("Longitude: ");
longitude = parseFloat(me.f.getline());
console.log("Altitude: ");
altitude = parseFloat(me.f.getline());
cmd = checkNumber(
altitude,
checkNumber(longitude, checkNumber(latitude, me.setTargetCoordinates))
);
break;
case "help":
cmd = displayMessage.bind(null, help);
break;
case "land":
cmd = me.triggerParachute;
break;
case "loiter":
cmd = me.loiter;
break;
case "positions":
cmd = displayDronePositions;
break;
case "speed":
console.log("Speed: ");
speed = parseFloat(me.f.getline());
cmd = checkNumber(speed, me.setAirspeed);
break;
default:
undefined_cmd = true;
cmd = displayMessage.bind(null, " Undefined command");
}
ret = cmd();
if (ret) {
console.log(" [ERROR] function:\n", cmd, "\nreturn value:", ret);
} else if (user_input !== "help" && !undefined_cmd) {
console.log(" Command successful");
}
};
}(console, me));
\ No newline at end of file
me.onStart = function() {
me.exit(0);
};
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global me*/
(function (me) {
"use strict";
me.onStart = function () {
me.exit(0);
};
}(me));
This diff is collapsed.
/*jslint indent2 */
/*global console */
const ALTITUDE_DIFF = 30;
const FLIGH_ALTITUDE = 100;
const PARACHUTE_ALTITUDE = 35;
const EPSILON_ALTITUDE = 5;
let INITIAL_ALTITUDE;
let START_ALTITUDE;
var leaderAltitudeAbs;
var leaderAltitudeRel;
var leaderLatitude;
var leaderLongitude;
function waitForAltitude(target_altitude) {
var altitude = getAltitude();
while(Math.abs(altitude - target_altitude) > EPSILON_ALTITUDE) {
console.log(
`[DEMO] Waiting for altitude... (${altitude} , ${target_altitude})`);
sleep(1000);
altitude = getAltitude();
}
}
function goToAltitude(target_altitude, wait, go) {
if(go) {
exit_on_fail(
setAltitude(target_altitude),
`Failed to go to altitude ${target_altitude} m`
);
}
if(wait) {
waitForAltitude(target_altitude);
}
}
function followLeader(leaderId, initialAltitude, altitudeDiff) {
goToAltitude(START_ALTITUDE + ALTITUDE_DIFF, false, true);
while(droneDict[leaderId].altitudeAbs == 0) {
console.log("[DEMO] Waiting for leader to send its altitude");
sleep(1000);
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, me, sleep*/
(function (console, me, sleep) {
"use strict";
var ALTITUDE_DIFF = 30,
EPSILON_ALTITUDE = 5,
FLIGH_ALTITUDE = 100,
INITIAL_ALTITUDE,
LEADER_ID = 0,
IS_LEADER = me.id === LEADER_ID,
PARACHUTE_ALTITUDE = 35,
START_ALTITUDE,
leaderAltitudeAbs,
leaderAltitudeRel,
leaderLatitude,
leaderLongitude;
function exit_on_fail(ret, msg) {
if (ret) {
console.log(msg);
me.exit(1);
}
}
while(droneDict[leaderId].altitudeAbs < initialAltitude) {
console.log(`[DEMO] Waiting for leader to reach altitude ${initialAltitude} (currently ${droneDict[leaderId].altitudeAbs})`);
sleep(1000);
function waitForAltitude(target_altitude) {
var altitude = me.getAltitude();
while (Math.abs(altitude - target_altitude) > EPSILON_ALTITUDE) {
console.log(
"[DEMO] Waiting for altitude... ("
+ altitude + " , " + target_altitude + ")"
);
sleep(1000);
altitude = me.getAltitude();
}
}
console.log("[DEMO] Switching to following mode...\n");
do {
leaderAltitudeAbs = droneDict[leaderId].altitudeAbs;
leaderAltitudeRel = droneDict[leaderId].altitudeRel;
leaderLatitude = droneDict[leaderId].latitude;
leaderLongitude = droneDict[leaderId].longitude;
setTargetCoordinates(
leaderLatitude,
leaderLongitude,
leaderAltitudeAbs + altitudeDiff,
30.001
);
sleep(500);
} while(leaderAltitudeRel > PARACHUTE_ALTITUDE);
console.log("[DEMO] Stop following...\n");
}
function waitForAltitude(altitude) {
var curAltitude;
do {
sleep(1000);
curAltitude = getAltitude();
console.log(
`[DEMO] Waiting for altitude... (${curAltitude} , ${altitude})`);
function goToAltitude(target_altitude, wait, go) {
if (go) {
exit_on_fail(
me.setAltitude(target_altitude),
"Failed to go to altitude " + target_altitude + " m"
);
}
if (wait) {
waitForAltitude(target_altitude);
}
}
while(curAltitude < altitude);
}
me.onStart = function() {
INITIAL_ALTITUDE = getInitialAltitude();
START_ALTITUDE = INITIAL_ALTITUDE + FLIGH_ALTITUDE;
goToAltitude(altitude, true, true);
waitForAltitude(START_ALTITUDE);
console.log("[DEMO] Setting loiter mode...\n");
loiter();
sleep(3000);
};
me.onUpdate = function() {
if(!IS_LEADER) {
followLeader(LEADER_ID, START_ALTITUDE, ALTITUDE_DIFF);
function followLeader(leaderId, initialAltitude, altitudeDiff) {
goToAltitude(START_ALTITUDE + ALTITUDE_DIFF, false, true);
while (me.droneDict[leaderId].altitudeAbs === 0) {
console.log("[DEMO] Waiting for leader to send its altitude");
sleep(1000);
}
while (me.droneDict[leaderId].altitudeAbs < initialAltitude) {
console.log(
"[DEMO] Waiting for leader to reach altitude " + initialAltitude
+ " (currently " + me.droneDict[leaderId].altitudeAbs + ")"
);
sleep(1000);
}
console.log("[DEMO] Switching to following mode...\n");
do {
leaderAltitudeAbs = me.droneDict[leaderId].altitudeAbs;
leaderAltitudeRel = me.droneDict[leaderId].altitudeRel;
leaderLatitude = me.droneDict[leaderId].latitude;
leaderLongitude = me.droneDict[leaderId].longitude;
me.setTargetCoordinates(
leaderLatitude,
leaderLongitude,
leaderAltitudeAbs + altitudeDiff,
30.001
);
sleep(500);
} while (leaderAltitudeRel > PARACHUTE_ALTITUDE);
console.log("[DEMO] Stop following...\n");
}
console.log("[DEMO] Loitering until manual intructions are given\n")
while(!landed()) {
sleep(1000);
}
};
me.onStart = function () {
INITIAL_ALTITUDE = me.getInitialAltitude();
START_ALTITUDE = INITIAL_ALTITUDE + FLIGH_ALTITUDE;
goToAltitude(START_ALTITUDE, true, true);
waitForAltitude(START_ALTITUDE);
console.log("[DEMO] Setting loiter mode...\n");
me.loiter();
sleep(3000);
};
me.onUpdate = function () {
if (!IS_LEADER) {
followLeader(LEADER_ID, START_ALTITUDE, ALTITUDE_DIFF);
}
console.log("[DEMO] Loitering until manual intructions are given\n");
while (!me.landed()) {
sleep(1000);
}
};
}(console, me));
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, me*/
(function (console, me) {
"use strict";
var ALTITUDE = 100,
EPSILON = 9,
CHECKPOINT_LIST = [
{
altitude: 585.1806861589965,
latitude: 45.64492790560583,
longitude: 14.25334942966329
},
{
altitude: 589.8802607573035,
latitude: 45.64316335436476,
longitude: 14.26332880184475
},
{
altitude: 608.6648153348965,
latitude: 45.64911917196595,
longitude: 14.26214792790128
},
{
altitude: 606.1448368129072,
latitude: 45.64122685351364,
longitude: 14.26590493128597
},
{
altitude: 630.0829598206344,
latitude: 45.64543355564817,
longitude: 14.27242391207985
},
{
altitude: 616.1839898415284,
latitude: 45.6372792927328,
longitude: 14.27533492411138
},
{
altitude: 598.0603137354178,
latitude: 45.64061299543953,
longitude: 14.26161958465814
},
{
altitude: 607.1243119862851,
latitude: 45.64032340702919,
longitude: 14.2682896662383
}
];
function distance(lat1, lon1, lat2, lon2) {
var R = 6371e3, // meters
la1 = lat1 * Math.PI / 180, // lat, lon in radians
la2 = lat2 * Math.PI / 180,
lo1 = lon1 * Math.PI / 180,
lo2 = lon2 * Math.PI / 180,
haversine_phi = Math.pow(Math.sin((la2 - la1) / 2), 2),
sin_lon = Math.sin((lo2 - lo1) / 2),
h = haversine_phi + Math.cos(la1) * Math.cos(la2) * sin_lon * sin_lon;
return 2 * R * Math.asin(Math.sqrt(h));
}
me.onStart = function () {
me.direction_set = false;
me.next_checkpoint = 0;
me.parachute_triggered = false;
};
me.onUpdate = function (timestamp) {
if (!me.direction_set) {
if (me.next_checkpoint < CHECKPOINT_LIST.length) {
me.setTargetCoordinates(
CHECKPOINT_LIST[me.next_checkpoint].latitude,
CHECKPOINT_LIST[me.next_checkpoint].longitude,
CHECKPOINT_LIST[me.next_checkpoint].altitude + ALTITUDE + ALTITUDE * me.id
);
console.log("[DEMO] Going to Checkpoint %d", me.next_checkpoint);
}
me.direction_set = true;
return;
}
if (me.next_checkpoint < CHECKPOINT_LIST.length) {
me.current_position = me.getCurrentPosition();
me.distance = distance(
me.current_position.x,
me.current_position.y,
CHECKPOINT_LIST[me.next_checkpoint].latitude,
CHECKPOINT_LIST[me.next_checkpoint].longitude
);
if (me.distance <= EPSILON) {
console.log("[DEMO] Reached Checkpoint %d", me.next_checkpoint);
me.next_checkpoint += 1;
me.direction_set = false;
}
return;
}
if (!me.parachute_triggered) {
console.log("[DEMO] Deploying parachute...");
me.triggerParachute();
me.parachute_triggered = true;
}
if (me.landed()) {
me.exit(0);
}
};
}(console, me));
\ No newline at end of file
(function (console, me) {
"use strict";
var ALTITUDE = 100,
EPSILON = 9,
CHECKPOINT_LIST = [
{
altitude: 585.1806861589965,
latitude: 45.64492790560583,
longitude: 14.25334942966329
},
{
altitude: 589.8802607573035,
latitude: 45.64316335436476,
longitude: 14.26332880184475
},
{
altitude: 608.6648153348965,
latitude: 45.64911917196595,
longitude: 14.26214792790128
},
{
altitude: 606.1448368129072,
latitude: 45.64122685351364,
longitude: 14.26590493128597
},
{
altitude: 630.0829598206344,
latitude: 45.64543355564817,
longitude: 14.27242391207985
},
{
altitude: 616.1839898415284,
latitude: 45.6372792927328,
longitude: 14.27533492411138
},
{
altitude: 598.0603137354178,
latitude: 45.64061299543953,
longitude: 14.26161958465814
},
{
altitude: 607.1243119862851,
latitude: 45.64032340702919,
longitude: 14.2682896662383
}
];
function distance(lat1, lon1, lat2, lon2) {
var R = 6371e3, // meters
la1 = lat1 * Math.PI / 180, // lat, lon in radians
la2 = lat2 * Math.PI / 180,
lo1 = lon1 * Math.PI / 180,
lo2 = lon2 * Math.PI / 180,
haversine_phi = Math.pow(Math.sin((la2 - la1) / 2), 2),
sin_lon = Math.sin((lo2 - lo1) / 2),
h = haversine_phi + Math.cos(la1) * Math.cos(la2) * sin_lon * sin_lon;
return 2 * R * Math.asin(Math.sqrt(h));
}
me.onStart = function () {
me.direction_set = false;
me.next_checkpoint = 0;
me.parachute_triggered = false;
};
me.onUpdate = function (timestamp) {
if (!me.direction_set) {
if (me.next_checkpoint < CHECKPOINT_LIST.length) {
me.setTargetCoordinates(
CHECKPOINT_LIST[me.next_checkpoint].latitude,
CHECKPOINT_LIST[me.next_checkpoint].longitude,
CHECKPOINT_LIST[me.next_checkpoint].altitude
+ ALTITUDE + ALTITUDE * me.id
);
console.log("[DEMO] Going to Checkpoint %d", me.next_checkpoint);
}
me.direction_set = true;
return;
}
if (me.next_checkpoint < CHECKPOINT_LIST.length) {
me.current_position = me.getCurrentPosition();
me.distance = distance(
me.current_position.x,
me.current_position.y,
CHECKPOINT_LIST[me.next_checkpoint].latitude,
CHECKPOINT_LIST[me.next_checkpoint].longitude
);
if (me.distance <= EPSILON) {
console.log("[DEMO] Reached Checkpoint %d", me.next_checkpoint);
me.next_checkpoint += 1;
me.direction_set = false;
}
return;
}
if (!me.parachute_triggered) {
console.log("[DEMO] Deploying parachute...");
me.triggerParachute();
me.parachute_triggered = true;
}
if (me.landed()) {
me.exit(0);
}
};
}(console, me));
me.onStart = function() {
me.f = me.fdopen(me.in, "r");
console.log("Use q to quit");
};
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, me*/
me.onUpdate= function(timestamp) {
while(me.f.getline() != "q") {
continue;
}
try {
me.f.close();;
} catch (error) {
console.error(error);
}
me.exit(0);
};
(function (console, me) {
"use strict";
me.onStart = function () {
me.f = me.fdopen(me.in, "r");
console.log("Use q to quit");
};
me.onUpdate = function (timestamp) {
if (me.f.getline() !== "q") {
return;
}
try {
me.f.close();
} catch (error) {
console.error(error);
}
me.exit(0);
};
}(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