-
Notifications
You must be signed in to change notification settings - Fork 0
/
2023-tello.js
113 lines (92 loc) · 2.78 KB
/
2023-tello.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//import dgram from 'node:dgram';
//import { Buffer } from 'node:buffer';
const dgram = require('node:dgram');
const { Buffer } = require('node:buffer');
class Tello {
constructor() {
this.io_port = 8889
this.state_port = 8890
this.host = "192.168.10.1";
this.server = dgram.createSocket('udp4');
this.state_info = dgram.createSocket('udp4');
this.state_info.bind(this.state_port)
this.server.bind(9000)
this.server.on("message", this._on_message)
this.state_info.on("message", this._on_state)
}
_on_state(msg, info) {
console.log(msg, info)
}
_on_message(msg, info){
console.log(msg)
console.log(info)
console.log(
"Received %d bytes from %s:%d\n",
msg.length,
info.address,
info.port
);
}
send_message(message_text) {
let message = Buffer.from(message_text);
this.server.send(message, 0, message.length, this.io_port, this.host, function (err, bytes) {
if (err) throw err;
});
}
takeoff() {
console.log("takeoff")
this.send_message("command")
this.send_message("takeoff")
}
land() {
console.log("land")
this.send_message("command")
setTimeout(() => this.send_message("land"), 1000)
}
}
const tello = new Tello()
module.exports.takeoff = () => tello.takeoff();
module.exports.land = () => tello.land();
module.exports.up = (value) => tello.send_message("up " + value);
module.exports.down = (value) => tello.send_message("down " + value);
/*
setTimeout(() => {
tello.send_message("command")
console.log("Requesting batter info.")
tello.send_message("battery?")
}, 9000);
*/
// Take off and land immediately
//setTimeout(tello.takeoff.bind(tello), 3000)
//setTimeout(tello.land.bind(tello), 6000)
/*
function sendMessage(message_text) {
let message = new Buffer(message_text);
let port = 8889;
let host = "192.168.10.1";
server.send(message, 0, message.length, port, host, function (err, bytes) {
if (err) throw err;
});
}
module.exports.init = () => {
server.on("message", (msg, info) => {
console.log("Data received from server : " + msg.toString());
console.log(info);
console.log(
"Received %d bytes from %s:%d\n",
msg.length,
info.address,
info.port
);
});
sendMessage("command");
};
*/
//module.exports.exit = () => server.close();
/*
module.exports.speed = (value) => sendMessage("speed " + value);
module.exports.up = (value) => sendMessage("up " + value);
module.exports.down = (value) => sendMessage("down " + value);
module.exports.battery = () => sendMessage("battery?");
module.exports.forward = (distance) => sendMessage("forward " + distance);
*/