]> git.mxchange.org Git - flightgear.git/blob - src/FDM/LaRCsim/LaRCsim.cxx
Merge branch 'maint' into next
[flightgear.git] / src / FDM / LaRCsim / LaRCsim.cxx
1 // LaRCsim.cxx -- interface to the LaRCsim flight model
2 //
3 // Written by Curtis Olson, started October 1998.
4 //
5 // Copyright (C) 1998  Curtis L. Olson  - http://www.flightgear.org/~curt
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20 //
21 // $Id$
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #endif
26
27 #include <math.h>
28 #include <string.h>             // strcmp()
29
30 #include <simgear/constants.h>
31 #include <simgear/debug/logstream.hxx>
32 #include <simgear/scene/model/location.hxx>
33 #include <simgear/scene/model/placement.hxx>
34
35 #include <Aircraft/aircraft.hxx>
36 #include <Aircraft/controls.hxx>
37 #include <FDM/flight.hxx>
38 #include <FDM/UIUCModel/uiuc_aircraft.h>
39 #include <Main/fg_props.hxx>
40 #include <Model/acmodel.hxx>
41
42 #include "ls_cockpit.h"
43 #include "ls_generic.h"
44 #include "ls_interface.h"
45 #include "ls_constants.h"
46
47 #include "IO360.hxx"
48 #include "LaRCsimIC.hxx"
49 #include "LaRCsim.hxx"
50
51
52 FGLaRCsim::FGLaRCsim( double dt ) {
53     mass=i_xx=i_yy=i_zz=i_xz=0;
54
55 //     set_delta_t( dt );
56
57     speed_up = fgGetNode("/sim/speed-up", true);
58     aero = fgGetNode("/sim/aero", true);
59     uiuc_type = fgGetNode("/sim/uiuc-type", true);
60
61     ls_toplevel_init( 0.0, (char *)(aero->getStringValue()) );
62
63     lsic=new LaRCsimIC; //this needs to be brought up after LaRCsim is
64     if ( !strcmp(aero->getStringValue(), "c172") ) {
65         copy_to_LaRCsim(); // initialize all of LaRCsim's vars
66
67         //this should go away someday -- formerly done in fg_init.cxx
68         Mass = 8.547270E+01;
69         I_xx = 1.048000E+03;
70         I_yy = 3.000000E+03;
71         I_zz = 3.530000E+03;
72         I_xz = 0.000000E+00;
73     }
74
75     if ( !strcmp(aero->getStringValue(), "basic") ) {
76         copy_to_LaRCsim(); // initialize all of LaRCsim's vars
77
78         //this should go away someday -- formerly done in fg_init.cxx
79         Mass = 2./32.174;
80         I_xx   = 0.0454;
81         I_yy   = 0.0191;
82         I_zz   = 0.0721;
83         I_xz   = 0;
84 //      Mass = 8.547270E+01;
85 //      I_xx = 1.048000E+03;
86 //      I_yy = 3.000000E+03;
87 //      I_zz = 3.530000E+03;
88 //      I_xz = 0.000000E+00;
89     }
90
91     ls_set_model_dt(dt);
92     
93     // Initialize our little engine that hopefully might
94     eng.init(dt);
95     // dcl - in passing dt to init rather than update I am assuming
96     // that the LaRCsim dt is fixed at one value (yes it is 120hz CLO)
97 }
98
99 FGLaRCsim::~FGLaRCsim(void) {
100     if ( lsic != NULL ) {
101         delete lsic;
102         lsic = NULL;
103     }
104 }
105
106 // Initialize the LaRCsim flight model, dt is the time increment for
107 // each subsequent iteration through the EOM
108 void FGLaRCsim::init() {
109    //do init common to all FDM's
110    common_init();
111 }
112
113
114 // Run an iteration of the EOM (equations of motion)
115 void FGLaRCsim::update( double dt ) {
116
117     if (is_suspended())
118       return;
119
120     int multiloop = _calc_multiloop(dt);
121
122     // if flying c172-larcsim, do the following
123     if ( !strcmp(aero->getStringValue(), "c172") ) {
124         // set control inputs
125         // cout << "V_calibrated_kts = " << V_calibrated_kts << '\n';
126         eng.set_IAS( V_calibrated_kts );
127         eng.set_Throttle_Lever_Pos( globals->get_controls()->get_throttle( 0 )
128                                     * 100.0 );
129         eng.set_Propeller_Lever_Pos( 100 );
130         eng.set_Mixture_Lever_Pos( globals->get_controls()->get_mixture( 0 )
131                                    * 100.0 );
132         eng.set_Magneto_Switch_Pos( globals->get_controls()->get_magnetos(0) );
133         eng.setStarterFlag( globals->get_controls()->get_starter(0) );
134         eng.set_p_amb( Static_pressure );
135         eng.set_T_amb( Static_temperature );
136
137         // update engine model
138         eng.update();
139
140         // Fake control-surface positions
141         fgSetDouble("/surface-positions/flap-pos-norm",
142                     fgGetDouble("/controls/flight/flaps"));
143                                 // FIXME: ignoring trim
144         fgSetDouble("/surface-positions/elevator-pos-norm",
145                     fgGetDouble("/controls/flight/elevator"));
146                                 // FIXME: ignoring trim
147         fgSetDouble("/surface-positions/left-aileron-pos-norm",
148                     fgGetDouble("/controls/flight/aileron"));
149                                 // FIXME: ignoring trim
150         fgSetDouble("/surface-positions/right-aileron-pos-norm",
151                     -1 * fgGetDouble("/controls/flight/aileron"));
152                                 // FIXME: ignoring trim
153         fgSetDouble("/surface-positions/rudder-pos-norm",
154                     fgGetDouble("/controls/flight/rudder"));
155
156         // copy engine state values onto "bus"
157         fgSetDouble("/engines/engine/rpm", eng.get_RPM());
158         fgSetDouble("/engines/engine/mp-osi", eng.get_Manifold_Pressure());
159         fgSetDouble("/engines/engine/max-hp", eng.get_MaxHP());
160         fgSetDouble("/engines/engine/power-pct", eng.get_Percentage_Power());
161         fgSetDouble("/engines/engine/egt-degf", eng.get_EGT());
162         fgSetDouble("/engines/engine/cht-degf", eng.get_CHT());
163         fgSetDouble("/engines/engine/prop-thrust", eng.get_prop_thrust_SI());
164         fgSetDouble("/engines/engine/fuel-flow-gph",
165                     eng.get_fuel_flow_gals_hr());
166         fgSetDouble("/engines/engine/oil-temperature-degf",
167                     eng.get_oil_temp());
168         fgSetDouble("/engines/engine/running", eng.getRunningFlag());
169         fgSetDouble("/engines/engine/cranking", eng.getCrankingFlag());
170
171         static const SGPropertyNode *fuel_freeze
172             = fgGetNode("/sim/freeze/fuel");
173
174         if ( ! fuel_freeze->getBoolValue() ) {
175             //Assume we are using both tanks equally for now
176             fgSetDouble("/consumables/fuel/tank[0]/level-gal_us",
177                         fgGetDouble("/consumables/fuel/tank[0]/level-gal_us")
178                         - (eng.get_fuel_flow_gals_hr() / (2 * 3600))
179                         * dt);
180             fgSetDouble("/consumables/fuel/tank[1]/level-gal_us",
181                         fgGetDouble("/consumables/fuel/tank[1]/level-gal_us")
182                         - (eng.get_fuel_flow_gals_hr() / (2 * 3600))
183                         * dt);
184         }
185
186         // Apparently the IO360 thrust model is not working.  
187         // F_X_engine is zero here.
188         F_X_engine = eng.get_prop_thrust_lbs();
189
190         Flap_handle = 30.0 * globals->get_controls()->get_flaps();
191     }
192     // done with c172-larcsim if-block
193
194     if ( !strcmp(aero->getStringValue(), "basic") ) {
195         // set control inputs
196         // cout << "V_calibrated_kts = " << V_calibrated_kts << '\n';
197         eng.set_IAS( V_calibrated_kts );
198         eng.set_Throttle_Lever_Pos( globals->get_controls()->get_throttle( 0 )
199                                     * 100.0 );
200         eng.set_Propeller_Lever_Pos( 100 );
201         eng.set_Mixture_Lever_Pos( globals->get_controls()->get_mixture( 0 )
202                                    * 100.0 );
203         eng.set_Magneto_Switch_Pos( globals->get_controls()->get_magnetos(0) );
204         eng.setStarterFlag( globals->get_controls()->get_starter(0) );
205         eng.set_p_amb( Static_pressure );
206         eng.set_T_amb( Static_temperature );
207
208         // update engine model
209         eng.update();
210
211         // Fake control-surface positions
212         fgSetDouble("/surface-positions/flap-pos-norm",
213                     fgGetDouble("/controls/flight/flaps"));
214                                 // FIXME: ignoring trim
215         fgSetDouble("/surface-positions/elevator-pos-norm",
216                     fgGetDouble("/controls/flight/elevator"));
217                                 // FIXME: ignoring trim
218         fgSetDouble("/surface-positions/left-aileron-pos-norm",
219                     fgGetDouble("/controls/flight/aileron"));
220                                 // FIXME: ignoring trim
221         fgSetDouble("/surface-positions/right-aileron-pos-norm",
222                     -1 * fgGetDouble("/controls/flight/aileron"));
223                                 // FIXME: ignoring trim
224         fgSetDouble("/surface-positions/rudder-pos-norm",
225                     fgGetDouble("/controls/flight/rudder"));
226
227         // copy engine state values onto "bus"
228         fgSetDouble("/engines/engine/rpm", eng.get_RPM());
229         fgSetDouble("/engines/engine/mp-osi", eng.get_Manifold_Pressure());
230         fgSetDouble("/engines/engine/max-hp", eng.get_MaxHP());
231         fgSetDouble("/engines/engine/power-pct", eng.get_Percentage_Power());
232         fgSetDouble("/engines/engine/egt-degf", eng.get_EGT());
233         fgSetDouble("/engines/engine/cht-degf", eng.get_CHT());
234         fgSetDouble("/engines/engine/prop-thrust", eng.get_prop_thrust_SI());
235         fgSetDouble("/engines/engine/fuel-flow-gph",
236                     eng.get_fuel_flow_gals_hr());
237         fgSetDouble("/engines/engine/oil-temperature-degf",
238                     eng.get_oil_temp());
239         fgSetDouble("/engines/engine/running", eng.getRunningFlag());
240         fgSetDouble("/engines/engine/cranking", eng.getCrankingFlag());
241
242         static const SGPropertyNode *fuel_freeze
243             = fgGetNode("/sim/freeze/fuel");
244
245         if ( ! fuel_freeze->getBoolValue() ) {
246             //Assume we are using both tanks equally for now
247             fgSetDouble("/consumables/fuel/tank[0]/level-gal_us",
248                         fgGetDouble("/consumables/fuel/tank[0]/level-gal_us")
249                         - (eng.get_fuel_flow_gals_hr() / (2 * 3600))
250                         * dt);
251             fgSetDouble("/consumables/fuel/tank[1]/level-gal_us",
252                         fgGetDouble("/consumables/fuel/tank[1]/level-gal_us")
253                         - (eng.get_fuel_flow_gals_hr() / (2 * 3600))
254                         * dt);
255         }
256
257         // Apparently the IO360 thrust model is not working.  
258         // F_X_engine is zero here.
259         F_X_engine = eng.get_prop_thrust_lbs();
260
261         Flap_handle = 30.0 * globals->get_controls()->get_flaps();
262     }
263     // done with basic-larcsim if-block
264
265     double save_alt = 0.0;
266
267     // lets try to avoid really screwing up the LaRCsim model
268     if ( get_Altitude() < -9000.0 ) {
269         save_alt = get_Altitude();
270         set_Altitude( 0.0 );
271     }
272
273     // copy control positions into the LaRCsim structure
274     Lat_control = globals->get_controls()->get_aileron() / speed_up->getIntValue();
275     Long_control = globals->get_controls()->get_elevator();
276     Long_trim = globals->get_controls()->get_elevator_trim();
277     Rudder_pedal = globals->get_controls()->get_rudder() / speed_up->getIntValue();
278
279     // IO360.cxx for the C172 thrust is broken (not sure why).  
280     // So force C172 to use engine model in c172_engine.c instead of the IO360.cxx.
281     //      if ( !strcmp(aero->getStringValue(), "c172") ) {
282     //          Use_External_Engine = 1;
283     //      } else {
284     //          Use_External_Engine = 0;
285     //      }
286     Use_External_Engine = 0;
287
288     Throttle_pct = globals->get_controls()->get_throttle( 0 ) * 1.0;
289
290     Brake_pct[0] = globals->get_controls()->get_brake_right();
291     Brake_pct[1] = globals->get_controls()->get_brake_left();
292
293     // Inform LaRCsim of the local terrain altitude
294     Runway_altitude
295       = get_groundlevel_m(Latitude, Longitude, Altitude * SG_FEET_TO_METER)
296       * SG_METER_TO_FEET;
297
298     // Weather
299     /* V_north_airmass = get_V_north_airmass();
300        V_east_airmass =  get_V_east_airmass();
301        V_down_airmass =  get_V_down_airmass(); */
302
303
304     // old -- FGInterface_2_LaRCsim() not needed except for Init()
305     // translate FG to LaRCsim structure
306     // FGInterface_2_LaRCsim(f);
307     // printf("FG_Altitude = %.2f\n", FG_Altitude * 0.3048);
308     // printf("Altitude = %.2f\n", Altitude * 0.3048);
309     // printf("Radius to Vehicle = %.2f\n", Radius_to_vehicle * 0.3048);
310
311     // for engine functions (sounds and instruments)
312     // drive the rpm gauge
313     fgSetDouble("/engines/engine/rpm", (globals->get_controls()->get_throttle( 0 ) * 100.0 * 25 ));
314     // manifold air pressure, which drives the sound (see *sound.xml file)
315     fgSetDouble("/engines/engine/mp-osi", (globals->get_controls()->get_throttle( 0 ) * 100.0 ));
316     // make the engine cranking and running sounds when fgfs starts up
317     fgSetDouble("/engines/engine/cranking", 1);
318     fgSetDouble("/engines/engine/running", 1);
319
320     // if flying uiuc, set some properties and over-ride some previous ones
321     if ( !strcmp(aero->getStringValue(), "uiuc")) {
322
323       // surface positions and other general properties
324       fgSetDouble("/surface-positions/flight/rudder-pos-norm",             fgGetDouble("/controls/flight/rudder"));
325       fgSetDouble("/surface-positions/flight/elevator-pos-norm",           fgGetDouble("/controls/flight/elevator")); // FIXME: ignoring trim
326       fgSetDouble("/surface-positions/flight/right-aileron-pos-norm", -1 * fgGetDouble("/controls/flight/aileron")); // FIXME: ignoring trim
327       fgSetDouble("/surface-positions/flight/left-aileron-pos-norm",       fgGetDouble("/controls/flight/aileron")); // FIXME: ignoring trim
328
329       // used by Instruments/accel.xml
330       fgSetDouble("/accelerations/pilot-g",   -N_Z_cg);
331
332       Flap_handle = flap_max * globals->get_controls()->get_flaps();
333
334       // flaps with transition occuring in uiuc_aerodeflections.cpp
335       if (use_flaps) {
336       fgSetDouble("/surface-positions/flight/flap-pos-norm",               flap_pos_norm);
337       }
338
339       // spoilers with transition occurring in uiuc_aerodeflections.cpp
340       if(use_spoilers) {
341         Spoiler_handle = spoiler_max * fgGetDouble("/controls/flight/spoilers");
342       }
343       // gear with transition occurring here in LaRCsim.cxx
344       if (use_gear) {
345         if(fgGetBool("/controls/gear/gear-down")) {
346           Gear_handle = 1.0;
347         }
348         else {
349           Gear_handle = 0.;
350         }
351         // commanded gear is 0 or 1
352         gear_cmd_norm = Gear_handle;
353         // amount gear moves per time step [relative to 1]
354         gear_increment_per_timestep = gear_rate * dt; 
355         // determine gear position with respect to gear command
356         if (gear_pos_norm < gear_cmd_norm) {
357           gear_pos_norm += gear_increment_per_timestep;
358           if (gear_pos_norm > gear_cmd_norm) 
359             gear_pos_norm = gear_cmd_norm;
360         } else if (gear_pos_norm > gear_cmd_norm) {
361           gear_pos_norm -= gear_increment_per_timestep;
362           if (gear_pos_norm < gear_cmd_norm)
363             gear_pos_norm = gear_cmd_norm;
364         } 
365         // set the gear position
366         fgSetDouble("/gear/gear[0]/position-norm", gear_pos_norm);
367         fgSetDouble("/gear/gear[1]/position-norm", gear_pos_norm);
368         fgSetDouble("/gear/gear[2]/position-norm", gear_pos_norm);
369       }
370
371
372       // engine functions (sounds and instruments)
373       // drive the rpm gauge
374       fgSetDouble("/engines/engine/rpm", (globals->get_controls()->get_throttle( 0 ) * 100.0 * 25 ));
375       // manifold air pressure
376       fgSetDouble("/engines/engine/mp-osi", (globals->get_controls()->get_throttle( 0 ) * 100.0 ));
377       // make the engine cranking and running sounds when fgfs starts up
378       fgSetDouble("/engines/engine/cranking", 1);
379       fgSetDouble("/engines/engine/running", 1);
380       if ( !strcmp(uiuc_type->getStringValue(), "uiuc-prop")) {
381         // uiuc prop driven airplane, e.g. Wright Flyer
382       }
383       else if ( !strcmp(uiuc_type->getStringValue(), "uiuc-jet")) {
384         // uiuc jet aircraft, e.g. a4d
385         // used for setting the sound
386         fgSetDouble("/engines/engine/n1", (75 + (globals->get_controls()->get_throttle( 0 ) * 100.0 )/400));
387         fgSetDouble("/engines/engine/prop-thrust", (4000 + F_X_engine/2));
388         // used for setting the instruments
389         fgSetDouble("/engines/engine[0]/n1", (50 + (globals->get_controls()->get_throttle( 0 ) * 50)));
390       }
391       else if ( !strcmp(uiuc_type->getStringValue(), "uiuc-sailplane")) {
392         // uiuc sailplane, e.g. asw20
393         fgSetDouble("/engines/engine/cranking", 0);
394       }
395       else if ( !strcmp(uiuc_type->getStringValue(), "uiuc-hangglider")) {
396         // uiuc hang glider, e.g. airwave
397         fgSetDouble("/engines/engine/cranking", 0);
398       }
399       else if ( !strcmp(uiuc_type->getStringValue(), "uiuc-ornithopter")) {
400         // flapping wings
401         fgSetDouble("/canopy/position-norm", 0);
402         fgSetDouble("/wing-phase/position-norm", sin(flapper_phi - 3 * LS_PI / 2));
403         fgSetDouble("/wing-phase/position-deg", flapper_phi*RAD_TO_DEG);
404         fgSetDouble("/wing-phase/position-one", 1);
405         fgSetDouble("/thorax/volume", 0);
406         fgSetDouble("/thorax/rpm",    0);
407         //      fgSetDouble("/thorax/volume", ((1+sin(2*(flapper_phi+LS_PI)))/2));
408         //      fgSetDouble("/thorax/rpm",    ((1+sin(2*(flapper_phi+LS_PI)))/2));
409       }
410     }
411
412
413     // add Gamma_horiz_deg to properties, mss 021213
414     if (use_gamma_horiz_on_speed) {
415       if (V_rel_wind > gamma_horiz_on_speed) {
416         fgSetDouble("/orientation/gamma-horiz-deg", (Gamma_horiz_rad * RAD_TO_DEG));
417       }
418       else {
419         fgSetDouble("/orientation/gamma-horiz-deg",  fgGetDouble("/orientation/heading-deg"));
420       }
421     }
422     else {
423       fgSetDouble("/orientation/gamma-horiz-deg",  fgGetDouble("/orientation/heading-deg"));
424     }
425     ls_update(multiloop);
426
427     // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
428     // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
429
430     // translate LaRCsim back to FG structure so that the
431     // autopilot (and the rest of the sim can use the updated
432     // values
433     copy_from_LaRCsim();
434
435     // but lets restore our original bogus altitude when we are done
436     if ( save_alt < -9000.0 ) {
437         set_Altitude( save_alt );
438     }
439 }
440
441
442 // Convert from the FGInterface struct to the LaRCsim generic_ struct
443 bool FGLaRCsim::copy_to_LaRCsim () {
444     Mass =      get_Mass();
445     I_xx =      get_I_xx();
446     I_yy =      get_I_yy();
447     I_zz =      get_I_zz();
448     I_xz =      get_I_xz();
449     // Dx_pilot =  get_Dx_pilot();
450     // Dy_pilot =  get_Dy_pilot();
451     // Dz_pilot =  get_Dz_pilot();
452     Dx_cg =     get_Dx_cg();
453     Dy_cg =     get_Dy_cg();
454     Dz_cg =     get_Dz_cg();
455     // F_X =       get_F_X();
456     // F_Y =       get_F_Y();
457     // F_Z =       get_F_Z();
458     // F_north =   get_F_north();
459     // F_east =    get_F_east();
460     // F_down =    get_F_down();
461     // F_X_aero =  get_F_X_aero();
462     // F_Y_aero =  get_F_Y_aero();
463     // F_Z_aero =  get_F_Z_aero();
464     // F_X_engine =        get_F_X_engine();
465     // F_Y_engine =        get_F_Y_engine();
466     // F_Z_engine =        get_F_Z_engine();
467     // F_X_gear =  get_F_X_gear();
468     // F_Y_gear =  get_F_Y_gear();
469     // F_Z_gear =  get_F_Z_gear();
470     // M_l_rp =    get_M_l_rp();
471     // M_m_rp =    get_M_m_rp();
472     // M_n_rp =    get_M_n_rp();
473     // M_l_cg =    get_M_l_cg();
474     // M_m_cg =    get_M_m_cg();
475     // M_n_cg =    get_M_n_cg();
476     // M_l_aero =  get_M_l_aero();
477     // M_m_aero =  get_M_m_aero();
478     // M_n_aero =  get_M_n_aero();
479     // M_l_engine =        get_M_l_engine();
480     // M_m_engine =        get_M_m_engine();
481     // M_n_engine =        get_M_n_engine();
482     // M_l_gear =  get_M_l_gear();
483     // M_m_gear =  get_M_m_gear();
484     // M_n_gear =  get_M_n_gear();
485     // V_dot_north =       get_V_dot_north();
486     // V_dot_east =        get_V_dot_east();
487     // V_dot_down =        get_V_dot_down();
488     // U_dot_body =        get_U_dot_body();
489     // V_dot_body =        get_V_dot_body();
490     // W_dot_body =        get_W_dot_body();
491     // A_X_cg =    get_A_X_cg();
492     // A_Y_cg =    get_A_Y_cg();
493     // A_Z_cg =    get_A_Z_cg();
494     // A_X_pilot = get_A_X_pilot();
495     // A_Y_pilot = get_A_Y_pilot();
496     // A_Z_pilot = get_A_Z_pilot();
497     // N_X_cg =    get_N_X_cg();
498     // N_Y_cg =    get_N_Y_cg();
499     // N_Z_cg =    get_N_Z_cg();
500     // N_X_pilot = get_N_X_pilot();
501     // N_Y_pilot = get_N_Y_pilot();
502     // N_Z_pilot = get_N_Z_pilot();
503     // P_dot_body =        get_P_dot_body();
504     // Q_dot_body =        get_Q_dot_body();
505     // R_dot_body =        get_R_dot_body();
506     // V_north =   get_V_north();
507     // V_east =    get_V_east();
508     // V_down =    get_V_down();
509     // V_north_rel_ground =        get_V_north_rel_ground();
510     // V_east_rel_ground = get_V_east_rel_ground();
511     // V_down_rel_ground = get_V_down_rel_ground();
512     // V_north_airmass =   get_V_north_airmass();
513     // V_east_airmass =    get_V_east_airmass();
514     // V_down_airmass =    get_V_down_airmass();
515     // V_north_rel_airmass =       get_V_north_rel_airmass();
516     // V_east_rel_airmass =        get_V_east_rel_airmass();
517     // V_down_rel_airmass =        get_V_down_rel_airmass();
518     // U_gust =    get_U_gust();
519     // V_gust =    get_V_gust();
520     // W_gust =    get_W_gust();
521     // U_body =    get_U_body();
522     // V_body =    get_V_body();
523     // W_body =    get_W_body();
524     // V_rel_wind =        get_V_rel_wind();
525     // V_true_kts =        get_V_true_kts();
526     // V_rel_ground =      get_V_rel_ground();
527     // V_inertial =        get_V_inertial();
528     // V_ground_speed =    get_V_ground_speed();
529     // V_equiv =   get_V_equiv();
530     // V_equiv_kts =       get_V_equiv_kts();
531     // V_calibrated =      get_V_calibrated();
532     // V_calibrated_kts =  get_V_calibrated_kts();
533     // P_body =    get_P_body();
534     // Q_body =    get_Q_body();
535     // R_body =    get_R_body();
536     // P_local =   get_P_local();
537     // Q_local =   get_Q_local();
538     // R_local =   get_R_local();
539     // P_total =   get_P_total();
540     // Q_total =   get_Q_total();
541     // R_total =   get_R_total();
542     // Phi_dot =   get_Phi_dot();
543     // Theta_dot = get_Theta_dot();
544     // Psi_dot =   get_Psi_dot();
545     // Latitude_dot =      get_Latitude_dot();
546     // Longitude_dot =     get_Longitude_dot();
547     // Radius_dot =        get_Radius_dot();
548     // Lat_geocentric =    get_Lat_geocentric();
549     // Lon_geocentric =    get_Lon_geocentric();
550     // Radius_to_vehicle = get_Radius_to_vehicle();
551     // Latitude =  get_Latitude();
552     // Longitude = get_Longitude();
553     // Altitude =  get_Altitude();
554     // Phi =       get_Phi();
555     // Theta =     get_Theta();
556     // Psi =       get_Psi();
557     // T_local_to_body_11 =        get_T_local_to_body_11();
558     // T_local_to_body_12 =        get_T_local_to_body_12();
559     // T_local_to_body_13 =        get_T_local_to_body_13();
560     // T_local_to_body_21 =        get_T_local_to_body_21();
561     // T_local_to_body_22 =        get_T_local_to_body_22();
562     // T_local_to_body_23 =        get_T_local_to_body_23();
563     // T_local_to_body_31 =        get_T_local_to_body_31();
564     // T_local_to_body_32 =        get_T_local_to_body_32();
565     // T_local_to_body_33 =        get_T_local_to_body_33();
566     // Gravity =   get_Gravity();
567     // Centrifugal_relief =        get_Centrifugal_relief();
568     // Alpha =     get_Alpha();
569     // Beta =      get_Beta();
570     // Alpha_dot = get_Alpha_dot();
571     // Beta_dot =  get_Beta_dot();
572     // Cos_alpha = get_Cos_alpha();
573     // Sin_alpha = get_Sin_alpha();
574     // Cos_beta =  get_Cos_beta();
575     // Sin_beta =  get_Sin_beta();
576     // Cos_phi =   get_Cos_phi();
577     // Sin_phi =   get_Sin_phi();
578     // Cos_theta = get_Cos_theta();
579     // Sin_theta = get_Sin_theta();
580     // Cos_psi =   get_Cos_psi();
581     // Sin_psi =   get_Sin_psi();
582     // Gamma_vert_rad =    get_Gamma_vert_rad();
583     // Gamma_horiz_rad =   get_Gamma_horiz_rad();
584     // Sigma =     get_Sigma();
585     // Density =   get_Density();
586     // V_sound =   get_V_sound();
587     // Mach_number =       get_Mach_number();
588     // Static_pressure =   get_Static_pressure();
589     // Total_pressure =    get_Total_pressure();
590     // Impact_pressure =   get_Impact_pressure();
591     // Dynamic_pressure =  get_Dynamic_pressure();
592     // Static_temperature =        get_Static_temperature();
593     // Total_temperature = get_Total_temperature();
594     // Sea_level_radius =  get_Sea_level_radius();
595     // Earth_position_angle =      get_Earth_position_angle();
596     // Runway_altitude =   get_Runway_altitude();
597     // Runway_latitude =   get_Runway_latitude();
598     // Runway_longitude =  get_Runway_longitude();
599     // Runway_heading =    get_Runway_heading();
600     // Radius_to_rwy =     get_Radius_to_rwy();
601     // D_cg_north_of_rwy = get_D_cg_north_of_rwy();
602     // D_cg_east_of_rwy =  get_D_cg_east_of_rwy();
603     // D_cg_above_rwy =    get_D_cg_above_rwy();
604     // X_cg_rwy =  get_X_cg_rwy();
605     // Y_cg_rwy =  get_Y_cg_rwy();
606     // H_cg_rwy =  get_H_cg_rwy();
607     // D_pilot_north_of_rwy =      get_D_pilot_north_of_rwy();
608     // D_pilot_east_of_rwy =       get_D_pilot_east_of_rwy();
609     // D_pilot_above_rwy = get_D_pilot_above_rwy();
610     // X_pilot_rwy =       get_X_pilot_rwy();
611     // Y_pilot_rwy =       get_Y_pilot_rwy();
612     // H_pilot_rwy =       get_H_pilot_rwy();
613
614     return true;
615 }
616
617
618 // Convert from the LaRCsim generic_ struct to the FGInterface struct
619 bool FGLaRCsim::copy_from_LaRCsim() {
620
621     // Mass properties and geometry values
622     _set_Inertias( Mass, I_xx, I_yy, I_zz, I_xz );
623     // set_Pilot_Location( Dx_pilot, Dy_pilot, Dz_pilot );
624     _set_CG_Position( Dx_cg, Dy_cg, Dz_cg );
625
626     // Forces
627     // set_Forces_Body_Total( F_X, F_Y, F_Z );
628     // set_Forces_Local_Total( F_north, F_east, F_down );
629     // set_Forces_Aero( F_X_aero, F_Y_aero, F_Z_aero );
630     // set_Forces_Engine( F_X_engine, F_Y_engine, F_Z_engine );
631     // set_Forces_Gear( F_X_gear, F_Y_gear, F_Z_gear );
632
633     // Moments
634     // set_Moments_Total_RP( M_l_rp, M_m_rp, M_n_rp );
635     // set_Moments_Total_CG( M_l_cg, M_m_cg, M_n_cg );
636     // set_Moments_Aero( M_l_aero, M_m_aero, M_n_aero );
637     // set_Moments_Engine( M_l_engine, M_m_engine, M_n_engine );
638     // set_Moments_Gear( M_l_gear, M_m_gear, M_n_gear );
639
640     // Accelerations
641     _set_Accels_Local( V_dot_north, V_dot_east, V_dot_down );
642     _set_Accels_Body( U_dot_body, V_dot_body, W_dot_body );
643     _set_Accels_CG_Body( A_X_cg, A_Y_cg, A_Z_cg );
644     _set_Accels_Pilot_Body( A_X_pilot, A_Y_pilot, A_Z_pilot );
645     _set_Accels_CG_Body_N( N_X_cg, N_Y_cg, -N_Z_cg );
646     // set_Accels_Pilot_Body_N( N_X_pilot, N_Y_pilot, N_Z_pilot );
647     // set_Accels_Omega( P_dot_body, Q_dot_body, R_dot_body );
648
649     // Velocities
650     _set_Velocities_Local( V_north, V_east, V_down );
651     // set_Velocities_Ground( V_north_rel_ground, V_east_rel_ground, 
652     //               V_down_rel_ground );
653     _set_Velocities_Local_Airmass( V_north_airmass, V_east_airmass,
654                                    V_down_airmass );
655     // set_Velocities_Local_Rel_Airmass( V_north_rel_airmass, 
656     //                          V_east_rel_airmass, V_down_rel_airmass );
657     // set_Velocities_Gust( U_gust, V_gust, W_gust );
658     _set_Velocities_Wind_Body( U_body, V_body, W_body );
659
660     _set_V_rel_wind( V_rel_wind );
661     // set_V_true_kts( V_true_kts );
662     // set_V_rel_ground( V_rel_ground );
663     // set_V_inertial( V_inertial );
664     _set_V_ground_speed( V_ground_speed );
665     // set_V_equiv( V_equiv );
666     _set_V_equiv_kts( V_equiv_kts );
667     // set_V_calibrated( V_calibrated );
668     _set_V_calibrated_kts( V_calibrated_kts );
669
670     _set_Omega_Body( P_body, Q_body, R_body );
671     // set_Omega_Local( P_local, Q_local, R_local );
672     // set_Omega_Total( P_total, Q_total, R_total );
673
674     _set_Euler_Rates( Phi_dot, Theta_dot, Psi_dot );
675     _set_Geocentric_Rates( Latitude_dot, Longitude_dot, Radius_dot );
676
677     _set_Mach_number( Mach_number );
678
679     SG_LOG( SG_FLIGHT, SG_DEBUG, "lon = " << Longitude 
680             << " lat_geoc = " << Lat_geocentric << " lat_geod = " << Latitude 
681             << " alt = " << Altitude << " sl_radius = " << Sea_level_radius 
682             << " radius_to_vehicle = " << Radius_to_vehicle );
683
684     double tmp_lon_geoc = Lon_geocentric;
685     while ( tmp_lon_geoc < -SGD_PI ) { tmp_lon_geoc += SGD_2PI; }
686     while ( tmp_lon_geoc > SGD_PI ) { tmp_lon_geoc -= SGD_2PI; }
687
688     double tmp_lon = Longitude;
689     while ( tmp_lon < -SGD_PI ) { tmp_lon += SGD_2PI; }
690     while ( tmp_lon > SGD_PI ) { tmp_lon -= SGD_2PI; }
691
692     // Positions
693     _set_Geocentric_Position( Lat_geocentric, tmp_lon_geoc, 
694                               Radius_to_vehicle );
695     _set_Geodetic_Position( Latitude, tmp_lon, Altitude );
696     _set_Euler_Angles( Phi, Theta, Psi );
697
698     _set_Altitude_AGL( Altitude - Runway_altitude );
699
700     // Miscellaneous quantities
701     // set_Gravity( Gravity );
702     // set_Centrifugal_relief( Centrifugal_relief );
703
704     _set_Alpha( Std_Alpha );
705     _set_Beta( Std_Beta );
706     // set_Alpha_dot( Std_Alpha_dot );
707     // set_Beta_dot( Std_Beta_dot );
708
709     // set_Cos_alpha( Cos_alpha );
710     // set_Sin_alpha( Sin_alpha );
711     // set_Cos_beta( Cos_beta );
712     // set_Sin_beta( Sin_beta );
713
714     // _set_Cos_phi( Cos_phi );
715     // set_Sin_phi( Sin_phi );
716     // _set_Cos_theta( Cos_theta );
717     // set_Sin_theta( Sin_theta );
718     // set_Cos_psi( Cos_psi );
719     // set_Sin_psi( Sin_psi );
720
721     _set_Gamma_vert_rad( Gamma_vert_rad );
722     // set_Gamma_horiz_rad( Gamma_horiz_rad );
723
724     // set_Sigma( Sigma );
725     _set_Density( Density );
726     // set_V_sound( V_sound );
727     // set_Mach_number( Mach_number );
728
729     _set_Static_pressure( Static_pressure );
730     // set_Total_pressure( Total_pressure );
731     // set_Impact_pressure( Impact_pressure );
732     // set_Dynamic_pressure( Dynamic_pressure );
733
734     _set_Static_temperature( Static_temperature );
735     // set_Total_temperature( Total_temperature );
736
737     _set_Sea_level_radius( Sea_level_radius );
738     _set_Earth_position_angle( Earth_position_angle );
739
740     _set_Runway_altitude( Runway_altitude );
741     // set_Runway_latitude( Runway_latitude );
742     // set_Runway_longitude( Runway_longitude );
743     // set_Runway_heading( Runway_heading );
744     // set_Radius_to_rwy( Radius_to_rwy );
745
746     // set_CG_Rwy_Local( D_cg_north_of_rwy, D_cg_east_of_rwy, D_cg_above_rwy);
747     // set_CG_Rwy_Rwy( X_cg_rwy, Y_cg_rwy, H_cg_rwy );
748     // set_Pilot_Rwy_Local( D_pilot_north_of_rwy, D_pilot_east_of_rwy, 
749     //                        D_pilot_above_rwy );
750     // set_Pilot_Rwy_Rwy( X_pilot_rwy, Y_pilot_rwy, H_pilot_rwy );
751
752     // printf("sin_lat_geo %f  cos_lat_geo %f\n", sin_Lat_geoc, cos_Lat_geoc);
753     // printf("sin_lat     %f  cos_lat     %f\n", 
754     //        get_sin_latitude(), get_cos_latitude());
755     // printf("sin_lon     %f  cos_lon     %f\n",
756     //        get_sin_longitude(), get_cos_longitude());
757
758     _set_Climb_Rate( -1 * V_down );
759     // cout << "climb rate = " << -V_down * 60 << endl;
760
761     if ( !strcmp(aero->getStringValue(), "uiuc") ) {
762         if (pilot_elev_no) {
763             globals->get_controls()->set_elevator(Long_control);
764             globals->get_controls()->set_elevator_trim(Long_trim);
765             //      controls.set_elevator(Long_control);
766             //      controls.set_elevator_trim(Long_trim);
767         }
768         if (pilot_ail_no) {
769             globals->get_controls()->set_aileron(Lat_control);
770             //    controls.set_aileron(Lat_control);
771         }
772         if (pilot_rud_no) {
773             globals->get_controls()->set_rudder(Rudder_pedal);
774             //    controls.set_rudder(Rudder_pedal);
775         }
776         if (pilot_throttle_no) {
777             globals->get_controls()->set_throttle(0,Throttle_pct);
778             //    controls.set_throttle(0,Throttle_pct);
779         }
780     }
781
782     return true;
783 }
784
785
786 void FGLaRCsim::set_ls(void) {
787     Phi=lsic->GetRollAngleRadIC();
788     Theta=lsic->GetPitchAngleRadIC();
789     Psi=lsic->GetHeadingRadIC();
790     V_north=lsic->GetVnorthFpsIC();
791     V_east=lsic->GetVeastFpsIC();
792     V_down=lsic->GetVdownFpsIC();
793     Altitude=lsic->GetAltitudeFtIC();
794     Latitude=lsic->GetLatitudeGDRadIC();
795     Longitude=lsic->GetLongitudeRadIC();
796     Runway_altitude=lsic->GetRunwayAltitudeFtIC();
797     V_north_airmass = lsic->GetVnorthAirmassFpsIC() * -1;
798     V_east_airmass = lsic->GetVeastAirmassFpsIC() * -1;
799     V_down_airmass = lsic->GetVdownAirmassFpsIC() * -1;
800     ls_loop(0.0,-1);
801     copy_from_LaRCsim();
802 }
803
804 void FGLaRCsim::snap_shot(void) {
805     lsic->SetLatitudeGDRadIC( get_Latitude() );
806     lsic->SetLongitudeRadIC( get_Longitude() );
807     lsic->SetAltitudeFtIC( get_Altitude() );
808     lsic->SetRunwayAltitudeFtIC( get_Runway_altitude() );
809     lsic->SetVtrueFpsIC( get_V_rel_wind() );
810     lsic->SetPitchAngleRadIC( get_Theta() );
811     lsic->SetRollAngleRadIC( get_Phi() );
812     lsic->SetHeadingRadIC( get_Psi() );
813     lsic->SetClimbRateFpsIC( get_Climb_Rate() );
814     lsic->SetVNEDAirmassFpsIC( get_V_north_airmass(),
815                                get_V_east_airmass(),
816                                get_V_down_airmass() );
817 }                               
818
819 //Positions
820 void FGLaRCsim::set_Latitude(double lat) {
821     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Latitude: " << lat  );
822     snap_shot();
823     lsic->SetLatitudeGDRadIC(lat);
824     set_ls();
825     copy_from_LaRCsim(); //update the bus
826 }
827
828 void FGLaRCsim::set_Longitude(double lon) {
829     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Longitude: " << lon  );
830     snap_shot();
831     lsic->SetLongitudeRadIC(lon);
832     set_ls();
833     copy_from_LaRCsim(); //update the bus
834 }
835
836 void FGLaRCsim::set_Altitude(double alt) {
837     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Altitude: " << alt  );
838     snap_shot();
839     lsic->SetAltitudeFtIC(alt);
840     set_ls();
841     copy_from_LaRCsim(); //update the bus
842 }
843
844 void FGLaRCsim::set_V_calibrated_kts(double vc) {
845     SG_LOG( SG_FLIGHT, SG_INFO, 
846             "FGLaRCsim::set_V_calibrated_kts: " << vc  );
847     snap_shot();
848     lsic->SetVcalibratedKtsIC(vc);
849     set_ls();
850     copy_from_LaRCsim(); //update the bus
851 }
852
853 void FGLaRCsim::set_Mach_number(double mach) {
854     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Mach_number: " << mach  );
855     snap_shot();
856     lsic->SetMachIC(mach);
857     set_ls();
858     copy_from_LaRCsim(); //update the bus
859 }
860
861 void FGLaRCsim::set_Velocities_Local( double north, double east, double down ){
862     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Velocities_local: " 
863             << north << "  " << east << "  " << down   );
864     snap_shot();
865     lsic->SetVNEDFpsIC(north, east, down);
866     set_ls();
867     copy_from_LaRCsim(); //update the bus
868 }
869
870 void FGLaRCsim::set_Velocities_Wind_Body( double u, double v, double w){
871     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Velocities_Wind_Body: " 
872             << u << "  " << v << "  " << w   );
873     snap_shot();
874     lsic->SetUVWFpsIC(u,v,w);
875     set_ls();
876     copy_from_LaRCsim(); //update the bus
877 }
878
879 //Euler angles 
880 void FGLaRCsim::set_Euler_Angles( double phi, double theta, double psi ) {
881     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Euler_angles: " 
882             << phi << "  " << theta << "  " << psi   );
883
884     snap_shot();
885     lsic->SetPitchAngleRadIC(theta);
886     lsic->SetRollAngleRadIC(phi);
887     lsic->SetHeadingRadIC(psi);
888     set_ls();
889     copy_from_LaRCsim(); //update the bus
890 }
891
892 //Flight Path
893 void FGLaRCsim::set_Climb_Rate( double roc) {
894     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Climb_rate: " << roc  );
895     snap_shot();
896     lsic->SetClimbRateFpsIC(roc);
897     set_ls();
898     copy_from_LaRCsim(); //update the bus
899 }
900
901 void FGLaRCsim::set_Gamma_vert_rad( double gamma) {
902     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Gamma_vert_rad: " << gamma  );
903     snap_shot();
904     lsic->SetFlightPathAngleRadIC(gamma);
905     set_ls();
906     copy_from_LaRCsim(); //update the bus
907 }
908
909 void FGLaRCsim::set_AltitudeAGL(double altagl) {
910     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_AltitudeAGL: " << altagl  );
911     snap_shot();
912     lsic->SetAltitudeAGLFtIC(altagl);
913     set_ls();
914     copy_from_LaRCsim();
915 }
916
917 /*  getting a namespace conflict...
918 void FGLaRCsim::set_Velocities_Local_Airmass (double wnorth, 
919                                               double weast, 
920                                               double wdown ) {
921     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Velocities_Local_Airmass: " 
922             << wnorth << "  " << weast << "  " << wdown );
923     snap_shot();
924     lsic->SetVNEDAirmassFpsIC( wnorth, weast, wdown );
925     set_ls();
926     copy_from_LaRCsim();
927 }
928 */
929
930 void FGLaRCsim::set_Static_pressure(double p) { 
931     SG_LOG( SG_FLIGHT, SG_INFO, 
932             "FGLaRCsim::set_Static_pressure: " << p  );
933     SG_LOG( SG_FLIGHT, SG_INFO, 
934             "LaRCsim does not support externally supplied atmospheric data" );
935 }
936
937 void FGLaRCsim::set_Static_temperature(double T) { 
938     SG_LOG( SG_FLIGHT, SG_INFO, 
939             "FGLaRCsim::set_Static_temperature: " << T  );
940     SG_LOG( SG_FLIGHT, SG_INFO, 
941             "LaRCsim does not support externally supplied atmospheric data" );
942
943 }
944
945 void FGLaRCsim::set_Density(double rho) {
946     SG_LOG( SG_FLIGHT, SG_INFO, "FGLaRCsim::set_Density: " << rho  );
947     SG_LOG( SG_FLIGHT, SG_INFO, 
948             "LaRCsim does not support externally supplied atmospheric data" );
949
950 }
951