Skip to content

Instantly share code, notes, and snippets.

@joshuakfarrar
Last active December 19, 2015 03:49
Show Gist options
  • Save joshuakfarrar/5893175 to your computer and use it in GitHub Desktop.
Save joshuakfarrar/5893175 to your computer and use it in GitHub Desktop.
an ar drone client from nodeconf2013
var arDrone = require('ar-drone')
, client = arDrone.createClient()
, stdin = process.openStdin()
, isAirborne
, speedFactor = 5
, speeds = {
x: 0,
y: 0,
z: 0
}
/*
// i wired up and arduino and used a potentiometer to control steering (x-axis). while cool, i felt i had less control.
// i also had a button on the board that controlled takeoff and landing
var five = require('johnny-five')
, board, button, pot
board = new five.Board();
board.on('ready', function() {
button = new five.Button(8);
pot = new five.Sensor({
pin: "A3",
freq: 250
});
pot.on('read', function (err, value) {
var potValue = (value * 200 / 1023) - 100;
if (potValue >= 0) {
speeds.x = potValue;
if (isAirborne)
Left();
}
else {
speeds.x = potValue;
if (isAirborne)
Right();
}
});
button.on('down', function() {
if (!isAirborne) Takeoff();
else SafeLanding();
});
});
*/
stdin.setRawMode(true);
stdin.setEncoding('utf8');
stdin.on('data', function(key) {
switch (key) {
case '\u0003': // ctrl+c
SafeQuit();
break;
case 'j':
FlipLeft();
break;
case 'k':
YawShake();
break;
case 'z':
Stop();
break;
case 'x':
SafeLanding();
break;
case 'c':
Takeoff();
break;
case 'w':
Forward();
break;
case 's':
Backwards();
break;
case 'a':
Left();
break;
case 'd':
Right();
break;
case 'r':
Up();
break;
case 'f':
Down();
break;
}
});
function FlipLeft() {
console.log('flipping!');
client.animate('flipLeft', 500);
}
function YawShake() {
console.log('shake that yaw!');
client.animate('yawShake', 2000);
}
function addSpeed (direction, cb) {
if (speeds[direction] + speedFactor <= 100) {
speeds[direction] += speedFactor;
return cb(true);
}
else return cb(false);
}
function subSpeed (direction, cb) {
if (speeds[direction] - speedFactor >= -100) {
speeds[direction] -= speedFactor;
return cb(true);
}
return cb(false);
}
function MoveZ() {
if (speeds.z < 0) {
console.log('going DOWN!: ' + speeds.z);
client.down(Math.abs(speeds.z / 100));
}
else {
console.log('going UP!: ' + speeds.z);
client.up(speeds.z / 100);
}
}
function MoveX () {
if (speeds.x < 0) {
console.log('turning LEFT!: ' + speeds.x);
client.counterClockwise(Math.abs(speeds.x / 100));
}
else {
console.log('turning RIGHT!: ' + speeds.x);
client.clockwise(speeds.x / 100)
}
}
function MoveY () {
if (speeds.y < 0) {
console.log('moving BACK!: ' + speeds.y);
client.back(Math.abs(speeds.y / 100));
}
else {
console.log('moving FORWARD!: ' + speeds.y);
client.front(speeds.y / 100)
}
}
function Left() {
subSpeed('x', function(success) {
MoveX();
});
}
function Right() {
addSpeed('x', function(success) {
MoveX();
});
}
function Forward() {
addSpeed('y', function(success) {
MoveY();
});
}
function Backwards() {
subSpeed('y', function(success) {
MoveY();
});
}
function Up() {
addSpeed('z', function(success) {
MoveZ();
});
}
function Down() {
subSpeed('z', function(success) {
MoveZ();
});
}
function Takeoff() {
console.log('Taking off!');
isAirborne = true;
client.takeoff();
}
function Stop() {
console.log('Stopping');
speeds = {
x: 0,
y: 0,
z: 0
}
client.stop();
}
function SafeQuit () {
SafeLanding(function(success) {
if (success) { console.log('Safely landed. Exiting.'); }
process.exit();
});
}
function SafeLanding (cb) {
if (!cb) cb = function(){};
console.log('Attempting to land.');
var landing = client.land();
isAirborne = false;
return cb(landing);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment