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

Comply to JSLint

parent 5c0b63f0
/*jslint indent2 */
/*global console, std */
const EPSILON_YAW = 6;
const EPSILON_ALTITUDE = 5;
const TARGET_YAW = 0;
var running = false;
const wrongParameters = displayMessage.bind(null, "Wrong parameters");
function displayDronePositions() {
if(!pubsubRunning)
console.log("You must start pubsub first !");
else {
for (const [id, drone] of Object.entries(droneDict)) {
console.log(id, drone.latitude, drone.longitude, drone.altitudeAbs, drone.altitudeRel);
}
}
return 0;
}
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, me*/
function land() {
var yaw;
(function (console, me) {
"use strict";
while(true) {
yaw = getYaw();
console.log(`[DEMO] Waiting for yaw... (${yaw} , ${TARGET_YAW})`);
if(Math.abs(yaw - TARGET_YAW) < EPSILON_YAW) {
break;
}
sleep(250);
function displayMessage(message) {
console.log(message);
return 0;
}
console.log("[DEMO] Deploying parachute...");
exit_on_fail(doParachute(2), "Failed to deploy parachute");
}
var help = ["altitude(altitude)", "exit",
"goto(latitude, longitude, altitude)", "help", "land", "loiter",
"positions", "speed(speed)"].join("\n"),
wrongParameters = displayMessage.bind(null, "Wrong parameters");
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 checkNumber(value, toExecute) {
return (
Number.isNaN(value)
? wrongParameters
: toExecute.bind(null, value)
);
}
}
function goToAltitude(target_altitude, wait, go) {
if(go) {
exit_on_fail(
setAltitude(target_altitude),
`Failed to go to altitude ${target_altitude} m`
function displayDronePositions() {
Object.entries(me.droneDict).forEach(function ([id, drone]) {
console.log(
id,
drone.latitude,
drone.longitude,
drone.altitudeAbs,
drone.altitudeRel
);
});
return 0;
}
if(wait) {
waitForAltitude(target_altitude);
function exit() {
try {
me.f.close();
} catch (error) {
console.error(error);
}
me.exit(0);
}
}
function checkNumber(value, toExecute) {
return (
Number.isNaN(value)
? wrongParameters
: toExecute.bind(null, value)
);
}
me.onStart = function () {
me.f = me.fdopen(me.in, "r");
console.log(help);
};
function displayMessage(message) {
console.log(message);
return 0;
}
me.onUpdate = function () {
var altitude,
latitude,
longitude,
speed,
user_input,
undefined_cmd = false,
cmd,
ret;
function exit() {
running = false;
return 0;
}
function getInput() {
let undefined_cmd;
let altitude;
let cmd;
let latitude;
let longitude;
let s;
let speed;
const help = `
land
goto(point)
gotoCoord(latitude, longitude)
altitude(altitude)
speed(speed)
positions
reboot
exit
help
`;
const f = std.fdopen(std.in, "r");
running = true;
while (running) {
std.printf("> ");
s = f.getline();
undefined_cmd = false;
switch (s) {
console.log("> ");
user_input = me.f.getline();
switch (user_input) {
case "altitude":
std.printf("Altitude: ");
altitude = parseFloat(f.getline());
cmd = checkNumber(altitude, setAltitude);
console.log("Altitude: ");
altitude = parseFloat(me.f.getline());
cmd = checkNumber(altitude, me.setAltitude);
break;
case "exit":
cmd = exit;
break;
/* case "gotoCoord":
std.printf("Latitude: ");
latitude = parseFloat(f.getline());
std.printf("Longitude: ");
longitude = parseFloat(f.getline());
cmd = checkNumber(longitude, checkNumber(latitude, setTargetLatLong));
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 = land;
cmd = me.triggerParachute;
break;
case "loiter":
cmd = loiter;
cmd = me.loiter;
break;
case "positions":
cmd = displayDronePositions;
break;
case "reboot":
cmd = reboot;
break;
case "speed":
std.printf("Speed: ");
speed = parseFloat(f.getline());
cmd = checkNumber(speed, setAirspeed);
console.log("Speed: ");
speed = parseFloat(me.f.getline());
cmd = checkNumber(speed, me.setAirspeed);
break;
default:
......@@ -154,18 +113,12 @@ function getInput() {
cmd = displayMessage.bind(null, " Undefined command");
}
let ret = cmd();
ret = cmd();
if (ret) {
console.log(" [ERROR] function:\n", cmd, "\nreturn value:", ret);
}
else if (s !== "help" && !undefined_cmd) {
} else if (user_input !== "help" && !undefined_cmd) {
console.log(" Command successful");
}
};
}(console, me));
\ No newline at end of file
f.close();
}
me.onUpdate = function() {
getInput();
};
/*jslint indent2 */
/*global console */
const ALTITUDE_DIFF = 30;
const FLIGH_ALTITUDE = 100;
const PARACHUTE_ALTITUDE = 35;
const EPSILON = 105;
const EPSILON_YAW = 6;
const EPSILON_ALTITUDE = 5;
const TARGET_YAW = 0;
const checkpointList = [
/*jslint nomen: true, indent: 2, maxerr: 3, maxlen: 80 */
/*global console, me*/
(function (console, me) {
"use strict";
var ALTITUDE_DIFF = 30,
FLIGH_ALTITUDE = 100,
PARACHUTE_ALTITUDE = 35,
EPSILON = 9,
EPSILON_ALTITUDE = 7,
CHECKPOINT_LIST = [
{
"latitude": 45.64492790560583,
"longitude": 14.25334942966329,
"altitude": 585.1806861589965
altitude: 585.1806861589965,
latitude: 45.64492790560583,
longitude: 14.25334942966329
},
{
"latitude": 45.64316335436476,
"longitude": 14.26332880184475,
"altitude": 589.8802607573035
altitude: 589.8802607573035,
latitude: 45.64316335436476,
longitude: 14.26332880184475
},
{
"latitude": 45.64911917196595,
"longitude": 14.26214792790128,
"altitude": 608.6648153348965
altitude: 608.6648153348965,
latitude: 45.64911917196595,
longitude: 14.26214792790128
},
{
"latitude": 45.64122685351364,
"longitude": 14.26590493128597,
"altitude": 606.1448368129072
altitude: 606.1448368129072,
latitude: 45.64122685351364,
longitude: 14.26590493128597
},
{
"latitude": 45.64543355564817,
"longitude": 14.27242391207985,
"altitude": 630.0829598206344
altitude: 630.0829598206344,
latitude: 45.64543355564817,
longitude: 14.27242391207985
},
{
"latitude": 45.6372792927328,
"longitude": 14.27533492411138,
"altitude": 616.1839898415284
altitude: 616.1839898415284,
latitude: 45.6372792927328,
longitude: 14.27533492411138
},
{
"latitude": 45.64061299543953,
"longitude": 14.26161958465814,
"altitude": 598.0603137354178
altitude: 598.0603137354178,
latitude: 45.64061299543953,
longitude: 14.26161958465814
},
{
"latitude": 45.64032340702919,
"longitude": 14.2682896662383,
"altitude": 607.1243119862851
altitude: 607.1243119862851,
latitude: 45.64032340702919,
longitude: 14.2682896662383
}
];
],
LANDING_POINT = {
latitude: 45.6398451,
longitude: 14.2699217
},
LEADER_ID = 0,
IS_LEADER = me.id === LEADER_ID;
const landingPoint = [
{
"latitude": 45.6398451,
"longitude": 14.2699217
function altitudeReached(altitude, target_altitude) {
console.log(
`[DEMO] Waiting for altitude... (${altitude} , ${target_altitude})`);
return Math.abs(altitude - target_altitude) < EPSILON_ALTITUDE;
}
];
let INITIAL_ALTITUDE;
let START_ALTITUDE;
function distance(lat1, lon1, lat2, lon2) {
var R = 6371e3, // meters
la1 = lat1 * Math.PI / 180, // la, lo 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));
}
var nextCheckpoint = 0;
function exitOnFail(ret, msg) {
if (ret) {
console.log(msg);
me.exit(1);
}
}
var distanceToLandingPoint = 100;
function mustWait(timestamp) {
if (me.timestamp === 0) {
me.timestamp = timestamp;
}
return timestamp - me.timestamp < me.must_wait;
}
var leaderAltitudeAbs;
var leaderAltitudeRel;
var leaderLatitude;
var leaderLongitude;
function leaderStartAltitude(drone) {
return drone.start_altitude - ALTITUDE_DIFF * me.id;
}
function distance(lat1, lon1, lat2, lon2) {
const R = 6371e3; // meters
const la1 = lat1 * Math.PI/180; // la, lo in radians
const la2 = lat2 * Math.PI/180;
const lo1 = lon1 * Math.PI/180;
const lo2 = lon2 * Math.PI/180;
function leaderReachedInitAltitude(drone) {
return drone.drone_dict[LEADER_ID].altitudeAbs >= leaderStartAltitude(me);
}
//haversine formula
const sinLat = Math.sin((la2 - la1)/2);
const sinLon = Math.sin((lo2 - lo1)/2);
const h = sinLat*sinLat + Math.cos(la1)*Math.cos(la2)*sinLon*sinLon
return 2*R*Math.asin(Math.sqrt(h));
}
function groundLevel(drone) {
return drone.getAltitudeAbs() - drone.getCurrentPosition().z;
}
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();
me.onStart = function () {
me.direction_set = false;
me.going_to_landing_point = false;
me.init_alt_reached = false;
me.landing = false;
me.landing_alt_reached = false;
me.landing_point_reached = false;
me.last_checkpoint_reached = false;
me.must_wait = 3000;
me.next_checkpoint = 0;
me.parachute_triggered = false;
me.start_altitude = me.getInitialAltitude() + FLIGH_ALTITUDE;
me.timestamp = 0;
if (!IS_LEADER) {
me.follow_leader = true;
me.leader_init_alt_reached = false;
me.start_altitude += ALTITUDE_DIFF * me.id;
me.must_wait = 0;
}
}
exitOnFail(
me.setAltitude(me.start_altitude + 1),
"Failed to set start altitude"
);
};
function goToAltitude(target_altitude, wait, go) {
if(go) {
exit_on_fail(
setAltitude(target_altitude),
`Failed to go to altitude ${target_altitude} m`
me.onUpdate = function (timestamp) {
if (!me.init_alt_reached) {
me.init_alt_reached = altitudeReached(
me.getAltitudeAbs(),
me.start_altitude
);
return;
}
if(wait) {
waitForAltitude(target_altitude);
if (me.must_wait > 0) {
if (!mustWait(timestamp)) {
me.must_wait = 0;
me.timestamp = 0;
}
return;
}
}
function land() {
var yaw;
if (!IS_LEADER && me.follow_leader) {
if (me.drone_dict[LEADER_ID].altitudeAbs === 0) {
return console.log("[DEMO] Waiting for leader to send its altitude");
}
while(true) {
yaw = getYaw();
console.log(`[DEMO] Waiting for yaw... (${yaw} , ${TARGET_YAW})`);
if(Math.abs(yaw - TARGET_YAW) < EPSILON_YAW) {
break;
if (!me.leader_init_alt_reached) {
me.leader_init_alt_reached = leaderReachedInitAltitude(me);
return console.log(
"[DEMO] Waiting for leader to reach altitude " +
leaderStartAltitude(me) +
`(currently ${me.drone_dict[LEADER_ID].altitudeAbs})`,
);
}
sleep(250);
if (me.drone_dict[LEADER_ID].altitudeRel > PARACHUTE_ALTITUDE) {
exitOnFail(
me.setTargetCoordinates(
me.drone_dict[LEADER_ID].latitude,
me.drone_dict[LEADER_ID].longitude,
me.drone_dict[LEADER_ID].altitudeAbs + ALTITUDE_DIFF * me.id
),
"Failed to follow leader"
);
} else {
me.follow_leader = false;
console.log("[DEMO] Stop following...\n");
}
return;
}
console.log("[DEMO] Deploying parachute...");
exit_on_fail(doParachute(2), "Failed to deploy parachute");
}
function goTo(latitude, longitude, altitude, radius, sleep_time, epsilon) {
var cur_latitude;
var cur_longitude;
var d;
console.log(`Going to (${latitude}, ${longitude}) from (${getLatitude()}, ${getLongitude()})`);
exit_on_fail(
setTargetCoordinates(latitude, longitude, altitude, radius),
`Failed to go to (${latitude}, ${longitude})`
if (!me.direction_set) {
if (me.next_checkpoint < CHECKPOINT_LIST.length) {
exitOnFail(
me.setTargetCoordinates(
CHECKPOINT_LIST[me.next_checkpoint].latitude,
CHECKPOINT_LIST[me.next_checkpoint].longitude,
CHECKPOINT_LIST[me.next_checkpoint].altitude + FLIGH_ALTITUDE
),
"Failed to set checkpoint coordinates"
);
console.log(`[DEMO] Going to Checkpoint ${me.next_checkpoint}\n`);
} else {
console.log("[DEMO] Setting last checkpoint coordinates...\n");
me.landing_altitude = groundLevel(me) + PARACHUTE_ALTITUDE;
exitOnFail(
me.setTargetCoordinates(
CHECKPOINT_LIST[CHECKPOINT_LIST.length - 1].latitude,
CHECKPOINT_LIST[CHECKPOINT_LIST.length - 1].longitude,
me.landing_altitude
),
"Failed to set last checkpoint coordinates"
);
}
me.direction_set = true;
return;
}
do {
sleep(sleep_time);
cur_latitude = getLatitude();
cur_longitude = getLongitude();
d = distance(cur_latitude, cur_longitude, latitude, longitude);
console.log(`Waiting for drone to get to destination (${d} m),
(${cur_latitude} , ${cur_longitude}), (${latitude}, ${longitude})`);
} while(d > epsilon);
}
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);
}
while(droneDict[leaderId].altitudeAbs < initialAltitude) {
console.log(`[DEMO] Waiting for leader to reach altitude ${initialAltitude} (currently ${droneDict[leaderId].altitudeAbs})`);
sleep(1000);
}
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
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(
"Waiting for drone to get to destination (checkpoint " +
`${me.next_checkpoint}: ${me.distance} m)`,
);
sleep(500);
} while(leaderAltitudeRel > PARACHUTE_ALTITUDE);
} else {
me.loiter(100);
console.log(`[DEMO] Reached Checkpoint ${me.next_checkpoint}\n`);
me.next_checkpoint += 1;
me.sendMsg(JSON.stringify({next_checkpoint: me.next_checkpoint}));
me.direction_set = false;
me.must_wait = 50000;
}
return;
}
console.log("[DEMO] Stop following...\n");
nextCheckpoint = droneDict[leaderId].lastCheckpoint + 1;
}
me.onStart = function() {
INITIAL_ALTITUDE = getInitialAltitude();
START_ALTITUDE = INITIAL_ALTITUDE + FLIGH_ALTITUDE;
goToAltitude(START_ALTITUDE + 1, 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);
}
for (let i = nextCheckpoint; i < checkpointList.length; i++) {
console.log(`[DEMO] Going to Checkpoint ${i}\n`);
goTo(
checkpointList[i].latitude,
checkpointList[i].longitude,
checkpointList[i].altitude + FLIGH_ALTITUDE,
100,
1000,
105
if (!me.last_checkpoint_reached) {
me.current_position = me.getCurrentPosition();
me.distance = distance(
me.current_position.x,
me.current_position.y,
CHECKPOINT_LIST[CHECKPOINT_LIST.length - 1].latitude,
CHECKPOINT_LIST[CHECKPOINT_LIST.length - 1].longitude
);
console.log(`[DEMO] Reached Checkpoint ${i}\n`);
setCheckpoint(i);
sleep(30000);
}
console.log("[DEMO] Setting altitude...\n");
goToAltitude(getAltitude() - getAltitudeRel() + PARACHUTE_ALTITUDE, true, true);
if(!IS_LEADER) {
goTo(
checkpointList[checkpointList.length - 1].latitude,
checkpointList[checkpointList.length - 1].longitude,
getAltitude(),
100,
1000,
105
if (me.distance > EPSILON) {
console.log(
"Waiting for drone to get to destination (last checkpoint: " +
`${me.distance} m)`,
);
} else {
me.loiter(100);
me.last_checkpoint_reached = true;
}
return;
}
if (!me.landing_alt_reached) {
me.landing_alt_reached = altitudeReached(
me.getAltitudeAbs(),
me.landing_altitude
);
me.setAltitude(me.landing_altitude);
return;
}
while(distanceToLandingPoint > 20) {
console.log(`[DEMO] Waiting to reach landing point (current distance is ${distanceToLandingPoint})`);
distanceToLandingPoint = distance(getLatitude(), getLongitude(), landingPoint.latitude, landingPoint.longitude);
if (!me.going_to_landing_point) {
exitOnFail(
me.setTargetCoordinates(
LANDING_POINT.latitude,
LANDING_POINT.longitude,
me.landing_altitude
),
"Failed to set landing point coordinates"
);
me.going_to_landing_point = true;
return;
}
if (!me.landing) {
me.current_position = me.getCurrentPosition();
me.distance = distance(
me.current_position.x,
me.current_position.y,
LANDING_POINT.latitude,
LANDING_POINT.longitude
);
if (me.distance > EPSILON) {
console.log(
"[DEMO] Waiting to reach landing point (current distance is " +
`${me.distance})`,
);
} else {
console.log("[DEMO] Landing...\n");
land();
};
me.landing = true;
}
return;
}
if (!me.parachute_triggered) {
console.log("[DEMO] Deploying parachute...");
exitOnFail(me.triggerParachute(), "Failed to deploy parachute");
me.parachute_triggered = true;
}
if (me.landed()) {
me.exit(0);
}
};
me.onGetMsg = function (msg) {
me.msgDict = JSON.parse(msg);
if (me.follow_leader && me.msgDict.hasOwnProperty("next_checkpoint")) {
me.next_checkpoint = me.msgDict.next_checkpoint;
console.log("next checkpoint is", me.next_checkpoint);
}
};
}(console, me));
\ No newline at end of file
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