Created
November 24, 2018 22:08
-
-
Save shpyo/12c78d4c3040f7d359b0f0a209803626 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Importujemy bibliotekę dgram dzięki której możemy użyć protokołu UDP | |
var dgram = require('dgram'); | |
// Tworzymy "klasę" Tello, którą potem będziemy używać do sterowania dronem, | |
var Tello = function() { | |
// Definiujemy kilka stałych | |
// @see https://dl-cdn.ryzerobotics.com/downloads/tello/0228/Tello+SDK+Readme.pdf | |
// Domyślny port drona. | |
var PORT = 8889; | |
// Domyślny adred IP drona po połączeniu z jego WiFi. | |
var HOST = '192.168.10.1'; | |
// Ustawienie dgram by używać UDP. | |
var SOCKET_OPTIONS = { | |
type: 'udp4' | |
}; | |
// "Prywatna" zmienna utrzymująca referencję do naszego połączenia z dronem. | |
var _telloServer = null; | |
// Inicjujemy połączenie z dronem przez UDP. | |
this.connect = function() { | |
_telloServer = dgram.createSocket(SOCKET_OPTIONS, function(msg) { | |
// Wyświetlamy odpowiedź od drona (przypatne gdy wyślemy błędną wiadomość lub wystąpi jakiś błąd. | |
console.log('Response from Tello:', msg.toString()); | |
}); | |
}; | |
// Zamykamy połączenie. | |
this.closeConnection = function() { | |
_telloServer.close(function(msg) { | |
console.log('Connection closed'); | |
}); | |
} | |
// Wysyłamy wiadomość tekstową do drona. | |
this.send = function(command) { | |
var message = new Buffer(command); | |
_telloServer.send(message, 0, message.length, PORT, HOST, function(err) { | |
if (err) { | |
console.error(err, command); | |
} | |
}); | |
} | |
}; | |
exports.default = Tello; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment