-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·131 lines (112 loc) · 2.89 KB
/
index.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
const express = require("express");
const app = express();
var server = require("http").createServer(app);
const keypress = require("keypress");
const tello = require("./2023-tello.js");
const readline = require("readline");
var socket = null;
var port = 8888;
// Server
server.on("error", function (e) {
console.log(e);
});
var Server = function (browserPort) {
this.io = require("socket.io")(server);
this.io.on("connection", this.handleConnection);
app.use(express.static(__dirname));
server.listen(browserPort, function () {
console.log("Server listening at port %d", browserPort);
});
};
let height = 120; //cm
let heightMaxThreshold = 400;
let heightMinThreshold = 10;
Server.prototype.handleConnection = function (sock) {
socket = sock;
socket.on("error", () => {});
socket.on("up", (distance) => {
if (height < heightMaxThreshold) {
console.log("up", distance, height);
tello.up(distance);
height = height + distance;
} else {
socket.emit("maxReached", height);
console.log("Height Max Reached");
}
});
socket.on("down", (distance) => {
if (height > heightMinThreshold) {
console.log("down", distance, height);
tello.down(distance);
height = height - distance;
} else {
socket.emit("minReached", height);
console.log("Height Min Reached");
}
});
socket.on("land", () => {
console.log("land");
tello.land();
});
socket.on("takeoff", () => {
console.log("takeoff");
height = 120;
tello.takeoff();
});
/*
socket.on("power", (msg) => {
power = msg;
console.log(msg);
});*/
};
Server.prototype.init = () => {
console.log("Server initialized!");
};
/*
Server.prototype.sendClientMsg = (id, msg) => {
if (socket) {
socket.emit(id, { msg: msg });
}
};
*/
let tookoff = false;
/*
function moveDrone(){
let distance = 30;
let threshold = 0.2;
let drone_speed = Math.floor(70 * engagement + 30);
if(engagement > threshold) {
if(tookoff == false) {
tookoff = true;
console.log('Taking off');
tello.takeoff();
setTimeout(moveDrone, 5000);
return;
}
console.log('Moving drone ' + distance + ' cm at ' + drone_speed + ' cm/s given engagement ' + Math.round(engagement * 100) / 100)
tello.speed(drone_speed);
tello.forward(distance);
setTimeout(moveDrone, distance/drone_speed * 1000 + 200);
} else {
console.log('Engagement (' + Math.round(engagement * 100) / 100 + ') not past threshold of ' + threshold);
setTimeout(moveDrone, 200);
}
}*/
server = new Server(port);
server.init();
//tello.init();
//tello.battery();
//setTimeout(() => tello.takeoff(), 3000);
//setTimeout(() => tello.land(), 3000);
/*
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Press enter to start ", (answer) => {
//moveDrone();
tello.takeoff();
//setTimeout(() => tello.land(), 3000);
rl.close();
});
*/