Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var ds = require("dualshock-controller");
var sphero = require("spheron").sphero();
var spheroReady = false;
sphero.on("open", function() {
console.log("sphero ready");
spheroReady = true;
sphero.setBackLED(0xff);
});
sphero.open(process.env.SPHERO_DEV);
const TO_DEGREES = 180 / Math.PI;
var controller = ds({
config: "dualShock3"
});
controller.on("error", function(err) {
console.log("some shit went wrong");
console.log(err);
});
var centered = false;
var center = {};
var offset = {};
var lastHeading = 0;
controller.on("left:move", function(pos) {
if(!centered) {
offset = center = pos;
centered = true;
console.log("centered");
}
var heading = Math.round((Math.atan2(pos.x - center.x, pos.y - center.y) * TO_DEGREES) + 180);
if(spheroReady) {
console.log(heading);
sphero.roll(0, heading, 2);
}
lastHeading = heading;
});
controller.connect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment