Update examples

This commit is contained in:
Andrew Stewart 2014-11-25 09:45:20 -08:00
parent b0f99d3ff3
commit 0541c225d8
7 changed files with 108 additions and 124 deletions

View File

@ -1,11 +1,10 @@
var Cylon = require('../..');
Cylon.config({
api: { host: '0.0.0.0', port: '8080' }
Cylon.api({
host: '0.0.0.0',
port: '8080'
});
Cylon.api();
var bots = {
'Thelma': '/dev/rfcomm0',
'Louise': '/dev/rfcomm1'

View File

@ -13,12 +13,11 @@ First, let's import Cylon:
Next up, we'll configure the API Cylon will serve, telling it to serve on port
`8080`.
Cylon.config({
api: { host: '0.0.0.0', port: '8080' }
Cylon.api({
host: '0.0.0.0',
port: '8080'
});
Cylon.api();
Since we're making two very similar robots (Spheros, in this case), let's put
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

View File

@ -1,59 +1,40 @@
var Cylon = require('../..');
var Cylon = require("../..");
SkynetBot = (function(){
function SkynetBot(){
this.connections = [
{ name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' },
{ name: 'skynet', adaptor: 'skynet'}
];
}
var bots = [
{
port: "/dev/ttyACM0",
uuid: "96630051-a3dc-11e3-8442-5bf31d98c912",
token: "2s67o7ek98pycik98f43reqr90t6s9k9"
},
SkynetBot.prototype.port = function(serialPort){
for (var i in this.connections){
if (this.connections[i].adaptor == 'firmata')
this.connections[i].port = serialPort;
{
port: "/dev/ttyACM1",
uuid: "e8f942f1-a49c-11e3-9270-795e22e700d8",
token: "0lpxpyafz7z7u8frgvp44g8mbr7o80k9"
},
];
bots.forEach(function(bot) {
Cylon.robot({
connections: {
arduino: { adaptor: "firmata", port: bot.port },
skynet: { adaptor: "skynet", uuid: bot.uuid, token: bot.port }
},
device: { name: "led13", driver: "led", pin: 13, connection: "arduino" },
work: function(my) {
my.skynet.on('message', function(data) {
if (data.led13 === 'on') {
my.led13.turnOn();
} else if (data.led13 === 'off') {
my.led13.turnOff();
}
console.log("Skynet instance on '" + my.name + "' is listening");
});
}
};
SkynetBot.prototype.creds = function(uuid, token, portNumber){
for (var i in this.connections){
if (this.connections[i].adaptor == 'skynet'){
this.connections[i].uuid = uuid;
this.connections[i].token = token;
this.connections[i].portNumber = portNumber;
}
}
};
SkynetBot.prototype.device = { name: 'led13', driver: 'led', pin: 13, connection: 'arduino' };
SkynetBot.prototype.work = function(my) {
my.skynet.on('message', function(data) {
if(data.led13 == 'on') {
my.led13.turnOn()
}
else if(data.led13 == 'off') {
my.led13.turnOff()
}
});
console.log("Skynet instance `" + my.name + "` is listening ...");
};
return SkynetBot;
})();
skynetBot0 = new SkynetBot();
skynetBot0.name = 'skynet0';
skynetBot0.port('/dev/ttyACM1');
skynetBot0.creds("96630051-a3dc-11e3-8442-5bf31d98c912", "2s67o7ek98pycik98f43reqr90t6s9k9");
skynetBot1 = new SkynetBot();
skynetBot1.name = 'skynet1';
skynetBot1.creds("e8f942f1-a49c-11e3-9270-795e22e700d8","0lpxpyafz7z7u8frgvp44g8mbr7o80k9");
Cylon.robot(skynetBot0);
Cylon.robot(skynetBot1);
});
});
Cylon.start();

View File

@ -1,20 +1,28 @@
var Cylon = require('..'),
arduino1, arduino2;
var Cylon = require('../..');
Arduino = (function(){
function Arduino(){}
var arduinos = [
{
name: "arduino0",
port: "/dev/ttyACM0",
devices: {
led00: { driver: 'led', pin: 13 }
}
},
Arduino.prototype.connection = { name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' };
Arduino.prototype.work = function(my) {
console.log("Name =====>");
console.log(my.name);
{
name: "arduin01",
port: "/dev/ttyACM1",
devices: {
led10: { driver: 'led', pin: 11 }
led11: { driver: 'led', pin: 12 }
led12: { driver: 'led', pin: 13 }
}
}
];
return Arduino;
})();
Cylon.robot({
name: "SkynetBot",
skynet = {
connection: {
name: 'skynet',
adaptor: 'skynet',
@ -22,44 +30,42 @@ skynet = {
token: "2s67o7ek98pycik98f43reqr90t6s9k9"
},
work: function(my) {
console.log("Skynet is listening...");
handler: function(data) {
if (data.payload == null) {
return;
}
my.skynet.on('message', function(data) {
console.log(data);
if (data.payload != null){
var robot,
bot,
robots = data.payload.robots;
for(var index in robots){
robot = robots[index];
console.log(robot);
console.log("Data: ", data);
for (var i in data.payload.robots) {
var robot = data.payload.robots[i],
bot = Cylon.robots[robot.name];
if (robot.cmd == 'on')
bot.devices[robot.device].turnOn();
else
bot.devices[robot.device].turnOff();
}
if (robot.cmd === 'on') {
bot.devices[robot.device].turnOn();
} else {
bot.devices[robot.device].turnOff();
}
});
}
},
work: function(my) {
my.skynet.on('message', my.handler)
console.log("Skynet is listening");
}
}
Cylon.robot(skynet);
});
arduino0 = new Arduino();
console.log(arduino0);
arduino0.name = 'arduino0';
arduino0.device = {name: 'led00', driver: 'led', pin: 13};
Cylon.robot(arduino0);
arduinos.forEach(function(bot) {
Cylon.robot({
name: bot.name,
arduino1 = new Arduino();
arduino1.name = 'arduino1'
arduino1.connection.port = '/dev/ttyACM1';
arduino1.devices = [
{name: 'led10', driver: 'led', pin: 11},
{name: 'led11', driver: 'led', pin: 12},
{name: 'led12', driver: 'led', pin: 13}
];
Cylon.robot(arduino1);
connection: { name: 'arduino', adaptor: 'firmata', port: bot.port },
devices: bot.devices,
work: function(my) {
console.log(my.name + " is online");
}
});
});
Cylon.start();

View File

@ -13,17 +13,17 @@ Cylon.robot({
},
work: function(my) {
my['x'] = 90;
my['z'] = 90;
my.x = 90;
my.z = 90;
my.leapmotion.on('hand', function(hand) {
my['x'] = hand.palmX.fromScale(-300, 300).toScale(30, 150);
my['z'] = hand.palmZ.fromScale(-300, 300).toScale(30, 150);
my.x = hand.palmX.fromScale(-300, 300).toScale(30, 150);
my.z = hand.palmZ.fromScale(-300, 300).toScale(30, 150);
});
every(100, function() {
my.servo1.angle(my['x']);
my.servo2.angle(my['z']);
my.servo1.angle(my.x);
my.servo2.angle(my.z);
console.log("Current Angle: " + my.servo1.currentAngle() + ", " + my.servo2.currentAngle());
});

View File

@ -25,17 +25,17 @@ Now that Cylon knows about the necessary hardware we're going to be using, we'll
tell it what work we want to do:
work: function(my) {
my['x'] = 90;
my['z'] = 90;
my.x = 90;
my.z = 90;
my.leapmotion.on('hand', function(hand) {
my['x'] = hand.palmX.fromScale(-300, 300).toScale(30, 150);
my['z'] = hand.palmZ.fromScale(-300, 300).toScale(30, 150);
my.x = hand.palmX.fromScale(-300, 300).toScale(30, 150);
my.z = hand.palmZ.fromScale(-300, 300).toScale(30, 150);
});
every(100, function() {
my.servo1.angle(my['x']);
my.servo2.angle(my['z']);
my.servo1.angle(my.x);
my.servo2.angle(my.z);
console.log("Current Angle: " + my.servo1.currentAngle() + ", " + my.servo2.currentAngle());
});

View File

@ -13,11 +13,10 @@ var bots = {
Object.keys(bots).forEach(function(name) {
var port = bots[name];
var robot = Cylon.robot({
name: name,
connection: { name: 'sphero', adaptor: 'sphero', port: port },
device: { name: 'sphero', driver: 'sphero' }
});
var robot = Cylon.robot({ name: name, });
robot.connection({ name: 'sphero', adaptor: 'sphero', port: port });
robot.device({ name: 'sphero', driver: 'sphero' });
robot.move = function() {
robot.sphero.roll(60, Math.floor(Math.random() * 360));