[nps] added --fg_time_offset parameter to set an offset in seconds so you can fly e.g. in daylight

This commit is contained in:
Felix Ruess
2013-01-23 16:21:26 +01:00
parent a7501408b0
commit 9d55d6f19c
3 changed files with 51 additions and 30 deletions
+19 -4
View File
@@ -1,6 +1,8 @@
#include "nps_flightgear.h"
#include <sys/socket.h>
#include <sys/time.h>
#include <time.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@@ -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.;
+1 -1
View File
@@ -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 */
+31 -25
View File
@@ -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 <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";
@@ -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: /* $B!G(B?$B!G(B */
printf("?? getopt returned character code 0%o ??\n", c);
fprintf(stderr, usage, argv[0]);
exit(EXIT_FAILURE);
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;