34 pthread_mutex_t clock_mutex;
37 pthread_mutex_lock(&clock_mutex);
40 host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock);
41 clock_get_time(cclock, &mts);
42 mach_port_deallocate(mach_task_self(), cclock);
43 ts->tv_sec = mts.tv_sec;
44 ts->tv_nsec = mts.tv_nsec;
45 pthread_mutex_unlock(&clock_mutex);
53 signal(SIGTSTP, SIG_DFL);
65 printf(
"Press <enter> to continue.\n");
71 return ((
double)t->tv_sec + (
double)(t->tv_usec * 1e-6));
76 return ((
double)t->tv_sec + (
double)(t->tv_nsec * 1e-9));
96 gettimeofday(&t, NULL);
103 printf(
"Simulating with dt of %f\n",
SIM_DT);
108 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");
121 if (time_factor < 0.0 || time_factor > 100.0) {
128 struct timeval tv_now;
129 double t_now, t_elapsed;
131 gettimeofday(&tv_now, NULL);
163 static const char *
usage =
164 "Usage: %s [options]\n"
166 " -h Display this help\n"
167 " --fg_host <flight gear host> e.g. 127.0.0.1\n"
168 " --fg_port <flight gear port> e.g. 5501\n"
169 " --fg_port_in <flight gear in port> e.g. 5502\n"
170 " --fg_time_offset <offset in seconds> e.g. 21600 for 6h\n"
171 " -j --js_dev <optional joystick index> e.g. 1 (default 0)\n"
172 " --spektrum_dev <spektrum device> e.g. /dev/ttyUSB0\n"
173 " --rc_script <number> e.g. 0\n"
174 " --norc e.g. disable RC\n"
175 " --ivy_bus <ivy bus> e.g. 127.255.255.255\n"
176 " --time_factor <factor> e.g. 2.5\n"
177 " --nodisplay e.g. disable NPS ivy messages\n"
183 static struct option long_options[] = {
184 {
"fg_host", 1, NULL, 0},
185 {
"fg_port", 1, NULL, 0},
186 {
"fg_time_offset", 1, NULL, 0},
187 {
"js_dev", 2, NULL, 0},
188 {
"spektrum_dev", 1, NULL, 0},
189 {
"rc_script", 1, NULL, 0},
190 {
"norc", 0, NULL, 0},
191 {
"ivy_bus", 1, NULL, 0},
192 {
"time_factor", 1, NULL, 0},
193 {
"fg_fdm", 0, NULL, 0},
194 {
"fg_port_in", 1, NULL, 0},
195 {
"nodisplay", 0, NULL, 0},
198 int option_index = 0;
199 int c = getopt_long(argc, argv,
"jh",
200 long_options, &option_index);
207 switch (option_index) {
245 fprintf(stderr,
usage, argv[0]);
249 printf(
"?? getopt returned character code 0%o ??\n",
c);
250 fprintf(stderr,
usage, argv[0]);
260 struct timespec requestStart;
261 struct timespec requestEnd;
262 struct timespec waitFor;
263 long int period_ns =
DISPLAY_DT * 1000000000L;
264 long int task_ns = 0;
285 task_ns = (requestEnd.tv_sec - requestStart.tv_sec) * 1000000000L + (requestEnd.tv_nsec - requestStart.tv_nsec);
288 if (task_ns < period_ns) {
290 waitFor.tv_nsec = period_ns - task_ns;
291 nanosleep(&waitFor, NULL);
295 printf(
"FG THREAD: task took longer than one period, exactly %f [ms], but the period is %f [ms]\n",
296 (
double)task_ns / 1E6, (
double)period_ns / 1E6);
308 struct timespec requestStart;
309 struct timespec requestEnd;
310 struct timespec waitFor;
311 long int period_ns = 3 *
DISPLAY_DT * 1000000000L;
312 long int task_ns = 0;
325 memcpy(&fdm_ivy, &
fdm,
sizeof(
fdm));
334 task_ns = (requestEnd.tv_sec - requestStart.tv_sec) * 1000000000L + (requestEnd.tv_nsec - requestStart.tv_nsec);
337 if (task_ns < period_ns) {
339 waitFor.tv_nsec = period_ns - task_ns;
340 nanosleep(&waitFor, NULL);
344 printf(
"IVY DISPLAY THREAD: task took longer than one period, exactly %f [ms], but the period is %f [ms]\n",
345 (
double)task_ns / 1E6, (
double)period_ns / 1E6);