-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsphero.js
More file actions
74 lines (71 loc) · 2.48 KB
/
sphero.js
File metadata and controls
74 lines (71 loc) · 2.48 KB
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
// The sphero object below mimics *some* of the basic commands
// you have access to when using the sphero.js library
module.exports =
{
// iniitial state
xPos: 0,
yPos: 0,
color: '',
// mimic Sphero's connect method
connect: function(work) {
console.log("...let's roll! \n")
work();
},
connect_random: function(work) {
this.random_roll();
console.log("...let's roll! \n")
work();
},
readOdometer: function () {
console.log("-> receiving sphero's coordinates from the odometer...");
var x = parseFloat(this.xPos.toFixed(2));
var y = parseFloat(this.yPos.toFixed(2));
return [x,y];
},
getColor: function () {
return this.color;
},
roll: function (distance, direction) {
console.log('-> moving sphero ' + distance + ' units at ' + direction + ' degrees...')
var rads = direction * ( Math.PI / 180 );
this.xPos += distance * Math.cos(rads);
this.yPos += distance * Math.sin(rads);
},
random_roll: function () {
var distance = Math.random() * 500;
var direction = Math.random() * 360;
var rads = direction * ( Math.PI / 180 );
this.xPos += distance * Math.cos(rads);
this.yPos += distance * Math.sin(rads);
},
setColor: function (color) {
console.log("-> changing sphero's color to " + color + '...');
this.color = color;
},
assertState: function (xPos, yPos, color) {
var colorMatches = false
var posMatches = false
xPos = parseFloat(xPos);
yPos = parseFloat(yPos);
console.log('-> asserting that sphero is ' + color + ' and at ' + '[' + xPos + ',' + yPos + ']...')
if (this.color == color) {
console.log('Color matches');
colorMatches = true;
} else if (color == undefined) {
console.log('Not checking color');
colorMatches = true;
} else {
console.log("Color doesn't match");
}
var position = this.readOdometer();
if (position[0] == xPos.toFixed(2) && position[1] == yPos.toFixed(2)) {
console.log('Position matches');
posMatches = true;
} else {
console.log("Position doesn't match");
console.log('Odometer reading is: ' + position);
console.log('Expecting Sphero at: ' + [xPos.toFixed(2), yPos.toFixed(2)]);
}
return (colorMatches && posMatches);
}
};