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

Comply to JSLint

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