johnny-five
Version:
The JavaScript Arduino Programming Framework.
491 lines (381 loc) • 9.8 kB
JavaScript
var five = require("../lib/johnny-five.js");
var keypress = require("keypress");
var temporal = require("temporal");
var configs = [
{ setup: { pin: 8, isInverted: true },
range: [ 35, 145 ], node: "rf", part: "coxa" },
{ setup: { pin: 9, isInverted: true },
range: [ 35, 145 ], node: "rf", part: "femur" },
{ setup: { pin: 10, isInverted: false },
range: [ 35, 145 ], node: "rf", part: "tibia" },
{ setup: { pin: 2, isInverted: true },
range: [ 35, 145 ], node: "rb", part: "coxa" },
{ setup: { pin: 3, isInverted: false },
range: [ 35, 145 ], node: "rb", part: "femur" },
{ setup: { pin: 4, isInverted: true },
range: [ 35, 145 ], node: "rb", part: "tibia" },
{ setup: { pin: 5, isInverted: true },
range: [ 35, 145 ], node: "lb", part: "coxa" },
{ setup: { pin: 6, isInverted: true },
range: [ 35, 145 ], node: "lb", part: "femur" },
{ setup: { pin: 7, isInverted: false },
range: [ 35, 145 ], node: "lb", part: "tibia" },
{ setup: { pin: 11, isInverted: true },
range: [ 35, 145 ], node: "lf", part: "coxa" },
{ setup: { pin: 12, isInverted: false },
range: [ 35, 145 ], node: "lf", part: "femur" },
{ setup: { pin: 13, isInverted: true },
range: [ 35, 145 ], node: "lf", part: "tibia" }
];
/*
rf, RF, Right Front = 1;
rb, RB, Right Back = 2;
lb, LB, Left Back = 3;
lf, LF, Left Front = 4;
*/
function scale() {
return Math.round(
five.Fn.scale.apply(null, arguments)
);
}
var siblings = {
lf: { r: "rf", l: "lb" },
rf: { r: "rb", l: "lf" },
lb: { r: "lf", l: "rb" },
rb: { r: "lb", l: "rf" }
};
(new five.Board()).on("ready", function() {
servos = configs.reduce(function(accum, config) {
if (!accum[config.node]) {
accum[config.node] = {};
}
accum[config.node][config.part] = new five.Servo(config.setup).center();
return accum;
}, {});
var task;
var all = new five.Servos();
var rf = servos.rf;
var lf = servos.lf;
var rb = servos.rb;
var lb = servos.lb;
var leg = 0;
function squat(ms) {
if (typeof ms === "undefined") {
ms = 500;
}
rf.tibia.to(140, ms);
rb.tibia.to(140, ms);
lb.tibia.to(140, ms);
lf.tibia.to(140, ms);
rf.femur.to(45, ms);
rb.femur.to(45, ms);
lb.femur.to(45, ms);
lf.femur.to(45, ms);
}
function stand() {
// this.straight();
rf.femur.to(120, 500);
rf.tibia.to(60, 500);
rb.femur.to(120, 500);
rb.tibia.to(60, 500);
lb.femur.to(120, 500);
lb.tibia.to(60, 500);
lf.femur.to(120, 500);
lf.tibia.to(60, 500);
}
function pushups() {
task = temporal.queue([
{
delay: 0,
task: squat
},
{
delay: 600,
task: stand
},
{
delay: 600,
task: pushups
}
]);
}
function rotate(degrees) {
rf.coxa.to(rf.coxa.position + degrees, 500);
lf.coxa.to(lf.coxa.position + degrees, 500);
rb.coxa.to(rb.coxa.position + degrees, 500);
lb.coxa.to(lb.coxa.position + degrees, 500);
}
function straight() {
all.to(90, 1000);
}
var limb = 0;
var limbs = configs.reduce(function(accum, config) {
if (accum.indexOf(config.node) === -1) {
accum.push(config.node);
}
return accum;
}, []);
function wave() {
var which, right, left;
which = limbs[limb];
if (++limb === 4) {
limb = 0;
}
if (!wave.inProgress) {
this.squat();
wave.inProgress = true;
}
task = temporal.queue([
{
delay: 100,
task: function() {
var l = 110;
var r = 70;
right = servos[siblings[which].r];
left = servos[siblings[which].l];
right.coxa.to(r, 500);
left.coxa.to(l, 500);
}
},
{
delay: 500,
task: function() {
var femur = 0;
var range = [ 60, 90 ];
servos[which].femur.to(45, 250);
servos[which].tibia.to(45, 250);
}
},
{
delay: 500,
task: function() {
wave.inProgress = false;
wave();
}
}
]);
}
function walk(dir) {
if (!dir) {
dir = "fwd";
}
if (task) {
task.stop();
}
task = temporal.queue([
{
wait: 250,
task: function() {
lf.coxa.to(82, 250);
lf.femur.to(111, 250);
lf.tibia.to(103, 250);
lb.coxa.to(98, 250);
lb.femur.to(111, 250);
lb.tibia.to(103, 250);
rf.coxa.to(77, 250);
rf.femur.to(70, 250);
rf.tibia.to(77, 250);
rb.coxa.to(103, 250);
rb.femur.to(70, 250);
rb.tibia.to(77, 250);
console.log( "a" );
}
},
{
wait: 750,
task: function() {
if (dir === "fwd") {
lf.femur.to(0, 250);
lf.coxa.to(130, 250);
} else {
lb.femur.to(0, 250);
lb.coxa.to(130, 250);
}
console.log( "b" );
}
},
{
wait: 250,
task: function() {
lf.coxa.to(103, 250);
lf.femur.to(70, 250);
lf.tibia.to(77, 250);
lb.coxa.to(77, 250);
lb.femur.to(70, 250);
lb.tibia.to(77, 250);
rf.coxa.to(98, 250);
rf.femur.to(111, 250);
rf.tibia.to(103, 250);
rb.coxa.to(82, 250);
rb.femur.to(111, 250);
rb.tibia.to(103, 250);
console.log( "c" );
}
},
{
wait: 750,
task: function() {
if (dir === "fwd") {
rf.femur.to(0, 250);
rf.coxa.to(50, 250);
} else {
rb.femur.to(0, 250);
rb.coxa.to(50, 250);
}
console.log( "d" );
walk(dir);
}
}
]);
}
walk.up = function() {
walk("fwd");
};
walk.down = function() {
walk("rev");
};
// Inject a Servo Arbay into the REPL as "s"
this.repl.inject({
rf: rf,
lf: lf,
rb: rb,
lb: lb,
get t() {
return task;
},
servos: servos,
all: new five.Servos(),
straight: function() {
all.to(90, 500);
},
rotate: rotate,
pushups: pushups,
squat: squat,
stand: stand,
temporal: temporal,
wave: wave,
walk: walk
});
var mode = null;
var index = 0;
var servo;
function controller( ch, key ) {
var value = 0;
var previous = mode;
var operand = 1;
var isCycling = false;
// console.log( key );
if (key) {
if (key.name === "escape") {
task.stop();
}
if (key.shift) {
if (walk[key.name]) {
walk[key.name]();
}
}
}
// if ( key ) {
// if (key.name === "r") {
// mode = "rotate";
// }
// if (key.name === "l") {
// mode = "lean";
// }
// if (key.name === "u") {
// // up(3, 5);
// }
// if (key.name === "s") {
// mode = "servo";
// }
// if (key.name === "z") {
// mode = null;
// }
// if (mode !== previous) {
// console.log( "Mode Changed: ", mode );
// }
// if (mode) {
// if (mode === "rotate") {
// value = key.name === "right" ? 1 : -1;
// if (key.shift) {
// value *= 10;
// }
// if (key.ctrl) {
// value *= 2;
// }
// rotate(value);
// }
// if (mode === "lean") {
// if (key.name === "right") {
// }
// value = key.name === "right" ? 1 : -1;
// if (key.shift) {
// value *= 10;
// }
// }
// if (mode === "servo") {
// if (key.name === "right") {
// if (key.shift) {
// while (++index % 3 !== 0);
// } else if (key.cntrl) {
// index += 3;
// } else {
// index++;
// }
// if (index >= configs.length) {
// index = 0;
// }
// isCycling = true;
// }
// if (key.name === "left") {
// if (key.shift) {
// while (--index % 3 !== 0);
// } else if (key.cntrl) {
// index -= 3;
// } else {
// index--;
// }
// if (index < 0) {
// index = configs.length - 1;
// if (key.shift) {
// index = 9;
// }
// }
// isCycling = true;
// }
// servo = servos[configs[index].node][configs[index].part];
// value = servo.position;
// if (key.shift) {
// operand = 10;
// }
// if (key.name === "up") {
// value += operand;
// }
// if (key.name === "down") {
// value -= operand;
// }
// if (isCycling) {
// console.log(
// "Switched to: %s %s", configs[index].node, configs[index].part
// );
// } else {
// console.log(
// "Moving: %s %s to %d",
// configs[index].node, configs[index].part, value
// );
// console.log( value );
// servo.to(value);
// }
// }
// // console.log( "action: ", mode, value, key );
// // modes[mode](value);
// }
// } else {
// // A number...
// }
}
keypress(process.stdin);
process.stdin.on("keypress", controller);
process.stdin.setRawMode(true);
process.stdin.resume();
});