]> 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     for( i=0; i<get_num_engines(); i++ ) {
263       FGEngInterface * e = get_engine(i);
264       FGEngine * eng = Propulsion->GetEngine(i);
265       FGThruster * thrust = Propulsion->GetThruster(i);
266       e->set_Manifold_Pressure( eng->getManifoldPressure_inHg() );
267       e->set_RPM( thrust->GetRPM() );
268       e->set_EGT( eng->getExhaustGasTemp_degF() );
269       e->set_CHT( eng->getCylinderHeadTemp_degF() );
270       e->set_Oil_Temp( eng->getOilTemp_degF() );
271       e->set_Running_Flag( eng->GetRunning() );
272       e->set_Cranking_Flag( eng->GetCranking() );
273     }
274
275     
276     update_gear();
277     
278     stall_warning->setDoubleValue( Aircraft->GetStallWarn() );
279     
280     // translate JSBsim back to FG structure so that the
281     // autopilot (and the rest of the sim can use the updated values
282     copy_from_JSBsim();
283     return true;
284 }
285
286 /******************************************************************************/
287
288 // Convert from the FGInterface struct to the JSBsim generic_ struct
289
290 bool FGJSBsim::copy_to_JSBsim() {
291     // copy control positions into the JSBsim structure
292
293     FCS->SetDaCmd( globals->get_controls()->get_aileron());
294     FCS->SetDeCmd( globals->get_controls()->get_elevator());
295     FCS->SetPitchTrimCmd(globals->get_controls()->get_elevator_trim());
296     FCS->SetDrCmd( -globals->get_controls()->get_rudder());
297     FCS->SetDfCmd(  globals->get_controls()->get_flaps() );
298     FCS->SetDsbCmd( 0.0 ); //speedbrakes
299     FCS->SetDspCmd( 0.0 ); //spoilers
300     FCS->SetLBrake( globals->get_controls()->get_brake( 0 ) );
301     FCS->SetRBrake( globals->get_controls()->get_brake( 1 ) );
302     FCS->SetCBrake( globals->get_controls()->get_brake( 2 ) );
303     for (int i = 0; i < get_num_engines(); i++) {
304       FCS->SetThrottleCmd(i, globals->get_controls()->get_throttle(i));
305       FCS->SetMixtureCmd(i, globals->get_controls()->get_mixture(i));
306     }
307
308     Position->SetSeaLevelRadius( get_Sea_level_radius() );
309     Position->SetRunwayRadius( scenery.get_cur_elev()*SG_METER_TO_FEET
310                                + get_Sea_level_radius() );
311
312     Atmosphere->SetExTemperature(get_Static_temperature());
313     Atmosphere->SetExPressure(get_Static_pressure());
314     Atmosphere->SetExDensity(get_Density());
315     Atmosphere->SetWindNED(get_V_north_airmass(),
316                            get_V_east_airmass(),
317                            get_V_down_airmass());
318 //    SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
319 //                  << get_V_north_airmass() << ", "
320 //                  << get_V_east_airmass()  << ", "
321 //                  << get_V_down_airmass() );
322
323     return true;
324 }
325
326 /******************************************************************************/
327
328 // Convert from the JSBsim generic_ struct to the FGInterface struct
329
330 bool FGJSBsim::copy_from_JSBsim() {
331     unsigned int i, j;
332
333     _set_Inertias( MassBalance->GetMass(),
334                    MassBalance->GetIxx(),
335                    MassBalance->GetIyy(),
336                    MassBalance->GetIzz(),
337                    MassBalance->GetIxz() );
338
339     _set_CG_Position( MassBalance->GetXYZcg(1),
340                       MassBalance->GetXYZcg(2),
341                       MassBalance->GetXYZcg(3) );
342
343     _set_Accels_Body( Aircraft->GetBodyAccel()(1),
344                       Aircraft->GetBodyAccel()(2),
345                       Aircraft->GetBodyAccel()(3) );
346
347     //_set_Accels_CG_Body( Aircraft->GetBodyAccel()(1),
348     //                     Aircraft->GetBodyAccel()(2),
349     //                     Aircraft->GetBodyAccel()(3) );
350     //
351     _set_Accels_CG_Body_N ( Aircraft->GetNcg()(1),
352                             Aircraft->GetNcg()(2),
353                             Aircraft->GetNcg()(3) );
354     
355     _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel()(1),
356                             Auxiliary->GetPilotAccel()(2),
357                             Auxiliary->GetPilotAccel()(3) );
358
359    // _set_Accels_Pilot_Body_N( Auxiliary->GetPilotAccel()(1)/32.1739,
360    //                           Auxiliary->GetNpilot(2)/32.1739,
361    //                           Auxiliary->GetNpilot(3)/32.1739 );
362
363     _set_Nlf( Aerodynamics->GetNlf() );
364
365     // Velocities
366
367     _set_Velocities_Local( Position->GetVn(),
368                            Position->GetVe(),
369                            Position->GetVd() );
370
371     _set_Velocities_Wind_Body( Translation->GetUVW(1),
372                                Translation->GetUVW(2),
373                                Translation->GetUVW(3) );
374
375     _set_V_rel_wind( Translation->GetVt() );
376
377     _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
378
379     // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
380
381     _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
382
383     _set_V_ground_speed( Position->GetVground() );
384
385     _set_Omega_Body( Rotation->GetPQR(1),
386                      Rotation->GetPQR(2),
387                      Rotation->GetPQR(3) );
388
389     _set_Euler_Rates( Rotation->GetEulerRates(1),
390                       Rotation->GetEulerRates(2),
391                       Rotation->GetEulerRates(3) );
392
393     _set_Geocentric_Rates(Position->GetLatitudeDot(),
394                           Position->GetLongitudeDot(),
395                           Position->Gethdot() );
396
397     _set_Mach_number( Translation->GetMach() );
398
399     // Positions
400     _updatePosition( Position->GetLatitude(),
401                      Position->GetLongitude(),
402                      Position->Geth() );
403
404     _set_Altitude_AGL( Position->GetDistanceAGL() );
405
406     _set_Euler_Angles( Rotation->Getphi(),
407                        Rotation->Gettht(),
408                        Rotation->Getpsi() );
409
410     _set_Alpha( Translation->Getalpha() );
411     _set_Beta( Translation->Getbeta() );
412
413
414     _set_Gamma_vert_rad( Position->GetGamma() );
415     // set_Gamma_horiz_rad( Gamma_horiz_rad );
416
417     _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
418
419     _set_Climb_Rate( Position->Gethdot() );
420
421
422     for ( i = 1; i <= 3; i++ ) {
423         for ( j = 1; j <= 3; j++ ) {
424             _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
425         }
426     }
427     return true;
428 }
429
430 bool FGJSBsim::ToggleDataLogging(void) {
431     return fdmex->GetOutput()->Toggle();
432 }
433
434
435 bool FGJSBsim::ToggleDataLogging(bool state) {
436     if (state) {
437       fdmex->GetOutput()->Enable();
438       return true;
439     } else {
440       fdmex->GetOutput()->Disable();
441       return false;
442     }
443 }
444
445
446 //Positions
447 void FGJSBsim::set_Latitude(double lat) {
448     static const SGPropertyNode *altitude
449         = fgGetNode("/position/altitude-ft");
450     double alt;
451     if ( altitude->getDoubleValue() > -9990 ) {
452         alt = altitude->getDoubleValue();
453     } else {
454         alt = 0.0;
455     }
456
457     double sea_level_radius_meters, lat_geoc;
458
459     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
460     SG_LOG(SG_FLIGHT,SG_INFO," cur alt (ft) =  " << alt );
461
462     sgGeodToGeoc( lat, alt * SG_FEET_TO_METER,
463                   &sea_level_radius_meters, &lat_geoc );
464     
465     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
466     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET  );
467     fgic->SetLatitudeRadIC( lat_geoc );
468     needTrim=true;
469 }
470
471 void FGJSBsim::set_Longitude(double lon) {
472
473     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
474
475     fgic->SetLongitudeRadIC( lon );
476     needTrim=true;
477 }
478
479 void FGJSBsim::set_Altitude(double alt) {
480     static const SGPropertyNode *latitude
481         = fgGetNode("/position/latitude-deg");
482
483     double sea_level_radius_meters,lat_geoc;
484
485     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
486     SG_LOG(SG_FLIGHT,SG_INFO, "  lat (deg) = " << latitude->getDoubleValue() );
487
488     sgGeodToGeoc( latitude->getDoubleValue() * SGD_DEGREES_TO_RADIANS, alt,
489                   &sea_level_radius_meters, &lat_geoc);
490     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
491     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
492     fgic->SetLatitudeRadIC( lat_geoc );
493     fgic->SetAltitudeFtIC(alt);
494     needTrim=true;
495 }
496
497 void FGJSBsim::set_V_calibrated_kts(double vc) {
498     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " <<  vc );
499
500     fgic->SetVcalibratedKtsIC(vc);
501     needTrim=true;
502 }
503
504 void FGJSBsim::set_Mach_number(double mach) {
505     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " <<  mach );
506
507     fgic->SetMachIC(mach);
508     needTrim=true;
509 }
510
511 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
512     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
513        << north << ", " <<  east << ", " << down );
514
515     fgic->SetVnorthFpsIC(north);
516     fgic->SetVeastFpsIC(east);
517     fgic->SetVdownFpsIC(down);
518     needTrim=true;
519 }
520
521 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
522     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
523        << u << ", " <<  v << ", " <<  w );
524
525     fgic->SetUBodyFpsIC(u);
526     fgic->SetVBodyFpsIC(v);
527     fgic->SetWBodyFpsIC(w);
528     needTrim=true;
529 }
530
531 //Euler angles
532 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
533     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
534        << phi << ", " << theta << ", " << psi );
535
536     fgic->SetPitchAngleRadIC(theta);
537     fgic->SetRollAngleRadIC(phi);
538     fgic->SetTrueHeadingRadIC(psi);
539     needTrim=true;
540 }
541
542 //Flight Path
543 void FGJSBsim::set_Climb_Rate( double roc) {
544     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
545
546     fgic->SetClimbRateFpsIC(roc);
547     needTrim=true;
548 }
549
550 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
551     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
552
553     fgic->SetFlightPathAngleRadIC(gamma);
554     needTrim=true;
555 }
556
557 //Earth
558 void FGJSBsim::set_Sea_level_radius(double slr) {
559     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
560
561     fgic->SetSeaLevelRadiusFtIC(slr);
562     needTrim=true;
563 }
564
565 void FGJSBsim::set_Runway_altitude(double ralt) {
566     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
567
568     _set_Runway_altitude( ralt );
569     fgic->SetTerrainAltitudeFtIC( ralt );
570     needTrim=true;
571 }
572
573 void FGJSBsim::set_Static_pressure(double p) {
574     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
575
576     Atmosphere->SetExPressure(p);
577     if(Atmosphere->External() == true)
578     needTrim=true;
579 }
580
581 void FGJSBsim::set_Static_temperature(double T) {
582     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
583     
584     Atmosphere->SetExTemperature(T);
585     if(Atmosphere->External() == true)
586     needTrim=true;
587 }
588  
589
590 void FGJSBsim::set_Density(double rho) {
591     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
592     
593     Atmosphere->SetExDensity(rho);
594     if(Atmosphere->External() == true)
595     needTrim=true;
596 }
597   
598 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth, 
599                          double weast, 
600                          double wdown ) {
601     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: " 
602        << wnorth << ", " << weast << ", " << wdown );
603     
604     _set_Velocities_Local_Airmass( wnorth, weast, wdown );
605     Atmosphere->SetWindNED(wnorth, weast, wdown );
606     if(Atmosphere->External() == true)
607         needTrim=true;
608 }     
609
610 void FGJSBsim::init_gear(void ) {
611     
612     FGGearInterface *gear;
613     FGGroundReactions* gr=fdmex->GetGroundReactions();
614     int Ngear=GroundReactions->GetNumGearUnits();
615     for (int i=0;i<Ngear;i++) {
616       add_gear_unit( FGGearInterface() );
617       gear=get_gear_unit(i);
618       gear->SetX( gr->GetGearUnit(i)->GetBodyLocation()(1) );
619       gear->SetY( gr->GetGearUnit(i)->GetBodyLocation()(2) );
620       gear->SetZ( gr->GetGearUnit(i)->GetBodyLocation()(3) );
621       gear->SetWoW( gr->GetGearUnit(i)->GetWOW() );
622       if ( gr->GetGearUnit(i)->GetBrakeGroup() > 0 ) {
623         gear->SetBrake(true);
624       }
625       if ( gr->GetGearUp() ) {
626         gear->SetPosition( 0.0 );
627       }    
628     }  
629 }
630
631 void FGJSBsim::update_gear(void) {
632     
633     FGGearInterface* gear;
634     FGGroundReactions* gr=fdmex->GetGroundReactions();
635     int Ngear=GroundReactions->GetNumGearUnits();
636     for (int i=0;i<Ngear;i++) {
637       gear=get_gear_unit(i);
638       gear->SetWoW( gr->GetGearUnit(i)->GetWOW() );
639       if ( gr->GetGearUp() ) {
640         gear->SetPosition( 0.0 );
641       }    
642     }  
643 }
644
645 void FGJSBsim::do_trim(void) {
646
647         FGTrim *fgtrim;
648         if(fgic->GetVcalibratedKtsIC() < 10 ) {
649             fgic->SetVcalibratedKtsIC(0.0);
650             fgtrim=new FGTrim(fdmex,fgic,tGround);
651         } else {
652             fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
653         }
654         if( !fgtrim->DoTrim() ) {
655             fgtrim->Report();
656             fgtrim->TrimStats();
657         } else {
658             trimmed->setBoolValue(true);
659         }
660         State->ReportState();
661         delete fgtrim;
662         pitch_trim->setDoubleValue( FCS->GetPitchTrimCmd() );
663         throttle_trim->setDoubleValue( FCS->GetThrottleCmd(0) );
664         aileron_trim->setDoubleValue( FCS->GetDaCmd() );
665         rudder_trim->setDoubleValue( FCS->GetDrCmd() );
666
667         globals->get_controls()->set_elevator_trim(FCS->GetPitchTrimCmd());
668         globals->get_controls()->set_elevator(FCS->GetDeCmd());
669         globals->get_controls()->set_throttle(FGControls::ALL_ENGINES,
670                                               FCS->GetThrottleCmd(0));
671
672         globals->get_controls()->set_aileron(FCS->GetDaCmd());
673         globals->get_controls()->set_rudder( FCS->GetDrCmd());
674     
675         SG_LOG( SG_FLIGHT, SG_INFO, "  Trim complete" );
676 }