-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtello_stream.cpp
More file actions
91 lines (81 loc) · 2.77 KB
/
tello_stream.cpp
File metadata and controls
91 lines (81 loc) · 2.77 KB
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
#include <chrono>
#include <iostream>
#include "opencv2/opencv.hpp"
#include "tello.h"
#include "unistd.h"
#define WIDGHT 720
#define HEIGHT 480
using tello::Tello;
int main() {
Tello tello{};
if (!tello.Bind()) {
std::cerr << "Falha na conexão com o Tello." << "\n";
return 0;
}
tello.SendCommand("streamon");
std::cout << "Comando enviado: 'streamon' \n";
std::optional<std::string> response;
for (int i = 0; i < 2; i++) {
response = tello.ReceiveResponse();
if (response.has_value()) {
std::cout << "Resposta: " << *response << "\n";
break;
}
sleep(1);
}
if (!response.has_value()) {
std::cerr << "Sem resposta para o comando 'streamon'." << "\n";
return 0;
}
cv::VideoCapture capture("udp://0.0.0.0:11111", cv::CAP_FFMPEG);
if (!capture.isOpened()) {
std::cerr << "Erro ao abrir o stream de vídeo!" << "\n";
return -1;
}
std::optional<std::string> bat;
cv::Mat frame;
cv::Mat resized_frame;
std::chrono::time_point<std::chrono::high_resolution_clock> start;
int frame_count = 0;
int fps;
bool start_timing = false;
int batx = WIDGHT - 214, baty = HEIGHT - 6;
while (true) {
tello.SendCommand("battery?");
capture >> frame;
if (!frame.empty()) {
if (!start_timing) {
cv::namedWindow("Tello");
start = std::chrono::high_resolution_clock::now();
start_timing = true;
} else {
frame_count++;
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed = end - start;
if (elapsed.count() >= 1.0) {
fps = frame_count / elapsed.count();
frame_count = 0;
start = std::chrono::high_resolution_clock::now();
}
cv::resize(frame, resized_frame, cv::Size(WIDGHT, HEIGHT));
std::string fps_text =
"FPS: " + std::to_string(static_cast<int>(fps));
cv::putText(resized_frame, fps_text, cv::Point(10, 30),
cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 0),
2);
bat = tello.ReceiveResponse();
cv::putText(resized_frame, "Bateria: " + *bat + "%",
cv::Point(batx, baty),
cv::FONT_HERSHEY_SIMPLEX, 1, cv::Scalar(0, 255, 0),
2);
cv::imshow("Tello", resized_frame);
}
}
if (cv::waitKey(1) == 'q') {
break;
}
}
capture.release();
cv::destroyAllWindows();
return 0;
}