Update Conway Sphero example
This commit is contained in:
parent
8d6bb1ba71
commit
8ca8da6711
|
@ -1,89 +1,86 @@
|
|||
var Cylon = require('../..');
|
||||
var Cylon = require("../..");
|
||||
|
||||
var bots = [
|
||||
{ port: '/dev/rfcomm0', name: 'Thelma' },
|
||||
{ port: '/dev/rfcomm1', name: 'Louise' },
|
||||
{ port: '/dev/rfcomm2', name: 'Grace' },
|
||||
{ port: '/dev/rfcomm3', name: 'Ada' }
|
||||
];
|
||||
var Green = 0x0000FF,
|
||||
Red = 0xFF0000;
|
||||
|
||||
var Green = 0x0000FF;
|
||||
var Red = 0xFF0000;
|
||||
var bots = {
|
||||
'Thelma': '/dev/rfcomm0',
|
||||
'Louise': '/dev/rfcomm1',
|
||||
'Grace': '/dev/rfcomm2',
|
||||
'Ada': '/dev/rfcomm3'
|
||||
};
|
||||
|
||||
var ConwayRobot = (function() {
|
||||
function ConwayRobot() {}
|
||||
Object.keys(bots).forEach(function(name) {
|
||||
var port = bots[name];
|
||||
|
||||
ConwayRobot.prototype.connection = { name: 'Sphero', adaptor: 'sphero' };
|
||||
ConwayRobot.prototype.device = { name: 'sphero', driver: 'sphero' };
|
||||
Cylon.robot({
|
||||
name: name,
|
||||
|
||||
ConwayRobot.prototype.born = function() {
|
||||
connection: { name: 'sphero', adaptor: 'sphero', port: port },
|
||||
device: { name: 'sphero', driver: 'sphero' },
|
||||
|
||||
work: function(my) {
|
||||
my.born();
|
||||
|
||||
my.sphero.on('collision', function() {
|
||||
my.contacts += 1;
|
||||
});
|
||||
|
||||
every((3).seconds(), function() {
|
||||
if (my.alive) {
|
||||
my.move();
|
||||
}
|
||||
});
|
||||
|
||||
every((10).seconds(), function() {
|
||||
my.birthday();
|
||||
});
|
||||
},
|
||||
|
||||
move: function() {
|
||||
this.sphero.roll(60, Math.floor(Math.random() * 360));
|
||||
},
|
||||
|
||||
born: function() {
|
||||
this.contacts = 0;
|
||||
this.age = 0;
|
||||
this.life();
|
||||
this.move();
|
||||
};
|
||||
},
|
||||
|
||||
ConwayRobot.prototype.move = function() {
|
||||
this.sphero.roll(60, Math.floor(Math.random() * 360));
|
||||
};
|
||||
|
||||
ConwayRobot.prototype.life = function() {
|
||||
life: function() {
|
||||
this.alive = true;
|
||||
this.sphero.setRGB(Green);
|
||||
};
|
||||
},
|
||||
|
||||
ConwayRobot.prototype.death = function() {
|
||||
death: function() {
|
||||
this.alive = false;
|
||||
this.sphero.setRGB(Red);
|
||||
this.sphero.stop();
|
||||
};
|
||||
},
|
||||
|
||||
ConwayRobot.prototype.enoughContacts = function() {
|
||||
return (this.contacts >= 2 && this.contacts < 7);
|
||||
};
|
||||
enoughContacts: function() {
|
||||
return this.contacts >= 2 && this.contacts < 7;
|
||||
},
|
||||
|
||||
ConwayRobot.prototype.birthday = function() {
|
||||
birthday: function() {
|
||||
this.age += 1;
|
||||
|
||||
if (this.alive) {
|
||||
console.log("Happy birthday, " + this.name + ". You are " + this.age + " and had " + this.contacts + " contacts.");
|
||||
}
|
||||
|
||||
if (this.enoughContacts()) {
|
||||
if (this.alive == null) { this.rebirth(); }
|
||||
if (!this.alive) {
|
||||
this.born();
|
||||
}
|
||||
} else {
|
||||
this.death();
|
||||
}
|
||||
|
||||
this.contacts = 0;
|
||||
};
|
||||
|
||||
ConwayRobot.prototype.work = function(me) {
|
||||
me.born();
|
||||
|
||||
me.sphero.on('collision', function() {
|
||||
this.contacts += 1;
|
||||
}
|
||||
});
|
||||
|
||||
every((3).seconds(), function() {
|
||||
if (me.alive != null) { me.move(); }
|
||||
});
|
||||
|
||||
every((10).seconds(), function() {
|
||||
if (me.alive != null) { me.birthday(); }
|
||||
});
|
||||
};
|
||||
|
||||
return ConwayRobot;
|
||||
|
||||
})();
|
||||
|
||||
for (var i = 0; i < bots.length; i++) {
|
||||
var bot = bots[i];
|
||||
var robot = new ConwayRobot;
|
||||
|
||||
robot.connection.port = bot.port;
|
||||
robot.name = bot.name;
|
||||
|
||||
Cylon.robot(robot);
|
||||
}
|
||||
});
|
||||
|
||||
Cylon.start();
|
||||
|
|
|
@ -26,138 +26,129 @@ First off, let's load up Cylon:
|
|||
|
||||
var Cylon = require('../..');
|
||||
|
||||
For easier use later, let's define the colors we'll be using with the Spheros,
|
||||
green for alive and red for dead:
|
||||
|
||||
var Green = 0x0000FF,
|
||||
Red = 0xFF0000;
|
||||
|
||||
We'll be using four robots for this example, but they'll have very similar
|
||||
programming so we just need to define what's different between them for now.
|
||||
Each of the robots will have a unique name, and will communicate on their own
|
||||
port.
|
||||
|
||||
var bots = [
|
||||
{ port: '/dev/rfcomm0', name: 'Thelma' },
|
||||
{ port: '/dev/rfcomm1', name: 'Louise' },
|
||||
{ port: '/dev/rfcomm2', name: 'Grace' },
|
||||
{ port: '/dev/rfcomm3', name: 'Ada' }
|
||||
];
|
||||
|
||||
For easier use later, let's define the colors we'll be using with the Spheros,
|
||||
green for alive and red for dead:
|
||||
|
||||
var Green = 0x0000FF;
|
||||
var Red = 0xFF0000;
|
||||
var bots = {
|
||||
'Thelma': '/dev/rfcomm0',
|
||||
'Louise': '/dev/rfcomm1',
|
||||
'Grace': '/dev/rfcomm2',
|
||||
'Ada': '/dev/rfcomm3'
|
||||
};
|
||||
|
||||
That gets the basics out of the way.
|
||||
|
||||
Since, as previously mentioned, our robots all have the same basic
|
||||
functionality, we can define that functionality in a CoffeeScript class. Since
|
||||
we're feeling particularly creative today, let's call this class `ConwayRobot`:
|
||||
functionality, we can just loop over the `bots` object, and create robots as we
|
||||
go.
|
||||
|
||||
var ConwayRobot = (function() {
|
||||
function ConwayRobot() {}
|
||||
Object.keys(bots).forEach(function(name) {
|
||||
var port = bots[name];
|
||||
|
||||
All of our robots will be connecting to a Sphero, and be operating via a single
|
||||
device (you guessed it, a Sphero).
|
||||
|
||||
ConwayRobot.prototype.connection = { name: 'Sphero', adaptor: 'sphero' };
|
||||
ConwayRobot.prototype.device = { name: 'sphero', driver: 'sphero' };
|
||||
Cylon.robot({
|
||||
name: name,
|
||||
|
||||
connection: { name: 'sphero', adaptor: 'sphero', port: port },
|
||||
device: { name: 'sphero', driver: 'sphero' },
|
||||
|
||||
Now that the pieces are there, we can set up our robot's work. It starts by
|
||||
being "born", then moves every three seconds if it's alive, celebrates it's
|
||||
birthday every ten seconds, and increments it's contacts every time the Sphero
|
||||
detects a collision.
|
||||
|
||||
work: function(my) {
|
||||
my.born();
|
||||
|
||||
my.sphero.on('collision', function() {
|
||||
my.contacts += 1;
|
||||
});
|
||||
|
||||
every((3).seconds(), function() {
|
||||
if (my.alive) {
|
||||
my.move();
|
||||
}
|
||||
});
|
||||
|
||||
every((10).seconds(), function() {
|
||||
my.birthday();
|
||||
});
|
||||
},
|
||||
|
||||
When a robot is asked to move, it rolls in a random direction at speed 60.
|
||||
|
||||
move: function() {
|
||||
this.sphero.roll(60, Math.floor(Math.random() * 360));
|
||||
},
|
||||
|
||||
When the robots are first started, they are born. This sets their contacts to
|
||||
zero, their age to zero, makes them "alive", and starts them moving for the
|
||||
first tick.
|
||||
|
||||
ConwayRobot.prototype.born = function() {
|
||||
born: function() {
|
||||
this.contacts = 0;
|
||||
this.age = 0;
|
||||
this.life();
|
||||
this.move();
|
||||
};
|
||||
|
||||
When a robot is asked to move, it rolls in a random direction at speed 60.
|
||||
|
||||
ConwayRobot.prototype.move = function() {
|
||||
this.sphero.roll(60, Math.floor(Math.random() * 360));
|
||||
};
|
||||
},
|
||||
|
||||
In the case of our robots, "life" just means the robot's internal "alive" state
|
||||
is set to `true`, and the Sphero's LED is set to green.
|
||||
|
||||
ConwayRobot.prototype.life = function() {
|
||||
life: function() {
|
||||
this.alive = true;
|
||||
this.sphero.setRGB(Green);
|
||||
};
|
||||
},
|
||||
|
||||
Similarly, "death" just sets the "alive" state to false, the Sphero's color to
|
||||
red, and stops the Sphero from moving.
|
||||
|
||||
ConwayRobot.prototype.death = function() {
|
||||
death: function() {
|
||||
this.alive = false;
|
||||
this.sphero.setRGB(Red);
|
||||
this.sphero.stop();
|
||||
};
|
||||
},
|
||||
|
||||
A robot is decided to have enough contacts if it has between two and six
|
||||
contacts.
|
||||
|
||||
ConwayRobot.prototype.enoughContacts = function() {
|
||||
return (this.contacts >= 2 && this.contacts < 7);
|
||||
};
|
||||
enoughContacts: function() {
|
||||
return this.contacts >= 2 && this.contacts < 7;
|
||||
},
|
||||
|
||||
On a robot's birthday, it increments it's age, prints it's name, age, and
|
||||
contacts to the console, and then determines if it's now alive or dead based on
|
||||
the number of contacts it had in the last tick.
|
||||
|
||||
ConwayRobot.prototype.birthday = function() {
|
||||
birthday: function() {
|
||||
this.age += 1;
|
||||
|
||||
if (this.alive) {
|
||||
console.log("Happy birthday, " + this.name + ". You are " + this.age + " and had " + this.contacts + " contacts.");
|
||||
}
|
||||
|
||||
if (this.enoughContacts()) {
|
||||
if (this.alive == null) { this.rebirth(); }
|
||||
if (!this.alive) {
|
||||
this.born();
|
||||
}
|
||||
} else {
|
||||
this.death();
|
||||
}
|
||||
|
||||
this.contacts = 0;
|
||||
};
|
||||
|
||||
Now that the pieces are there, we can set up our robot's work. It starts by
|
||||
being "born", then moves every three seconds if it's alive, celebrates it's
|
||||
birthday every ten seconds if it's alive, and increments it's contacts every
|
||||
time the Sphero detects a collision.
|
||||
|
||||
ConwayRobot.prototype.work = function(me) {
|
||||
me.born();
|
||||
|
||||
me.sphero.on('collision', function() {
|
||||
this.contacts += 1;
|
||||
});
|
||||
|
||||
every((3).seconds(), function() {
|
||||
if (me.alive != null) { me.move(); }
|
||||
});
|
||||
|
||||
every((10).seconds(), function() {
|
||||
if (me.alive != null) { me.birthday(); }
|
||||
});
|
||||
};
|
||||
|
||||
return ConwayRobot;
|
||||
})();
|
||||
|
||||
Now that we've defined the behaviour of our Conway's Game robots, we can make
|
||||
the robots themselves.
|
||||
|
||||
For each of the sets of unique characteristics in the earlier `bots` array,
|
||||
we'll create a new `ConwayRobot`, assign it it's unique characteristics, and
|
||||
pass it to Cylon so it will keep track of them.
|
||||
|
||||
for (var i = 0; i < bots.length; i++) {
|
||||
var bot = bots[i];
|
||||
var robot = new ConwayRobot;
|
||||
|
||||
robot.connection.port = bot.port;
|
||||
robot.name = bot.name;
|
||||
|
||||
Cylon.robot(robot);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Now that Cylon knows about our robots and what they do, we can get started!
|
||||
|
||||
|
|
Loading…
Reference in New Issue