Skip to content

Instantly share code, notes, and snippets.

@KeanW
Last active February 15, 2016 04:10
Show Gist options
  • Save KeanW/a25b8668894ebab9556d to your computer and use it in GitHub Desktop.
Save KeanW/a25b8668894ebab9556d to your computer and use it in GitHub Desktop.
JavaScript code using Cylon.js to drive an Ollie and a BB-8 via the keyboard
var Cylon = require('cylon');
Cylon.robot({
connections: {
bluetooth_ollie: { adaptor: 'central', uuid: '1259a8ff5b694bf39c77d235fa8ade26', module: 'cylon-ble'},
bluetooth_bb8: { adaptor: 'central', uuid: '971cf6561dec4974ab9d266927873778', module: 'cylon-ble'},
keyboard: {adaptor: 'keyboard'}
},
devices: {
keyboard: { driver: 'keyboard', connection: 'keyboard' },
ollie: { driver: 'ollie', connection: 'bluetooth_ollie', duration: 600},
bb8: { driver: 'ollie', connection: 'bluetooth_bb8', duration: 1500}
},
robots: [ "ollie", "bb8"],
robotApply: function(f) {
var my = this;
for (var i = 0; i < my.robots.length; i++) {
var robot = my[my.robots[i]];
f(robot);
}
},
wake: function() {
console.log("wake");
this.robotApply(function(robot) { robot.wake(2, 2, 2, 2); });
},
moveInDirection: function(direction) {
var my = this;
var speed = 60;
switch (direction) {
case "forward":
my.moveThenStop(speed, 0);
break;
case "backward":
my.moveThenStop(speed, 180);
break;
case "left":
my.moveThenStop(speed, 270);
break;
case "right":
my.moveThenStop(speed, 90);
break;
}
return "ok";
},
moveThenStop: function(speed, direction) {
var my = this;
my.robotApply(function(robot) {
robot.roll(speed, direction, 1);
after(robot.details.duration, function(){
robot.stop();
});
});
},
stopMoving: function() {
my.robotApply(function(robot) {
robot.stop();
});
},
commands: function() {
return {
wake: this.wake,
moveInDirection: this.moveInDirection,
moveThenStop: this.moveThenStop,
stopMoving: this.stopMoving
};
},
work: function(my) {
my.wake();
after(100, function() {
my.robotApply(function(robot) { robot.setRGB(0x00FFFF); });
});
my.keyboard.on("f", function() {
console.log("forward");
my.moveInDirection("forward");
});
my.keyboard.on("b", function() {
console.log("backward");
my.moveInDirection("backward");
});
my.keyboard.on("r", function() {
console.log("right");
my.moveInDirection("right");
});
my.keyboard.on("l", function() {
console.log("left");
my.moveInDirection("left");
});
my.keyboard.on("z", function() {
console.log("stop");
my.stopMoving();
});
}
}).start();
@Ganize
Copy link

Ganize commented Feb 15, 2016

Hi, how do i configure the keyboard device?

Please help :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment