-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrobot.js
151 lines (111 loc) · 2.54 KB
/
robot.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
var Player = require('player');
var five = require('johnny-five');
var Easing = require('easing');
var board = new five.Board();
var pan;
var led;
var tilt;
var panValue;
var tiltValue;
function Queue(executor) {
this.queue = [];
this.executor = executor;
}
Queue.prototype.add = function(value, done) {
this.queue.push({ value: value, callback: done });
if(!this.running) {
this.next();
}
}
Queue.prototype.next = function() {
this.running = this.queue.length > 0;
if(!this.running) return;
var self = this;
var item = this.queue.shift()
this.executor(item.value, function() {
setImmediate(function() {
if(item.callback) item.callback();
self.next();
});
});
}
var tiltQueue = new Queue(function(value, done) {
internalTiltTo(value, done);
})
var panQueue = new Queue(function(value, done) {
internalPanTo(value, done);
})
board.on('ready', function () {
pan = new five.Servo(9);
led = new five.Led(11);
tilt = new five.Servo(10);
panValue = 90;
pan.to(panValue);
tiltValue = 90;
tilt.to(tiltValue);
});
function smooth(start, stop, steps, time, callback, complete) {
if(start === stop) return complete(stop);
var ease = Easing(steps, 'sinusoidal');
var currentStep = 0;
function step() {
var current = ease[currentStep] * (stop - start) + start;
callback(current);
currentStep++;
if(currentStep >= steps) {
return complete(current); // done
}
setTimeout(step, time);
}
step();
}
function arise() {
tiltQueue.add(10);
tiltQueue.add(170);
tiltQueue.add(90);
// tiltTo(10, function() {
// tiltTo(170, function() {
// tiltTo(90);
// });
// });
panQueue.add(10);
panQueue.add(170);
panQueue.add(90);
// panTo(10, function() {
// panTo(170, function() {
// panTo(90);
// });
// });
}
exports.arise = arise;
function fireLaser(time){
led.strobe(100);
setTimeout(function () {
led.stop().off();
}, time);
}
exports.fireLaser = fireLaser;
function internalPanTo(value, done) {
smooth(panValue, value, 120, 15, function(value) {
pan.to(value);
}, function(value) {
panValue = value;
if(done) done(value);
});
}
function panTo(value, done) {
panQueue.add(value, done);
}
exports.panTo = panTo;
function internalTiltTo(value, done) {
smooth(tiltValue, value, 120, 15, function(value) {
tilt.to(value);
}, function(value) {
tiltValue = value;
if(done) done(value);
});
}
function tiltTo(value, done) {
tiltQueue.add(value, done);
}
exports.tiltTo = tiltTo;