Skip to content

Instantly share code, notes, and snippets.

@AlexJWayne
Created January 20, 2015 03:41
Show Gist options
  • Save AlexJWayne/40cf53a1f3c1fdf85608 to your computer and use it in GitHub Desktop.
Save AlexJWayne/40cf53a1f3c1fdf85608 to your computer and use it in GitHub Desktop.
var Eggbot, SerialPort;
SerialPort = require("serialport").SerialPort;
Eggbot = (function() {
function Eggbot(serialPath, onReady) {
this.shaftAngle = 0;
this.armAngle = 0;
this.serial = new SerialPort(serialPath);
this.serial.open(function(error) {
if (error) {
return console.log(error);
}
if (onReady) {
return setTimeout(onReady, 100);
}
});
}
Eggbot.prototype.sleepUntil = function(time) {
var i;
i = 0;
while (i < 1e7) {
if (new Date().getTime() >= time) {
break;
}
i++;
}
return time;
};
Eggbot.prototype.move = function(newShaftAngle, newArmAngle, ms) {
var armSteps, shaftSteps;
if (ms == null) {
ms = this.duration;
}
console.log("" + (Math.round(newShaftAngle)) + " \t " + (Math.round(newArmAngle)));
shaftSteps = newShaftAngle - this.shaftAngle;
armSteps = newArmAngle - this.armAngle;
this.serial.write("SM," + ms + "," + (Math.round(shaftSteps)) + "," + (Math.round(armSteps)) + "\r", function(err) {
if (err) {
return console.log(err);
}
});
this.shaftAngle = newShaftAngle;
return this.armAngle = newArmAngle;
};
Eggbot.prototype.startLoop = function(duration, updateLoop) {
this.duration = duration;
this.updateLoop = updateLoop;
this.last = new Date().getTime();
return setImmediate((function(_this) {
return function() {
return _this.loop();
};
})(this));
};
Eggbot.prototype.loop = function() {
this.last = this.sleepUntil(this.last + this.duration);
this.updateLoop(this.last);
return setImmediate((function(_this) {
return function() {
return _this.loop();
};
})(this));
};
return Eggbot;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment