]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim.cxx
Timezone fixes for Cygwin.
[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
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     Propulsion  = fdmex->GetPropulsion();
72     Aircraft    = fdmex->GetAircraft();
73     Translation = fdmex->GetTranslation();
74     Rotation    = fdmex->GetRotation();
75     Position    = fdmex->GetPosition();
76     Auxiliary   = fdmex->GetAuxiliary();
77
78     fgic=new FGInitialCondition(fdmex);
79     needTrim=true;
80   
81     SGPath aircraft_path( globals->get_fg_root() );
82     aircraft_path.append( "Aircraft" );
83
84     SGPath engine_path( globals->get_fg_root() );
85     engine_path.append( "Engine" );
86     set_delta_t( dt );
87     State->Setdt( dt );
88
89     result = fdmex->LoadModel( aircraft_path.str(),
90                                engine_path.str(),
91                                fgGetString("/sim/aircraft") );
92     
93     int Neng = Propulsion->GetNumEngines();
94     SG_LOG(SG_FLIGHT,SG_INFO, "Neng: " << Neng );
95     
96     for(int i=0;i<Neng;i++) {
97         add_engine( FGEngInterface() );
98     }  
99     
100     fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
101     fgSetDouble("/fdm/trim/throttle",   FCS->GetThrottleCmd(0));
102     fgSetDouble("/fdm/trim/aileron",    FCS->GetDaCmd());
103     fgSetDouble("/fdm/trim/rudder",     FCS->GetDrCmd());
104
105     trimmed = fgGetValue("/fdm/trim/trimmed",true);
106     trimmed->setBoolValue(false);
107
108 }
109
110 /******************************************************************************/
111 FGJSBsim::~FGJSBsim(void) {
112     if(fdmex != NULL) {
113         delete fdmex;
114         delete fgic;
115     }  
116 }
117
118 /******************************************************************************/
119
120 // Initialize the JSBsim flight model, dt is the time increment for
121 // each subsequent iteration through the EOM
122
123 void FGJSBsim::init() {
124                 // Explicitly call the superclass's
125                 // init method first.
126     FGInterface::init();
127
128     bool result;
129
130     SG_LOG( SG_FLIGHT, SG_INFO, "Starting and initializing JSBsim" );
131
132     Atmosphere->UseInternal();
133
134     SG_LOG( SG_FLIGHT, SG_INFO, "  Initializing JSBSim with:" );
135
136     switch(fgic->GetSpeedSet()) {
137     case setned:
138         SG_LOG(SG_FLIGHT,SG_INFO, "  Vn,Ve,Vd= "
139                << Position->GetVn() << ", "
140                << Position->GetVe() << ", "
141                << Position->GetVd() << " ft/s");
142     break;
143     case setuvw:
144         SG_LOG(SG_FLIGHT,SG_INFO, "  U,V,W= "
145                << Translation->GetUVW(1) << ", "
146                << Translation->GetUVW(2) << ", "
147                << Translation->GetUVW(3) << " ft/s");
148     break;
149     case setmach:
150         SG_LOG(SG_FLIGHT,SG_INFO, "  Mach: "
151                << Translation->GetMach() );
152     break;
153     case setvc:
154     default:
155         SG_LOG(SG_FLIGHT,SG_INFO, "  Indicated Airspeed: "
156                << Auxiliary->GetVcalibratedKTS() << " knots" );
157     break;
158     }
159
160     SG_LOG( SG_FLIGHT, SG_INFO, "  Bank Angle: "
161             <<  Rotation->Getphi()*RADTODEG << " deg");
162     SG_LOG( SG_FLIGHT, SG_INFO, "  Pitch Angle: "
163             << Rotation->Gettht()*RADTODEG << " deg"  );
164     SG_LOG( SG_FLIGHT, SG_INFO, "  True Heading: "
165             << Rotation->Getpsi()*RADTODEG << " deg"  );
166     SG_LOG( SG_FLIGHT, SG_INFO, "  Latitude: "
167             <<  Position->GetLatitude() << " deg" );
168     SG_LOG( SG_FLIGHT, SG_INFO, "  Longitude: "
169             <<  Position->GetLongitude() << " deg"  );
170
171     SG_LOG( SG_FLIGHT, SG_INFO, "  loaded initial conditions" );
172
173     SG_LOG( SG_FLIGHT, SG_INFO, "  set dt" );
174
175     SG_LOG( SG_FLIGHT, SG_INFO, "Finished initializing JSBSim" );
176 }
177
178 /******************************************************************************/
179
180 // Run an iteration of the EOM (equations of motion)
181
182 bool FGJSBsim::update( int multiloop ) {
183
184     int i;
185
186     double save_alt = 0.0;
187
188     copy_to_JSBsim();
189
190     trimmed->setBoolValue(false);
191
192     if (needTrim && fgGetBool("/sim/startup/trim")) {
193
194       //fgic->SetSeaLevelRadiusFtIC( get_Sea_level_radius() );
195       //fgic->SetTerrainAltitudeFtIC( scenery.cur_elev * SG_METER_TO_FEET );
196
197     FGTrim *fgtrim;
198     if(fgic->GetVcalibratedKtsIC() < 10 ) {
199         fgic->SetVcalibratedKtsIC(0.0);
200         fgtrim=new FGTrim(fdmex,fgic,tGround);
201     } else {
202         fgtrim=new FGTrim(fdmex,fgic,tLongitudinal);
203     }
204     if(!fgtrim->DoTrim()) {
205         fgtrim->Report();
206         fgtrim->TrimStats();
207     } else {
208         trimmed->setBoolValue(true);
209     }
210     fgtrim->ReportState();
211     delete fgtrim;
212
213     needTrim=false;
214
215     fgSetDouble("/fdm/trim/pitch-trim", FCS->GetPitchTrimCmd());
216     fgSetDouble("/fdm/trim/throttle",   FCS->GetThrottleCmd(0));
217     fgSetDouble("/fdm/trim/aileron",    FCS->GetDaCmd());
218     fgSetDouble("/fdm/trim/rudder",     FCS->GetDrCmd());
219
220     controls.set_elevator_trim(FCS->GetPitchTrimCmd());
221     controls.set_elevator(FCS->GetDeCmd());
222     controls.set_throttle(FGControls::ALL_ENGINES,
223                           FCS->GetThrottleCmd(0));
224
225     controls.set_aileron(FCS->GetDaCmd());
226     controls.set_rudder( FCS->GetDrCmd());
227     
228     SG_LOG( SG_FLIGHT, SG_INFO, "  Trim complete" );
229     }  
230   
231     for( i=0; i<get_num_engines(); i++ ) {
232       get_engine(i)->set_RPM( Propulsion->GetThruster(i)->GetRPM() );
233       get_engine(i)->set_Throttle( controls.get_throttle(i) );
234     }
235
236     for ( i=0; i < multiloop; i++ ) {
237         fdmex->Run();
238     }
239
240     // printf("%d FG_Altitude = %.2f\n", i, FG_Altitude * 0.3048);
241     // printf("%d Altitude = %.2f\n", i, Altitude * 0.3048);
242
243     // translate JSBsim back to FG structure so that the
244     // autopilot (and the rest of the sim can use the updated values
245
246     copy_from_JSBsim();
247     return true;
248 }
249
250 /******************************************************************************/
251
252 // Convert from the FGInterface struct to the JSBsim generic_ struct
253
254 bool FGJSBsim::copy_to_JSBsim() {
255     // copy control positions into the JSBsim structure
256
257     FCS->SetDaCmd( controls.get_aileron());
258     FCS->SetDeCmd( controls.get_elevator());
259     FCS->SetPitchTrimCmd(controls.get_elevator_trim());
260     FCS->SetDrCmd( -controls.get_rudder());
261     FCS->SetDfCmd(  controls.get_flaps() );
262     FCS->SetDsbCmd( 0.0 ); //speedbrakes
263     FCS->SetDspCmd( 0.0 ); //spoilers
264     FCS->SetThrottleCmd( FGControls::ALL_ENGINES,
265                          controls.get_throttle( 0 ));
266     FCS->SetLBrake( controls.get_brake( 0 ) );
267     FCS->SetRBrake( controls.get_brake( 1 ) );
268     FCS->SetCBrake( controls.get_brake( 2 ) );
269
270     Position->SetSeaLevelRadius( get_Sea_level_radius() );
271     Position->SetRunwayRadius( scenery.cur_elev*SG_METER_TO_FEET
272                                + get_Sea_level_radius() );
273
274     Atmosphere->SetExTemperature(get_Static_temperature());
275     Atmosphere->SetExPressure(get_Static_pressure());
276     Atmosphere->SetExDensity(get_Density());
277     Atmosphere->SetWindNED(get_V_north_airmass(),
278                            get_V_east_airmass(),
279                            get_V_down_airmass());
280 //    SG_LOG(SG_FLIGHT,SG_INFO, "Wind NED: "
281 //                  << get_V_north_airmass() << ", "
282 //                  << get_V_east_airmass()  << ", "
283 //                  << get_V_down_airmass() );
284
285     return true;
286 }
287
288 /******************************************************************************/
289
290 // Convert from the JSBsim generic_ struct to the FGInterface struct
291
292 bool FGJSBsim::copy_from_JSBsim() {
293     unsigned int i, j;
294
295     _set_Inertias( Aircraft->GetMass(),
296                    Aircraft->GetIxx(),
297                    Aircraft->GetIyy(),
298                    Aircraft->GetIzz(),
299                    Aircraft->GetIxz() );
300
301     _set_CG_Position( Aircraft->GetXYZcg(1),
302                       Aircraft->GetXYZcg(2),
303                       Aircraft->GetXYZcg(3) );
304
305     _set_Accels_Body( Translation->GetUVWdot(1),
306                       Translation->GetUVWdot(2),
307                       Translation->GetUVWdot(3) );
308
309     _set_Accels_CG_Body( Translation->GetUVWdot(1),
310                          Translation->GetUVWdot(2),
311                          Translation->GetUVWdot(3) );
312
313     //_set_Accels_CG_Body_N ( Translation->GetNcg(1),
314     //                       Translation->GetNcg(2),
315     //                       Translation->GetNcg(3) );
316     //
317     _set_Accels_Pilot_Body( Auxiliary->GetPilotAccel(1),
318                             Auxiliary->GetPilotAccel(2),
319                             Auxiliary->GetPilotAccel(3) );
320
321     //_set_Accels_Pilot_Body_N( Auxiliary->GetNpilot(1),
322     //                         Auxiliary->GetNpilot(2),
323     //                         Auxiliary->GetNpilot(3) );
324
325     _set_Nlf( Aircraft->GetNlf() );
326
327     // Velocities
328
329     _set_Velocities_Local( Position->GetVn(),
330                            Position->GetVe(),
331                            Position->GetVd() );
332
333     _set_Velocities_Wind_Body( Translation->GetUVW(1),
334                                Translation->GetUVW(2),
335                                Translation->GetUVW(3) );
336
337     _set_V_rel_wind( Translation->GetVt() );
338
339     _set_V_equiv_kts( Auxiliary->GetVequivalentKTS() );
340
341     // _set_V_calibrated( Auxiliary->GetVcalibratedFPS() );
342
343     _set_V_calibrated_kts( Auxiliary->GetVcalibratedKTS() );
344
345     _set_V_ground_speed( Position->GetVground() );
346
347     _set_Omega_Body( Rotation->GetPQR(1),
348                      Rotation->GetPQR(2),
349                      Rotation->GetPQR(3) );
350
351     _set_Euler_Rates( Rotation->GetEulerRates(1),
352                       Rotation->GetEulerRates(2),
353                       Rotation->GetEulerRates(3) );
354
355     _set_Geocentric_Rates(Position->GetLatitudeDot(),
356                           Position->GetLongitudeDot(),
357                           Position->Gethdot() );
358
359     _set_Mach_number( Translation->GetMach() );
360
361     // Positions
362     _updatePosition( Position->GetLatitude(),
363                      Position->GetLongitude(),
364                      Position->Geth() );
365
366     _set_Euler_Angles( Rotation->Getphi(),
367                        Rotation->Gettht(),
368                        Rotation->Getpsi() );
369
370     _set_Alpha( Translation->Getalpha() );
371     _set_Beta( Translation->Getbeta() );
372
373
374     _set_Gamma_vert_rad( Position->GetGamma() );
375     // set_Gamma_horiz_rad( Gamma_horiz_rad );
376
377     _set_Earth_position_angle( Auxiliary->GetEarthPositionAngle() );
378
379     _set_Climb_Rate( Position->Gethdot() );
380
381
382     for ( i = 1; i <= 3; i++ ) {
383         for ( j = 1; j <= 3; j++ ) {
384             _set_T_Local_to_Body( i, j, State->GetTl2b(i,j) );
385         }
386     }
387     return true;
388 }
389
390 void FGJSBsim::snap_shot(void) {
391     fgic->SetLatitudeRadIC(get_Lat_geocentric() );
392     fgic->SetLongitudeRadIC( get_Longitude() );
393     fgic->SetAltitudeFtIC( get_Altitude() );
394     fgic->SetTerrainAltitudeFtIC( get_Runway_altitude() );
395     fgic->SetVtrueFpsIC( get_V_rel_wind() );
396     fgic->SetPitchAngleRadIC( get_Theta() );
397     fgic->SetRollAngleRadIC( get_Phi() );
398     fgic->SetTrueHeadingRadIC( get_Psi() );
399     fgic->SetClimbRateFpsIC( get_Climb_Rate() );
400 }
401
402
403 bool FGJSBsim::ToggleDataLogging(void) {
404     return fdmex->GetOutput()->Toggle();
405 }
406
407
408 bool FGJSBsim::ToggleDataLogging(bool state) {
409     if (state) {
410       fdmex->GetOutput()->Enable();
411       return true;
412     } else {
413       fdmex->GetOutput()->Disable();
414       return false;
415     }
416 }
417
418
419 //Positions
420 void FGJSBsim::set_Latitude(double lat) {
421     double sea_level_radius_meters,lat_geoc;
422
423     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Latitude: " << lat );
424
425     snap_shot();
426     sgGeodToGeoc( lat, get_Altitude() , &sea_level_radius_meters, &lat_geoc);
427     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
428     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET  );
429     fgic->SetLatitudeRadIC( lat_geoc );
430     fdmex->RunIC(fgic); //loop JSBSim once
431     copy_from_JSBsim(); //update the bus
432     needTrim=true;
433 }
434
435 void FGJSBsim::set_Longitude(double lon) {
436
437     SG_LOG(SG_FLIGHT,SG_INFO,"FGJSBsim::set_Longitude: " << lon );
438
439     snap_shot();
440     fgic->SetLongitudeRadIC(lon);
441     fdmex->RunIC(fgic); //loop JSBSim once
442     copy_from_JSBsim(); //update the bus
443     needTrim=true;
444 }
445
446 void FGJSBsim::set_Altitude(double alt) {
447     double sea_level_radius_meters,lat_geoc;
448
449     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Altitude: " << alt );
450
451     snap_shot();
452     sgGeodToGeoc( get_Latitude(), alt , &sea_level_radius_meters, &lat_geoc);
453     _set_Sea_level_radius( sea_level_radius_meters * SG_METER_TO_FEET  );
454     fgic->SetSeaLevelRadiusFtIC( sea_level_radius_meters * SG_METER_TO_FEET );
455     fgic->SetLatitudeRadIC( lat_geoc );
456     fgic->SetAltitudeFtIC(alt);
457     fdmex->RunIC(fgic); //loop JSBSim once
458     copy_from_JSBsim(); //update the bus
459     needTrim=true;
460 }
461
462 void FGJSBsim::set_V_calibrated_kts(double vc) {
463     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_V_calibrated_kts: " <<  vc );
464
465     snap_shot();
466     fgic->SetVcalibratedKtsIC(vc);
467     fdmex->RunIC(fgic); //loop JSBSim once
468     copy_from_JSBsim(); //update the bus
469     needTrim=true;
470 }
471
472 void FGJSBsim::set_Mach_number(double mach) {
473     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Mach_number: " <<  mach );
474
475     snap_shot();
476     fgic->SetMachIC(mach);
477     fdmex->RunIC(fgic); //loop JSBSim once
478     copy_from_JSBsim(); //update the bus
479     needTrim=true;
480 }
481
482 void FGJSBsim::set_Velocities_Local( double north, double east, double down ){
483     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local: "
484        << north << ", " <<  east << ", " << down );
485
486     snap_shot();
487     fgic->SetVnorthFpsIC(north);
488     fgic->SetVeastFpsIC(east);
489     fgic->SetVdownFpsIC(down);
490     fdmex->RunIC(fgic); //loop JSBSim once
491     copy_from_JSBsim(); //update the bus
492     needTrim=true;
493 }
494
495 void FGJSBsim::set_Velocities_Wind_Body( double u, double v, double w){
496     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Wind_Body: "
497        << u << ", " <<  v << ", " <<  w );
498
499     snap_shot();
500     fgic->SetUBodyFpsIC(u);
501     fgic->SetVBodyFpsIC(v);
502     fgic->SetWBodyFpsIC(w);
503     fdmex->RunIC(fgic); //loop JSBSim once
504     copy_from_JSBsim(); //update the bus
505     needTrim=true;
506 }
507
508 //Euler angles
509 void FGJSBsim::set_Euler_Angles( double phi, double theta, double psi ) {
510     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Euler_Angles: "
511        << phi << ", " << theta << ", " << psi );
512
513     snap_shot();
514     fgic->SetPitchAngleRadIC(theta);
515     fgic->SetRollAngleRadIC(phi);
516     fgic->SetTrueHeadingRadIC(psi);
517     fdmex->RunIC(fgic); //loop JSBSim once
518     copy_from_JSBsim(); //update the bus
519     needTrim=true;
520 }
521
522 //Flight Path
523 void FGJSBsim::set_Climb_Rate( double roc) {
524     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Climb_Rate: " << roc );
525
526     snap_shot();
527     fgic->SetClimbRateFpsIC(roc);
528     fdmex->RunIC(fgic); //loop JSBSim once
529     copy_from_JSBsim(); //update the bus
530     needTrim=true;
531 }
532
533 void FGJSBsim::set_Gamma_vert_rad( double gamma) {
534     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Gamma_vert_rad: " << gamma );
535
536     snap_shot();
537     fgic->SetFlightPathAngleRadIC(gamma);
538     fdmex->RunIC(fgic); //loop JSBSim once
539     copy_from_JSBsim(); //update the bus
540     needTrim=true;
541 }
542
543 //Earth
544 void FGJSBsim::set_Sea_level_radius(double slr) {
545     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Sea_level_radius: " << slr );
546
547     snap_shot();
548     fgic->SetSeaLevelRadiusFtIC(slr);
549     fdmex->RunIC(fgic); //loop JSBSim once
550     copy_from_JSBsim(); //update the bus
551     needTrim=true;
552 }
553
554 void FGJSBsim::set_Runway_altitude(double ralt) {
555     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Runway_altitude: " << ralt );
556
557     snap_shot();
558     _set_Runway_altitude( ralt );
559     fgic->SetTerrainAltitudeFtIC( ralt );
560     fdmex->RunIC(fgic); //loop JSBSim once
561     copy_from_JSBsim(); //update the bus
562     needTrim=true;
563 }
564
565 void FGJSBsim::set_Static_pressure(double p) {
566     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_pressure: " << p );
567
568     snap_shot();
569     Atmosphere->SetExPressure(p);
570     if(Atmosphere->External() == true)
571     needTrim=true;
572 }
573
574 void FGJSBsim::set_Static_temperature(double T) {
575     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Static_temperature: " << T );
576     
577     snap_shot();
578     Atmosphere->SetExTemperature(T);
579     if(Atmosphere->External() == true)
580     needTrim=true;
581 }
582  
583
584 void FGJSBsim::set_Density(double rho) {
585     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Density: " << rho );
586     
587     snap_shot();
588     Atmosphere->SetExDensity(rho);
589     if(Atmosphere->External() == true)
590     needTrim=true;
591 }
592   
593
594 void FGJSBsim::set_Velocities_Local_Airmass (double wnorth, 
595                          double weast, 
596                          double wdown ) {
597     SG_LOG(SG_FLIGHT,SG_INFO, "FGJSBsim::set_Velocities_Local_Airmass: " 
598        << wnorth << ", " << weast << ", " << wdown );
599     
600     _set_Velocities_Local_Airmass( wnorth, weast, wdown );
601     snap_shot();
602     Atmosphere->SetWindNED(wnorth, weast, wdown );
603     if(Atmosphere->External() == true)
604         needTrim=true;
605 }     
606