2013-10-25 05:25:42 +08:00
|
|
|
/*
|
|
|
|
* robot
|
|
|
|
* cylonjs.com
|
|
|
|
*
|
2014-02-28 03:17:02 +08:00
|
|
|
* Copyright (c) 2013-2014 The Hybrid Group
|
2013-10-25 05:25:42 +08:00
|
|
|
* Licensed under the Apache 2.0 license.
|
|
|
|
*/
|
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
'use strict';
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
require('./cylon');
|
2014-05-07 23:38:06 +08:00
|
|
|
var Connection = require("./connection");
|
2014-05-07 10:18:13 +08:00
|
|
|
var Device = require("./device");
|
2014-02-28 03:17:02 +08:00
|
|
|
require('./digital-pin');
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-03-04 06:33:29 +08:00
|
|
|
var Async = require("async"),
|
|
|
|
EventEmitter = require('events').EventEmitter,
|
|
|
|
namespace = require('node-namespace');
|
2013-10-30 01:43:11 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// A Robot is the primary interface for interacting with a collection of physical
|
|
|
|
// computing capabilities.
|
2014-02-28 03:17:02 +08:00
|
|
|
namespace("Cylon", function() {
|
|
|
|
this.Robot = (function(klass) {
|
|
|
|
subclass(Robot, klass);
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Creates a new Robot
|
|
|
|
//
|
|
|
|
// opts - object containing Robot options
|
|
|
|
// name - optional, string name of the robot
|
|
|
|
// master - Cylon.Master class that orchestrates robots
|
|
|
|
// connection/connections - object connections to connect to
|
|
|
|
// device/devices - object devices to connect to
|
|
|
|
// work - work to be performed when the Robot is started
|
|
|
|
//
|
|
|
|
// Returns a new Robot
|
|
|
|
// Example (CoffeeScript):
|
|
|
|
// Cylon.robot
|
|
|
|
// name: "Spherobot!"
|
|
|
|
//
|
|
|
|
// connection:
|
|
|
|
// name: 'sphero', adaptor: 'sphero', port: '/dev/rfcomm0'
|
|
|
|
//
|
|
|
|
// device:
|
|
|
|
// name: 'sphero', driver: 'sphero'
|
|
|
|
//
|
|
|
|
// work: (me) ->
|
|
|
|
// every 1.second(), ->
|
|
|
|
// me.sphero.roll 60, Math.floor(Math.random() * 360//
|
2014-02-28 03:17:02 +08:00
|
|
|
function Robot(opts) {
|
2014-03-01 04:13:19 +08:00
|
|
|
if (opts == null) { opts = {}; }
|
|
|
|
|
|
|
|
var methods = [
|
|
|
|
"toString",
|
|
|
|
"registerDriver",
|
|
|
|
"requireDriver",
|
|
|
|
"registerAdaptor",
|
|
|
|
"requireAdaptor",
|
2014-03-12 01:44:11 +08:00
|
|
|
"halt",
|
2014-03-01 04:13:19 +08:00
|
|
|
"startDevices",
|
|
|
|
"startConnections",
|
|
|
|
"start",
|
|
|
|
"initDevices",
|
|
|
|
"initConnections"
|
|
|
|
];
|
|
|
|
|
|
|
|
for (var i = 0; i < methods.length ; i++) {
|
|
|
|
var method = methods[i];
|
|
|
|
this[method] = bind(this[method], this);
|
2013-10-25 05:25:42 +08:00
|
|
|
}
|
2014-03-01 04:13:19 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
this.robot = this;
|
|
|
|
this.name = opts.name || this.constructor.randomName();
|
|
|
|
this.master = opts.master;
|
2014-03-01 04:13:19 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
this.connections = {};
|
|
|
|
this.devices = {};
|
|
|
|
this.adaptors = {};
|
|
|
|
this.drivers = {};
|
|
|
|
this.commands = [];
|
2014-03-01 04:13:19 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
this.running = false;
|
2014-03-01 04:13:19 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
this.registerAdaptor("./test/loopback", "loopback");
|
|
|
|
this.registerAdaptor("./test/test-adaptor", "test");
|
|
|
|
this.registerDriver("./test/ping", "ping");
|
|
|
|
this.registerDriver("./test/test-driver", "test");
|
2014-03-01 04:13:19 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
this.initConnections(opts.connection || opts.connections);
|
|
|
|
this.initDevices(opts.device || opts.devices);
|
2014-03-01 04:13:19 +08:00
|
|
|
|
|
|
|
this.work = opts.work || function() { Logger.info("No work yet"); };
|
|
|
|
|
|
|
|
for (var n in opts) {
|
|
|
|
var func = opts[n],
|
|
|
|
reserved = ['connection', 'connections', 'device', 'devices', 'work'];
|
|
|
|
|
|
|
|
if (reserved.indexOf(n) < 0) { this.robot[n] = func; }
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Generates a random name for a Robot.
|
|
|
|
//
|
|
|
|
// Returns a string name
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.randomName = function() {
|
|
|
|
return "Robot " + (Math.floor(Math.random() * 100000));
|
|
|
|
};
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Exports basic data for the Robot
|
|
|
|
//
|
|
|
|
// Returns an Object containing Robot data
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.data = function() {
|
2014-03-01 04:22:32 +08:00
|
|
|
var connections = (function() {
|
|
|
|
var results = [];
|
|
|
|
for (var n in this.connections) {
|
|
|
|
var conn = this.connections[n];
|
|
|
|
results.push(conn.data());
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}).call(this);
|
|
|
|
|
|
|
|
var devices = (function() {
|
|
|
|
var results = [];
|
|
|
|
for (var n in this.devices) {
|
|
|
|
var device = this.devices[n];
|
|
|
|
results.push(device.data());
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}).call(this);
|
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
return {
|
|
|
|
name: this.name,
|
2014-03-01 04:22:32 +08:00
|
|
|
connections: connections,
|
|
|
|
devices: devices,
|
2014-02-28 03:17:02 +08:00
|
|
|
commands: this.commands
|
2013-11-05 03:57:27 +08:00
|
|
|
};
|
2014-02-28 03:17:02 +08:00
|
|
|
};
|
2013-11-05 03:57:27 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Initializes all connections for the robot
|
|
|
|
//
|
|
|
|
// connections - connections to initialize
|
|
|
|
//
|
|
|
|
// Returns initialized connections
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.initConnections = function(connections) {
|
|
|
|
Logger.info("Initializing connections...");
|
2014-03-01 12:30:12 +08:00
|
|
|
if (connections == null) { return; }
|
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
connections = [].concat(connections);
|
2014-03-01 12:30:12 +08:00
|
|
|
|
|
|
|
for (var i = 0; i < connections.length; i++) {
|
|
|
|
var connection = connections[i];
|
2014-02-28 03:17:02 +08:00
|
|
|
Logger.info("Initializing connection '" + connection.name + "'...");
|
|
|
|
connection['robot'] = this;
|
2014-05-07 23:38:06 +08:00
|
|
|
this.connections[connection.name] = new Connection(connection);
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
2014-03-01 12:30:12 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
return this.connections;
|
|
|
|
};
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Initializes all devices for the robot
|
|
|
|
//
|
|
|
|
// devices - devices to initialize
|
|
|
|
//
|
|
|
|
// Returns initialized devices
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.initDevices = function(devices) {
|
|
|
|
Logger.info("Initializing devices...");
|
2014-03-01 12:30:12 +08:00
|
|
|
if (devices == null) { return; }
|
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
devices = [].concat(devices);
|
2014-03-01 12:30:12 +08:00
|
|
|
|
|
|
|
for (var i = 0; i < devices.length; i++) {
|
|
|
|
var device = devices[i];
|
2014-02-28 03:17:02 +08:00
|
|
|
Logger.info("Initializing device '" + device.name + "'...");
|
|
|
|
device['robot'] = this;
|
2014-05-07 10:18:13 +08:00
|
|
|
this.devices[device.name] = this._createDevice(device);
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
2014-03-01 12:30:12 +08:00
|
|
|
|
|
|
|
return this.devices;
|
2014-02-28 03:17:02 +08:00
|
|
|
};
|
|
|
|
|
2014-05-07 10:18:13 +08:00
|
|
|
Robot.prototype._createDevice = function(device) {
|
|
|
|
return new Device(device);
|
|
|
|
};
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Starts the Robot working.
|
|
|
|
//
|
|
|
|
// Starts the connections, devices, and work.
|
|
|
|
//
|
|
|
|
// Returns the result of the work
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.start = function() {
|
2014-03-01 12:34:38 +08:00
|
|
|
var self = this;
|
2014-02-28 03:17:02 +08:00
|
|
|
return this.startConnections(function() {
|
2014-04-02 07:53:34 +08:00
|
|
|
return self.robot.startDevices(function(err) {
|
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}else{
|
|
|
|
self.robot.work.call(self.robot, self.robot);
|
|
|
|
self.running = true;
|
|
|
|
Logger.info("Working...");
|
|
|
|
self.robot.emit('working');
|
|
|
|
}
|
2013-10-26 14:27:39 +08:00
|
|
|
});
|
2014-02-28 03:17:02 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Starts the Robot's connections and triggers a callback
|
|
|
|
//
|
|
|
|
// callback - callback function to be triggered
|
|
|
|
//
|
|
|
|
// Returns nothing
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.startConnections = function(callback) {
|
2014-03-08 15:33:59 +08:00
|
|
|
var starters = {};
|
2014-03-01 12:34:38 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
Logger.info("Starting connections...");
|
2014-03-01 12:34:38 +08:00
|
|
|
|
|
|
|
for (var n in this.connections) {
|
|
|
|
var connection = this.connections[n];
|
2014-02-28 03:17:02 +08:00
|
|
|
this.robot[n] = connection;
|
|
|
|
starters[n] = connection.connect;
|
|
|
|
}
|
2014-03-01 12:34:38 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
return Async.parallel(starters, callback);
|
|
|
|
};
|
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Starts the Robot's devices and triggers a callback
|
|
|
|
//
|
|
|
|
// callback - callback function to be triggered
|
|
|
|
//
|
|
|
|
// Returns nothing
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.startDevices = function(callback) {
|
2014-03-08 15:33:59 +08:00
|
|
|
var starters = {};
|
2014-03-01 12:34:38 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
Logger.info("Starting devices...");
|
2014-03-01 12:34:38 +08:00
|
|
|
|
|
|
|
for (var n in this.devices) {
|
|
|
|
var device = this.devices[n];
|
2014-02-28 03:17:02 +08:00
|
|
|
this.robot[n] = device;
|
|
|
|
starters[n] = device.start;
|
|
|
|
}
|
2014-03-01 12:34:38 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
return Async.parallel(starters, callback);
|
|
|
|
};
|
|
|
|
|
2014-03-12 01:44:11 +08:00
|
|
|
// Public: Halts the Robot.
|
2014-02-28 06:42:33 +08:00
|
|
|
//
|
2014-03-12 01:44:11 +08:00
|
|
|
// Halts the devices, disconnects the connections.
|
2014-02-28 06:42:33 +08:00
|
|
|
//
|
|
|
|
// Returns nothing
|
2014-03-12 01:44:11 +08:00
|
|
|
Robot.prototype.halt = function() {
|
|
|
|
for (var d in this.devices) { this.devices[d].halt(); }
|
|
|
|
for (var c in this.connections) { this.connections[c].halt(); }
|
2014-02-28 03:17:02 +08:00
|
|
|
};
|
2013-11-06 13:34:23 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Initialize an adaptor and adds it to @robot.adaptors
|
|
|
|
//
|
|
|
|
// adaptorName - module name of adaptor to require
|
|
|
|
// connection - the Connection that requested the adaptor be required
|
|
|
|
//
|
|
|
|
// Returns the adaptor
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.initAdaptor = function(adaptorName, connection, opts) {
|
2014-03-01 12:50:45 +08:00
|
|
|
if (opts == null) { opts = {}; }
|
|
|
|
|
|
|
|
var adaptor = this.robot.requireAdaptor(adaptorName).adaptor({
|
2014-02-28 03:17:02 +08:00
|
|
|
name: adaptorName,
|
|
|
|
connection: connection,
|
|
|
|
extraParams: opts
|
|
|
|
});
|
2014-03-01 12:50:45 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
if (CylonConfig.testing_mode) {
|
2014-03-01 12:50:45 +08:00
|
|
|
var testAdaptor = this.robot.requireAdaptor('test').adaptor({
|
2013-11-24 09:13:20 +08:00
|
|
|
name: adaptorName,
|
|
|
|
connection: connection,
|
|
|
|
extraParams: opts
|
|
|
|
});
|
2014-03-01 12:50:45 +08:00
|
|
|
return proxyTestStubs(adaptor.commands(), testAdaptor);
|
2014-02-28 03:17:02 +08:00
|
|
|
} else {
|
2014-03-01 12:50:45 +08:00
|
|
|
return adaptor;
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
|
|
|
};
|
2013-11-24 09:13:20 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Requires a hardware adaptor and adds it to @robot.adaptors
|
|
|
|
//
|
|
|
|
// adaptorName - module name of adaptor to require
|
|
|
|
//
|
|
|
|
// Returns the module for the adaptor
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.requireAdaptor = function(adaptorName) {
|
|
|
|
if (this.robot.adaptors[adaptorName] == null) {
|
|
|
|
this.robot.registerAdaptor("cylon-" + adaptorName, adaptorName);
|
|
|
|
this.robot.adaptors[adaptorName].register(this);
|
|
|
|
}
|
|
|
|
return this.robot.adaptors[adaptorName];
|
|
|
|
};
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Registers an Adaptor with the Robot
|
|
|
|
//
|
|
|
|
// moduleName - name of the Node module to require
|
|
|
|
// adaptorName - name of the adaptor to register the moduleName under
|
|
|
|
//
|
|
|
|
// Returns the registered module name
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.registerAdaptor = function(moduleName, adaptorName) {
|
|
|
|
if (this.adaptors[adaptorName] == null) {
|
2014-03-27 05:32:52 +08:00
|
|
|
try {
|
|
|
|
return this.adaptors[adaptorName] = require(moduleName);
|
|
|
|
} catch (e) {
|
2014-04-02 01:30:14 +08:00
|
|
|
if (e.code === "MODULE_NOT_FOUND") {
|
|
|
|
this._missingModuleError(moduleName);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
2014-03-27 05:32:52 +08:00
|
|
|
}
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
|
|
|
};
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Init a hardware driver
|
|
|
|
//
|
|
|
|
// driverName - driver name
|
|
|
|
// device - the Device that requested the driver be initialized
|
|
|
|
// opts - object containing options when initializing driver
|
|
|
|
//
|
|
|
|
// Returns the new driver
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.initDriver = function(driverName, device, opts) {
|
2014-03-01 12:50:45 +08:00
|
|
|
if (opts == null) { opts = {}; }
|
|
|
|
|
|
|
|
var driver = this.robot.requireDriver(driverName).driver({
|
2014-02-28 03:17:02 +08:00
|
|
|
name: driverName,
|
|
|
|
device: device,
|
|
|
|
extraParams: opts
|
|
|
|
});
|
2014-03-01 12:50:45 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
if (CylonConfig.testing_mode) {
|
2014-03-01 12:50:45 +08:00
|
|
|
var testDriver = this.robot.requireDriver('test').driver({
|
2013-11-24 09:13:20 +08:00
|
|
|
name: driverName,
|
|
|
|
device: device,
|
|
|
|
extraParams: opts
|
|
|
|
});
|
2014-03-01 12:50:45 +08:00
|
|
|
|
|
|
|
return proxyTestStubs(driver.commands(), testDriver);
|
2014-02-28 03:17:02 +08:00
|
|
|
} else {
|
2014-03-01 12:50:45 +08:00
|
|
|
return driver;
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
2014-02-28 09:39:02 +08:00
|
|
|
};
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Requires module for a driver and adds it to @robot.drivers
|
|
|
|
//
|
|
|
|
// driverName - module name of driver to require
|
|
|
|
//
|
|
|
|
// Returns the module for driver
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.requireDriver = function(driverName) {
|
|
|
|
if (this.robot.drivers[driverName] == null) {
|
|
|
|
this.robot.registerDriver("cylon-" + driverName, driverName);
|
|
|
|
this.robot.drivers[driverName].register(this);
|
|
|
|
}
|
|
|
|
return this.robot.drivers[driverName];
|
|
|
|
};
|
2013-11-05 03:57:27 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Registers an Driver with the Robot
|
|
|
|
//
|
|
|
|
// moduleName - name of the Node module to require
|
|
|
|
// driverName - name of the driver to register the moduleName under
|
|
|
|
//
|
|
|
|
// Returns the registered module nam//
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.registerDriver = function(moduleName, driverName) {
|
|
|
|
if (this.drivers[driverName] == null) {
|
2014-03-27 05:32:52 +08:00
|
|
|
try {
|
|
|
|
return this.drivers[driverName] = require(moduleName);
|
|
|
|
} catch (e) {
|
2014-04-02 01:30:14 +08:00
|
|
|
if (e.code === "MODULE_NOT_FOUND") {
|
|
|
|
this._missingModuleError(moduleName);
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
2014-03-27 05:32:52 +08:00
|
|
|
}
|
2014-02-28 03:17:02 +08:00
|
|
|
}
|
|
|
|
};
|
2013-12-07 23:18:31 +08:00
|
|
|
|
2014-02-28 06:42:33 +08:00
|
|
|
// Public: Returns basic info about the robot as a String
|
|
|
|
//
|
|
|
|
// Returns a String
|
2014-02-28 03:17:02 +08:00
|
|
|
Robot.prototype.toString = function() {
|
|
|
|
return "[Robot name='" + this.name + "']";
|
|
|
|
};
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-03-27 05:32:52 +08:00
|
|
|
Robot.prototype._missingModuleError = function(module) {
|
|
|
|
var string = "Cannot find the '" + module + "' module. ";
|
|
|
|
string += "Please install it with 'npm install " + module + "' and try again.";
|
|
|
|
|
|
|
|
console.log(string);
|
|
|
|
|
|
|
|
process.emit('SIGINT');
|
|
|
|
};
|
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
return Robot;
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
})(EventEmitter);
|
|
|
|
});
|
2013-10-25 05:25:42 +08:00
|
|
|
|
2014-02-28 03:17:02 +08:00
|
|
|
module.exports = Cylon.Robot;
|