-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
nps_main.c
333 lines (274 loc) · 9.12 KB
/
nps_main.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
/*
* Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
* Copyright (C) 2012 The Paparazzi Team
*
* This file is part of paparazzi.
*
* paparazzi 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 2, or (at your option)
* any later version.
*
* paparazzi 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 paparazzi; see the file COPYING. If not, write to
* the Free Software Foundation, 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <glib.h>
#include <sys/time.h>
#include <getopt.h>
#include "nps_fdm.h"
#include "nps_sensors.h"
#include "nps_atmosphere.h"
#include "nps_autopilot.h"
#include "nps_ivy.h"
#include "nps_flightgear.h"
#include "mcu_periph/sys_time.h"
#define SIM_DT (1./SYS_TIME_FREQUENCY)
#define DISPLAY_DT (1./30.)
#define HOST_TIMEOUT_MS 40
static struct {
double real_initial_time;
double scaled_initial_time;
double host_time_factor;
double sim_time;
double display_time;
char* fg_host;
unsigned int fg_port;
unsigned int fg_time_offset;
char* js_dev;
char* spektrum_dev;
int rc_script;
char* ivy_bus;
} nps_main;
static bool_t nps_main_parse_options(int argc, char** argv);
static void nps_main_init(void);
static void nps_main_display(void);
static void nps_main_run_sim_step(void);
static gboolean nps_main_periodic(gpointer data __attribute__ ((unused)));
int pauseSignal = 0;
void tstp_hdl(int n __attribute__ ((unused))) {
if (pauseSignal) {
pauseSignal = 0;
signal (SIGTSTP, SIG_DFL);
raise(SIGTSTP);
} else {
pauseSignal = 1;
}
}
void cont_hdl (int n __attribute__ ((unused))) {
signal (SIGCONT, cont_hdl);
signal (SIGTSTP, tstp_hdl);
printf("Press <enter> to continue.\n");
}
double time_to_double(struct timeval *t) {
return ((double)t->tv_sec + (double)(t->tv_usec * 1e-6));
}
int main (int argc, char** argv) {
if (!nps_main_parse_options(argc, argv)) return 1;
/* disable buffering for stdout,
* so it properly works in paparazzi center
* where it is not detected as interactive
* and hence fully buffered instead of line buffered
*/
setbuf(stdout, NULL);
nps_main_init();
signal(SIGCONT, cont_hdl);
signal(SIGTSTP, tstp_hdl);
printf("Time factor is %f. (Press Ctrl-Z to change)\n", nps_main.host_time_factor);
GMainLoop *ml = g_main_loop_new(NULL, FALSE);
g_timeout_add(HOST_TIMEOUT_MS, nps_main_periodic, NULL);
g_main_loop_run(ml);
return 0;
}
static void nps_main_init(void) {
nps_main.sim_time = 0.;
nps_main.display_time = 0.;
struct timeval t;
gettimeofday (&t, NULL);
nps_main.real_initial_time = time_to_double(&t);
nps_main.scaled_initial_time = time_to_double(&t);
nps_ivy_init(nps_main.ivy_bus);
nps_fdm_init(SIM_DT);
nps_atmosphere_init();
nps_sensors_init(nps_main.sim_time);
printf("Simulating with dt of %f\n", SIM_DT);
enum NpsRadioControlType rc_type;
char* rc_dev = NULL;
if (nps_main.js_dev) {
rc_type = JOYSTICK;
rc_dev = nps_main.js_dev;
}
else if (nps_main.spektrum_dev) {
rc_type = SPEKTRUM;
rc_dev = nps_main.spektrum_dev;
}
else {
rc_type = SCRIPT;
}
nps_autopilot_init(rc_type, nps_main.rc_script, rc_dev);
if (nps_main.fg_host)
nps_flightgear_init(nps_main.fg_host, nps_main.fg_port, nps_main.fg_time_offset);
#if DEBUG_NPS_TIME
printf("host_time_factor,host_time_elapsed,host_time_now,scaled_initial_time,sim_time_before,display_time_before,sim_time_after,display_time_after\n");
#endif
}
static void nps_main_run_sim_step(void) {
// printf("sim at %f\n", nps_main.sim_time);
nps_atmosphere_update(SIM_DT);
nps_autopilot_run_systime_step();
nps_fdm_run_step(autopilot.launch, autopilot.commands, NPS_COMMANDS_NB);
nps_sensors_run_step(nps_main.sim_time);
nps_autopilot_run_step(nps_main.sim_time);
}
static void nps_main_display(void) {
// printf("display at %f\n", nps_main.display_time);
nps_ivy_display();
if (nps_main.fg_host)
nps_flightgear_send();
}
static gboolean nps_main_periodic(gpointer data __attribute__ ((unused))) {
struct timeval tv_now;
double host_time_now;
if (pauseSignal) {
char line[128];
double tf = 1.0;
double t1, t2, irt;
gettimeofday(&tv_now, NULL);
t1 = time_to_double(&tv_now);
/* unscale to initial real time*/
irt = t1 - (t1 - nps_main.scaled_initial_time)*nps_main.host_time_factor;
printf("Press <enter> to continue (or CTRL-Z to suspend).\nEnter a new time factor if needed (current: %f): ", nps_main.host_time_factor);
fflush(stdout);
if (fgets(line,127,stdin)) {
if ((sscanf(line," %le ", &tf) == 1)) {
if (tf > 0 && tf < 1000)
nps_main.host_time_factor = tf;
}
printf("Time factor is %f\n", nps_main.host_time_factor);
}
gettimeofday(&tv_now, NULL);
t2 = time_to_double(&tv_now);
/* add the pause to initial real time */
irt += t2 - t1;
nps_main.real_initial_time += t2 - t1;
/* convert to scaled initial real time */
nps_main.scaled_initial_time = t2 - (t2 - irt)/nps_main.host_time_factor;
pauseSignal = 0;
}
gettimeofday (&tv_now, NULL);
host_time_now = time_to_double(&tv_now);
double host_time_elapsed = nps_main.host_time_factor *(host_time_now - nps_main.scaled_initial_time);
#if DEBUG_NPS_TIME
printf("%f,%f,%f,%f,%f,%f,",nps_main.host_time_factor,host_time_elapsed,host_time_now,nps_main.scaled_initial_time,nps_main.sim_time,nps_main.display_time);
#endif
int cnt = 0;
static int prev_cnt = 0;
static int grow_cnt = 0;
while (nps_main.sim_time <= host_time_elapsed) {
nps_main_run_sim_step();
nps_main.sim_time += SIM_DT;
if (nps_main.display_time < (host_time_now - nps_main.real_initial_time)) {
nps_main_display();
nps_main.display_time += DISPLAY_DT;
}
cnt++;
}
/* Check to make sure the simulation doesn't get too far behind real time looping */
if (cnt > (prev_cnt)) {grow_cnt++;}
else { grow_cnt--;}
if (grow_cnt < 0) {grow_cnt = 0;}
prev_cnt = cnt;
if (grow_cnt > 10) {
printf("Warning: The time factor is too large for efficient operation! Please reduce the time factor.\n");
}
#if DEBUG_NPS_TIME
printf("%f,%f\n",nps_main.sim_time,nps_main.display_time);
#endif
return TRUE;
}
static bool_t nps_main_parse_options(int argc, char** argv) {
nps_main.fg_host = NULL;
nps_main.fg_port = 5501;
nps_main.fg_time_offset = 0;
nps_main.js_dev = NULL;
nps_main.spektrum_dev = NULL;
nps_main.rc_script = 0;
nps_main.ivy_bus = NULL;
nps_main.host_time_factor = 1.0;
static const char* usage =
"Usage: %s [options]\n"
" Options :\n"
" -h Display this help\n"
" --fg_host <flight gear host> e.g. 127.0.0.1\n"
" --fg_port <flight gear port> e.g. 5501\n"
" --fg_time_offset <offset in seconds> e.g. 21600 for 6h\n"
" -j --js_dev <optional joystick index> e.g. 1 (default 0)\n"
" --spektrum_dev <spektrum device> e.g. /dev/ttyUSB0\n"
" --rc_script <number> e.g. 0\n"
" --ivy_bus <ivy bus> e.g. 127.255.255.255\n"
" --time_factor <factor> e.g. 2.5\n";
while (1) {
static struct option long_options[] = {
{"fg_host", 1, NULL, 0},
{"fg_port", 1, NULL, 0},
{"fg_time_offset", 1, NULL, 0},
{"js_dev", 2, NULL, 0},
{"spektrum_dev", 1, NULL, 0},
{"rc_script", 1, NULL, 0},
{"ivy_bus", 1, NULL, 0},
{"time_factor", 1, NULL, 0},
{0, 0, 0, 0}
};
int option_index = 0;
int c = getopt_long(argc, argv, "jh",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
switch (option_index) {
case 0:
nps_main.fg_host = strdup(optarg); break;
case 1:
nps_main.fg_port = atoi(optarg); break;
case 2:
nps_main.fg_time_offset = atoi(optarg); break;
case 3:
if (optarg == NULL) {nps_main.js_dev = strdup("0");}
else {nps_main.js_dev = strdup(optarg);}
break;
case 4:
nps_main.spektrum_dev = strdup(optarg); break;
case 5:
nps_main.rc_script = atoi(optarg); break;
case 6:
nps_main.ivy_bus = strdup(optarg); break;
case 7:
nps_main.host_time_factor = atof(optarg); break;
}
break;
case 'j':
if (optarg == NULL) {nps_main.js_dev = strdup("0");}
else {nps_main.js_dev = strdup(optarg);}
break;
case 'h':
fprintf(stderr, usage, argv[0]);
exit(0);
default: /* $B!G(B?$B!G(B */
printf("?? getopt returned character code 0%o ??\n", c);
fprintf(stderr, usage, argv[0]);
exit(EXIT_FAILURE);
}
}
return TRUE;
}