-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDroneClient.cpp
290 lines (260 loc) · 7.64 KB
/
DroneClient.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <map>
#include <iostream>
#include <pthread.h>
#include <unistd.h>
#include <fstream>
#include <sstream>
using namespace std;
// Value definitions for string values
enum StringValue {
evNotDefined,
evHelp,
evQuit,
evTakeoff,
evLand,
evYaw,
evPitch,
evRoll,
evGaz,
evReset,
evFile,
evWait,
};
// Function was missing for some reason
void* memcpy(void* dest, const void* src, size_t count);
// Used to associate strings with enums
static map<string, StringValue> s_mapStringValues;
// Initialization method
static void initMap();
void *send_command(void *ptr);
void handleInput(string szInput, string val);
void runScript(string name);
// Initial Drone values
float roll = 0, pitch = 0, gaz = 0, yaw = 0;
int seq = 1, secWait = 0;
// Networking vars
int socketId, serverPort, bufferSize, size;
sockaddr_in serverAddr, clientAddr;
char buffer[200];
int main( int argc, char *argv[] ) {
string script_name;
// Initialize the string map
initMap();
// Initialize variables
string szInput = "", hostName = "localhost";
socketId = socket(AF_INET,SOCK_DGRAM,0);
serverPort = 5556;
pthread_t command_thread;
sockaddr &serverAddrCast = (sockaddr &)serverAddr;
//specify server address, port
serverAddr.sin_family=AF_INET;
serverAddr.sin_port=htons(serverPort);
struct hostent*hp=gethostbyname(hostName.c_str());
memcpy(( char *) &serverAddr.sin_addr, (char *) hp->h_addr, hp->h_length);
size = sizeof(serverAddr);
// Immediately reset the trim of the drone
bufferSize = sprintf(buffer, "AT*FTRIM=%d,\r", seq);
sendto(socketId, &buffer, bufferSize, 0, &serverAddrCast, size);
seq++;
bufferSize = 0;
// Start a thread that sends a command every 3 seconds
pthread_create(&command_thread, NULL, send_command, NULL);
// Check for any arguments (would be a script)
if(argc == 2) {
script_name = argv[1];
// Run script in argument
runScript(script_name);
} else {
// If no script given, use user input area
while(1) {
cout << "Enter a command or 'help' for list of commands: ";
cin >> szInput;
cout << "\n";
switch(s_mapStringValues[szInput]) {
case evFile:
cout << "Enter a file name for the script: ";
cin >> script_name;
runScript(script_name);
break;
case evQuit:
return 0;
break;
default: handleInput(szInput, ""); break;
}
cout << endl;
// Send the command as UDP packet to Drone
sendto(socketId, &buffer, bufferSize, 0, &serverAddrCast, size);
seq++;
bufferSize = 0;
cout << endl;
}
}
close(socketId);
return 0;
}
// Maps user inputs to enums
void initMap() {
s_mapStringValues["help"] = evHelp;
s_mapStringValues["takeoff"] = evTakeoff;
s_mapStringValues["land"] = evLand;
s_mapStringValues["yaw"] = evYaw;
s_mapStringValues["pitch"] = evPitch;
s_mapStringValues["roll"] = evRoll;
s_mapStringValues["gaz"] = evGaz;
s_mapStringValues["reset"] = evReset;
s_mapStringValues["file"] = evFile;
s_mapStringValues["q"] = evQuit;
s_mapStringValues["wait"] = evWait;
}
// Thread method that sends commands every 100ms or so
void *send_command( void * ptr) {
sockaddr &serverAddrCast = (sockaddr &)serverAddr;
// Sends a command every 1.5 seconds (right now it just sets yaz, pitch, etc as they are)
while(1) {
bufferSize = sprintf(buffer, "AT*PCMD=%d,%d,%d,%d,%d,%d\r",
seq,
1,
*(int*)(&roll),
*(int*)(&pitch),
*(int*)(&gaz),
*(int*)(&yaw) );
sendto(socketId, &buffer, bufferSize, 0, &serverAddrCast, size);
seq++;
bufferSize = sprintf(buffer, "AT*COMWDG=%d\r", seq);
sendto(socketId, &buffer, bufferSize, 0, &serverAddrCast, size);
seq++;
usleep(100000);
}
}
// Main function for handling user input
void handleInput(string szInput, string val) {
switch(s_mapStringValues[szInput]) {
case evHelp:
cout << "List of available commands:" << endl;
cout << "reset: Resets the drone's emergency state and all values (pitch, roll, gaz, yaw)" << endl;
cout << "takeoff: Tell the Drone to takeoff" << endl;
cout << "land: Tell the Drone to land" << endl;
cout << "yaw: Set the yaw (spin) for the Drone" << endl;
cout << "pitch: Set the pitch (speed forward/backward) for the Drone" << endl;
cout << "roll: Set the roll (speed left/right) for the Drone" << endl;
cout << "gaz: Set the gaz (height) for the Drone" << endl;
cout << "q: Exit program" << endl;
break;
case evTakeoff:
bufferSize = sprintf(buffer, "AT*REF=%d,%d\r", seq, 290718208);
break;
case evLand:
bufferSize = sprintf(buffer, "AT*REF=%d,%d\r", seq, 290717696);
break;
case evReset:
roll = 0;
pitch = 0;
gaz = 0;
yaw = 0;
bufferSize = sprintf(buffer, "AT*REF=%d,%d\r", seq, 290717952);
break;
case evWait:
case evYaw:
case evPitch:
case evRoll:
case evGaz:
if(val == "") {
cout << "Enter a floating point value (-1 to 1): ";
switch(s_mapStringValues[szInput]) {
case evYaw: cin >> yaw; break;
case evPitch: cin >> pitch; break;
case evRoll: cin >> roll; break;
case evGaz: cin >> gaz; break;
default: break;
}
} else {
switch(s_mapStringValues[szInput]) {
case evYaw: yaw = strtod(val.c_str(), NULL); break;
case evPitch: pitch = strtod(val.c_str(), NULL); break;
case evRoll: roll = strtod(val.c_str(), NULL); break;
case evGaz: gaz = strtod(val.c_str(), NULL); break;
case evWait:
secWait = strtol(val.c_str(), NULL, 10);
char sleepCommand[50];
// Uses /bin/sleep system command because there were problems
// with the C++ sleep() command
sprintf(sleepCommand, "/bin/sleep %d", secWait);
int result;
result = system(sleepCommand);
break;
default: break;
}
}
bufferSize = sprintf(buffer, "AT*PCMD=%d,%d,%d,%d,%d,%d\r",
seq,
1,
*(int*)(&roll),
*(int*)(&pitch),
*(int*)(&gaz),
*(int*)(&yaw) );
break;
default:
bufferSize = 0;
cout << "Invalid command" << endl;
break;
}
}
// Main function for running a script
void runScript(string name) {
sockaddr &serverAddrCast = (sockaddr &)serverAddr;
string line;
ifstream script_file (name.c_str());
// Work through the script line by line
if(script_file.is_open()) {
while(script_file.good()) {
getline(script_file, line);
cout << endl << "Read line: " << line << endl;
if(line != "") {
string sub1, sub2;
int numSubs = 0;
istringstream iss(line);
do {
if(numSubs == 0)
iss >> sub1;
if(numSubs == 1)
iss >> sub2;
numSubs++;
} while(iss && numSubs < 2);
if(numSubs == 2) {
handleInput(sub1, sub2);
} else {
handleInput(line, "");
}
if(bufferSize != 0) {
cout << "Sending command to localhost:" << serverPort << " ";
for(unsigned int i = 0; i < sizeof(buffer); i++) {
cout << buffer[i];
}
cout << endl;
sendto(socketId, &buffer, bufferSize, 0, &serverAddrCast, size);
}
seq++;
bufferSize = 0;
}
}
script_file.close();
} else {
cout << "Unable to open file";
}
}
// When compiling for ARM, I had an error saying memcpy was not found,
// so I'm just including it here
void* memcpy(void* dest, const void* src, size_t count) {
char* dst8 = (char*)dest;
char* src8 = (char*)src;
--src8;
--dst8;
while (count--) {
*++dst8 = *++src8;
}
return dest;
}