-
Notifications
You must be signed in to change notification settings - Fork 0
/
tellokeepalive.h
227 lines (198 loc) · 8.14 KB
/
tellokeepalive.h
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
MarTELLO - Brain-Drone Interface for Ryze Tello Drones
Copyright (C) 2021 Barkin Ilhan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If no:t, see <https://www.gnu.org/licenses/>.
Contact info:
E-Mail: [email protected]
Website: http://icon.unrlabs.org/staff/barkin/
Repo: https://github.com/4e0n/
*/
#ifndef TELLO_KEEPALIVE_H
#define TELLO_KEEPALIVE_H
#include <QThread>
#include <QMutex>
#include <QtNetwork>
#include "tellostruct.h"
class TelloKeepAlive : public QThread {
Q_OBJECT
public:
TelloKeepAlive(QObject* p,TelloStruct *ts,QMutex *m,bool *t) : QThread(p) {
parent=p; tello=ts; mutex=m; active=t;
}
protected:
virtual void run() {
QTime timer1,timer2,timer3,respGrace; bool wasDead,wasAlive; int sba;
QByteArray cmd; QString cmdResult; QProcess p; QString pingCmd;
qDebug("Keepalive Thread: Activated..");
wasDead=true; wasAlive=true; socket=new QUdpSocket();
p.setStandardOutputFile("/dev/null");
pingCmd=QString("ping -c 1 -w 1 -q ").append(tello->ip);
//qDebug("%s",pingCmd.toAscii().data());
timer1.start(); timer2.start(); respGrace.start();
while (*active) {
if (timer1.elapsed()>=250) { // KEEP ALIVE & AUTOCONNECT - ITSELF
p.start(pingCmd); p.waitForFinished();
if (p.exitCode()==0) { // PING ALIVE
if (wasDead) { wasDead=false; wasAlive=true;
// IF UNCONNECTED, CONNECT
if (socket->state()==QAbstractSocket::UnconnectedState)
socket->connectToHost(tello->ip,8889);
while (socket->state()!=QAbstractSocket::ConnectedState);
// ENTER COMMAND MODE AFTER CONNECTION
if (socket->write(QByteArray("command").data())==-1)
qDebug("ERROR !! during data writing to socket->");
// WAIT FOR OK, WITHOUT ANY ENTRAPMENT INSIDE WHILE LOOP (2seconds max)
respGrace.restart();
//while(respGrace.elapsed()<500 && (sba=socket->pendingDatagramSize())<=0);
while(respGrace.elapsed()<500 && (sba=socket->bytesAvailable())<=0);
if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
if (cmdResult=="ok") {
tello->connected=true;
emit signalTelloConnected();
}
}
}
} else { // PING DEAD
if (wasAlive) { wasAlive=false; wasDead=true;
tello->connected=false;
emit signalTelloConnected();
}
}
timer1.restart();
} // timer1
if (tello->connected) {
if (timer2.elapsed()>=250) {
// MAIN GETINFO LOOP - ONLY IF CONNECTED
// ***** GET INFO *****
// CURRENT SPEED
if (socket->write(QByteArray("speed?").data())==-1)
qDebug("ERROR !! during data writing to socket->");
respGrace.restart();
while(respGrace.elapsed()<500 && (sba=socket->bytesAvailable())<=0);
if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
cmdResult=QString(socket->readAll()).toAscii();
tello->speed=cmdResult.toInt();
}
// CURRENT BATTERY %
if (socket->write(QByteArray("battery?").data())==-1)
qDebug("ERROR !! during data writing to socket->");
respGrace.restart();
while(respGrace.elapsed()<500 && (sba=socket->bytesAvailable())<=0);
if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
cmdResult=QString(socket->readAll()).toAscii();
tello->battery=cmdResult.toInt();
}
// CURRENT FLIGHT TIME
if (socket->write(QByteArray("time?").data())==-1)
qDebug("ERROR !! during data writing to socket->");
respGrace.restart();
while(respGrace.elapsed()<500 && (sba=socket->bytesAvailable())<=0);
if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
cmdResult=QString(socket->readAll()).toAscii();
tello->time=cmdResult.toInt();
}
// CURRENT WIFI SNR LEVEL
if (socket->write(QByteArray("wifi?").data())==-1)
qDebug("ERROR !! during data writing to socket->");
respGrace.restart();
while(respGrace.elapsed()<500 && (sba=socket->bytesAvailable())<=0);
if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
cmdResult=QString(socket->readAll()).toAscii();
tello->wifi=cmdResult.toInt();
}
// SDK VERSION
//if (socket->write(QByteArray("sdk?").data())==-1)
// qDebug("ERROR !! during data writing to socket->");
//respGrace.restart();
//while(respGrace.elapsed()<500 && (sba=socket->bytesAvailable())<=0);
//if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
// cmdResult=QString(socket->readAll()).toAscii();
// tello->sdk=QString(socket->readAll()).toAscii().toInt();
//}
emit signalTelloInfoUpdate();
timer2.restart();
} // timer2
//if (timer3.elapsed()>=100) {
if (true) {
// MAIN APPLYCOMMAND LOOP - ONLY IF CONNECTED
// TAKEOFF, LAND, and EMERGENCY ARE HIGH PRIORITY
if (tello->cmdTakeOff && !tello->cmdLand && !tello->cmdEmergency) {
qDebug("TAKEOFF");
socket->write(QByteArray("takeoff").data());
respGrace.restart();
while(respGrace.elapsed()<5000 && (sba=socket->bytesAvailable())<=0);
if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
tello->cmdTakeOff=false;
qDebug("takeoff: %s",cmdResult.toAscii().data()); }
} else if (!tello->cmdTakeOff && tello->cmdLand && !tello->cmdEmergency) {
qDebug("LAND");
socket->write(QByteArray("land").data());
respGrace.restart();
while(respGrace.elapsed()<5000 && (sba=socket->bytesAvailable())<=0);
if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
tello->cmdLand=false;
qDebug("land: %s",cmdResult.toAscii().data()); }
} else if (!tello->cmdTakeOff && !tello->cmdLand && tello->cmdEmergency) {
qDebug("EMERGENCY");
socket->write(QByteArray("emergency").data());
respGrace.restart();
while(respGrace.elapsed()<5000 && (sba=socket->bytesAvailable())<=0);
if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
tello->cmdEmergency=false;
qDebug("emergency: %s",cmdResult.toAscii().data()); }
} else if (tello->cmd!=Tello::cmdNULL) {
// MOVEMENT DUE TO SPECIFIC KEYSTROKES (WSAD,IKJL)
if (tello->cmd==Tello::cmdForward)
// qDebug("Forward");
socket->write(QByteArray("forward 20").data());
else if (tello->cmd==Tello::cmdBackward)
// qDebug("Backward");
socket->write(QByteArray("backward 20").data());
else if (tello->cmd==Tello::cmdLeft)
// qDebug("Left");
socket->write(QByteArray("left 20").data());
else if (tello->cmd==Tello::cmdRight)
// qDebug("Right");
socket->write(QByteArray("right 20").data());
else if (tello->cmd==Tello::cmdUp)
// qDebug("Up");
socket->write(QByteArray("up 20").data());
else if (tello->cmd==Tello::cmdDown)
// qDebug("Down");
socket->write(QByteArray("down 20").data());
else if (tello->cmd==Tello::cmdYawL)
// qDebug("YawL");
socket->write(QByteArray("ccw 10").data());
else if (tello->cmd==Tello::cmdYawR)
// qDebug("YawR");
socket->write(QByteArray("cw 10").data());
respGrace.restart();
while(respGrace.elapsed()<1000 && (sba=socket->bytesAvailable())<=0);
if (sba>0) { cmdResult=QString(socket->readAll()).toAscii();
// qDebug("Move result: %s",cmdResult.toAscii().data());
}
}
}
timer3.restart();
} // timer3
} // while
delete socket;
qDebug("Keepalive Thread: Stopping..");
}
signals:
void signalTelloConnected();
void signalTelloInfoUpdate();
private:
QObject *parent; TelloStruct *tello; QMutex *mutex; bool *active;
QUdpSocket *socket;
};
#endif