-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold_test.js
More file actions
374 lines (264 loc) · 10.1 KB
/
Copy pathold_test.js
File metadata and controls
374 lines (264 loc) · 10.1 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
var noble = require('noble');
var gamepad = require("gamepad");
var CLI = require('clui');
var clc = require('cli-color');
var Spinner = CLI.Spinner;
var Line = CLI.Line;
var Gauge = CLI.Gauge;
var loading = new Spinner('', ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷']);
var headers = new Line()
.padding(2)
.column('Column One', 20, [clc.cyan])
.column('Column Two', 20, [clc.cyan])
.column('Column Three', 20, [clc.cyan])
.column('Column Four', 20, [clc.cyan])
.fill()
.output();
var peripheralIdOrAddress = process.argv[2] ? process.argv[2].toLowerCase() : "ce5023d3be43";
var drone = null;
var CHARACTERISTICS = {
ERROR : "00000002000e11e1ac360002a5d5c51b",
RESPONSE : "00000001000e11e1ac360002a5d5c51b",
ACCEVENT : "00000400000111e1ac360002a5d5c51b",
LED2W : "20000000000111e1ac360002a5d5c51b",
SENSORS : "00e00000000111e1ac360002a5d5c51b",
COMMAND : "00008000000111e1ac360002a5d5c51b"
}
var padValues = {
yaw : 0,
roll : 0,
pitch : 0,
power : 1
}
var char_controller = null;
var statusDrone = {
armed :false,
accelerometer : {x : 0, y : 0, z : 0},
gyroscope : {x : 0, y : 0, z : 0},
magnetometer : {x : 0, y : 0, z : 0},
power : 0,
yaw : 0,
roll : 0,
pitch : 0,
connected : false
}
// Initialize the library
gamepad.init();
// List the state of all currently attached devices
for (var i = 0, l = gamepad.numDevices(); i < l; i++) {
// console.log(i, gamepad.deviceAtIndex());
}
setInterval(gamepad.processEvents, 16);
gamepad.on("move", function (id, axis, value) {
/* console.log("move", {
id: id,
axis: axis,
value: value,
});*/
switch(axis){
case 1 :
padValues.pitch = remapping(value,0.6627451181411743,-0.7411764860153198,0,255);
break;
case 2 :
padValues.power = remapping(value,-0.6313725709915161,0.7333333492279053,0,128);
break;
case 4 :
padValues.yaw = remapping(value,-0.6549019813537598,0.7333333492279053,0,255);
break;
case 0 :
padValues.roll = remapping(value, -0.6313725709915161,0.7568627595901489,0,255);
break;
}
});
noble.on('stateChange', function(state) {
if (state === 'poweredOn') {
noble.startScanning();
} else {
drone = null;
noble.stopScanning();
}
});
noble.on('scanStart', () => {
console.log("Scan start");
setTimeout(() => {
noble.stopScanning();
} , 20000);
});
noble.on('scanStop', () => {
console.info("Scan stopped");
if(drone){
drone.once('connect', () => {
console.info("Drone connected");
statusDrone.connected = true;
explore(drone);
});
drone.once('disconnect', () => {
console.info("Drone disconnected");
drone = null;
statusDrone.connected = false;
noble.startScanning();
});
drone.once('servicesDiscover', (services) => {
});
if(drone.status=="connected"){
drone.disconnect();
}
drone.connect();
} else {
console.info("Drone not found");
}
});
noble.on('discover', function(peripheral) {
if (peripheral.id === peripheralIdOrAddress || peripheral.address === peripheralIdOrAddress) {
drone = peripheral;
noble.stopScanning();
}
});
function calibrateDrone(){
var dataToSend = new Buffer([ 0, 0, 0, 0, 0 , 0, 2]);
if(char_controller){
char_controller.write(dataToSend, true, (error) => {
if(error) console.error("ERROR", error);
setTimeout(() => {
var dataToSend = new Buffer([ 0, 0, 0, 0, 0 , 0, 0]);
char_controller.write(dataToSend, true);
console.info("===== DRONE CALIBRATED =====");
armDrone();
},5000)
}); // data is a buffer, withoutResponse is true|false
}
}
function disarmDrone(){
statusDrone.armed = false;
command(0,0,0,0,false);
}
function armDrone(){
console.log("--- ARMING DRONE ---")
// if(padValues.power == 0){
statusDrone.armed = true;
command(0,0,0,0,true);
console.info("######## DRONE ARMED #######")
// } else {
// console.log("--- SET POWER TO 0 BEFORE! ---")
// }
setInterval(() => {
if(statusDrone.connected && statusDrone.armed){
// console.log(Gauge(padValues.power, 100, 20, 18, "POWER"));
command(padValues.yaw, padValues.roll, padValues.pitch, padValues.power, statusDrone.armed);
console.log("[CONTROLLER]", padValues)
}
}, 100);
}
function command(yaw, roll, pitch, power, armed){
var dataToSend = new Buffer([ 0, yaw, power, roll, pitch , 0, armed ? 4 : 0]);
if(char_controller){
char_controller.write(dataToSend, true, (error) => {
if(error) console.error("ERROR", error);
}); // data is a buffer, withoutResponse is true|false
}
}
function explore(peripheral) {
peripheral.discoverServices([], function(error, services) {
var servicesList = [];
var promises = [];
console.log();
console.log("======== SERVICES LOADING ========");
services.forEach((service)=>{
servicesList[service.uuid] = service;
servicesList[service.uuid]["charaList"] = [];
var auxPromise = new Promise( (resolve, reject) => {
service.discoverCharacteristics([], function(error, characteristics) {
servicesList[service.uuid]["charaList"] = characteristics;
resolve();
});
service.discoverIncludedServices([], function(error, includedServiceUuids) {
servicesList[service.uuid]["included"] = includedServiceUuids;
});
setTimeout(resolve,5000);
});
promises.push(auxPromise);
});
Promise.all(promises).then((result) => {
// console.log();
// console.log("======== SERVICES ========");
Object.keys(servicesList).forEach((uuidservice) => {
var service = servicesList[uuidservice];
// console.info("[SERVICE]",service.uuid);
service.charaList.forEach((characteristic) => {
switch(characteristic.uuid){
case CHARACTERISTICS.ERROR:
characteristic.name = "[ERROR]";
break;
case CHARACTERISTICS.RESPONSE:
characteristic.name = "[RESPONSE]";
break;
case CHARACTERISTICS.SENSORS :
characteristic.name = "[MOVE]";
break;
case CHARACTERISTICS.ACCEVENT :
characteristic.name = "[ACC. EVENT]";
break;
case CHARACTERISTICS.LED2W :
characteristic.name = "[LEDW2ST]";
break;
case CHARACTERISTICS.COMMAND :
characteristic.name = "[COMMAND]";
char_controller = characteristic;
break;
default :
characteristic.name = "[???]";
break;
}
if(characteristic.properties.indexOf("notify") != -1 &&
(characteristic.uuid==CHARACTERISTICS.ERROR || characteristic.uuid==CHARACTERISTICS.RESPONSE || characteristic.uuid==CHARACTERISTICS.COMMAND)
){
characteristic.once('notify', (state) => {console.info("|_____",characteristic.uuid, state)});
characteristic.subscribe();
}
characteristic.on('data', (data, isNotification) => {
/*
DATA : [2byte TIMESTAMP], [18 byte PAYLOAD]
*/
var timestamp = data.readUInt16LE(0) / 1000;
var payload = data.slice(2);
switch(characteristic.uuid){
case CHARACTERISTICS.ERROR:
console.error(characteristic.name, timestamp, payload.toString())
break;
case CHARACTERISTICS.RESPONSE:
console.error(characteristic.name, timestamp, payload.toString())
break;
case CHARACTERISTICS.SENSORS:
var datas = [];
for( var i = 0; i < payload.length; i = i+2){
datas.push(payload.readInt16LE(i))
}
console.info(characteristic.name,timestamp, datas);
break;
case CHARACTERISTICS.ACCEVENT:
console.info(characteristic.name,timestamp, payload)
break;
case CHARACTERISTICS.LED2W:
console.info(characteristic.name,timestamp, payload)
break;
case CHARACTERISTICS.COMMAND:
console.info(characteristic.name,timestamp, payload)
break;
default:
console.info("[NON GESTITO]", characteristic.uuid, timestamp, payload)
break;
}
});
});
});
console.log("====== SERVICES LOADED ======");
calibrateDrone();
});
});
}
function remapping(value, min1, max1, min2, max2){
value = min2 + (value - (min1)) * (max2 - min2) / (max1 - (min1))
if(value < min2) value = min2;
if(value > max2) value = max2;
return Math.ceil(value);
}