Skip to content

Instantly share code, notes, and snippets.

@KeanW
Last active April 22, 2016 04:05
Show Gist options
  • Save KeanW/b5f3ebd806f14c68baed to your computer and use it in GitHub Desktop.
Save KeanW/b5f3ebd806f14c68baed to your computer and use it in GitHub Desktop.
JavaScript code using Cylon.js to drive an Ollie and a BB-8 behind a web-service
var Cylon = require('cylon');
var cyrob = Cylon.robot({
connections: {
bluetooth_ollie: { adaptor: 'central', uuid: '1259a8ff5b694bf39c77d235fa8ade26', module: 'cylon-ble'},
bluetooth_bb8: { adaptor: 'central', uuid: '971cf6561dec4974ab9d266927873778', module: 'cylon-ble'}
},
devices: {
ollie: { driver: 'ollie', connection: 'bluetooth_ollie', duration: 600, speed: 50},
bb8: { driver: 'ollie', connection: 'bluetooth_bb8', duration: 1500, speed: 150}
},
connectedRobots: [ 'ollie', 'bb8'],
robotApply: function(f, robots) {
var my = this;
robots = (robots || my.connectedRobots);
for (var i = 0; i < robots.length; i++) {
var robot = my[robots[i]];
f(robot);
}
},
wake: function(robots) {
var my = this;
var rs = robots || my.connectedRobots;
console.log('Waking ' + rs);
my.robotApply(function(r) { r.wake(2, 2, 2, 2); }, robots);
after(100, function() {
my.robotApply(function(robot) { robot.setRGB(0x00FFFF); });
});
},
moveInDirection: function(direction, robots) {
var my = this;
var speed = 60;
var rs = robots || my.connectedRobots;
console.log('Moving ' + rs + ' ' + direction);
switch (direction) {
case 'forward':
my.moveThenStop(0, robots);
break;
case 'backward':
my.moveThenStop(180, robots);
break;
case 'left':
my.moveThenStop(270, robots);
break;
case 'right':
my.moveThenStop(90, robots);
break;
default:
my.moveThenStop(parseInt(direction), robots);
}
},
moveThenStop: function(direction, robots) {
var my = this;
my.robotApply(function(r) {
r.roll(r.details.speed, direction, 1);
after(r.details.duration, function(){
r.stop();
});
}, robots);
},
commands: function() {
return {
wake: this.wake,
moveInDirection: this.moveInDirection,
moveThenStop: this.moveThenStop
};
}
});
cyrob.start();
exports.controller = function() {
return cyrob;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment