Skip to content

Instantly share code, notes, and snippets.

@klamping
Created May 30, 2013 20:24
Show Gist options
  • Save klamping/5680876 to your computer and use it in GitHub Desktop.
Save klamping/5680876 to your computer and use it in GitHub Desktop.
var arDrone = require('ar-drone');
var client = arDrone.createClient();
var direction = "unset";
var altitude = 0;
client.config('general:navdata_demo', 'FALSE');
client.on('batteryChange', function (data) {
if (data < 21) {
console.log("PLUG ME IN!");
this.land();
}
});
client.on('navdata', function (data) {
if (data.demo) {
direction = Math.floor(data.demo.clockwiseDegrees);
controlState = data.demo.controlState;
altitude = data.demo.altitudeMeters;
}
});
function setHeight (desiredHeight, cb) {
console.log("altitude: " + altitude);
var variation = 0.1;
if (desiredHeight > altitude + variation) {
client.up(0.5);
setTimeout(function () {
setHeight(desiredHeight, cb);
}, 20);
} else if (desiredHeight < altitude - variation) {
client.down(0.5);
setTimeout(function () {
setHeight(desiredHeight, cb);
}, 20);
} else {
client.stop();
setTimeout(cb, 1000);
}
}
client.takeoff(function() {
var start = direction;
var height = 2;
setHeight(height, function () {
rotateTo(start, "clockwise", function() {
moveForward(0.4, 2000, function () {
setHeight(height, function () {
rotateTo(start + 180, "clockwise", function() {
moveForward(0.4, 2000, function () {
client.land();
}, 2000);
});
});
}, 2000);
});
});
});
function moveForward (speed, duration, cb, wait) {
wait = wait || 0;
client.front(speed);
setTimeout(function () {
client.stop();
setTimeout(cb, wait);
}, duration);
}
function rotateTo(finalDirection, spinDirection, cb) {
// TODO set speed based on how much rotation is left
// if FD && D > 0
// else if FD && D < 0
var rotationSpeed = 0.2;
// reset finalDirection if OOB
if (finalDirection > 180) {
// need to convert to -180
finalDirection = finalDirection - 360;
rotateTo(finalDirection, spinDirection, cb);
return false;
} else if (finalDirection < -180) {
// need to convert to 180
finalDirection = finalDirection + 360;
rotateTo(finalDirection, spinDirection, cb);
return false;
}
if (direction !== "unset" && ((direction > finalDirection + 2) || (direction < finalDirection - 2)) ) {
console.log(direction);
client[spinDirection](rotationSpeed);
setTimeout(function () {
rotateTo(finalDirection, spinDirection, cb);
}, 20);
} else {
client.stop();
setTimeout(cb, 2500);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment