]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim.cxx
Updated --help message.
[flightgear.git] / src / FDM / JSBSim.cxx
1 // JSBsim.cxx -- interface to the JSBsim flight model
2 //
3 // Written by Curtis Olson, started February 1999.
4 //
5 // Copyright (C) 1999  Curtis L. Olson  - curt@flightgear.org
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #include <simgear/compiler.h>
25
26 #ifdef SG_MATH_EXCEPTION_CLASH
27 #  include <math.h>
28 #endif
29
30 #include STL_STRING
31
32 #include <simgear/constants.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/math/sg_geodesy.hxx>
35 #include <simgear/misc/sg_path.hxx>
36
37 #include <Scenery/scenery.hxx>
38
39 #include <Aircraft/aircraft.hxx>
40 #include <Controls/controls.hxx>
41 #include <Main/globals.hxx>
42 #include <Main/fg_props.hxx>
43
44 #include <FDM/JSBSim/FGFDMExec.h>
45 #include <FDM/JSBSim/FGAircraft.h>
46 #include <FDM/JSBSim/FGFCS.h>
47 #include <FDM/JSBSim/FGPosition.h>
48 #include <FDM/JSBSim/FGRotation.h>
49 #include <FDM/JSBSim/FGState.h>
50 #include <FDM/JSBSim/FGTranslation.h>
51 #include <FDM/JSBSim/FGAuxiliary.h>
52 #include <FDM/JSBSim/FGInitialCondition.h>
53 #include <FDM/JSBSim/FGTrim.h>
54 #include <FDM/JSBSim/FGAtmosphere.h>
55 #include <FDM/JSBSim/FGMassBalance.h>
56 #include <FDM/JSBSim/FGAerodynamics.h>
57 #include <FDM/JSBSim/FGLGear.h>
58 #include "JSBSim.hxx"
59
60 /******************************************************************************/
61
62 FGJSBsim::FGJSBsim( double dt ) 
63   : FGInterface(dt)
64 {
65     bool result;
66    
67     fdmex = new FGFDMExec;
68     
69     State           = fdmex->GetState();
70     Atmosphere      = fdmex->GetAtmosphere();
71     FCS             = fdmex->GetFCS();
72     MassBalance     = fdmex->GetMassBalance();
73     Propulsion      = fdmex->GetPropulsion();
74     Aircraft        = fdmex->GetAircraft();
75     Translation     = fdmex->GetTranslation();
76     Rotation        = fdmex->GetRotation();
77     Position        = fdmex->GetPosition();
78     Auxiliary       = fdmex->GetAuxiliary();
79     Aerodynamics    = fdmex->GetAerodynamics();
80     GroundReactions = fdmex->GetGroundReactions();  
81   
82     
83     Atmosphere->UseInternal();
84     
85     fgic=new FGInitialCondition(fdmex);
86     needTrim=true;
87   
88     SGPath aircraft_path( globals->get_fg_root() );
89     aircraft_path.append( "Aircraft" );
90
91     SGPath engine_path( globals->get_fg_root() );
92     engine_path.append( "Engine" );
93     set_delta_t( dt );
94     State->Setdt( dt );
95
96     result = fdmex->LoadModel( aircraft_path.str(),
97                                engine_path.str(),
98                                fgGetString("/sim/aero") );
99     
100     if (result) {
101       SG_LOG( SG_FLIGHT, SG_INFO, "  loaded aero.");
102     } else {
103       SG_LOG( SG_FLIGHT, SG_INFO,
104               "  aero does not exist (you may have mis-typed the name).");
105       throw(-1);
106     }
107
108     SG_LOG( SG_FLIGHT, SG_INFO, "" );
109     SG_LOG( SG_FLIGHT, SG_INFO, "" );
110     SG_LOG( SG_FLIGHT, SG_INFO, "After loading aero definition file ..." );
111
112     int Neng = Propulsion->GetNumEngines();
113     SG_LOG( SG_FLIGHT, SG_INFO, "num engines = " << Neng );
114     
115     if ( GroundReactions->GetNumGearUnits() <= 0 ) {
116         SG_LOG( SG_FLIGHT, SG_ALERT, "num gear units = "
117                 << GroundReactions->GetNumGearUnits() );
118         SG_LOG( SG_FLIGHT, SG_ALERT, "This is a very bad thing because with 0 gear units, the ground trimming");
119          SG_LOG( SG_FLIGHT, SG_ALERT, "routine (coming up later in the code) will core dump.");
120          SG_LOG( SG_FLIGHT, SG_ALERT, "Halting the sim now, and hoping a solution will present itself soon!");
121          exit(-1);
122     }
123         
124     
125     init_gear();
126
127                                 // Set initial fuel levels if provided.
128     for (unsigned int i = 0; i < Propulsion->GetNumTanks(); i++) {
129       SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
130       if (node->getChild("level-gal_us", 0, false) != 0)
131         Propulsion->GetTank(i)
132           ->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
133     }
134     
135     fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
136     fgSetDouble("/fdm/trim/throttle",   FCS->GetThrottleCmd(0));
137     fgSetDouble("/fdm/trim/aileron",    FCS->GetDaCmd());
138     fgSetDouble("/fdm/trim/rudder",     FCS->GetDrCmd());
139
140     startup_trim = fgGetNode("/sim/startup/trim", true);
141
142     trimmed = fgGetNode("/fdm/trim/trimmed", true);
143     trimmed->setBoolValue(false);
144
145     pitch_trim = fgGetNode("/fdm/trim/pitch-trim", true );
146     throttle_trim = fgGetNode("/fdm/trim/throttle", true );
147     aileron_trim = fgGetNode("/fdm/trim/aileron", true );
148     rudder_trim = fgGetNode("/fdm/trim/rudder", true );
149     
150     
151     stall_warning = fgGetNode("/sim/aero/alarms/stall-warning",true);
152     stall_warning->setDoubleValue(0);
153 }
154
155 /******************************************************************************/
156 FGJSBsim::~FGJSBsim(void) {
157     if (fdmex != NULL) {
158         delete fdmex; fdmex=NULL;
159         delete fgic; fgic=NULL;
160     }  
161 }
162
163 /******************************************************************************/
164
165 // Initialize the JSBsim flight model, dt is the time increment for
166 // each subsequent iteration through the EOM
167
168 void FGJSBsim::init() {
169     
170     SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
171    
172     // Explicitly call the superclass's
173     // init method first.
174     common_init();
175
176     fdmex->GetState()->Initialize(fgic);
177     fdmex->RunIC(fgic); //loop JSBSim once w/o integrating
178     // fdmex->Run();       //loop JSBSim once
179     copy_from_JSBsim(); //update the bus
180
181     SG_LOG( SG_FLIGHT, SG_INFO, "  Initialized JSBSim with:" );
182
183     switch(fgic->GetSpeedSet()) {
184     case setned:
185         SG_LOG(SG_FLIGHT,SG_INFO, "  Vn,Ve,Vd= "
186                << Position->GetVn() << ", "
187                << Position->GetVe() << ", "
188                << Position->GetVd() << " ft/s");
189     break;
190     case setuvw:
191         SG_LOG(SG_FLIGHT,SG_INFO, "  U,V,W= "
192                << Translation->GetUVW(1) << ", "
193                << Translation->GetUVW(2) << ", "
194                << Translation->GetUVW(3) << " ft/s");
195     break;
196     case setmach:
197         SG_LOG(SG_FLIGHT,SG_INFO, "  Mach: "
198                << Translation->GetMach() );
199     break;
200     case setvc:
201     default:
202         SG_LOG(SG_FLIGHT,SG_INFO, "  Indicated Airspeed: "
203                << Auxiliary->GetVcalibratedKTS() << " knots" );
204     break;
205     }
206     
207     stall_warning->setDoubleValue(0);
208     
209     SG_LOG( SG_FLIGHT, SG_INFO, "  Bank Angle: "
210             <<  Rotation->Getphi()*RADTODEG << " deg" );
211     SG_LOG( SG_FLIGHT, SG_INFO, "  Pitch Angle: "
212             << Rotation->Gettht()*RADTODEG << " deg" );
213     SG_LOG( SG_FLIGHT, SG_INFO, "  True Heading: "
214             << Rotation->Getpsi()*RADTODEG << " deg" );
215     SG_LOG( SG_FLIGHT, SG_INFO, "  Latitude: "
216             << Position->GetLatitude() << " deg" );
217     SG_LOG( SG_FLIGHT, SG_INFO, "  Longitude: "
218             << Position->GetLongitude() << " deg" );
219     SG_LOG( SG_FLIGHT, SG_INFO, "  Altitude: "
220         << Position->Geth() << " feet" );
221     SG_LOG( SG_FLIGHT, SG_INFO, "  loaded initial conditions" );
222
223     SG_LOG( SG_FLIGHT, SG_INFO, "  set dt" );
224
225     SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
226     
227     SG_LOG( SG_FLIGHT, SG_INFO, "FGControls::get_gear_down()= " << 
228                                   globals->get_controls()->get_gear_down() );
229     
230
231    
232 }
233
234 /******************************************************************************/
235
236 // Run an iteration of the EOM (equations of motion)
237
238 void
239 FGJSBsim::update( int multiloop ) {
240
241     int i;
242
243     // double save_alt = 0.0;
244
245     copy_to_JSBsim();
246
247     trimmed->setBoolValue(false);
248
249     if ( needTrim ) {
250       if ( startup_trim->getBoolValue() ) {
251         do_trim();
252       } else {
253         fdmex->RunIC(fgic);  //apply any changes made through the set_ functions
254       }
255       needTrim = false;  
256     }    
257     
258     for ( i=0; i < multiloop; i++ ) {
259         fdmex->Run();
260     }
261
262     FGJSBBase::Message* msg;
263     while (fdmex->ReadMessage()) {
264       msg = fdmex->ProcessMessage();
265       switch (msg->type) {
266       case FGJSBBase::Message::eText:
267         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text );
268         break;
269       case FGJSBBase::Message::eBool:
270         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->bVal );
271         break;
272       case FGJSBBase::Message::eInteger:
273         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->iVal );
274         break;
275       case FGJSBBase::Message::eDouble:
276         SG_LOG( SG_FLIGHT, SG_INFO, msg->messageId << ": " << msg->text << " " << msg->dVal );
277         break;
278       default:
279         SG_LOG( SG_FLIGHT, SG_INFO, "Unrecognized message type." );
280         break;
281       }
282     }
283
284     // translate JSBsim back to FG structure so that the
285     // autopilot (and the rest of the sim can use the updated values
286     copy_from_JSBsim();
287 }
288
289 /******************************************************************************/
290
291 // Convert from the FGInterface struct to the JSBsim generic_ struct
292
293 bool FGJSBsim::copy_to_JSBsim() {
294     unsigned int i;
295
296     // copy control positions into the JSBsim structure
297
298     FCS->SetDaCmd( globals->get_controls()->get_aileron());
299     FCS->SetRollTrimCmd( globals->get_controls()->get_aileron_trim() );
300     FCS->SetDeCmd( globals->get_controls()->get_elevator());
301     FCS->SetPitchTrimCmd( globals->get_controls()->get_elevator_trim() );
302     FCS->SetDrCmd( -globals->get_controls()->get_rudder() );
303     FCS->SetYawTrimCmd( -globals->get_controls()->get_rudder_trim() );
304     FCS->SetDfCmd(  globals->get_controls()->get_flaps() );
305     FCS->SetDsbCmd( 0.0 ); //speedbrakes
306     FCS->SetDspCmd( 0.0 ); //spoilers
307     FCS->SetLBrake( globals->get_controls()->get_brake( 0 ) );
308     FCS->SetRBrake( globals->get_controls()->get_brake( 1 ) );
309     FCS->SetCBrake( globals->get_controls()->get_brake( 2 ) );
310     FCS->SetGearCmd( globals->get_controls()->get_gear_down());
311     for (i = 0; i < Propulsion->GetNumEngines(); i++) {
312       FGEngine * eng = Propulsion->GetEngine(i);
313       SGPropertyNode * node = fgGetNode("engines/engine", i, true);
314       FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
315       FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
316       FCS->SetPropAdvanceCmd(i, globals->get_controls()->get_prop_advance(i));
317       Propulsion->GetThruster(i)->SetRPM(node->getDoubleValue("rpm"));
318       eng->SetMagnetos( globals->get_controls()->get_magnetos(i) );
319       eng->SetStarter( globals->get_controls()->get_starter(i) );
320     }
321
322     Position->SetSeaLevelRadius( get_Sea_level_radius() );
323     Position->SetRunwayRadius( scenery.get_cur_elev()*SG_METER_TO_FEET
324                                + get_Sea_level_radius() );
325
326     Atmosphere->SetExTemperature(get_Static_temperature());
327     Atmosphere->SetExPressure(get_Static_pressure());
328     Atmosphere->SetExDensity(get_Density());
329     Atmosphere->SetWindNED(get_V_north_airmass(),
330                            get_V_east_airmass(),
331                            get_V_down_airmass());
332 //    SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
333 //                  << get_V_north_airmass() << ", "
334 //                  << get_V_east_airmass()  << ", "
335 //                  << get_V_down_airmass() );
336
337     for (i = 0; i < Propulsion->GetNumTanks(); i++) {
338       SGPropertyNode * node = fgGetNode("/consumables/fuel/tank", i, true);
339       FGTank * tank = Propulsion->GetTank(i);
340       tank->SetContents(node->getDoubleValue("level-gal_us") * 6.6);
341 //       tank->SetContents(node->getDoubleValue("level-lb"));
342     }
343
344     return true;
345 }
346
347 /******************************************************************************/
348
349 // Convert from the JSBsim generic_ struct to the FGInterface struct
350
351 bool FGJSBsim::copy_from_JSBsim() {
352     unsigned int i, j;
353
354     _set_Inertias( MassBalance->GetMass(),
355                    MassBalance->GetIxx(),
356                    MassBalance->GetIyy(),
357                    MassBalance->GetIzz(),
358                    MassBalance->GetIxz() );
359
360     _set_CG_Position( MassBalance->GetXYZcg(1),
361                       MassBalance->GetXYZcg(2),
362                       MassBalance->GetXYZcg(3) );
363
364     _set_Accels_Body( Aircraft->GetBodyAccel()(1),
365                       Aircraft->GetBodyAccel()(2),
366                       Aircraft->GetBodyAccel()(3) );
367
368     //_set_Accels_CG_Body( Aircraft->GetBodyAccel()(1),
369     //                     Aircraft->GetBodyAccel()(2),
370     //                     Aircraft->GetBodyAccel()(3) );
371     //
372     _set_Accels_CG_Body_N ( Aircraft->GetNcg()(1),
373                             Aircraft->GetNcg()(2),
374                             Aircraft->GetNcg()(3) );
375     
376     _set_Accels_Pilot_Body( Auxiliary->GetNpilot()(1),
377                             Auxiliary->GetNpilot()(2),
378                             Auxiliary->GetNpilot()(3) );
379
380    // _set_Accels_Pilot_Body_N( Auxiliary->GetPilotAccel()(1)/32.1739,
381    //                           Auxiliary->GetNpilot(2)/32.1739,
382    //                           Auxiliary->GetNpilot(3)/32.1739 );
383
384     _set_Nlf( Aircraft->GetNlf() );
385
386     // Velocities
387
388     _set_Velocities_Local( Position->GetVn(),
389                            Position->GetVe(),
390                            Position->GetVd() );
391
392     _set_Velocities_Wind_Body( Translation->GetUVW(1),
393                                Translation->GetUVW(2),
394                                Translation->GetUVW(3) );
395
396     _set_V_rel_wind( Translation->GetVt() );
397
398     _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
399
400     // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
401
402     _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
403
404     _set_V_ground_speed( Position->GetVground() );
405
406     _set_Omega_Body( Rotation->GetPQR(1),
407                      Rotation->GetPQR(2),
408                      Rotation->GetPQR(3) );
409
410     _set_Euler_Rates( Rotation->GetEulerRates(1),
411                       Rotation->GetEulerRates(2),
412                       Rotation->GetEulerRates(3) );
413
414     _set_Geocentric_Rates(Position->GetLatitudeDot(),
415                           Position->GetLongitudeDot(),
416                           Position->Gethdot() );
417
418     _set_Mach_number( Translation->GetMach() );
419
420     // Positions
421     _updateGeocentricPosition( Position->GetLatitude(),
422                                Position->GetLongitude(),
423                                Position->Geth() );
424
425     _set_Altitude_AGL( Position->GetDistanceAGL() );
426
427     _set_Euler_Angles( Rotation->Getphi(),
428                        Rotation->Gettht(),
429                        Rotation->Getpsi() );
430
431     _set_Alpha( Translation->Getalpha() );
432     _set_Beta( Translation->Getbeta() );
433
434
435     _set_Gamma_vert_rad( Position->GetGamma() );
436     // set_Gamma_horiz_rad( Gamma_horiz_rad );
437
438     _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
439
440     _set_Climb_Rate( Position->Gethdot() );
441
442
443     for ( i = 1; i <= 3; i++ ) {
444         for ( j = 1; j <= 3; j++ ) {
445             _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
446         }
447     }
448
449                                 // Copy the engine values from JSBSim.
450     for( i=0; i < Propulsion->GetNumEngines(); i++ ) {
451       SGPropertyNode * node = fgGetNode("engines/engine", i, true);
452       FGEngine * eng = Propulsion->GetEngine(i);
453       FGThruster * thrust = Propulsion->GetThruster(i);
454
455       node->setDoubleValue("mp-osi", eng->getManifoldPressure_inHg());
456       node->setDoubleValue("rpm", thrust->GetRPM());
457       node->setDoubleValue("egt-degf", eng->getExhaustGasTemp_degF());
458       node->setDoubleValue("fuel-flow-gph", eng->getFuelFlow_gph());
459       node->setDoubleValue("cht-degf", eng->getCylinderHeadTemp_degF());
460       node->setDoubleValue("oil-temperature-degf", eng->getOilTemp_degF());
461       node->setDoubleValue("oil-pressure-psi", eng->getOilPressure_psi());
462       node->setBoolValue("running", eng->GetRunning());
463       node->setBoolValue("cranking", eng->GetCranking());
464     }
465
466     static const SGPropertyNode *fuel_freeze
467         = fgGetNode("/sim/freeze/fuel");
468
469                                 // Copy the fuel levels from JSBSim if fuel
470                                 // freeze not enabled.
471     if ( ! fuel_freeze->getBoolValue() ) {
472         for (i = 0; i < Propulsion->GetNumTanks(); i++) {
473             SGPropertyNode * node
474                 = fgGetNode("/consumables/fuel/tank", i, true);
475             double contents = Propulsion->GetTank(i)->GetContents();
476             node->setDoubleValue("level-gal_us", contents/6.6);
477             // node->setDoubleValue("level-lb", contents);
478         }
479     }
480
481     update_gear();
482     
483     stall_warning->setDoubleValue( Aircraft->GetStallWarn() );
484     
485     return true;
486 }
487
488 bool FGJSBsim::ToggleDataLogging(void) {
489     return fdmex->GetOutput()->Toggle();
490 }
491
492
493 bool FGJSBsim::ToggleDataLogging(bool state) {
494     if (state) {
495       fdmex->GetOutput()->Enable();
496       return true;
497     } else {
498       fdmex->GetOutput()->Disable();
499       return false;
500     }
501 }
502
503
504 //Positions
505 void FGJSBsim::set_Latitude(double lat) {
506     static const SGPropertyNode *altitude = fgGetNode("/position/altitude-ft");
507     double alt;
508     if ( altitude->getDoubleValue() > -9990 ) {
509       alt = altitude->getDoubleValue();
510     } else {
511       alt = 0.0;
512     }
513
514     double sea_level_radius_meters, lat_geoc;
515
516     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
517     SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) =  " << alt );
518
519     sgGeodToGeoc( lat, alt * SG_FEET_TO_METER, &sea_level_radius_meters, &lat_geoc );
520     
521     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
522     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET  );
523     fgic->SetLatitudeRadIC( lat_geoc );
524     needTrim=true;
525 }
526
527 void FGJSBsim::set_Longitude(double lon) {
528
529     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
530
531     fgic->SetLongitudeRadIC( lon );
532     needTrim=true;
533 }
534
535 void FGJSBsim::set_Altitude(double alt) {
536     static const SGPropertyNode *latitude = fgGetNode("/position/latitude-deg");
537
538     double sea_level_radius_meters,lat_geoc;
539
540     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
541     SG_LOG(SG_FLIGHT,SG_INFO, "  lat (deg) = " << latitude->getDoubleValue() );
542
543     sgGeodToGeoc( latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS, alt,
544       &sea_level_radius_meters, &lat_geoc);
545     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
546     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
547     fgic->SetLatitudeRadIC( lat_geoc );
548     fgic->SetAltitudeFtIC(alt);
549     needTrim=true;
550 }
551
552 void FGJSBsim::set_V_calibrated_kts(double vc) {
553     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " <<  vc );
554
555     fgic->SetVcalibratedKtsIC(vc);
556     needTrim=true;
557 }
558
559 void FGJSBsim::set_Mach_number(double mach) {
560     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " <<  mach );
561
562     fgic->SetMachIC(mach);
563     needTrim=true;
564 }
565
566 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
567     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
568        << north << ", " <<  east << ", " << down );
569
570     fgic->SetVnorthFpsIC(north);
571     fgic->SetVeastFpsIC(east);
572     fgic->SetVdownFpsIC(down);
573     needTrim=true;
574 }
575
576 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
577     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
578        << u << ", " <<  v << ", " <<  w );
579
580     fgic->SetUBodyFpsIC(u);
581     fgic->SetVBodyFpsIC(v);
582     fgic->SetWBodyFpsIC(w);
583     needTrim=true;
584 }
585
586 //Euler angles
587 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
588     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
589        << phi << ", " << theta << ", " << psi );
590
591     fgic->SetPitchAngleRadIC(theta);
592     fgic->SetRollAngleRadIC(phi);
593     fgic->SetTrueHeadingRadIC(psi);
594     needTrim=true;
595 }
596
597 //Flight Path
598 void FGJSBsim::set_Climb_Rate( double roc) {
599     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
600
601     fgic->SetClimbRateFpsIC(roc);
602     needTrim=true;
603 }
604
605 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
606     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
607
608     fgic->SetFlightPathAngleRadIC(gamma);
609     needTrim=true;
610 }
611
612 //Earth
613 void FGJSBsim::set_Sea_level_radius(double slr) {
614     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
615
616     fgic->SetSeaLevelRadiusFtIC(slr);
617     needTrim=true;
618 }
619
620 void FGJSBsim::set_Runway_altitude(double ralt) {
621     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
622
623     _set_Runway_altitude( ralt );
624     fgic->SetTerrainAltitudeFtIC( ralt );
625     needTrim=true;
626 }
627
628 void FGJSBsim::set_Static_pressure(double p) {
629     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
630
631     Atmosphere->SetExPressure(p);
632     if(Atmosphere->External() == true)
633     needTrim=true;
634 }
635
636 void FGJSBsim::set_Static_temperature(double T) {
637     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
638     
639     Atmosphere->SetExTemperature(T);
640     if(Atmosphere->External() == true)
641     needTrim=true;
642 }
643  
644
645 void FGJSBsim::set_Density(double rho) {
646     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
647     
648     Atmosphere->SetExDensity(rho);
649     if(Atmosphere->External() == true)
650     needTrim=true;
651 }
652   
653 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth, 
654                          double weast, 
655                          double wdown ) {
656     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: " 
657        << wnorth << ", " << weast << ", " << wdown );
658     
659     _set_Velocities_Local_Airmass( wnorth, weast, wdown );
660     Atmosphere->SetWindNED(wnorth, weast, wdown );
661     if(Atmosphere->External() == true)
662         needTrim=true;
663 }     
664
665 void FGJSBsim::init_gear(void ) {
666     
667     FGGroundReactions* gr=fdmex->GetGroundReactions();
668     int Ngear=GroundReactions->GetNumGearUnits();
669     for (int i=0;i<Ngear;i++) {
670       SGPropertyNode * node = fgGetNode("gear/gear", i, true);
671       node->setDoubleValue("xoffset-in",
672                            gr->GetGearUnit(i)->GetBodyLocation()(1));
673       node->setDoubleValue("yoffset-in",
674                            gr->GetGearUnit(i)->GetBodyLocation()(2));
675       node->setDoubleValue("zoffset-in",
676                            gr->GetGearUnit(i)->GetBodyLocation()(3));
677       node->setBoolValue("wow", gr->GetGearUnit(i)->GetWOW());
678       node->setBoolValue("has-brake", gr->GetGearUnit(i)->GetBrakeGroup() > 0);
679       node->setDoubleValue("position", FCS->GetGearPos());
680     }  
681 }
682
683 void FGJSBsim::update_gear(void) {
684     
685     FGGroundReactions* gr=fdmex->GetGroundReactions();
686     int Ngear=GroundReactions->GetNumGearUnits();
687     for (int i=0;i<Ngear;i++) {
688       SGPropertyNode * node = fgGetNode("gear/gear", i, true);
689       node->getChild("wow", 0, true)
690         ->setBoolValue(gr->GetGearUnit(i)->GetWOW());
691       node->getChild("position", 0, true)
692         ->setDoubleValue(FCS->GetGearPos());
693     }  
694 }
695
696 void FGJSBsim::do_trim(void) {
697
698         FGTrim *fgtrim;
699         if(fgic->GetVcalibratedKtsIC() < 10 ) {
700             fgic->SetVcalibratedKtsIC(0.0);
701             fgtrim=new FGTrim(fdmex,fgic,tGround);
702         } else {
703             fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
704         }
705         if( !fgtrim->DoTrim() ) {
706             fgtrim->Report();
707             fgtrim->TrimStats();
708         } else {
709             trimmed->setBoolValue(true);
710         }
711         State->ReportState();
712         delete fgtrim;
713         pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
714         throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
715         aileron_trim->setDoubleValue( FCS->GetDaCmd() );
716         rudder_trim->setDoubleValue( FCS->GetDrCmd() );
717
718         globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
719         globals->get_controls()->set_elevator(FCS->GetDeCmd());
720         globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
721                                               FCS->GetThrottleCmd(0));
722
723         globals->get_controls()->set_aileron(FCS->GetDaCmd());
724         globals->get_controls()->set_rudder( FCS->GetDrCmd());
725     
726         SG_LOG( SG_FLIGHT, SG_INFO, "  Trim complete" );
727 }