39 #define SIM_DT (1./SYS_TIME_FREQUENCY)
40 #define DISPLAY_DT (1./30.)
41 #define HOST_TIMEOUT_MS 40
45 double real_initial_time;
46 double scaled_initial_time;
47 double host_time_factor;
52 unsigned int fg_port_in;
53 unsigned int fg_time_offset;
73 signal(SIGTSTP, SIG_DFL);
84 printf(
"Press <enter> to continue.\n");
89 return ((
double)t->tv_sec + (
double)(t->tv_usec * 1e-6));
92 int main(
int argc,
char **argv)
102 setbuf(stdout, NULL);
108 printf(
"Time factor is %f. (Press Ctrl-Z to change)\n",
nps_main.host_time_factor);
110 GMainLoop *ml = g_main_loop_new(NULL,
FALSE);
124 gettimeofday(&t, NULL);
132 printf(
"Simulating with dt of %f\n",
SIM_DT);
152 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");
194 if (time_factor < 0.0 || time_factor > 100.0) {
197 if (fabs(
nps_main.host_time_factor - time_factor) < 0.01) {
201 struct timeval tv_now;
202 double t_now, t_elapsed;
204 gettimeofday(&tv_now, NULL);
208 t_elapsed = (t_now -
nps_main.scaled_initial_time) *
nps_main.host_time_factor;
211 nps_main.host_time_factor = time_factor;
212 printf(
"Time factor is %f\n",
nps_main.host_time_factor);
216 nps_main.scaled_initial_time = t_now - t_elapsed /
nps_main.host_time_factor;
222 struct timeval tv_now;
223 double host_time_now;
230 gettimeofday(&tv_now, NULL);
235 printf(
"Press <enter> to continue (or CTRL-Z to suspend).\nEnter a new time factor if needed (current: %f): ",
238 if (fgets(line, 127, stdin)) {
239 if ((sscanf(line,
" %le ", &tf) == 1)) {
240 if (tf > 0 && tf < 1000) {
244 printf(
"Time factor is %f\n",
nps_main.host_time_factor);
246 gettimeofday(&tv_now, NULL);
250 nps_main.real_initial_time += t2 - t1;
256 gettimeofday(&tv_now, NULL);
258 double host_time_elapsed =
nps_main.host_time_factor * (host_time_now -
nps_main.scaled_initial_time);
261 printf(
"%f,%f,%f,%f,%f,%f,",
nps_main.host_time_factor, host_time_elapsed, host_time_now,
nps_main.scaled_initial_time,
266 static int prev_cnt = 0;
267 static int grow_cnt = 0;
268 while (
nps_main.sim_time <= host_time_elapsed) {
279 if (cnt > (prev_cnt)) {grow_cnt++;}
281 if (grow_cnt < 0) {grow_cnt = 0;}
285 printf(
"Warning: The time factor is too large for efficient operation! Please reduce the time factor.\n");
311 static const char *usage =
312 "Usage: %s [options]\n"
314 " -h Display this help\n"
315 " --fg_host <flight gear host> e.g. 127.0.0.1\n"
316 " --fg_port <flight gear port> e.g. 5501\n"
317 " --fg_port_in <flight gear in port> e.g. 5502\n"
318 " --fg_time_offset <offset in seconds> e.g. 21600 for 6h\n"
319 " -j --js_dev <optional joystick index> e.g. 1 (default 0)\n"
320 " --spektrum_dev <spektrum device> e.g. /dev/ttyUSB0\n"
321 " --rc_script <number> e.g. 0\n"
322 " --ivy_bus <ivy bus> e.g. 127.255.255.255\n"
323 " --time_factor <factor> e.g. 2.5\n"
329 static struct option long_options[] = {
330 {
"fg_host", 1, NULL, 0},
331 {
"fg_port", 1, NULL, 0},
332 {
"fg_time_offset", 1, NULL, 0},
333 {
"js_dev", 2, NULL, 0},
334 {
"spektrum_dev", 1, NULL, 0},
335 {
"rc_script", 1, NULL, 0},
336 {
"ivy_bus", 1, NULL, 0},
337 {
"time_factor", 1, NULL, 0},
338 {
"fg_fdm", 0, NULL, 0},
339 {
"fg_port_in", 1, NULL, 0},
342 int option_index = 0;
343 int c = getopt_long(argc, argv,
"jh",
344 long_options, &option_index);
351 switch (option_index) {
353 nps_main.fg_host = strdup(optarg);
break;
355 nps_main.fg_port = atoi(optarg);
break;
357 nps_main.fg_time_offset = atoi(optarg);
break;
359 if (optarg == NULL) {
nps_main.js_dev = strdup(
"0");}
360 else {
nps_main.js_dev = strdup(optarg);}
363 nps_main.spektrum_dev = strdup(optarg);
break;
365 nps_main.rc_script = atoi(optarg);
break;
367 nps_main.ivy_bus = strdup(optarg);
break;
369 nps_main.host_time_factor = atof(optarg);
break;
373 nps_main.fg_port_in = atoi(optarg);
break;
378 if (optarg == NULL) {
nps_main.js_dev = strdup(
"0");}
379 else {
nps_main.js_dev = strdup(optarg);}
383 fprintf(stderr, usage, argv[0]);
387 printf(
"?? getopt returned character code 0%o ??\n", c);
388 fprintf(stderr, usage, argv[0]);
static gboolean nps_main_periodic(gpointer data)
void nps_flightgear_send_fdm()
Send FlightGear FDM packet For visualization with moving surfaces (elevator, propeller etc)...
void nps_ivy_init(char *ivy_bus)
void nps_sensors_run_step(double time)
#define NPS_COMMANDS_NB
Number of commands sent to the FDM of NPS.
static void nps_main_display(void)
void nps_ivy_display(void)
static void nps_main_run_sim_step(void)
Architecture independent timing functions.
void nps_autopilot_run_step(double time)
void nps_flightgear_init(const char *host, unsigned int port, unsigned int port_in, unsigned int time_offset)
int main(int argc, char **argv)
struct NpsAutopilot autopilot
static void nps_main_init(void)
void nps_autopilot_run_systime_step(void)
void nps_fdm_run_step(bool launch, double *commands, int commands_nb)
void nps_fdm_init(double dt)
void nps_flightgear_send()
Send FlightGear GUI packet For visualization of airplane position and attitude only start fgfs with â€...
void nps_set_time_factor(float time_factor)
void nps_atmosphere_update(double dt)
void nps_flightgear_receive()
Receive Flight Gear environment messages.
double time_to_double(struct timeval *t)
static bool nps_main_parse_options(int argc, char **argv)
void nps_atmosphere_init(void)
Atmosphere model (pressure, wind) for NPS.
double commands[NPS_COMMANDS_NB]
static struct @328 nps_main
void(* rc_script)(double)
void nps_sensors_init(double time)
void nps_autopilot_init(enum NpsRadioControlType type, int num_script, char *js_dev)