1 /***************************************************************************
5 ----------------------------------------------------------------------------
7 FUNCTION: Real-time synchronization routines for LaRCSIM
9 ----------------------------------------------------------------------------
11 MODULE STATUS: Developmental
13 ----------------------------------------------------------------------------
15 GENEALOGY: Written 921229 by Bruce Jackson
17 ----------------------------------------------------------------------------
25 ----------------------------------------------------------------------------
31 930104 Added ls_resync() call to avoid having to pass DT as a
33 940204 Added calculation of sim_control variable overrun to
34 indicate a frame overrun has occurred. EBJ
35 940506 Added support for sim_control_.debug flag, which disables
36 synchronization (there is still a local dbg flag that
37 enables synch error logging) EBJ
43 Revision 1.1 1997/05/29 00:10:00 curt
44 Initial Flight Gear revision.
46 * Revision 1.7 1994/05/06 15:34:54 bjax
47 * Removed "freerun" variable, and substituted sim_control_.debug flag.
49 * Revision 1.6 1994/02/16 13:01:22 bjax
50 * Added logic to signal frame overrun; corrected syntax on ls_catch call
51 * (old style may be BSD format). EBJ
53 * Revision 1.5 1993/07/30 18:33:14 bjax
54 * Added 'dt' parameter to call to ls_sync from ls_resync routine.
56 * Revision 1.4 1993/03/15 14:56:13 bjax
57 * Removed call to ls_pause; this should be done in cockpit routine.
59 * Revision 1.3 93/03/13 20:34:09 bjax
60 * Modified to allow for sync times longer than a second; added ls_pause() EBJ
62 * Revision 1.2 93/01/06 09:50:47 bjax
63 * Added ls_resync() function.
65 * Revision 1.1 92/12/30 13:19:51 bjax
68 * Revision 1.3 93/12/31 10:34:11 bjax
69 * Added $Log marker as well.
72 ----------------------------------------------------------------------------
76 ----------------------------------------------------------------------------
80 ----------------------------------------------------------------------------
84 ----------------------------------------------------------------------------
88 ----------------------------------------------------------------------------
92 --------------------------------------------------------------------------*/
97 #include "ls_sim_control.h"
100 extern SCALAR Simtime;
102 /* give the time interval data structure FILE visibility */
104 static struct itimerval t, ot;
108 /*void ls_catch( sig, code, sc) /* signal handler */
111 struct sigcontext *sc;*/
114 static DATA lastSimtime = -99.9;
116 /* printf("In ls_catch()\n"); */
118 /*if (lastSimtime == Simtime) fprintf(stderr, "Overrun.\n"); */
119 if (dbug) printf("ls_catch called\n");
120 sim_control_.overrun = (lastSimtime == Simtime);
121 lastSimtime = Simtime;
122 signal(SIGALRM, ls_catch);
128 /* this routine syncs up the interval timer for a new dt value */
134 if (sim_control_.debug!=0) return;
137 usec = 1000000* (dt - (float) isec);
139 t.it_interval.tv_sec = isec;
140 t.it_interval.tv_usec = usec;
141 t.it_value.tv_sec = isec;
142 t.it_value.tv_usec = usec;
143 if (dbug) printf("ls_sync called\n");
144 ls_catch(); /* set up for SIGALRM signal catch */
145 terr = setitimer( ITIMER_REAL, &t, &ot );
146 if (terr) perror("Error returned from setitimer");
150 /* this routine unsyncs the interval timer */
154 if (sim_control_.debug!=0) return;
155 t.it_interval.tv_sec = 0;
156 t.it_interval.tv_usec = 0;
157 t.it_value.tv_sec = 0;
158 t.it_value.tv_usec = 0;
159 if (dbug) printf("ls_unsync called\n");
161 terr = setitimer( ITIMER_REAL, &t, &ot );
162 if (terr) perror("Error returned from setitimer");
167 /* this routine resynchronizes the interval timer to the old
168 interrupt period, stored in struct ot by a previous call
173 if (sim_control_.debug!=0) return;
174 if (dbug) printf("ls_resync called\n");
175 dt = ((float) ot.it_interval.tv_usec)/1000000. +
176 ((float) ot.it_interval.tv_sec);
181 /* this routine waits for the next interrupt */
183 if (sim_control_.debug!=0) return;
184 if (dbug) printf("ls_pause called\n");