mambo-angular-service
Version:
Angular Service to control Parrot Mambo drone
216 lines • 8.61 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const rxjs_1 = require("rxjs");
const _1 = require(".");
class Drone {
constructor(device) {
this.device = device;
this.roll = 0;
this.pitch = 0;
this.yaw = 0;
this.altitude = 0;
this.flightLoopHandle = -1;
this.ready = false;
}
get connection$() {
return this.connectionSubject.asObservable();
}
connect() {
return __awaiter(this, void 0, void 0, function* () {
this.connectionSubject = new rxjs_1.Subject();
// [web-ble] Connect to the device
const server = yield this.device.gatt.connect();
console.log('Drone connected');
yield this.initialise(server);
});
}
initialise(server) {
return __awaiter(this, void 0, void 0, function* () {
yield this.prepareCharacteristics(server);
yield this.initialiseFlightDefaults();
this.startFlightLoop();
this.listenToOnDisconnected();
yield this.flightStatus.startNotifications();
this.connectionSubject.next(true);
console.log('Drone ready to fly');
});
}
prepareCharacteristics(server) {
return __awaiter(this, void 0, void 0, function* () {
// [web-ble] Get Services
const instructionService = yield server.getPrimaryService(_1.MamboUUIDs.serviceUUIDa);
const notificationService = yield server.getPrimaryService(_1.MamboUUIDs.serviceUUIDb);
this.flightCommandInstructions = new _1.DroneCharacteristic(
// [web-ble] Get Commands Characteristic
yield instructionService.getCharacteristic(_1.MamboUUIDs.characteristic_command_instructions));
this.flightParamsInstructions = new _1.DroneCharacteristic(
// [web-ble] Get Flight Params Characteristic
yield instructionService.getCharacteristic(_1.MamboUUIDs.characteristic_flight_params));
this.flightStatus = yield notificationService.getCharacteristic(_1.MamboUUIDs.characteristic_flightStatus);
});
}
initialiseFlightDefaults() {
return __awaiter(this, void 0, void 0, function* () {
// info: Setting max altitude to 2m
yield this.setMaxAltitude(2);
// info: Setting max tilt to 40% (20° max)
yield this.setMaxTilt(40);
// info: Setting max vertical speed to 0.5 m/s
yield this.setMaxVerticalSpeed(0);
// info: Setting max rotation speed to 150 °/s
yield this.setMaxRotationSpeed(150);
});
}
listenToOnDisconnected() {
this.device.addEventListener('gattserverdisconnected', () => this.onDisconnected());
}
onDisconnected() {
console.log('Drone disconnected');
this.stopFlightLoop();
// this.flightStatus.stopNotifications();
this.connectionSubject.complete();
}
/**
* Convenience method for setting the drone's altitude limitation
* @param {Integer} altitude the altitude in meters (2m-10m for Airborne Cargo / 2m - 25m for Mambo)
*/
setMaxAltitude(maxAltitude) {
console.log('setMaxAltitude: ' + maxAltitude);
maxAltitude = this.ensureBoundaries(maxAltitude, 2, 25);
return this.flightCommandInstructions.write([8, 0, 0, maxAltitude, 0]);
}
/**
* Convenience method for setting the drone's max tilt limitation
* @param {integer} tilt The max tilt from 0-100 (0 = 5° - 100 = 20°)
*/
setMaxTilt(maxTilt) {
console.log('setMaxTilt: ' + maxTilt);
maxTilt = this.ensureBoundaries(maxTilt, 0, 100);
return this.flightCommandInstructions.write([8, 1, 0, maxTilt, 0]);
}
/**
* Convenience method for setting the drone's max vertical speed limitation
* @param {integer} verticalSpeed The max vertical speed from 0.5m/s - 2m/s
*/
setMaxVerticalSpeed(maxVerticalSpeed) {
console.log('setMaxVerticalSpeed: ' + maxVerticalSpeed);
maxVerticalSpeed = this.ensureBoundaries(maxVerticalSpeed, 0, 100);
return this.flightCommandInstructions.write([1, 0, 0, maxVerticalSpeed, 0]);
}
/**
* Convenience method for setting the drone's max rotation speed limitation
* @param {integer} tilt The max rotation speed from (50°-360° for Airborne Cargo / 50° - 180° for Mambo)
*/
setMaxRotationSpeed(maxRotationSpeed) {
console.log('setMaxRotationSpeed: ' + maxRotationSpeed);
maxRotationSpeed = this.ensureBoundaries(maxRotationSpeed, 50, 180);
return this.flightCommandInstructions.write([1, 1, 0, maxRotationSpeed, 0]);
}
startFlightLoop() {
this.flightLoopHandle = window.setInterval(() => this.sendFlightParams(), 100);
}
stopFlightLoop() {
if (this.flightLoopHandle > 0) {
clearInterval(this.flightLoopHandle);
this.flightLoopHandle = -1;
}
}
/**
* Instructs the drone to take off
*/
takeOff() {
this.flightCommandInstructions.writeWithoutResponse([0, 1, 0]);
}
/**
* Instructs the drone to land
*/
land() {
this.flightCommandInstructions.writeWithoutResponse([0, 3, 0]);
this.roll = 0;
this.pitch = 0;
this.yaw = 0;
this.altitude = 0;
}
/**
* Instructs the drone to fire the cannon
*/
fire() {
this.flightCommandInstructions.writeWithoutResponse([16, 2, 0, 0, 0, 0, 0, 0]);
}
/**
* Sets the roll, pitch, yaw and altitude of drone's flight params in one call
* @param roll turn speed, expected value from -1 (move left) to 1 (move right)
* @param pitch turn speed, expected value from -1 (move back) to 1 (move forward)
* @param yaw turn speed, expected value from -1 (turn counter-clocwise) to 1 (turn clocwise)
* @param altitude turn speed, expected value from -1 (move down) to 1 (move up)
*/
sendFlightParams() {
const command = [0, 2, 0, 1, this.roll, this.pitch, this.yaw, this.altitude, 0, 0, 0, 0, 0, 0, 0, 0];
this.flightParamsInstructions.writeWithoutResponse(command);
}
updateFlightParams(roll, pitch, yaw, altitude) {
this.setRoll(roll);
this.setPitch(pitch);
this.setYaw(yaw);
this.setAltitude(altitude);
}
/**
* Sets the roll speed of drone's flight params
* @param roll turn speed, expected value from -1 (move left) to 1 (move right)
*/
setRoll(roll) {
this.roll = this.convertToInputValue(roll);
}
/**
* Sets the pitch of drone's flight params
* @param pitch turn speed, expected value from -1 (move back) to 1 (move forward)
*/
setPitch(pitch) {
this.pitch = this.convertToInputValue(pitch);
}
/**
* Sets the turn speed of drone's flight params
* @param yaw turn speed, expected value from -1 (turn counter-clocwise) to 1 (turn clocwise)
*/
setYaw(yaw) {
this.yaw = this.convertToInputValue(yaw);
}
/**
* Sets the altitude of drone's flight params
* @param altitude turn speed, expected value from -1 (move down) to 1 (move up)
*/
setAltitude(altitude) {
this.altitude = this.convertToInputValue(altitude);
}
convertToInputValue(value) {
value = this.ensureBoundaries(value, -1, 1);
if (value >= 0) {
return Math.round(value * 127);
}
else {
return 255 + Math.round(value * 127);
}
}
ensureBoundaries(val, min, max) {
if (val < min) {
return min;
}
if (val > max) {
return max;
}
return val;
}
ensureBoundariesRound(val, min, max) {
return Math.round(this.ensureBoundaries(val, min, max));
}
}
exports.Drone = Drone;
//# sourceMappingURL=drone.js.map