/
index.js
144 lines (115 loc) · 2.87 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
143
144
var Aerogel = require('aerogel');
var driver = new Aerogel.CrazyDriver();
var copter = new Aerogel.Copter(driver);
driver.findCopters()
.then(function(copters)
{
console.log(copters)
if (copters.length === 0)
{
console.error('No copters found! Is your copter turned on?');
process.exit(1);
}
var uri = copters[0];
console.log('Using copter at', uri);
return uri;
})
.then(function(uri) { return copter.connect(uri); })
.then(function() { console.log("Ready"); ready = true; })
.done();
var stdin = process.stdin
, ready = false
// without this, we would only get streams once enter is pressed
stdin.setRawMode(true)
// resume stdin in the parent process (node app won't quit all by itself
// unless an error or process.exit() happens)
stdin.resume()
// i don't want binary, do you?
stdin.setEncoding("utf8")
var thrust = 40000
// on any data into stdin
stdin.on("data", function (key) {
// ctrl-c (end of text)
if (key === "\u0003") return exit()
if (!ready) {
console.warn("Copter not ready!")
return
}
switch (key) {
case "t":
console.log("take off...")
copter.takeoff().then(function () {
copter.hover()
copter.setPitch(0)
copter.setRoll(0)
copter.setThrust(thrust)
ready = true
})
break;
case "l":
console.log("land...")
ready = false
copter.land().then(function () {
ready = true
})
break;
case "h":
console.log("hover...")
copter.hover()
break;
case "\u001b[A": // Up
console.log("up...")
copter.setPitch(15)
ready = false
setTimeout(function () {
copter.setPitch(0)
ready = true
}, 250)
break;
case "\u001b[B": // Down
console.log("down...")
copter.setPitch(-15)
ready = false
setTimeout(function () {
copter.setPitch(0)
ready = true
}, 250)
break;
case "\u001b[D": // Left
console.log("left...")
copter.setRoll(15)
ready = false
setTimeout(function () {
copter.setRoll(0)
ready = true
}, 250)
break;
case "\u001b[C": // Right
console.log("right...")
copter.setRoll(-15)
ready = false
setTimeout(function () {
copter.setRoll(0)
ready = true
}, 250)
break;
case "a":
console.log("thrust up...")
thrust = thrust + 1000
copter.setThrust(thrust)
break;
case "z":
console.log("thrust down...")
thrust = thrust - 1000
copter.setThrust(thrust)
break;
default: console.log("Unkown command %s", key)
}
})
process.on('SIGINT', exit);
// Graceful(ish) exit
function exit () {
copter.land()
copter.shutdown()
process.exit()
}