forked from amfio/crazyflie2-ble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrazyflie.js
108 lines (86 loc) · 2.55 KB
/
crazyflie.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
'use strict';
var Promise = require('bluebird');
var noble = require('noble');
var bufferpack = require('bufferpack');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var SERVICE_UUID = '000002011c7f4f9e947b43b7c00a9a08';
var CHARACTERISTIC_UUID = '000002021c7f4f9e947b43b7c00a9a08';
var UPS = 10; //updates to quadcopter per second
function Crazyflie(peripheral, characteristic) {
var _this = this;
var updateInterval = null;
var thrust = 0;
var yaw = 0;
var pitch = 0;
var roll = 0;
function update() {
var buffer = bufferpack.pack('<BfffH', [0x30, roll, pitch, yaw, thrust]);
characteristic.write(buffer, false);
}
function startUpdates() {
updateInterval = setInterval(update, 1000 / UPS);
}
function stopUpdates() {
clearInterval(updateInterval);
}
_this.disconnect = function () {
stopUpdates();
peripheral.removeAllListeners();
_this.emit('disconnect');
};
_this.setThrust = function (value) {
thrust = value;
};
_this.setYaw = function (value) {
yaw = value;
};
_this.setPitch = function (value) {
pitch = value;
};
_this.setRoll = function (value) {
roll = value;
};
peripheral.on('disconnect', _this.disconnect);
startUpdates();
}
util.inherits(Crazyflie, EventEmitter);
function getConnection() {
return new Promise(function (resolve, reject) {
noble.on('stateChange', function (state) {
if (state === 'poweredOn') {
noble.startScanning();
}
});
noble.on('discover', function (peripheral) {
if (peripheral.advertisement.localName !== 'Crazyflie') {
return;
}
Promise.promisifyAll(peripheral);
return peripheral.connectAsync().then(function () {
return peripheral.discoverServicesAsync([SERVICE_UUID]);
}).then(function (services) {
if (services.length === 0) {
return Promise.reject('No services discovered');
}
Promise.promisifyAll(services[0]);
return services[0].discoverCharacteristicsAsync([CHARACTERISTIC_UUID]);
}).then(function (characteristics) {
if (characteristics.length === 0) {
return Promise.reject('No characteristics discovered');
}
return new Crazyflie(peripheral, characteristics[0]);
}).done(function (crazyflie) {
resolve(crazyflie);
});
});
}).finally(function () {
noble.startScanning();
noble.removeAllListeners();
});
}
module.exports = {
getConnection: function (callback) {
return getConnection().nodeify(callback);
}
};