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