-
Notifications
You must be signed in to change notification settings - Fork 3
/
swarm-cli.js
153 lines (133 loc) · 3 KB
/
swarm-cli.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
145
146
147
148
149
150
151
152
var
keypress = require( "keypress" ),
libswarm = require( "./lib/swarm" ),
swarm = new libswarm(),
speed = { step: 10, min: -100, max: 100 }
;
// Add a couple of drones
//swarm.add("127.0.0.1");
//swarm.add("192.168.1.51");
swarm.add("192.168.1.52");
//swarm.add("192.168.1.53");
// Add some state
function resetSwarm() {
swarm.x = 0;
swarm.y = 0;
swarm.z = 0;
swarm.a = 0;
}
resetSwarm();
// Yo, how's this work?
console.log( "" );
console.log( " CONTROLS " );
console.log( "" );
console.log( " [ esc ] takeoff" );
console.log( " [ space ] land" );
console.log( " [ q ] quit" );
console.log( " wasd / up / down as expected" );
console.log( "" );
process.stdin.setRawMode( true );
process.stdin.resume();
process.stdin.on( "keypress", onKeypress);
function onKeypress(ch,key){
// Cancel setInterval
// (This is used to resend some commands as a fail-safe).
// Forward / Backward
if ( key.name === "w" ) {
if (swarm.y != speed.max)
swarm.y += speed.step;
}
if ( key.name === "s" ) {
if (swarm.y != speed.min)
swarm.y -= speed.step;
}
// Left / Right
if ( key.name === "a" ) {
if (swarm.x != speed.max)
swarm.x += speed.step;
}
if ( key.name === "d" ) {
if (swarm.x != speed.min)
swarm.x -= speed.step;
}
// Up / Down
if ( key.name === "up" ) {
if (swarm.z != speed.max)
swarm.z += speed.step;
}
if ( key.name === "down" ) {
if (swarm.z != speed.min)
swarm.z -= speed.step;
}
// Spin Clockwise / Counterclockwise
if ( key.name === "left" ) {
if (swarm.a != speed.max)
swarm.a -= speed.step;
}
if ( key.name === "right" ) {
if (swarm.a != speed.min)
swarm.a -= speed.step;
}
// Stop
if ( key.name === "enter" ) {
resetSwarm();
swarm.send( ["stop"] );
}
// Disable emergency
if ( key.name === "p" ) {
swarm.send( ["disableEmergency"] );
}
// Takeoff
if ( key.name === "escape" ) {
swarm.send( ["disableEmergency"] );
swarm.send( ["takeoff"] );
return;
}
// Land
if ( key.name === "space" ) {
swarm.z = 0;
swarm.send( ["land"] );
return;
}
if ( key.name === "q" ) {
swarm.send( ["land"] );
console.log( "Goodbye!" );
var bye = function(){
process.exit(0);
};
setTimeout(bye ,1000);
}
sendAll();
}
function sendAll() {
// X - Left / Right
if ( swarm.x >= 0 ) {
swarm.send( ["left", swarm.x / speed.max] );
}
if (swarm.x <= 0 ) {
swarm.send( ["right", swarm.x / -speed.max] );
}
// Y - Front / Back
if ( swarm.y >= 0 ) {
swarm.send( ["front", swarm.y / speed.max] );
}
if (swarm.y <= 0 ) {
swarm.send( ["back", swarm.y / -speed.max] );
}
// Z - Up / Down
if ( swarm.z >= 0 ) {
swarm.send( ["up", swarm.z / speed.max] );
}
if (swarm.z <= 0 ) {
swarm.send( ["down", swarm.z / -speed.max] );
}
// A - Clockwise / Counter-clockwise
if ( swarm.a >= 0 ) {
swarm.send( ["clockwise", swarm.a / speed.max] );
}
if (swarm.a <= 0 ) {
swarm.send( ["counterClockwise", swarm.a / -speed.max] );
}
}
// Let's rock!
keypress( process.stdin );