Created
November 21, 2013 03:01
-
-
Save morganrallen/7575419 to your computer and use it in GitHub Desktop.
left overs from NKO4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Math.TAU = Math.PI + Math.PI; | |
const TO_DEGREES = 180 / Math.PI; | |
const TO_RADIANS = Math.PI / 180; | |
function headingFromDelta(x, y) { | |
var a = Math.atan2(y, x); | |
if(a < 0) a = (a + Math.PI) + Math.PI; | |
// janky rotation by 90deg | |
a = Math.round(a * TO_DEGREES); | |
a -= 270; | |
if(a < 0) a = 360 + a; | |
return a; | |
}; | |
function magFromDelta(x, y) { | |
return Math.round(Math.sqrt((x * x) + (y * y))); | |
}; | |
module.exports = function(server) { | |
var spheron = require("spheron"); | |
var sphero = spheron.sphero(); | |
sphero.open(process.env.SPHERO_DEV); | |
sphero.on("open", function() { | |
//sphero.write(commands.api.setStabalisation(false)); | |
console.log("sphero opened"); | |
sphero.getPowerState(0x01); | |
sphero.setPowerNotification(0x01); | |
}); | |
sphero.on("error", console.log.bind(console)); | |
sphero.on("oob", console.log.bind(console)); | |
sphero.on("message", console.log.bind(console)); | |
sphero.on("notification", function(data) { | |
switch(data.ID_CODE) { | |
case 0x01: // power | |
switch(parseInt(data.data.toString())) { | |
case 0x01: //changing | |
break; | |
case 0x02: //ok | |
break; | |
case 0x03: // | |
break; | |
case 0x01: //changing | |
break; | |
} | |
break; | |
} | |
}); | |
console.log("setting up socket.io"); | |
var socketio = require("socket.io"); | |
var sio = socketio.listen(server); | |
var lastHeading = 0x00; | |
sio.sockets.on("connection", function(socket) { | |
console.log("connection"); | |
socket.on("control", function(d) { | |
var heading = headingFromDelta(d[0], d[1]); | |
var mag = magFromDelta(d[0], d[1]); | |
console.log(heading, mag); | |
if(heading === 90 && mag === 0) { | |
console.log("Stopping"); | |
sphero.roll(0x00, lastHeading, 0x00); | |
} else { | |
sphero.roll(mag * 3, heading, 1); | |
lastHeading = heading; | |
} | |
}); | |
}); | |
}; | |
module.exports.headingFromDelta = headingFromDelta; | |
module.exports.magFromDelta = magFromDelta; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment