Browse files

Initial support for functionality to find connected copters

  • Loading branch information...
1 parent a85a4cc commit 95986b48f4b2a28eb9a37434797e13b32b39e3bd @deadprogram deadprogram committed Feb 26, 2014
Showing with 71 additions and 9 deletions.
  1. +29 −1 dist/adaptor.js
  2. +1 −1 dist/commands.js
  3. +5 −3 examples/crazyflie.js
  4. +12 −0 examples/list-copters.js
  5. +22 −3 src/adaptor.coffee
  6. +2 −1 src/commands.coffee
View
30 dist/adaptor.js
@@ -42,8 +42,19 @@
};
Crazyflie.prototype.connect = function(callback) {
+ var port;
Logger.info("Connecting to Crazyflie '" + this.name + "' on port '" + this.connection.port + "'...");
- return this.copter.connect(this.connection.port.toString()).then(function() {
+ port = this.connection.port.toString();
+ Logger.info(port);
+ if (port === "none") {
+ return this.connectFirstCopter(callback);
+ } else {
+ return this.doConnect(port, callback);
+ }
+ };
+
+ Crazyflie.prototype.doConnect = function(port, callback) {
+ return this.copter.connect(port).then(function() {
callback(null);
return this.connection.emit('connect');
});
@@ -58,6 +69,23 @@
return this.copter.driver.parameters.set(param, value);
};
+ Crazyflie.prototype.connectFirstCopter = function(callback) {
+ return this.aerogelDriver.findCopters().then(function(copters) {
+ if (copters.length === 0) {
+ console.error('No copters found! Is your copter turned on?');
+ return process.exit(1);
+ } else {
+ return this.doConnect(copters[0], callback);
+ }
+ });
+ };
+
+ Crazyflie.prototype.findCopters = function(callback) {
+ return this.aerogelDriver.findCopters().then(function(copters) {
+ return callback(copters);
+ });
+ };
+
return Crazyflie;
})(Cylon.Adaptor);
View
2 dist/commands.js
@@ -14,7 +14,7 @@
namespace = require('node-namespace');
namespace("Cylon.Crazyflie", function() {
- return this.Commands = ['takeoff', 'land', 'hover', 'setPitch', 'setYaw', 'setThrust', 'shutdown', 'setParam'];
+ return this.Commands = ['takeoff', 'land', 'hover', 'setPitch', 'setYaw', 'setThrust', 'shutdown', 'setParam', 'findCopters'];
});
}).call(this);
View
8 examples/crazyflie.js
@@ -1,16 +1,18 @@
var Cylon = require('cylon');
Cylon.robot({
- connection: { name: 'crazyflie', adaptor: 'crazyflie', port: 'radio://1/10/250KPS' },
+ connection: { name: 'crazyflie', adaptor: 'crazyflie', port: 'radio://1/20/1MPS' },
device: {name: 'drone', driver: 'crazyflie'},
work: function(my) {
my.drone.on('start', function() {
+ Logger.info('yo');
+
my.drone.takeoff();
- after((10).seconds(), function() {
+ after((2).seconds(), function() {
my.drone.land();
});
- after((15).seconds(), function() {
+ after((5).seconds(), function() {
my.drone.stop();
});
});
View
12 examples/list-copters.js
@@ -0,0 +1,12 @@
+var Cylon = require('cylon');
+
+Cylon.robot({
+ connection: { name: 'crazyflie', adaptor: 'crazyflie', port: 'radio://1/20/1MPS' },
+ device: {name: 'drone', driver: 'crazyflie'},
+
+ work: function(my) {
+ my.crazyflie.findCopters(function(copters) {
+ Logger.info(copters);
+ });
+ }
+}).start();
View
25 src/adaptor.coffee
@@ -27,10 +27,17 @@ namespace "Cylon.Adaptors", ->
commands: ->
Cylon.Crazyflie.Commands
- connect: (callback) ->
+ connect: (callback) ->
Logger.info "Connecting to Crazyflie '#{@name}' on port '#{@connection.port}'..."
-
- @copter.connect(@connection.port.toString()).then ->
+ port = @connection.port.toString()
+ Logger.info port
+ if port is "none"
+ @connectFirstCopter(callback)
+ else
+ @doConnect(port, callback)
+
+ doConnect: (port, callback) ->
+ @copter.connect(port).then ->
(callback)(null)
@connection.emit 'connect'
@@ -40,3 +47,15 @@ namespace "Cylon.Adaptors", ->
setParam: (param, value) ->
@copter.driver.parameters.set(param, value)
+
+ connectFirstCopter: (callback) ->
+ @aerogelDriver.findCopters().then (copters) ->
+ if copters.length is 0
+ console.error('No copters found! Is your copter turned on?');
+ process.exit(1)
+ else
+ @doConnect(copters[0], callback)
+
+ findCopters: (callback) ->
+ @aerogelDriver.findCopters().then (copters) ->
+ return (callback)(copters);
View
3 src/commands.coffee
@@ -27,5 +27,6 @@ namespace "Cylon.Crazyflie", ->
'setYaw',
'setThrust',
'shutdown',
- 'setParam'
+ 'setParam',
+ 'findCopters'
]

0 comments on commit 95986b4

Please sign in to comment.