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