-
Notifications
You must be signed in to change notification settings - Fork 1
/
drone_auto.js
158 lines (146 loc) · 4 KB
/
drone_auto.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
153
154
155
156
157
158
const spawn = require('child_process').spawn;
const py = spawn('python3', ['DroneDetection.py']);
const speed = 0.1;
var ev = '';
const readline = require('readline')
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);
const arDrone = require('ar-drone');
console.log("ar drone made");
const client = arDrone.createClient();
console.log("client made");
client.takeoff();
console.log("taking off");
//anytime we info back this process should be done.
py.stdout.on('data', function(data){
ev = data.toString().trim();
if(ev == "left"){
client.counterClockwise(speed);
console.log("turn left")
}
else if(ev == "right"){
client.clockwise(speed);
console.log("turn right")
}
else{
// console.log('Math shit');
if(ev ==0){
move(.5,0);
}
else if (ev ==30){
move(0, .5);
}
else if (ev == 330){
move(0,-.5);
}
/*rads = ev*(Math.PI/180)
console.log(rads);
if(ev>=0 & ev<=90){
move(Math.cos(rads), Math.sin(rads))
//(Math.cos(rads),0,0,Math.sin(rads));
console.log('1',rads);
}else if (ev>90 & ev<=180){
move(Math.cos(rads), Math.sin(rads))
//move(0,Math.abs(Math.cos(rads)),0,Math.sin(rads));
console.log('2',rads);
}else if (ev>180 & ev<=270){
move(Math.cos(rads), Math.sin(rads))
//move(0,Math.abs(Math.cos(rads)),Math.abs(Math.sin(rads)),0);
console.log('3',rads);
}else if (ev>270 & ev<=360){
move(Math.cos(rads), Math.sin(rads))
//move(Math.cos(rads),0,Math.abs(Math.sin(rads)),0);
console.log('4',rads);
}*/
setTimeout(function(){
client.stop();
},500)
}
});
py.stdout.on('end', function(){
client.stop();
client.land();
console.log("landing");
});
function move(f,r){
client.front(f);
client.right(r);
};
process.stdin.on('keypress', (str, key) => {
if (key.ctrl && key.name === 'c') {
client.stop();
client.land();
process.exit();
} else {
try {
keyReturn(str, key);
} catch (error) {
console.error(error);
client.stop();
client.land();
}
}
});
function keyReturn(str, key) {
if (key.name === 'w') {
up = client.up(speed);
console.log('Move Up',up);
} else if (key.name === 'a') {
conter_clockwise = client.clockwise(-speed);
console.log('Turn Counter-Clockwise', Math.abs(conter_clockwise));
} else if (key.name === 'd') {
clockwise = client.clockwise(speed);
console.log('Turn Clockwise', clockwise);
} else if (key.name === 's') {
down = client.down(speed);
console.log('Move Down', down);
} else if (key.name === 'c') {
// pngStream = client.getPngStream();
// pngStream.on('data', console.log);
// console.log('Png Stream', pngStream);
} else if (key.name === 'v') {
video = client.getVideoStream();
video.on('data', console.log);
console.log('Video', video);
} else if (key.name === 'space') {
stop = client.stop();
console.log('Stop', stop);
} else if (key.name === 'escape') {
land = client.land();
console.log('Land', land);
py.kill('SIGINT');
} else if (key.name === 'tab') {
takeoff = client.takeoff();
console.log('Take Off',takeoff);
} else if (key.name === 'return') {
// data = client.on('navdata', console.log);
// console.log('Data', data);
//
} else if (key.code === '[A') {
front = client.front(speed);
console.log('Move Forward', front);
} else if (key.code === '[B') {
back = client.back(speed);
console.log('Move Backward', back);
} else if (key.code === '[C') {
right = client.right(speed);
console.log('Move Right', right);
} else if (key.code === '[D') {
left = client.left(speed);
console.log('Move Left', left);
} else {
console.log(`str: ${str}`);
console.log(key);
}
}
function landDrone() {
client.land();
console.log('Drone landing');
}
function getInfo(str, key) {
console.log(`You pressed the ${str} key`);
console.log();
console.log(key);
console.log();
}
console.log("starting program");