UNPKG

swarms

Version:

The ultimate node.js library for controlling Bitcraze Crazyflie 2.0 drones

101 lines 3.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../constants"); const packet_1 = require("../packet"); const utils_1 = require("../utils"); const toc_fetcher_1 = require("./toc-fetcher"); const events_1 = require("events"); class Parameters extends events_1.EventEmitter { /** * Class for dealing with the 'parameters' port * (https://wiki.bitcraze.io/doc:crazyflie:crtp:commander) */ constructor(crazyflie) { super(); this.crazyflie = crazyflie; this.tocFetcher = new toc_fetcher_1.TOCFetcher(this.crazyflie, 0 /* PARAM */); this.crazyflie.radio.on('parameters', (ackPack) => { try { // Route the packet to the correct handler function switch (ackPack.channel) { case constants_1.CHANNELS.TOC: // Find out which command switch (ackPack.data[0]) { case constants_1.COMMANDS.TOC.GET_ITEM: this.tocFetcher.handleTOCItem(ackPack.data.slice(1)); break; case constants_1.COMMANDS.TOC.GET_INFO: this.tocFetcher.handleTOCInfo(ackPack.data.slice(1)); break; } break; case constants_1.CHANNELS.PARAM.READ: this.handleParam(ackPack.data, 'get'); break; case constants_1.CHANNELS.PARAM.WRITE: this.handleParam(ackPack.data, 'set'); break; case constants_1.CHANNELS.PARAM.MISC: break; } } catch (err) { this.emit('error', err); } }); } /** * Retrieve logging TOC from the Crazyflie. * Required before getting any logging data! */ getTOC() { return this.tocFetcher.start(); } /** * Fetch the value of a parameter from the Crazyflie */ get(item) { const packet = new packet_1.Packet(); packet.port = constants_1.PORTS.PARAMETERS; packet.channel = constants_1.CHANNELS.PARAM.READ; packet.write('int8', item.id); return this.crazyflie.radio.sendPacket(packet) .then(utils_1.waitUntilEvent(this, 'get')) .then(data => data.value); } /** * Set the value of a parameter on the Crazyflie */ set(item, value) { if (item.readOnly) { return Promise.reject(`Cannot set property "${item.group}.${item.name}" because it is read-only!`); } const packet = new packet_1.Packet(); packet.port = constants_1.PORTS.PARAMETERS; packet.channel = constants_1.CHANNELS.PARAM.WRITE; packet .write('int8', item.id) .write(item.type, value); return this.crazyflie.radio.sendPacket(packet) .then(utils_1.waitUntilEvent(this, 'set')) .then(data => data.value); } /** * Handle parameter response. Identical for both getting and setting a parameter. * (https://wiki.bitcraze.io/doc:crazyflie:crtp:param#parameter_read) * (https://wiki.bitcraze.io/doc:crazyflie:crtp:param#parameter_write) */ handleParam(data, mode) { const types = constants_1.BUFFER_TYPES(data); const id = types.int8.read(0); // Find out type of param so we can read appropriately const tocItem = this.tocFetcher.toc.getItemById(id); const paramValue = types[tocItem.type].read(1); this.emit(mode, { item: tocItem, value: paramValue }); } } exports.Parameters = Parameters; //# sourceMappingURL=parameters.js.map