Update API example

This commit is contained in:
Andrew Stewart 2014-09-29 15:15:42 -07:00
parent da9fc2ced6
commit 733c13df13
2 changed files with 38 additions and 54 deletions

View File

@ -6,14 +6,18 @@ Cylon.config({
Cylon.api();
var bots = [
{ port: '/dev/rfcomm0', name: 'Thelma' },
{ port: '/dev/rfcomm1', name: 'Louise' }
];
var bots = {
'Thelma': '/dev/rfcomm0',
'Louise': '/dev/rfcomm1'
};
var SpheroRobot = {
connection: { name: 'Sphero', adaptor: 'sphero' },
Object.keys(bots).forEach(function(name) {
var port = bots[name];
Cylon.robot({
name: name,
connection: { name: 'sphero', adaptor: 'sphero', port: port },
device: { name: 'sphero', driver: 'sphero' },
work: function(my) {
@ -23,16 +27,7 @@ var SpheroRobot = {
my.sphero.roll(60, Math.floor(Math.random() * 360));
});
}
};
for (var i = 0; i < bots.length; i++) {
var bot = bots[i];
var robot = Object.create(SpheroRobot);
robot.connection.port = bot.port;
robot.name = bot.name;
Cylon.robot(robot);
}
});
});
Cylon.start();

View File

@ -24,19 +24,23 @@ the different parts of each robot in objects so we can initialize them later.
The only differences between the bots are their names and the port they'll be
using.
var bots = [
{ port: '/dev/rfcomm0', name: 'Thelma' },
{ port: '/dev/rfcomm1', name: 'Louise' }
];
var bots = {
'Thelma': '/dev/rfcomm0',
'Louise': '/dev/rfcomm1'
};
Now we can define the basic robot both of our Sphero robots will be based on.
var SpheroRobot = {
Object.keys(bots).forEach(function(name) {
var port = bots[name];
Cylon.robot({
name: name,
Both robots will be connecting to Spheros, and so using the cylon-sphero
adaptor:
connection: { name: 'Sphero', adaptor: 'sphero' },
connection: { name: 'sphero', adaptor: 'sphero', port: port },
And both will be connecting to the same kind of device (you guessed it,
a Sphero).
@ -54,22 +58,7 @@ roll in a random direction.
my.sphero.roll(60, Math.floor(Math.random() * 360));
});
}
};
Now that we know what hardware the robots have and what work they'll be doing,
let's create them! We'll iterate through our earlier `bots` array, creating
a new Robot, assigning it it's unique characteristics, then passing it off to
Cylon so it can keep track of it.
for (var i = 0; i < bots.length; i++) {
var bot = bots[i];
var robot = Object.create(SpheroRobot);
robot.connection.port = bot.port;
robot.name = bot.name;
Cylon.robot(robot);
}
});
And now that Cylon has all the robots we're intending to give it, let's get
started!