-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontrol.cpp
443 lines (343 loc) · 9.89 KB
/
control.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
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <math.h>
#include <termbox.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include "net_ctrls.h"
#include "net_fdm.h"
#include <iostream>
using namespace std;
extern "C" void draw_table (int ,int, int, double *, FGNetFDM *, FGNetCtrls *);
void err_sys (const char *msg) {
perror (msg);
exit (1);
}
int
Socket (int family, int type, int protocol)
{
int n;
if ( (n = socket(family, type, protocol)) < 0)
err_sys("socket error");
return(n);
}
void
Bind(int fd, const struct sockaddr *sa, socklen_t salen)
{
if (bind(fd, sa, salen) < 0)
err_sys("bind error");
}
size_t
Recvfrom(int fd, void *ptr, size_t nbytes, int flags,
struct sockaddr *sa, socklen_t *salenptr)
{
ssize_t n;
if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0)
err_sys("recvfrom error");
return(n);
}
size_t
AsyncRecvfrom(int fd, void *ptr, size_t nbytes, int flags,
struct sockaddr *sa, socklen_t *salenptr)
{
ssize_t n;
if ( (n = recvfrom(fd, ptr, nbytes, flags, sa, salenptr)) < 0) {
/* Nothing new to report */
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return n;
}
err_sys("recvfrom error");
}
return(n);
}
void
Sendto(int fd, const void *ptr, size_t nbytes, int flags,
const struct sockaddr *sa, socklen_t salen)
{
if (sendto(fd, ptr, nbytes, flags, sa, salen) != (ssize_t)nbytes)
err_sys("sendto error");
}
void net_parse (FGNetFDM *net, const char *data) {
int i;
sscanf (data, "R=%f\nP=%f\nH=%f\nA=%f\nV=%f\n\n",
&(net->phi), &(net->theta), &(net->psi), &(net->agl), &(net->vcas));
return ;
}
void net_serialize (FGNetCtrls *net, char *data, size_t datalen) {
snprintf (data, datalen, "%f\t%f\t%f\t%f\n",
net->aileron, net->elevator, net->rudder, net->throttle[0]);
return ;
}
template <typename T>
struct channel_t {
int sd;
struct sockaddr_in addr;
T msg;
};
template <typename T>
void async_server (channel_t<T> &ch, int port) {
ch.sd = Socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
/* Make the receiver non-blocking. */
int flags = fcntl (ch.sd, F_GETFL);
flags = O_NONBLOCK;
fcntl (ch.sd, F_SETFL, flags);
memset(&ch.addr, 0, sizeof(ch.addr)); /* Clear struct */
ch.addr.sin_family = AF_INET; /* Internet/IP */
ch.addr.sin_addr.s_addr = htonl (INADDR_ANY);
ch.addr.sin_port = htons (port); /* server port */
Bind (ch.sd, (struct sockaddr*)&ch.addr, sizeof(ch.addr));
}
template <typename T>
int receive (channel_t<T> &ch) {
socklen_t addrlen = 0;
static char buf[1024];
int read;
read = AsyncRecvfrom (ch.sd, buf, 1024, 0, (struct sockaddr*)&ch.addr, &addrlen);
if (read == -1) {
return read;
}
buf[read] = '\0';
net_parse (&ch.msg, buf);
return 0;
}
template <typename T>
void client (channel_t<T> &ch, const char *strptr, int port) {
ch.sd = Socket(AF_INET, SOCK_DGRAM, 0);
int n;
memset(&ch.addr, 0, sizeof(ch.addr));
ch.addr.sin_family = AF_INET;
ch.addr.sin_port = htons(port);
if ( (n = inet_pton(AF_INET, strptr, &ch.msg)) < 0)
err_sys("inet_pton error");
else if (n == 0)
err_sys("inet_pton error");
}
template <typename T>
void send (channel_t<T> &ch) {
socklen_t addrlen = sizeof(ch.addr);
char buf[1024];
net_serialize (&ch.msg, buf, 1024);
Sendto (ch.sd, buf, strlen(buf), 0, (struct sockaddr*)&ch.addr, addrlen);
return;
}
typedef void * mission;
extern "C" {
void control_setup ();
void control_law (FGNetFDM *sensors, FGNetCtrls *actuators, double *targets);
/* If a "thread" is running pass it to the control law. */
mission control_law_mission (FGNetFDM *sensors, FGNetCtrls *actuators, double *targets, void *mission);
mission trigger_takeoff (FGNetFDM *sensors, FGNetCtrls *actuators, double *targets);
}
#define CMD_LEN 32
struct command {
char *c;
char buf[CMD_LEN];
};
struct config {
unsigned int manual; /* Let the pilot fly the plane. */
unsigned int command_listen; /* Read characters as a command. */
struct command command;
unsigned int alert; /* The user should be alerted. */
string msg; /* A message to be displayed to the
user. */
unsigned int takeoff; /* Initiate take off */
mission ctx; /* Context for a thread of control */
};
void draw_text (int x, int y, string text, uint16_t fg, uint16_t bg) {
int w = tb_width ();
int h = tb_height ();
if (y >= h) {
return ;
}
for (int i = x, j = 0; j < text.length (); i++, j++) {
if (i >= w || i < 0) {
continue;
}
tb_change_cell (i, y, text[j], fg, bg);
}
return ;
}
extern "C"
void draw_text_c (int x, int y, const char * text, uint16_t fg, uint16_t bg) {
int w = tb_width ();
int h = tb_height ();
if (y >= h) {
return ;
}
for (int i = x, j = 0; j < strlen(text); i++, j++) {
if (i >= w || i < 0) {
continue;
}
tb_change_cell (i, y, text[j], fg, bg);
}
return ;
}
void draw_horizontal_line (int x, int y, int len, uint16_t bg) {
int w = tb_width ();
int h = tb_height ();
if (y >= h) {
return ;
}
for (int i = x; i < len; i++ ) {
if (i >= w) {
break;
}
tb_change_cell (i, y, ' ', TB_DEFAULT, bg);
}
return ;
}
void append_char (struct command *cmd, char ch) {
if (cmd->c == &cmd->buf[CMD_LEN]) {
return;
}
*cmd->c++ = ch;
return ;
}
void command_reset (struct command *cmd) {
cmd->c = cmd->buf;
memset (cmd->buf, 0, 32);
}
void command_back (struct command *cmd) {
*cmd->c = '\0';
cmd->c = max ((char*)cmd->buf, cmd->c - 1);
}
void publish_message (struct config *conf, string msg) {
conf->msg = msg;
}
void update_display (double targets[256], struct config *c,
FGNetFDM *sensors, FGNetCtrls *actuators) {
int w = tb_width ();
int h = tb_height ();
struct tb_event ev;
if (tb_peek_event (&ev, 10)) {
/* Clear any alerts after input */
c->alert = 0;
switch (ev.type) {
case TB_EVENT_KEY:
switch (ev.key) {
case TB_KEY_ESC:
tb_shutdown ();
exit (0);
break;
case TB_KEY_CTRL_A:
c->manual ^= 1;
break;
case TB_KEY_CTRL_C:
c->command_listen ^= 1;
command_reset (&c->command);
break;
case TB_KEY_CTRL_L:
/* Start takeoff */
c->takeoff = 1;
break;
case TB_KEY_ENTER:
if (c->command_listen) { /* Parse the command */
char plant;
double t;
int match = sscanf (c->command.buf, "%c %lf", &plant, &t);
if (match != 2) {
c->command_listen = 0;
command_reset (&c->command);
publish_message (c, "Incorrect format, user [plant] [target]");
c->alert = 1;
break;
}
/* Update the target for the plant */
targets[plant] = t;
c->command_listen = 0;
}
break;
case TB_KEY_SPACE:
if (c->command_listen) {
append_char (&c->command, ' ');
}
break;
case TB_KEY_BACKSPACE:
tb_shutdown ();
exit (0);
if (c->command_listen) {
command_back (&c->command);
}
break;
default:
/* Add the character to the command */
if (c->command_listen) {
append_char (&c->command, ev.ch);
}
break;
}
}
}
tb_clear ();
draw_horizontal_line (0, 0, w, TB_GREEN);
string title = "ATS Mission Control";
int len = title.length ();
/* Center the title */
draw_text ((w/2) - (len/2), 0, title, TB_DEFAULT, TB_GREEN);
/* Display Auto pilot status */
string autopilot_status = (!c->manual) ? "On" : "Off";
int bg = (!c->manual) ? TB_GREEN : TB_RED;
string autopilot_label = "Auto-Pilot: ";
int row = 2;
draw_text (0, row, autopilot_label, TB_DEFAULT, TB_DEFAULT);
draw_text (autopilot_label.length(), row, autopilot_status, TB_DEFAULT, bg);
if (c->command_listen) {
int bottom = h-1;
string cmd_label = "Command: ";
string cmd (c->command.buf);
draw_horizontal_line (0, bottom, w, TB_GREEN);
draw_text (0, bottom, cmd_label, TB_DEFAULT, TB_GREEN);
draw_text (cmd_label.length(), bottom, cmd, TB_DEFAULT, TB_GREEN);
}
if (c->alert) {
int bottom = h-1;
string label = "Error: ";
draw_horizontal_line (0, bottom, w, TB_RED);
draw_text (0, bottom, label, TB_DEFAULT, TB_RED);
draw_text (label.length(), bottom, c->msg, TB_DEFAULT, TB_RED);
}
draw_table (w, h, 5, targets, sensors, actuators);
tb_present ();
return;
}
int main () {
static double targets[256];
struct config conf = {};
conf.manual = 1;
channel_t<FGNetCtrls> actuators;
channel_t<FGNetFDM> sensors;
async_server (sensors, 5000);
client (actuators, "127.0.0.1", 5010);
/*
Start off by keeping the aircraft steady
*/
targets['r'] = 0.0;
targets['p'] = 5.0;
control_setup ();
tb_init ();
while (1) {
int ready;
actuators.msg.throttle[0] = 0.75; /* By default, keep throttle at
75% */
update_display (targets, &conf, &sensors.msg, &actuators.msg);
ready = receive (sensors);
if (ready == 0 && !conf.manual) {
if (conf.takeoff) {
/* Kickoff the takeoff sequence */
conf.ctx = trigger_takeoff (&sensors.msg, &actuators.msg, targets);
conf.takeoff = 0;
}
if (conf.ctx) {
conf.ctx = control_law_mission (&sensors.msg, &actuators.msg, targets, conf.ctx);
} else {
control_law (&sensors.msg, &actuators.msg, targets);
}
send (actuators);
}
usleep (1000);
}
}