-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrone.cpp
108 lines (102 loc) · 3.36 KB
/
drone.cpp
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
#include "drone.hpp"
Drone::Drone(){
std::cout << "Binding port\n";
m_sock.bind(m_recvPort, m_recvIP);
m_sock.setBlocking(true);
std::cout << "Connecting to drone\n";
executeFunction("command");
std::this_thread::sleep_for(std::chrono::milliseconds(100));
getBattery();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
streamon();
std::this_thread::sleep_for(std::chrono::milliseconds(100));
std::cout << "starting video thread\n";
m_videoThread = std::thread(getVideo, this);
//std::this_thread::sleep_for(std::chrono::seconds(2));
}
Drone::~Drone(){
std::this_thread::sleep_for(std::chrono::seconds(3));
land();
streamoff();
};
void Drone::executeFunction(const std::string & command, const bool & waitForResponse){
const char * encodedmsg = command.c_str();
m_sock.send(encodedmsg, strlen(encodedmsg), m_sendIP, m_sendPort);
if (waitForResponse){
sf::Packet msg;
m_sock.receive(msg, m_recvIP, m_recvPort);
auto data = static_cast<const char*>(msg.getData());
auto result = std::string(data);
std::cout << "command: " << command << " received message: " << result << "\n";
}
}
void Drone::streamon(){
executeFunction("streamon");
}
void Drone::streamoff(){
executeFunction("streamoff");
}
void Drone::getBattery(){
executeFunction("battery?");
}
void Drone::takeOff(){
executeFunction("takeoff");
}
void Drone::rotate(const int & rotation){
if (rotation >= 0 ){
executeFunction("cw " + std::to_string(rotation%360));
} else {
executeFunction("ccw " + std::to_string(-(rotation%360)));
}
}
void Drone::move(const int & fb, const int & lr, const int & ud, const int & speed){
executeFunction("go " + std::to_string(fb) + " " + std::to_string(lr) + " " + std::to_string(ud) + " " + std::to_string(speed));
}
void Drone::rc(const int & lr, const int & fb, const int & ud, const int & yaw){
executeFunction("rc " + std::to_string(lr) + " " + std::to_string(fb) + " " + std::to_string(ud) + " " + std::to_string(yaw), false);
}
void Drone::land(){
executeFunction("land");
}
void getVideo(Drone * d){
cv::VideoCapture cap("udp://@0.0.0.0:11111");
zbar::ImageScanner scanner;
scanner.set_config(zbar::ZBAR_QRCODE, zbar::ZBAR_CFG_ENABLE, 1);
int frameSkip = 3;
cv::Mat frame;
cv::namedWindow("output");
while(true){
for (int i = 0; i < frameSkip; i ++){
cap.grab();
}
bool success = cap.read(frame);
if (success){
//std::cout << "success\n";
cv::Mat frameGrey;
cv::cvtColor(frame, frameGrey, CV_BGR2GRAY);
zbar::Image image(frame.cols, frame.rows, "Y800", (uchar *)frameGrey.data, frame.cols * frame.rows);
scanner.scan(image);
Qrbb bb;
if (image.get_symbols().get_size() == 1){
for(zbar::Image::SymbolIterator symbol = image.symbol_begin(); symbol != image.symbol_end(); ++symbol){
bb = Qrbb(17, *symbol);
bb.draw(frame);
const std::lock_guard<std::mutex> lock(d->m_writeMutex);
d->m_toRotate = bb.getHorizontalDegree();
d->m_toGoForwardAndBackward = -(100.f-bb.getDistanceInCm());
d->m_toGoLeftAndRight = -((bb.getCenter().x - (frame.cols/2)) * bb.cmPerPixel());
d->m_toGoUpAndDown = -((bb.getCenter().y - (frame.rows/2)) * bb.cmPerPixel());
d->m_valuesSet = true;
}
} else {
d->m_toRotate = 0;
d->m_toGoForwardAndBackward = 0;
d->m_toGoLeftAndRight = 0;
d->m_toGoUpAndDown = 0;
d->m_valuesSet = false;
}
}
cv::imshow("output", frame);
cv::waitKey(1);
}
}