UNPKG

swarms

Version:

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

198 lines 7.04 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const constants_1 = require("../constants"); const packet_1 = require("../packet"); exports.defaultSetpoint = { roll: 0, pitch: 0, yaw: 0, thrust: 0 }; exports.defaultVelocityWorldSetpoint = { velocityX: 0, velocityY: 0, velocityZ: 0, yawRate: 0 }; exports.defaultZDistanceSetpoint = { roll: 0, pitch: 0, yawRate: 0, zDistance: 0 }; exports.defaultHoverSetpoint = { velocityX: 0, velocityY: 0, yawRate: 0, zDistance: 0 }; class Commander { /** * Class for dealing with the 'commander' port * (https://wiki.bitcraze.io/doc:crazyflie:crtp:commander) */ constructor(crazyflie) { this.crazyflie = crazyflie; this.lastFunction = null; // Stop setpoint interval upon disconnect this.crazyflie.radio.on('disconnect', () => { this.clearSetpointInterval(); }); } get currentSetpoint() { return this.lastArgs; } /** * We need to update the Crazyflie's position every so often otherwise it will stop after a few seconds */ startSetpointInterval() { this.clearSetpointInterval(); this.setpointInterval = setInterval(() => { switch (this.lastFunction) { case 0 /* SETPOINT */: this.setpoint(); break; case 1 /* VELOCITY_WORLD */: this.velocityWorldSetpoint(); break; case 2 /* Z_DISTANCE */: this.zDistanceSetpoint(); break; case 3 /* HOVER */: this.hoverSetpoint(); break; } }, 100); } /** * Clear the setpoint interval */ clearSetpointInterval() { if (this.setpointInterval) { clearInterval(this.setpointInterval); } } /** * Set the setpoint point */ setpoint(setpoint) { // Default to previous values let target; if (this.lastFunction === 0 /* SETPOINT */) { target = Object.assign({}, exports.defaultSetpoint, this.lastArgs, setpoint); } else { target = Object.assign({}, exports.defaultSetpoint, setpoint); } // Set thrust limits // (https://forum.bitcraze.io/viewtopic.php?t=442) if (target.thrust < 0) { target.thrust = 0; } if (target.thrust > 60000) { target.thrust = 60000; } this.lastFunction = 0 /* SETPOINT */; this.lastArgs = target; const packet = new packet_1.Packet(); packet.port = constants_1.PORTS.COMMANDER; packet .write('float', target.roll) .write('float', -target.pitch) .write('float', target.yaw) .write('uInt16', target.thrust); return this.crazyflie.radio.sendPacket(packet); } /** * Deals with the generic commander. Stops all motors and potentially makes it fall. * (https://wiki.bitcraze.io/doc:crazyflie:crtp:generic_setpoint#stop) */ stopSetpoint() { this.lastFunction = null; this.lastArgs = null; const packet = new packet_1.Packet(); packet.port = constants_1.PORTS.COMMANDER_GENERIC; packet.write('int8', constants_1.COMMANDS.COMMANDER_GENERIC.STOP); return this.crazyflie.radio.sendPacket(packet); } /** * Send Velocity in the world frame of reference setpoint. * Velocity x, y, and z are in m/s while yaw rate is in degrees/second * (https://github.com/bitcraze/crazyflie-lib-python/blob/master/cflib/crazyflie/commander.py#L91) */ velocityWorldSetpoint(setpoint) { // Default to previous values let target; if (this.lastFunction === 1 /* VELOCITY_WORLD */) { target = Object.assign({}, exports.defaultVelocityWorldSetpoint, this.lastArgs, setpoint); } else { target = Object.assign({}, exports.defaultVelocityWorldSetpoint, setpoint); } this.lastFunction = 1 /* VELOCITY_WORLD */; this.lastArgs = target; const packet = new packet_1.Packet(); packet.port = constants_1.PORTS.COMMANDER_GENERIC; packet .write('int8', constants_1.COMMANDS.COMMANDER_GENERIC.VELOCITY_WORLD) .write('float', target.velocityX) .write('float', target.velocityY) .write('float', target.velocityZ) .write('float', target.yawRate); return this.crazyflie.radio.sendPacket(packet); } /** * Setpoint with absolute height. * Roll and pitch are in degrees, yaw rate is in degrees/second, and z distance is in meters. * (https://github.com/bitcraze/crazyflie-lib-python/blob/master/cflib/crazyflie/commander.py#L104) */ zDistanceSetpoint(setpoint) { // Default to previous values let target; if (this.lastFunction === 2 /* Z_DISTANCE */) { target = Object.assign({}, exports.defaultZDistanceSetpoint, this.lastArgs, setpoint); } else { target = Object.assign({}, exports.defaultZDistanceSetpoint, setpoint); } this.lastFunction = 2 /* Z_DISTANCE */; this.lastArgs = target; const packet = new packet_1.Packet(); packet.port = constants_1.PORTS.COMMANDER_GENERIC; packet .write('int8', constants_1.COMMANDS.COMMANDER_GENERIC.Z_DISTANCE) .write('float', target.roll) .write('float', target.pitch) .write('float', target.yawRate) .write('float', target.zDistance); return this.crazyflie.radio.sendPacket(packet); } /** * Setpoint with absolute height. * Velocity x and y are in m/s, yaw rate is in degrees/second, and z distance is in meters. * (https://github.com/bitcraze/crazyflie-lib-python/blob/master/cflib/crazyflie/commander.py#L104) */ hoverSetpoint(setpoint) { // Default to previous values let target; if (this.lastFunction === 3 /* HOVER */) { target = Object.assign({}, exports.defaultHoverSetpoint, this.lastArgs, setpoint); } else { target = Object.assign({}, exports.defaultHoverSetpoint, setpoint); } this.lastFunction = 3 /* HOVER */; this.lastArgs = target; const packet = new packet_1.Packet(); packet.port = constants_1.PORTS.COMMANDER_GENERIC; packet .write('int8', constants_1.COMMANDS.COMMANDER_GENERIC.HOVER) .write('float', target.velocityX) .write('float', target.velocityY) .write('float', target.yawRate) .write('float', target.zDistance); return this.crazyflie.radio.sendPacket(packet); } } exports.Commander = Commander; //# sourceMappingURL=commander.js.map