-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleap_sphero.js
141 lines (124 loc) · 5.96 KB
/
leap_sphero.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
var frameCount = 0;
var speedFlag = process.argv[2]; //1 for speed controlled by hand position, everything else is constant
var Cylon = require('cylon');
Cylon.robot({
name: 'leapBot',
connections: {
leapmotion: { adaptor: 'leapmotion' }
},
devices: {
leapmotion: { driver: 'leapmotion', connection: 'leapmotion' },
},
work: function(my) {
}
}).start();
Cylon.robot({
name: 'bb8Bot',
connections: {
bluetooth: { adaptor: 'central', uuid: '4d131c5eb1d24763ae6253342020d5f4', module: 'cylon-ble'}
},
devices: {
bb8: { driver: 'bb8', module: 'cylon-sphero-ble'}
},
work: function(my) {
leap.on('frame', function(frame) {
frameCount++;
if(frameCount % 20 != 0) {return;}
if(frame.hands.length > 0) {
if (frame.hands[0].sphereCenter[1] > 300){
//gesture support
if(frame.data.gestures.length > 0) {
if(frame.data.gestures[0].type == "circle"){
console.log("Disco Party Mode Activated!");
my.bb8.spin("left", 250);
my.bb8.randomColor();
}
}
else { //just stop if no gestures and hand is above Y axis too high
my.bb8.stop();
my.bb8.color(0xFFFFFF);
}
}
else {//movement if hand is below 300 in Y axis
//move forward
if (frame.hands[0].sphereCenter[2] < -50) {
//move forward right //orange ff8000
if (frame.hands[0].sphereCenter[0] > 50) {
console.log("moving forward right with speed of: 160. Position is: " + Math.abs(frame.hands[0].sphereCenter[0]));
my.bb8.color(0xff8000);
if(speedFlag != 1) { my.bb8.roll(160, 45); }
else { my.bb8.roll(100 + Math.abs(frame.hands[0].sphereCenter[0]), 45); }
}
//move forward left //pink ff00bf
else if (frame.hands[0].sphereCenter[0] < -50) {
console.log("moving forward left with speed of: 160. Position is: " + Math.abs(frame.hands[0].sphereCenter[0]));
my.bb8.color(0xff00bf);
if(speedFlag != 1) { my.bb8.roll(160, 315); }
else { my.bb8.roll(100 + Math.abs(frame.hands[0].sphereCenter[0]), 315); }
}
//move forward straight //red ff0000
else {
console.log("moving forward straight with speed of: 160. Position is: " + Math.abs(frame.hands[0].sphereCenter[2]));
my.bb8.color(0xff0000);
if(speedFlag != 1) { my.bb8.roll(160, 0); }
else { my.bb8.roll(100 + Math.abs(frame.hands[0].sphereCenter[2]), 0); }
}
}
//move backward
else if (frame.hands[0].sphereCenter[2] > 50) {
//move backward right //green 00ff00
if (frame.hands[0].sphereCenter[0] > 50) {
console.log("moving backward right with speed of: 160. Position is: " + Math.abs(frame.hands[0].sphereCenter[0]));
my.bb8.color(0x00ff00);
if(speedFlag != 1) { my.bb8.roll(160, 135); }
else { my.bb8.roll(100 + Math.abs(frame.hands[0].sphereCenter[0]), 135); }
}
//move backward left //blue 0000ff
else if (frame.hands[0].sphereCenter[0] < -50) {
console.log("moving backward left with speed of: 160. Position is: " + Math.abs(frame.hands[0].sphereCenter[0]));
my.bb8.color(0x0000ff);
if(speedFlag != 1) { my.bb8.roll(160, 225); }
else { my.bb8.roll(100 + Math.abs(frame.hands[0].sphereCenter[0]), 225); }
}
//move backward straight //cyan 00ffff
else {
console.log("moving backward straight with speed of: 160. Position is: " + Math.abs(frame.hands[0].sphereCenter[2]));
my.bb8.color(0x00ffff);
if(speedFlag != 1) { my.bb8.roll(160, 180); }
else { my.bb8.roll(100 + Math.abs(frame.hands[0].sphereCenter[2]), 180); }
}
}
//move right //yellow ffff00
else if (frame.hands[0].sphereCenter[2] < 50 && frame.hands[0].sphereCenter[2] > -50 && frame.hands[0].sphereCenter[0] > 0) {
//console.log("moving right with speed of: " + 3*(Math.abs(frame.hands[0].sphereCenter[0])));
//my.bb8.roll(3*(Math.abs(frame.hands[0].sphereCenter[0])), 45);
console.log("moving right with speed of: 160. Position is: " + Math.abs(frame.hands[0].sphereCenter[0]));
my.bb8.color(0xffff00);
if(speedFlag != 1) { my.bb8.roll(160, 90); }
else { my.bb8.roll(100 + Math.abs(frame.hands[0].sphereCenter[0]), 90); }
}
//move left //purple bf00ff
else if (frame.hands[0].sphereCenter[2] < 50 && frame.hands[0].sphereCenter[2] > -50 && frame.hands[0].sphereCenter[0] < 0) {
//console.log("moving left with speed of: " + 3*(Math.abs(frame.hands[0].sphereCenter[0])));
//my.bb8.roll(3*(Math.abs(frame.hands[0].sphereCenter[0])), 315);
console.log("moving left with speed of: 160. Position is: " + Math.abs(frame.hands[0].sphereCenter[0]));
my.bb8.color(0xbf00ff);
if(speedFlag != 1) { my.bb8.roll(160, 270); }
else { my.bb8.roll(100 + Math.abs(frame.hands[0].sphereCenter[0]), 270); }
}
//stop if not going forward, backward, left, or right
else {
my.bb8.color(0xFFFFFF);
my.bb8.stop();
}
}
}
else { //frame.hands.length = 0
my.bb8.color(0xFFFFFF);
my.bb8.stop();
}
});
}
}).start();
bb8 = Cylon.MCP.robots.bb8Bot.devices.bb8;
leap = Cylon.MCP.robots.leapBot.devices.leapmotion;