-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest-servos.js
49 lines (48 loc) · 1.64 KB
/
test-servos.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const five = require('johnny-five');
const _ = require('lodash');
const utils = require('./utilities');
const board = new five.Board();
const legServoPins = [12, 9, 8, 5];
const bodyServoPins = [ 11, 10, 7, 6];
const servoTest = _.curry((time, servos) =>
servos.forEach((servo, index) => {
const {range} = servo;
const [min, max] = range;
const currentFrame = (2 * time + 50) * index;
setTimeout(() => servo.to(min), currentFrame);
setTimeout(() => servo.to(max), currentFrame + time);
setTimeout(() => servo.to(utils.average([min, max])), currentFrame + (2 * time));
})
);
const fastServoTest = servoTest(150);
const slowServoTest = servoTest(300);
const continuousServoTest = (servos, time = 200) => {
const doubleTime = 2 * time;
const intervalTime = servos.length * doubleTime;
const customServoTest = servoTest(time);
customServoTest(servos); // immediate action
return setInterval(() => customServoTest(servos), intervalTime); // ∞
}
board.on('ready', function () {
const legServos = legServoPins.map(pin => new five.Servo({
pin,
range: [0, 180],
startAt: 90
}));
const bodyServos = bodyServoPins.map(pin => new five.Servo({
pin,
range: [45, 135],
startAt: 90
}));
const allServos = utils.sortServosByPin([...legServos, ...bodyServos]);
this.repl.inject({
legServos,
bodyServos,
allServos,
servoTest: () => fastServoTest(allServos),
continuousServoTest: () => continuousServoTest(allServos),
stopContinous: clearInterval
});
console.log('Try servoTest() or continuousServoTest() .');
console.log('Stop continuous test via stopContinous(fnId)');
});