Skip to content

Instantly share code, notes, and snippets.

@BrianGenisio
Created November 1, 2014 18:18
Show Gist options
  • Save BrianGenisio/b91edccfeb51165aac6c to your computer and use it in GitHub Desktop.
Save BrianGenisio/b91edccfeb51165aac6c to your computer and use it in GitHub Desktop.
Example of controlling an Omni-directional robot (Alf) with Johnny-Five
var five = require("johnny-five");
var ports = [
{ id: "Alf", port: "/dev/tty.Alf-DevB"},
{ id: "Joystick", port: "/dev/tty.usbmodem1451" }
];
var boards = five.Boards(ports);
var normalizedPosition = { x: 0, y: 0 };
boards.on("ready", function() {
var alf = this[0];
var joystick = this[1];
var m1 = new five.Motor({
name: "M1",
controller: 'ADAMOTOV1',
board: alf
});
var m2 = new five.Motor({
name: "M2",
controller: 'ADAMOTOV1',
board: alf
});
var m3 = new five.Motor({
name: "M3",
controller: 'ADAMOTOV1',
board: alf
});
var m4 = new five.Motor({
name: "M4",
controller: 'ADAMOTOV1',
board: alf
});
alf.repl.inject({
m1: m1,
m2: m2,
m3: m3,
m4: m4,
move: move,
moveAngle: moveAngle,
stop: stop,
spin: spin
});
function getComponents(angle, magnitude) {
var vx = Math.cos(angle) * magnitude;
var vy = Math.sin(angle) * magnitude;
return {
vx: vx,
vy: vy
}
}
function getVectors(vx, vy) {
var c = Math.sqrt(3/2);
return {
w1: -vx,
w2: 0.5 * vx - c * vy,
w3: 0.5 * vx + c * vy
}
}
function moveWheel(motor, magnitude) {
var minValue = 0;
if(magnitude < 0 ) {
motor.rev(Math.abs(magnitude) + minValue);
} else {
motor.fwd(magnitude + minValue);
}
}
function moveAngle(angle, magnitude) {
var components = getComponents(angle, magnitude);
move(components.vx, components.vy);
}
function move(vx, vy) {
var wheels = getVectors(vx, vy);
moveWheel(m1, wheels.w1);
moveWheel(m2, wheels.w2);
moveWheel(m3, wheels.w3);
}
function spin() {
m1.fwd();
m2.fwd();
m3.fwd();
}
function stop() {
m1.stop();
m2.stop();
m3.stop();
m4.stop();
}
var position = { x: 0, y: 0 };
var x = five.Sensor({
pin: 'A0',
board: joystick
});
var y = five.Sensor({
pin: 'A1',
board: joystick
});
x.on("data", function() {
position.x = this.value;
process();
});
y.on("data", function() {
position.y = this.value;
process();
});
function normalized(position) {
var x = position.x - 528;
var y = -1 * (position.y - 513);
return {
x: position.x - 528,
y: -1 * (position.y - 513)
};
}
function process() {
normalizedPosition = normalized(position);
}
setInterval(function() {
move(normalizedPosition.x, normalizedPosition.y);
}, 200);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment