-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
142 lines (113 loc) · 3.22 KB
/
index.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
var sphero = require("spheron").sphero();
var easing = require("easing");
var heading = 0;
var rollLen = 0;
var speed = 0x77;
var train = require("./train");
var rollDuration = 15;
var backlightOn = true;
var controls = new train({
left: function() {
// setHeading alters Spheros reference heading
// meaning 270 from current is now 0
// this means post turn 0 will be forward
sphero.setHeading(270);
heading = 270;
return this;
},
right: function() {
sphero.setHeading(90);
heading = 90;
return this;
},
forward: function(len) {
// async call, start queing commands
this._startQueing();
var self = this;
if(!len) {
len = 10;
}
console.log("forward %s", len);
var ticks = 0;
var ease = easing(rollDuration, "sin");
ease = ease.concat(easing(rollDuration, "sin").reverse());
var spd;
function bump() {
setTimeout(function() {
spd = Math.round(ease[ticks++] * 255);
sphero.roll(spd, heading, 2);
if(ticks < ease.length) {
bump();
} else {
self._stopQueing();
}
}, 15);
}
bump();
return this;
},
setColor: function(color) {
sphero.setRGB(color);
},
stop: function() {
console.log("stop");
sphero.roll(0x00, 0, 2);
return this;
},
toggleBackLight: function() {
sphero.setBackLED((backlightOn = !backlightOn) ? 0x00 : 0xff);
}
});
module.exports = function(dev, s, d) {
// override default speed, you want to change this value
// depending on your surface. Higher (0xff max) for rougher surfaces
// lower (0x00 min) for smoother, lower friction surfaces
s && (speed = s);
d && (rollDuration = d);
// connect with spheron
sphero.open(dev);
// wait for open or error
sphero.on("open", function() {
console.log("Sphero Connected");
// run self leveling routine
sphero.selfLevel(0,0,0);
sphero.on("message", function(msg) {
console.log(msg);
});
sphero.on("notification", function(msg) {
// 0x06 is self leveling completed successful
if(msg.ID_CODE === 0x0b && msg.DATA[0] === 0x06) {
// reenable stablilization
sphero.setStabilisation(0x01);
// set rotate to slow XXX doesn't appear to work
sphero.setRotationRate(0x00);
// errrr, forget what this is for
sphero.setTemporaryOptionFlags(0x01);
// enable back led
controls.toggleBackLight();
// enable power notifications
sphero.setPowerNotification(0x01);
console.log("Calibration Successful");
// start sequencing queued commands
controls._stopQueing();
controls.emit("ready");
} else if(msg.ID_CODE === 0x01) {
switch(msg.DATA[0]) {
case 0x01: controls.power = "Charging";
case 0x02: controls.power = "OK";
case 0x03: controls.power = "Low";
case 0x04: controls.power = "Critical";
}
}
});
});
// queue commands until Spheron connects
controls._startQueing();
// propate sphero errors to control
sphero.on("error", function(err) {
controls.emit("error", err);
});
controls.power = "Unkown";
return controls;
};
module.exports.sphero = sphero;