/
controller.c
395 lines (335 loc) · 9.21 KB
/
controller.c
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <pthread.h>
#include <semaphore.h>
#include <assert.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdbool.h>
#define BUFF_SIZE 4096
FILE *out_file;
int throttle = 0;
//controls last log if landed or crashed (not flying)
bool crashed = false;
bool printCrashed = false;
bool printLanded = false;
char condition[BUFF_SIZE];
char terrain[BUFF_SIZE];
char state[BUFF_SIZE];
struct addrinfo *address;
struct addrinfo *address2;
int fd;
bool isActive = true;
pthread_t logThread, serverCommandThread;
int rc3, rc4;
sem_t logSemaphore;
sem_t serverSemaphore;
void *sendCommand(void *arg);
void *getState(void *arg);
void *getTerrain(void *arg);
void *getCondition(void *arg);
void logCommand(char msg[BUFF_SIZE]);
//Logs data into a text file
void *logData(void *arg){
char *msg = (char*)arg;
out_file = fopen("DataLog.txt","a+");
fprintf(out_file, "%s\n", msg);
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, getState, NULL);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
fprintf(out_file, "%s\n", state);
fprintf(out_file, "%s\n", condition);
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, getTerrain, NULL);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
fprintf(out_file, "%s\n", terrain);
fclose(out_file);
return 0;
}
//gets input and sends prepares commands for the server
void *getInput(void *arg){
char input;
while (isActive){
char outgoing[BUFF_SIZE];
scanf("%s", &input);
switch (input){
case 'a':
if(throttle <= 90)
throttle += 10;
sprintf(outgoing, "command:!\nmain-engine:%d", throttle);
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, sendCommand, (void *) &outgoing);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
logCommand("throttle up \n");
printf("throttle: %d \n", throttle);
break;
case 's':
if(throttle >= 10)
throttle -= 10;
sprintf(outgoing, "command:!\nmain-engine:%d", throttle);
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, sendCommand, (void *) &outgoing);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
logCommand("throttle down \n");
printf("throttle: %d \n", throttle);
break;
case 'q':
printf("spin left \n");
strcpy(outgoing, "command:!\nrcs-roll:-0.5");
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, sendCommand, (void *) &outgoing);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
logCommand("spin left \n");
break;
case 'w':
printf("stop turning \n");
strcpy(outgoing, "command:!\nrcs-roll:0");
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, sendCommand, (void *) &outgoing);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
logCommand("stop spinning \n");
break;
case 'e':
printf("turn right \n");
strcpy(outgoing, "command:!\nrcs-roll:0.5");
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, sendCommand, (void *) &outgoing);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
logCommand("spin right \n");
break;
case 'r':
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, getState, NULL);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
printf("%s", state);
break;
case 'd':
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, getTerrain, NULL);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
printf("%s", terrain);
break;
case 'l':
isActive = false;
printf("exiting...\n");
break;
case 'h':
printf("a: throttle up\n");
printf("s: throttle down\n");
printf("q: spin left\n");
printf("w: stop spinning\n");
printf("e: spin right\n");
printf("r: get the current state of the lander \n");
printf("d: get terrain details beneath the lander \n");
default:
printf("invalid input, enter h for controls. \n");
break;
}
}
pthread_exit(NULL);
}
//Updates dashboard every 0.1s
void *updateDash(void *arg){
while(isActive){
sem_wait(&serverSemaphore);
rc4 = pthread_create( &serverCommandThread, NULL, getCondition, NULL);
assert(rc4 == 0);
pthread_join(serverCommandThread, NULL);
sem_post(&serverSemaphore);
char cond[BUFF_SIZE];
strcpy(cond, condition);
//removes % sign which is causing parsing error in Java file
for (int i = 0; cond[i] != '\0'; i++)
{
if (cond[i] == '%')
cond[i] = ' ';
}
sendto(fd, cond, strlen(cond), 0, address2->ai_addr, address2->ai_addrlen);
//PRINTS LAST MESSAGE IF CRASHED OR LANDED.
if(printCrashed && crashed)
{
printCrashed = false;
logCommand("--CRASHED--");
}
if(printLanded && crashed)
{
printLanded = false;
logCommand("--LANDED--");
}
usleep (100000);
}
return 0;
}
//initialises and closes the program
int main ( int argc, char *argv[] )
{
char *host = "127.0.1.1";
char *port = "65200";
char *port2 = "65250";
out_file = fopen("DataLog.txt", "w");
if (out_file == NULL)
{
printf("Error! Could not open file\n");
}
//empties file on start
fclose(out_file);
pthread_t inputThread, updateDashThread;
int rc1, rc2;
sem_init(&serverSemaphore, 0, 1);
sem_init(&logSemaphore, 0, 1);
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_DGRAM,
};
const struct addrinfo hints2 = {
.ai_family = AF_INET,
.ai_socktype = SOCK_DGRAM,
};
int err, err2;
err = getaddrinfo( host, port, &hints, &address);
if (err) {
fprintf(stderr, "Error getting address: %s\n", gai_strerror(err));
exit(1);
}
err2 = getaddrinfo( host, port2, &hints2, &address2);
if (err) {
fprintf(stderr, "Error getting address: %s\n", gai_strerror(err2));
exit(1);
}
fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd == -1) {
fprintf(stderr, "error making socket: %s\n", strerror(errno));
exit(1);
}
rc1 = pthread_create( &inputThread, NULL, getInput, NULL);
rc2 = pthread_create( &updateDashThread, NULL, updateDash, NULL);
assert(rc1 == 0);
pthread_join(inputThread, NULL);
assert(rc2 == 0);
pthread_join(updateDashThread, NULL);
sem_destroy(&serverSemaphore);
sem_destroy(&logSemaphore);
}
//gets the state of the lander and stores it in a variable
void *getState(void *arg){
char msg[BUFF_SIZE];
char incoming[BUFF_SIZE];
size_t msgsize;
strcpy(msg, "state:?");
sendto(fd, msg, strlen(msg), 0, address->ai_addr, address->ai_addrlen);
msgsize = recvfrom(fd, incoming, BUFF_SIZE, 0, NULL, 0);
incoming[msgsize] = '\0';
char *s;
s = strstr(incoming, "state:=");
if(s != NULL){
strcpy(state, incoming);
}
return 0;
}
//gets the terrain information beneath the lander and stores it in the variable
void *getTerrain(void *arg){
char msg[BUFF_SIZE];
char incoming[BUFF_SIZE];
size_t msgsize;
strcpy(msg, "terrain:?");
sendto(fd, msg, strlen(msg), 0, address->ai_addr, address->ai_addrlen);
msgsize = recvfrom(fd, incoming, BUFF_SIZE, 0, NULL, 0);
incoming[msgsize] = '\0';
char *s;
s = strstr(incoming, "terrain:=");
if(s != NULL){
strcpy(terrain, incoming);
}
return 0;
}
//gets the condition and stores it in the variable
void *getCondition(void *arg){
char msg[BUFF_SIZE];
char incoming[BUFF_SIZE];
size_t msgsize;
strcpy(msg, "condition:?");
sendto(fd, msg, strlen(msg), 0, address->ai_addr, address->ai_addrlen);
msgsize = recvfrom(fd, incoming, BUFF_SIZE, 0, NULL, 0);
incoming[msgsize] = '\0';
char *s;
s = strstr(incoming, "condition:=");
if(s != NULL){
strcpy(condition, incoming);
s = strstr(incoming, "crashed");
if(s != NULL)
{
if(!crashed){
printCrashed = true;
}
crashed = true;
return 0;
}
s = strstr(incoming, "down");
if(s != NULL)
{
if(!crashed){
printLanded = true;
}
crashed = true;
return 0;
}
crashed = false;
}
return 0;
}
//initialises the data log thread
void logCommand(char msg[BUFF_SIZE])
{
sem_wait(&logSemaphore);
rc3 = pthread_create( &logThread, NULL, logData, msg);
assert(rc3 == 0);
pthread_join(logThread, NULL);
sem_post(&logSemaphore);
}
// sends a control command to the server
void *sendCommand(void *arg)
{
char *msg;
msg = (char*) arg;
char incoming[BUFF_SIZE];
size_t msgsize;
sendto(fd, msg, strlen(msg), 0, address->ai_addr, address->ai_addrlen);
msgsize = recvfrom(fd, incoming, BUFF_SIZE, 0, NULL, 0);
incoming[msgsize] = '\0';
return 0;
}
void finished(int sig)
{
exit(0);
}
//static int fd;
void cleanup(void)
{
close(fd);
}