diff --git a/sw/simulator/nps/nps_flightgear.c b/sw/simulator/nps/nps_flightgear.c index d7995af910..0b84bd4b1a 100644 --- a/sw/simulator/nps/nps_flightgear.c +++ b/sw/simulator/nps/nps_flightgear.c @@ -1,6 +1,8 @@ #include "nps_flightgear.h" #include +#include +#include #include #include #include @@ -15,10 +17,12 @@ static struct { int socket; struct sockaddr_in addr; + unsigned int initial_time; + unsigned int time_offset; } flightgear; -void nps_flightgear_init(const char* host, unsigned int port) { +void nps_flightgear_init(const char* host, unsigned int port, unsigned int time_offset) { int so_reuseaddr = 1; struct protoent * pte = getprotobyname("UDP"); flightgear.socket = socket( PF_INET, SOCK_DGRAM, pte->p_proto); @@ -27,6 +31,15 @@ void nps_flightgear_init(const char* host, unsigned int port) { flightgear.addr.sin_family = PF_INET; flightgear.addr.sin_port = htons(port); flightgear.addr.sin_addr.s_addr = inet_addr(host); + + // get current time to use as inital when computing cur_time for FG + //struct timeval t; + //gettimeofday(&t, NULL); + time_t t = time(NULL); + //struct tm tm; + //localtime_r(t, &tm); + flightgear.initial_time = t; + flightgear.time_offset = time_offset; } void nps_flightgear_send() { @@ -52,9 +65,11 @@ void nps_flightgear_send() { gui.num_tanks = 1; gui.fuel_quantity[0] = 0.; - //gui.cur_time = 3198060679ul + rint(fdm.time); - gui.cur_time = 3198101679ul + rint(fdm.time); - gui.warp = 1122474394ul; + gui.cur_time = flightgear.initial_time + rint(fdm.time); + // if cur_time is zero, flightgear would take the real current time + //gui.cur_time = 0; + // warp is used as an offset to the current time in seconds + gui.warp = flightgear.time_offset; gui.ground_elev = 0.; diff --git a/sw/simulator/nps/nps_flightgear.h b/sw/simulator/nps/nps_flightgear.h index 3c07072219..e7032ee00c 100644 --- a/sw/simulator/nps/nps_flightgear.h +++ b/sw/simulator/nps/nps_flightgear.h @@ -2,7 +2,7 @@ #define NPS_FLIGHTGEAR_H -extern void nps_flightgear_init(const char* host, unsigned int port); +extern void nps_flightgear_init(const char* host, unsigned int port, unsigned int time_offset); extern void nps_flightgear_send(); #endif /* NPS_FLIGHTGEAR_H */ diff --git a/sw/simulator/nps/nps_main.c b/sw/simulator/nps/nps_main.c index 4a2012c446..9d6c3d7bf6 100644 --- a/sw/simulator/nps/nps_main.c +++ b/sw/simulator/nps/nps_main.c @@ -49,6 +49,7 @@ static struct { double display_time; char* fg_host; unsigned int fg_port; + unsigned int fg_time_offset; char* js_dev; char* spektrum_dev; int rc_script; @@ -130,7 +131,7 @@ static void nps_main_init(void) { 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_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"); @@ -239,6 +240,7 @@ 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; @@ -249,6 +251,7 @@ static bool_t nps_main_parse_options(int argc, char** argv) { " -h Display this help\n" " --fg_host e.g. 127.0.0.1\n" " --fg_port e.g. 5501\n" +" --fg_time_offset e.g. 21600 for 6h\n" " -j --js_dev e.g. 1 (default 0)\n" " --spektrum_dev e.g. /dev/ttyUSB0\n" " --rc_script e.g. 0\n"; @@ -259,6 +262,7 @@ static bool_t nps_main_parse_options(int argc, char** argv) { 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}, @@ -271,36 +275,38 @@ static bool_t nps_main_parse_options(int argc, char** argv) { 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: + 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; + } + break; + + case 'j': if (optarg == NULL) {nps_main.js_dev = strdup("0");} else {nps_main.js_dev = strdup(optarg);} break; - case 3: - nps_main.spektrum_dev = strdup(optarg); break; - case 4: - nps_main.rc_script = atoi(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); - case 'h': - fprintf(stderr, usage, argv[0]); - exit(0); - - default: /* ’?’ */ - printf("?? getopt returned character code 0%o ??\n", c); - fprintf(stderr, usage, argv[0]); - exit(EXIT_FAILURE); + default: /* ’?’ */ + printf("?? getopt returned character code 0%o ??\n", c); + fprintf(stderr, usage, argv[0]); + exit(EXIT_FAILURE); } } return TRUE;