Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
nps_main_sitl.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Antoine Drouin <poinix@gmail.com>
3  * Copyright (C) 2012 The Paparazzi Team
4  * Copyright (C) 2016 Michal Podhradsky <http://github.com/podhrmic>
5  *
6  * This file is part of paparazzi.
7  *
8  * paparazzi is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * paparazzi is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with paparazzi; see the file COPYING. If not, write to
20  * the Free Software Foundation, 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/time.h>
28 
29 #include "nps_main.h"
30 #include "nps_fdm.h"
31 
32 
33 
34 
35 int main(int argc, char **argv)
36 {
37  if (nps_main_init(argc, argv)) {
38  return 1;
39  }
40 
41  if (nps_main.fg_host) {
42  pthread_create(&th_flight_gear, NULL, nps_flight_gear_loop, NULL);
43  }
44  pthread_create(&th_display_ivy, NULL, nps_main_display, NULL);
45  pthread_create(&th_main_loop, NULL, nps_main_loop, NULL);
46  pthread_join(th_main_loop, NULL);
47 
48  return 0;
49 }
50 
51 
52 void nps_update_launch_from_dl(uint8_t value __attribute__((unused))) {}
53 
54 
56 {
57  enum NpsRadioControlType rc_type;
58  char *rc_dev = NULL;
59  if (nps_main.norc) {
60  rc_type = NORC;
61  } else if (nps_main.js_dev) {
62  rc_type = JOYSTICK;
63  rc_dev = nps_main.js_dev;
64  } else if (nps_main.spektrum_dev) {
65  rc_type = SPEKTRUM;
66  rc_dev = nps_main.spektrum_dev;
67  } else {
68  rc_type = SCRIPT;
69  }
70  nps_autopilot_init(rc_type, nps_main.rc_script, rc_dev);
71 }
72 
73 
75 {
77 
79 
81 
83 
85 
86 }
87 
88 
89 void *nps_main_loop(void *data __attribute__((unused)))
90 {
91  struct timespec requestStart;
92  struct timespec requestEnd;
93  struct timespec waitFor;
94  long int period_ns = HOST_TIMEOUT_MS * 1000000LL; // thread period in nanoseconds
95  long int task_ns = 0; // time it took to finish the task in nanoseconds
96 
97  struct timeval tv_now;
98  double host_time_now;
99 
100  while (TRUE) {
101  if (pauseSignal) {
102  char line[128];
103  double tf = 1.0;
104  double t1, t2, irt;
105 
106  gettimeofday(&tv_now, NULL);
107  t1 = time_to_double(&tv_now);
108  // unscale to initial real time
110 
111  printf("Press <enter> to continue (or CTRL-Z to suspend).\nEnter a new time factor if needed (current: %f): ",
113  fflush(stdout);
114  if (fgets(line, 127, stdin)) {
115  if ((sscanf(line, " %le ", &tf) == 1)) {
116  if (tf > 0 && tf < 1000) {
118  }
119  }
120  printf("Time factor is %f\n", nps_main.host_time_factor);
121  }
122  gettimeofday(&tv_now, NULL);
123  t2 = time_to_double(&tv_now);
124  // add the pause to initial real time
125  irt += t2 - t1;
126  nps_main.real_initial_time += t2 - t1;
127  // convert to scaled initial real time
129  pauseSignal = 0;
130  }
131 
132  clock_get_current_time(&requestStart); // init measurement (after the pause signal)
133 
134  gettimeofday(&tv_now, NULL);
135  host_time_now = time_to_double(&tv_now);
136  double host_time_elapsed = nps_main.host_time_factor * (host_time_now - nps_main.scaled_initial_time);
137 
138 #if DEBUG_NPS_TIME
139  printf("%f,%f,%f,%f,%f,%f,", nps_main.host_time_factor, host_time_elapsed, host_time_now, nps_main.scaled_initial_time,
141 #endif
142 
143  int cnt = 0;
144  static int prev_cnt = 0;
145  static int grow_cnt = 0;
146  while (nps_main.sim_time <= host_time_elapsed) {
147  pthread_mutex_lock(&fdm_mutex);
150  pthread_mutex_unlock(&fdm_mutex);
151  cnt++;
152  }
153 
154  // Check to make sure the simulation doesn't get too far behind real time looping
155  if (cnt > (prev_cnt)) {grow_cnt++;}
156  else { grow_cnt--;}
157  if (grow_cnt < 0) {grow_cnt = 0;}
158  prev_cnt = cnt;
159 
160  if (grow_cnt > 10) {
161  printf("Warning: The time factor is too large for efficient operation! Please reduce the time factor.\n");
162  }
163 
164 #if DEBUG_NPS_TIME
165  printf("%f,%f\n", nps_main.sim_time, nps_main.display_time);
166 #endif
167 
168  clock_get_current_time(&requestEnd); // end measurement
169 
170  // Calculate time it took
171  task_ns = (requestEnd.tv_sec - requestStart.tv_sec) * 1000000000L + (requestEnd.tv_nsec - requestStart.tv_nsec);
172 
173  if (task_ns > 0) {
174  waitFor.tv_sec = 0;
175  waitFor.tv_nsec = period_ns - task_ns;
176  nanosleep(&waitFor, NULL);
177  } else {
178  // task took longer than the period
179 #ifdef PRINT_TIME
180  printf("MAIN THREAD: task took longer than one period, exactly %f [ms], but the period is %f [ms]\n",
181  (double)task_ns / 1E6, (double)period_ns / 1E6);
182 #endif
183  }
184  }
185  return(NULL);
186 }
nps_fdm_run_step
void nps_fdm_run_step(bool launch, double *commands, int commands_nb)
Update the simulation state.
Definition: nps_fdm_crrcsim.c:150
NPS_COMMANDS_NB
#define NPS_COMMANDS_NB
Number of commands sent to the FDM of NPS.
Definition: nps_autopilot.h:42
fdm_mutex
pthread_mutex_t fdm_mutex
Definition: nps_main.h:28
nps_main_run_sim_step
void nps_main_run_sim_step(void)
Definition: nps_main_sitl.c:74
NpsMain::real_initial_time
double real_initial_time
Definition: nps_main.h:52
nps_main_init
int nps_main_init(int argc, char **argv)
Definition: nps_main_common.c:79
NpsMain::spektrum_dev
char * spektrum_dev
Definition: nps_main.h:63
NpsRadioControlType
NpsRadioControlType
Definition: nps_radio_control.h:31
HOST_TIMEOUT_MS
#define HOST_TIMEOUT_MS
Definition: nps_main.h:22
nps_flight_gear_loop
void * nps_flight_gear_loop(void *data)
Definition: nps_main_common.c:258
nps_fdm.h
NpsMain::host_time_factor
double host_time_factor
Definition: nps_main.h:54
nps_autopilot
struct NpsAutopilot nps_autopilot
Definition: nps_autopilot_fixedwing.c:63
nps_main.h
time_to_double
double time_to_double(struct timeval *t)
Definition: nps_main_common.c:69
SCRIPT
@ SCRIPT
Definition: nps_radio_control.h:32
NORC
@ NORC
Definition: nps_radio_control.h:35
SPEKTRUM
@ SPEKTRUM
Definition: nps_radio_control.h:34
pauseSignal
int pauseSignal
Definition: nps_main.h:30
nps_autopilot_init
void nps_autopilot_init(enum NpsRadioControlType type, int num_script, char *js_dev)
Definition: nps_autopilot_fixedwing.c:80
SIM_DT
#define SIM_DT
Definition: nps_main.h:20
nps_main_display
void * nps_main_display(void *data)
Definition: nps_main_common.c:306
clock_get_current_time
#define clock_get_current_time(_x)
Definition: nps_main.h:17
nps_autopilot_run_systime_step
void nps_autopilot_run_systime_step(void)
Definition: nps_autopilot_fixedwing.c:98
NpsMain::display_time
double display_time
Definition: nps_main.h:56
NpsAutopilot::launch
bool launch
Definition: nps_autopilot.h:48
NpsMain::sim_time
double sim_time
Definition: nps_main.h:55
uint8_t
unsigned char uint8_t
Definition: types.h:14
nps_atmosphere_update
void nps_atmosphere_update(double dt)
Definition: nps_atmosphere.c:98
nps_sensors_run_step
void nps_sensors_run_step(double time)
Definition: nps_sensors.c:28
nps_radio_and_autopilot_init
void nps_radio_and_autopilot_init(void)
Definition: nps_main_sitl.c:55
NpsMain::norc
bool norc
Definition: nps_main.h:65
nps_main
struct NpsMain nps_main
Definition: nps_main.h:70
nps_main_loop
void * nps_main_loop(void *data)
Definition: nps_main_sitl.c:89
th_main_loop
pthread_t th_main_loop
Definition: nps_main.h:26
nps_autopilot_run_step
void nps_autopilot_run_step(double time)
Definition: nps_autopilot_fixedwing.c:106
th_flight_gear
pthread_t th_flight_gear
Definition: nps_main.h:24
th_display_ivy
pthread_t th_display_ivy
Definition: nps_main.h:25
JOYSTICK
@ JOYSTICK
Definition: nps_radio_control.h:33
NpsMain::scaled_initial_time
double scaled_initial_time
Definition: nps_main.h:53
nps_update_launch_from_dl
void nps_update_launch_from_dl(uint8_t value)
Definition: nps_main_sitl.c:52
NpsMain::fg_host
char * fg_host
Definition: nps_main.h:57
NpsAutopilot::commands
double commands[NPS_COMMANDS_NB]
Definition: nps_autopilot.h:47
TRUE
#define TRUE
Definition: std.h:4
NpsMain::js_dev
char * js_dev
Definition: nps_main.h:62
NpsMain::rc_script
int rc_script
Definition: nps_main.h:64
main
int main(int argc, char **argv)
Definition: nps_main_sitl.c:35