Skip to content

Commit

Permalink
Adding Laura's changes back
Browse files Browse the repository at this point in the history
  • Loading branch information
devnook committed Dec 12, 2014
1 parent c5a00ff commit 7a07aba
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
40 changes: 31 additions & 9 deletions sphero/sphero_driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ Cylon.robot({
connection: {
name: 'sphero',
adaptor: 'sphero',
// port: '/dev/cu.Sphero-OYR-AMP-SPP'
// port: '/dev/tty.Sphero-RYR-AMP-SPP-6'
port: '/dev/cu.Sphero-RRP-AMP-SPP'
},
// device: { name: 'Sphero-RYR', driver: 'sphero' },
device: { name: 'Sphero-RRP', driver: 'sphero' },
// device: { name: 'Sphero-OYR', driver: 'sphero' },

name: ROBOT_NAME,

Expand All @@ -30,16 +29,25 @@ Cylon.robot({
rolling : false,

work: function(my) {
var white = true;
console.log('Setting up collision detection.');
my.sphero.detectCollisions();
my.sphero.on('collision', function() {
console.log("Ouch, collision!")
my.sphero.setColor('purple');
my.sphero.stop();
my.markCollision();
});

var black = true;
every((0.5).second(), function(){
if (!my.amIRolling()) {
var color = my.readMyColor();
if (white) {
my.sphero.setColor('white');
if (black) {
my.sphero.setColor('black');
} else {
my.sphero.setColor(color);
}
white = !white
black = !black
}
});
},
Expand Down Expand Up @@ -103,18 +111,32 @@ Cylon.robot({
after((units).seconds(), function() {
console.log("Sphero stopping after " + units + " seconds");
this.stop();
callback(null, {'message': "Sphero rolled " + units + " units " + direction});
var justHitSomething = this.hitSomething;
this.clearCollision();
message = "Sphero rolled " + units + " units " + direction;
if (justHitSomething) message = message + " but hit something on the way";
callback(null, {
'message': message,
'collision': justHitSomething,
});
}.bind(this));
},

stop: function() {
this.sphero.roll(0, this.myAngle, 0);
this.stoppedRolling();
}
},

hitSomething: false,
markCollision: function() {
this.hitSomething = true;
},
clearCollision: function() {
this.hitSomething = false;
},
});

function spheroStart() {
console.log('aaa')
Cylon.robots[ROBOT_NAME].on('ready', function() {
spheroReady = true;
});
Expand Down
32 changes: 16 additions & 16 deletions sphero/sphero_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ var router = new Director.http.Router({

startSpheroDriver();
function startSpheroDriver() {
var port = Number(process.argv[2]);
var port = Number(process.argv[2]);
var server = Http.createServer(spheroServerDispatch);
SpheroDriver.startAndDoWhenReady(function() {
SpheroDriver.startAndDoWhenReady(function() {
console.log("Listening on port " + port)
server.listen(port);
});
Expand Down Expand Up @@ -51,39 +51,39 @@ function printRouter() {
this.res.end();
}

function spheroStop() {
SpheroDriver.stop();
endOk(this.res);
}

function spheroRoll() {
var params = this.req.body;
var units = params.units;
var direction = params.direction;
console.log("Roll " + units + " units in direction " + direction);
var response = this.res;
SpheroDriver.roll(units, direction, function(err) {
endOk(response);
SpheroDriver.roll(units, direction, function(error, data) {
endResponse(response, error, data);
});
}

function spheroColor() {
var color = this.req.body.color;
console.log("Set color to " + color);
SpheroDriver.setColor(color, function(err) {
endOk(this.res);
SpheroDriver.setColor(color, function(error, data) {
endResponse(this.res, error, data);
}.bind(this));
}

function spheroTurn() {
var direction = this.req.body.direction;
console.log("Turning " + direction);
SpheroDriver.turn(direction, function(err) {
endOk(this.res);
SpheroDriver.turn(direction, function(error, data) {
endResponse(this.res, error, data);
}.bind(this));
}

function endOk(response, message) {
response.writeHead(200);
response.end();
function endResponse(response, error, data) {
if (error) {
response.writeHead(500);
response.end(error);
} else {
response.writeHead(200);
response.end(JSON.stringify(data));
}
}
4 changes: 2 additions & 2 deletions sphero/sphero_tests.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var SpheroDriver = require('./sphero_driver.js');

logResult = function(err, data) {
console.log("Test reads: " + data.message);
console.log("Test reads: " + JSON.stringify(data));
}

// Draws a square and turns pink
Expand All @@ -27,4 +27,4 @@ workWhenReady = function() {
setTimeout(function() {workWhenReady.call()}, 2000);
}
}
workWhenReady();
workWhenReady();

0 comments on commit 7a07aba

Please sign in to comment.