Additional WIP on converting literate coffeescript examples to markdown+js

This commit is contained in:
Andrew Stewart 2014-03-26 17:06:26 -07:00
parent e7a340fd0c
commit 479388e7ad
7 changed files with 96 additions and 92 deletions

View File

@ -6,33 +6,37 @@ take off and land. Before we get started, make sure you've got the
To start us off, let's import Cylon:
Cylon = require '../..'
var Cylon = require('../..');
Now that we've got that set up, we can start defining our robot:
Cylon.robot
Cylon.robot({
We'll be using one connection and one device for this robot, both using the
crazyflie adaptor. We'll be connecting to the Crazyflie using the CrazyRadio.
connection:
name: 'crazyflie', adaptor: 'crazyflie', port: "radio://1/10/250KPS"
connection: {
name: 'crazyflie',
adaptor: 'crazyflie',
port: "radio://1/10/250KPS"
},
device:
name: 'drone', driver: 'crazyflie'
device: { name: 'drone', driver: 'crazyflie' }
With the parts in place, we can start defining our robot's work.
work: (my) ->
work: function(my) {
When our drone is ready, we'll make it takeoff. After ten seconds, we'll tell it
to land, and five seconds after that to stop.
my.drone.on 'start', ->
my.drone.on('start', function() {
my.drone.takeoff()
after 10.seconds(), -> my.drone.land()
after 15.seconds(), -> my.drone.stop()
after((10).seconds(), my.drone.land);
after((15).seconds(), my.drone.stop);
});
}
With all that done, we can start up the robot and get the Crazyflie flying:
.start()
}).start();

View File

@ -2,27 +2,26 @@
First, let's import Cylon:
Cylon = require '../..'
var Cylon = require('../..');
Now that we have Cylon imported, we can start defining our robot
# Initialize the robot
Cylon.robot
Cylon.robot({
Let's define the connections and devices:
connection: { name: 'digispark', adaptor: 'digispark'}
connection: { name: 'digispark', adaptor: 'digispark' },
device: { name: 'led', driver: 'led', pin: 1 }
device: { name: 'led', driver: 'led', pin: 1 },
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: (my) ->
every 1.second(), -> my.led.toggle()
work: function(my) {
every((1).second(), my.led.toggle);
}
Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it:
.start()
}).start();

View File

@ -6,22 +6,25 @@ an interval, and after a timeout.
Let's start by importing Cylon:
Cylon = require '../..'
var Cylon = require('../..');
Now we can define our robot:
Cylon.robot
Cylon.robot({
For work, it's going to print a message to the console every second, and another
message after ten seconds have elapsed.
work: ->
every 1.second(), ->
work: function() {
every((1).second(), function() {
console.log("Hello, human!")
});
after 10.seconds(), ->
after((10).seconds(), function() {
console.log "Impressive."
});
}
Simple as can be. Now that we're done, let's start the robot:
.start()
}).start();

View File

@ -1,14 +1,28 @@
# Keyboard
For this Cylon example, we're going to quickly demonstrate getting keyboard
input.
First, let's import Cylon:
Cylon = require '../..'
var Cylon = require('../..');
Cylon.robot
device: { name: 'keyboard', driver: 'keyboard' }
connection: { name: 'keyboard', adaptor: 'keyboard' }
work: (my) ->
my.keyboard.on 'a', (key) ->
console.log "A PRESSED!"
With that done, let's define our robot:
.start()
Cylon.robot({
It will have a single connection and device, both to the keyboard.
connection: { name: 'keyboard', adaptor: 'keyboard' },
device: { name: 'keyboard', driver: 'keyboard' },
When we tell this robot to work, it's going to listen to the 'a' key on the
keyboard and let us know when it's been pressed.
work: (my) function() {
my.keyboard.on('a', function(key) { console.log "A PRESSED!" });
}
With that done, let's get started!
}).start();

View File

@ -2,32 +2,34 @@
First, let's import Cylon:
Cylon = require '../..'
var Cylon = require('../..');
Now that we have Cylon imported, we can start defining our robot
Cylon.robot
Cylon.robot({
Let's define the connections and devices:
connections: [
{ name: 'leapmotion', adaptor: 'leapmotion', port: '127.0.0.1:6437' },
{ name: 'arduino', adaptor: 'firmata', port: '/dev/ttyACM0' }
]
],
devices: [
{ name: 'leapmotion', driver: 'leapmotion', connection: 'leapmotion' },
{ name: 'led', driver: 'led', pin: 13, connection: 'arduino' }
]
],
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: (my) ->
my.leapmotion.on 'frame', (frame) ->
if (frame.hands.length > 0) then my.led.turnOn() else my.led.turnOff()
work: function(my) {
my.leapmotion.on('frame', function(frame) {
frame.hands.length > 0 ? my.led.turnOn() : my.led.turnOff();
});
}
Now that our robot knows what work to do, and the work it will be doing that
hardware with, we can start it:
.start()
}).start();

View File

@ -1,31 +1,21 @@
var Cylon = require('../..');
var bots = [
{ port: '/dev/cu.Sphero-RGB', name: 'Huey' },
{ port: '/dev/cu.Sphero-GRB', name: 'Dewey' },
{ port: '/dev/cu.Sphero-BRG', name: 'Louie' }
];
var bots = [ 'Huey', 'Dewey', 'Louie' ];
var SpheroRobot = (function() {
function SpheroRobot() {}
var MinionBot = (function() {
function MinionBot() {}
SpheroRobot.prototype.connection = { name: 'Sphero', adaptor: 'sphero' };
SpheroRobot.prototype.work = function(my) {
MinionBot.prototype.work = function(my) {
console.log("Robot " + my.name + " is now working!");
};
return SpheroRobot;
return MinionBot;
})();
for (var i = 0; i < bots.length; i++) {
var bot = bots[i];
var robot = new SpheroRobot;
robot.connection.port = bot.port;
robot.name = bot.name;
var robot = new MinionBot;
robot.name = bots[i];
Cylon.robot(robot);
}

View File

@ -5,57 +5,49 @@ master functionality works. Cylon can be fed an arbitrary number of robots, and
it will then take care of all of them, starting and stopping all of them as
needed.
For this example, we're going to be using three Spheros, but you don't actually
need the hardware for the example to work. Do make sure you have the
`cylon-sphero` module installed, though.
First, let's load up Cylon:
First, let's load up Cylon
Cylon = require '../..'
var Cylon = require('../..');
With that in place, now we can start defining our robots. They'll all behave
similarly, but have unique characteristics (their name and port), so let's
define an array of objects to hold these differences. Later, we can instantiate
similarly, but have unique characteristics (their name), so let's
define an array to hold these names for now. Later, we can instantiate
a base robot and change it's attributes as needed.
bots = [
{ port: '/dev/cu.Sphero-RGB', name: 'Huey' },
{ port: '/dev/cu.Sphero-GRB', name: 'Dewey' },
{ port: '/dev/cu.Sphero-BRG', name: 'Louie' }
]
var bots = [ 'Huey', 'Dewey', 'Louie' ];
Now we can define our cleverly-named `SpheroRobot` class. This will be the base
Now we can define our `MinionBot` class. This will be the base
class for all three of the robots.
class SpheroRobot
Every robot needs to connect to a Sphero. We're doing this using the
`cylon-sphero` adaptor.
connection: { name: 'Sphero', adaptor: 'sphero' }
var MinionBot = (function() {
function MinionBot() {};
We'll just give our robots some basic work so we can tell they're actually
working:
work: (my) ->
console.log "Robot #{my.name} is now working!"
MinionBot.prototype.work = function(my) {
console.log("Robot " + my.name + " is now working!");
};
return MinionBot;
})();
And that's all we need for that.
Next up, we'll create Cylon robots by making an instance of the `SpheroRobot`
Next up, we'll create Cylon robots by making an instance of the `MinionBot`
class, and modifying the attributes that are unique to each robot. After the
customized robot is ready, we'll feed it into Cylon.
for bot in bots
robot = new SpheroRobot
robot.connection.port = bot.port
robot.name = bot.name
Cylon.robot robot
for (var i = 0; i < bots.length; i++) {
var robot = new MinionBot;
robot.name = bots[i];
Cylon.robot(robot);
}
And now Cylon knows about all the robots we care about for this example, and
what they do. All that's left is to start them all. The `.start()` method on
Cylon triggers the `.start()` command on all the robots we've told Cylon about,
so all three robots will start at once.
Cylon.start()
Cylon.start();