Commit 27b42d7e authored by TJ Horner's avatar TJ Horner

initial commit

parents
Pipeline #146 canceled with stages
in 2 minutes and 12 seconds
This diff is collapsed.
# dronelib
Control Chinese drones with Node.js.
I tested this on a "Propel Ultra-X + WiFi", but it should work on any similar model.
## Features
- Move your drone (throttle, direction, turning)
- Auto take-off
- Calibrate gyro
- Unlock motor
## Example
First, make sure you are connected to your drone's AP.
```javascript
const { Drone } = require('dronelib')
const drone = new Drone()
// Enabling will start sending commands to your drone
// and the lights on it should stop blinking
drone.enable()
// Take flight!
drone.takeOff()
```
## Fields
### `throttle`
This is the current throttle of the drone. It ranges from 0-255. Equivalent to pushing the left stick up or down.
Set it below 128 to make it go down, set it above 128 to make it go up.
```javascript
// FULL THROTTLE!
drone.throttle = 255
```
### `turn`
The current turn value of the drone. It ranges from 0-255. Equivalent to pushing the left stick left or right.
Set it below 128 to make it turn left, set it above 128 to make it turn right.
```javascript
// Spin left!
drone.turn = 0
```
### `forwardBackward`
The current forward/backward direction value of the drone. It ranges from 0-255. Equivalent to pushing the right stick up or down.
Set it below 128 to make it go forward, set it above 128 to make it go backward.
```javascript
// I hope it doesn't run into any trees
drone.forwardBackward = 255
```
### `leftRight`
The current left/right direction value of the drone. It ranges from 0-255. Equivalent to pushing the right stick left or right.
Set it below 128 to make it go left, set it above 128 to make it go right.
```javascript
// Moves the drone slightly left
drone.leftRight = 100
```
### `currentCommand`
The command that is currently being sent to the drone. You probably shouldn't touch this directly, and instead use one of the provided command methods.
## Methods
### `enable()`
Use this method to start sending commands to the drone. While enabled, this library will send a message to the drone every 100ms. This is how the drone knows it hasn't disconnected from the client.
### `disable()`
Use this method to stop sending commands to the drone.
### `takeOff()`
Take flight! This is equivalent to pressing the "Auto Take-Off/Land" button.
### `land()`
Fall with style! This is equivalent to pressing the "Auto Take-Off/Land" button.
### `calibrateGyro()`
Calibrate the drone's gyro. The user should put the drone on a flat surface before this is called.
### `toggleMotorLock()`
Toggle the drone's motor lock. This enables/disables the altitude lock. There's no way to programmatically determine whether it is enabled or disabled.
## License
This project is licensed under the GNU GPL v3.
\ No newline at end of file
module.exports = {
Drone: require('./lib/Drone')
}
\ No newline at end of file
const dgram = require('dgram')
const COMMAND_TAKE_OFF = 0x01
const COMMAND_CALIBRATE_GYRO = 0x80
const COMMAND_UNLOCK_MOTOR = 0x40
function debug(msg) {
if(process.env.DRONELIB_DEBUG) console.log(`[dronelib] ${msg}`)
}
class Drone {
constructor() {
this.enabled = false
this.sendInterval = null
this.socket = new dgram.createSocket("udp4")
this.throttle = 128
this.turn = 128
this.forwardBackward = 128
this.leftRight = 128
this.currentCommand = 0
}
enable() {
this.enabled = true
this.sendInterval = setInterval(() => { this._sendMessage() }, 100)
}
disable() {
this.enabled = false
clearInterval(this.sendInterval)
this.sendInterval = null
}
_buildMessage() {
var message = [ 0x66 ]
message.push(this.leftRight)
message.push(this.throttle)
message.push(this.forwardBackward)
message.push(this.turn)
message.push(this.currentCommand)
if(this.currentCommand === 0)
message.push(this.leftRight ^ this.throttle ^ this.forwardBackward ^ this.turn)
else
message.push(this.currentCommand)
message.push(0x99)
return Buffer.from(message)
}
_sendMessage() {
this.socket.send(this._buildMessage(), 50000, "192.168.0.1")
}
_sendCommand(cmd) {
// Send the command for 1 second-ish
if(this.currentCommand === 0) {
this.currentCommand = cmd
setTimeout(() => { this.currentCommand = 0 }, 1000)
}
}
takeOff() {
this._sendCommand(COMMAND_TAKE_OFF)
}
land() {
this._sendCommand(COMMAND_TAKE_OFF)
}
calibrateGyro() {
this._sendCommand(COMMAND_CALIBRATE_GYRO)
}
toggleMotorLock() {
this._sendCommand(COMMAND_UNLOCK_MOTOR)
}
}
module.exports = Drone
\ No newline at end of file
{
"name": "dronelib",
"version": "1.0.0",
"description": "Control your Chinese drone from Node.js",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "GPL-3.0"
}
// This is not a real test.
const Drone = require('./lib/Drone')
const drone = new Drone()
drone.enable()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment