]> git.mxchange.org Git - flightgear.git/blob - src/Main/bfi.cxx
ca61b498c3c4fb806c790cf05930c8eba613741c
[flightgear.git] / src / Main / bfi.cxx
1 // bfi.cxx - Big Friendly Interface implementation
2 //
3 // Written by David Megginson, started February, 2000.
4 //
5 // Copyright (C) 2000  David Megginson - david@megginson.com
6 //
7 // THIS CLASS IS DEPRECATED; USE THE PROPERTY MANAGER INSTEAD.
8 //
9 // This program is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU General Public License as
11 // published by the Free Software Foundation; either version 2 of the
12 // License, or (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 //
23 // $Id$
24
25 #include "fgfs.hxx"
26
27 #include <simgear/constants.h>
28 #include <simgear/debug/logstream.hxx>
29 #include <simgear/ephemeris/ephemeris.hxx>
30 #include <simgear/math/sg_types.hxx>
31 #include <simgear/misc/props.hxx>
32 #include <simgear/timing/sg_time.hxx>
33
34 #include <Aircraft/aircraft.hxx>
35 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
36 #include <Controls/controls.hxx>
37 #include <Autopilot/newauto.hxx>
38 #include <Scenery/scenery.hxx>
39 #include <Time/light.hxx>
40 #include <Time/event.hxx>
41 #include <Time/sunpos.hxx>
42 #include <Time/tmp.hxx>
43 #include <Cockpit/radiostack.hxx>
44 #include <Cockpit/panel.hxx>
45 #ifndef FG_OLD_WEATHER
46 #  include <WeatherCM/FGLocalWeatherDatabase.h>
47 #else
48 #  include <Weather/weather.hxx>
49 #endif
50
51 #include "globals.hxx"
52 #include "fg_init.hxx"
53 #include "fg_props.hxx"
54
55 FG_USING_NAMESPACE(std);
56
57
58 #include "bfi.hxx"
59
60
61 \f
62 ////////////////////////////////////////////////////////////////////////
63 // Static variables.
64 ////////////////////////////////////////////////////////////////////////
65
66                                 // Yech -- not thread-safe, etc. etc.
67 static bool _needReinit = false;
68 static string _temp;
69
70 static inline void needReinit ()
71 {
72   _needReinit = true;
73 }
74
75
76 /**
77  * Reinitialize FGFS to use the new BFI settings.
78  */
79 static inline void
80 reinit ()
81 {
82                                 // Save the state of everything
83                                 // that's going to get clobbered
84                                 // when we reinit the subsystems.
85
86   FG_LOG(FG_GENERAL, FG_INFO, "Starting BFI reinit");
87
88                                 // TODO: add more AP stuff
89   bool apHeadingLock = FGBFI::getAPHeadingLock();
90   double apHeadingMag = FGBFI::getAPHeadingMag();
91   bool apAltitudeLock = FGBFI::getAPAltitudeLock();
92   double apAltitude = FGBFI::getAPAltitude();
93   const string &targetAirport = FGBFI::getTargetAirport();
94   bool gpsLock = FGBFI::getGPSLock();
95   // double gpsLatitude = FGBFI::getGPSTargetLatitude();
96   // double gpsLongitude = FGBFI::getGPSTargetLongitude();
97
98   FGBFI::setTargetAirport("");
99
100   fgReInitSubsystems();
101
102                                 // FIXME: this is wrong.
103                                 // All of these are scheduled events,
104                                 // and it should be possible to force
105                                 // them all to run once.
106   fgUpdateSunPos();
107   fgUpdateMoonPos();
108   cur_light_params.Update();
109   fgUpdateLocalTime();
110   fgUpdateWeatherDatabase();
111   current_radiostack->search();
112
113                                 // Restore all of the old states.
114   FGBFI::setAPHeadingLock(apHeadingLock);
115   FGBFI::setAPHeadingMag(apHeadingMag);
116   FGBFI::setAPAltitudeLock(apAltitudeLock);
117   FGBFI::setAPAltitude(apAltitude);
118   FGBFI::setTargetAirport(targetAirport);
119   FGBFI::setGPSLock(gpsLock);
120
121   _needReinit = false;
122
123   FG_LOG(FG_GENERAL, FG_INFO, "Ending BFI reinit");
124 }
125
126 // BEGIN: kludge 2000-12-07
127 // This is a kludge around a LaRCsim problem; see setAltitude()
128 // for details.
129 static int _altitude_countdown = 0;
130 static double _requested_altitude = -9999;
131 static bool _saved_freeze = false;
132 static inline void _check_altitude ()
133 {
134   if (_altitude_countdown > 0) {
135     _altitude_countdown--;
136     if (_altitude_countdown == 0) {
137       current_aircraft.fdm_state->set_Altitude(_requested_altitude);
138       globals->set_freeze(_saved_freeze);
139     }
140   }
141 }
142
143 static int _lighting_countdown = 0;
144 static inline void _check_lighting ()
145 {
146   if (_lighting_countdown > 0) {
147     _lighting_countdown--;
148     if (_lighting_countdown == 0)
149       fgUpdateSkyAndLightingParams();
150   }
151 }
152 // END: kludge
153
154
155 // BEGIN: kludge
156 // Allow the view to be set from two axes (i.e. a joystick hat)
157 // This needs to be in FGViewer itself, somehow.
158 static double axisLong = 0.0;
159 static double axisLat = 0.0;
160
161 static inline void
162 _set_view_from_axes ()
163 {
164                                 // Take no action when hat is centered
165   if (axisLong == 0 && axisLat == 0)
166     return;
167
168   double viewDir = 0;
169
170   if (axisLong < 0) {           // Longitudinal axis forward
171     if (axisLat < 0)
172       viewDir = 45;
173     else if (axisLat > 0)
174       viewDir = 315;
175     else
176       viewDir = 0;
177   } else if (axisLong > 0) {    // Longitudinal axis backward
178     if (axisLat < 0)
179       viewDir = 135;
180     else if (axisLat > 0)
181       viewDir = 225;
182     else
183       viewDir = 180;
184   } else {                      // Longitudinal axis neutral
185     if (axisLat < 0)
186       viewDir = 90;
187     else
188       viewDir = 270;
189   }
190
191   globals->get_current_view()->set_goal_view_offset(viewDir*DEG_TO_RAD);
192 //   globals->get_current_view()->set_view_offset(viewDir*DEG_TO_RAD);
193 }
194
195 // END: kludge
196
197
198 \f
199 ////////////////////////////////////////////////////////////////////////
200 // Local functions
201 ////////////////////////////////////////////////////////////////////////
202
203 /**
204  * Initialize the BFI by binding its functions to properties.
205  *
206  * TODO: perhaps these should migrate into the individual modules
207  * (i.e. they should register themselves).
208  */
209 void
210 FGBFI::init ()
211 {
212   FG_LOG(FG_GENERAL, FG_INFO, "Starting BFI init");
213                                 // Simulation
214   fgTie("/sim/flight-model", getFlightModel, setFlightModel);
215   fgTie("/sim/aircraft", getAircraft, setAircraft);
216   fgTie("/sim/aircraft-dir", getAircraftDir, setAircraftDir);
217   fgTie("/sim/time/gmt", getDateString, setDateString);
218   fgTie("/sim/time/gmt-string", getGMTString);
219   fgTie("/sim/hud/visibility", getHUDVisible, setHUDVisible);
220
221                                 // Position
222   fgTie("/position/airport-id", getTargetAirport, setTargetAirport);
223   fgTie("/position/latitude", getLatitude, setLatitude);
224   fgTie("/position/longitude", getLongitude, setLongitude);
225   fgTie("/position/altitude", getAltitude, setAltitude);
226   fgTie("/position/altitude-agl", getAGL);
227
228                                 // Orientation
229   fgTie("/orientation/heading", getHeading, setHeading);
230   fgTie("/orientation/heading-magnetic", getHeadingMag);
231   fgTie("/orientation/pitch", getPitch, setPitch);
232   fgTie("/orientation/roll", getRoll, setRoll);
233
234                                 // Engine
235   fgTie("/engines/engine0/rpm", getRPM);
236   fgTie("/engines/engine0/egt", getEGT);
237   fgTie("/engines/engine0/cht", getCHT);
238   fgTie("/engines/engine0/mp", getMP);
239
240                                 // Velocities
241   fgTie("/velocities/airspeed", getAirspeed, setAirspeed);
242   fgTie("/velocities/side-slip", getSideSlip);
243   fgTie("/velocities/vertical-speed", getVerticalSpeed);
244   fgTie("/velocities/speed-north", getSpeedNorth);
245   fgTie("/velocities/speed-east", getSpeedEast);
246   fgTie("/velocities/speed-down", getSpeedDown);
247
248                                 // Autopilot
249   fgTie("/autopilot/locks/altitude", getAPAltitudeLock, setAPAltitudeLock);
250   fgTie("/autopilot/settings/altitude", getAPAltitude, setAPAltitude);
251   fgTie("/autopilot/locks/heading", getAPHeadingLock, setAPHeadingLock);
252   fgTie("/autopilot/settings/heading", getAPHeading, setAPHeading);
253   fgTie("/autopilot/settings/heading-magnetic",
254              getAPHeadingMag, setAPHeadingMag);
255   fgTie("/autopilot/locks/nav1", getAPNAV1Lock, setAPNAV1Lock);
256
257                                 // Weather
258   fgTie("/environment/visibility", getVisibility, setVisibility);
259   fgTie("/environment/wind-north", getWindNorth, setWindNorth);
260   fgTie("/environment/wind-east", getWindEast, setWindEast);
261   fgTie("/environment/wind-down", getWindDown, setWindDown);
262
263                                 // View
264   fgTie("/sim/view/axes/long", (double(*)())0, setViewAxisLong);
265   fgTie("/sim/view/axes/lat", (double(*)())0, setViewAxisLat);
266
267   _altitude_countdown = 0;
268   _needReinit = false;
269
270   FG_LOG(FG_GENERAL, FG_INFO, "Ending BFI init");
271 }
272
273
274 /**
275  * Reinitialize FGFS if required.
276  *
277  * Some changes (especially those in aircraft position) require that
278  * FGFS be reinitialized afterwards.  Rather than reinitialize after
279  * every change, the setter methods simply set a flag so that there
280  * can be a single reinit at the end of the frame.
281  */
282 void
283 FGBFI::update ()
284 {
285   _check_altitude();
286   _check_lighting();
287   _set_view_from_axes();
288   if (_needReinit) {
289     reinit();
290   }
291 }
292
293
294 \f
295 ////////////////////////////////////////////////////////////////////////
296 // Simulation.
297 ////////////////////////////////////////////////////////////////////////
298
299
300 /**
301  * Return the flight model as an integer.
302  *
303  * TODO: use a string instead.
304  */
305 int
306 FGBFI::getFlightModel ()
307 {
308   return globals->get_options()->get_flight_model();
309 }
310
311
312 /**
313  * Return the current aircraft as a string.
314  */
315 string
316 FGBFI::getAircraft ()
317 {
318   _temp = globals->get_options()->get_aircraft();
319   return _temp;
320 }
321
322
323 /**
324  * Return the current aircraft directory (UIUC) as a string.
325  */
326 string 
327 FGBFI::getAircraftDir ()
328 {
329   _temp = aircraft_dir;
330   return _temp;
331 }
332
333
334 /**
335  * Set the flight model as an integer.
336  *
337  * TODO: use a string instead.
338  */
339 void
340 FGBFI::setFlightModel (int model)
341 {
342   if (getFlightModel() != model) {
343     globals->get_options()->set_flight_model(model);
344     needReinit();
345   }
346 }
347
348
349 /**
350  * Set the current aircraft.
351  */
352 void
353 FGBFI::setAircraft (string aircraft)
354 {
355   if (getAircraft() != aircraft) {
356     globals->get_options()->set_aircraft(aircraft);
357     needReinit();
358   }
359 }
360
361
362 /**
363  * Set the current aircraft directory (UIUC).
364  */
365 void
366 FGBFI::setAircraftDir (string dir)
367 {
368   if (getAircraftDir() != dir) {
369     aircraft_dir = dir;
370     needReinit();
371   }
372 }
373
374
375 /**
376  * Return the current Zulu time.
377  */
378 string 
379 FGBFI::getDateString ()
380 {
381   string out;
382   char buf[64];
383   struct tm * t = globals->get_time_params()->getGmt();
384   sprintf(buf, "%.4d-%.2d-%.2dT%.2d:%.2d:%.2d",
385           t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
386           t->tm_hour, t->tm_min, t->tm_sec);
387   out = buf;
388   return out;
389 }
390
391
392 /**
393  * Set the current Zulu time.
394  */
395 void
396 FGBFI::setDateString (string date_string)
397 // FGBFI::setTimeGMT (time_t time)
398 {
399   SGTime * st = globals->get_time_params();
400   struct tm * current_time = st->getGmt();
401   struct tm new_time;
402
403                                 // Scan for basic ISO format
404                                 // YYYY-MM-DDTHH:MM:SS
405   int ret = sscanf(date_string.c_str(), "%d-%d-%dT%d:%d:%d",
406                    &(new_time.tm_year), &(new_time.tm_mon),
407                    &(new_time.tm_mday), &(new_time.tm_hour),
408                    &(new_time.tm_min), &(new_time.tm_sec));
409
410                                 // Be pretty picky about this, so
411                                 // that strange things don't happen
412                                 // if the save file has been edited
413                                 // by hand.
414   if (ret != 6) {
415     FG_LOG(FG_INPUT, FG_ALERT, "Date/time string " << date_string
416            << " not in YYYY-MM-DDTHH:MM:SS format; skipped");
417     return;
418   }
419
420                                 // OK, it looks like we got six
421                                 // values, one way or another.
422   new_time.tm_year -= 1900;
423   new_time.tm_mon -= 1;
424
425                                 // Now, tell flight gear to use
426                                 // the new time.  This was far
427                                 // too difficult, by the way.
428   long int warp =
429     mktime(&new_time) - mktime(current_time) + globals->get_warp();
430   double lon = current_aircraft.fdm_state->get_Longitude();
431   double lat = current_aircraft.fdm_state->get_Latitude();
432   double alt = current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER;
433   globals->set_warp(warp);
434   st->update(lon, lat, warp);
435   fgUpdateSkyAndLightingParams();
436 }
437
438
439 /**
440  * Return the GMT as a string.
441  */
442 string 
443 FGBFI::getGMTString ()
444 {
445   string out;
446   char buf[16];
447   struct tm * t = globals->get_time_params()->getGmt();
448   sprintf(buf, " %.2d:%.2d:%.2d",
449           t->tm_hour, t->tm_min, t->tm_sec);
450   out = buf;
451   return out;
452 }
453
454
455 /**
456  * Return true if the HUD is visible.
457  */
458 bool
459 FGBFI::getHUDVisible ()
460 {
461   return globals->get_options()->get_hud_status();
462 }
463
464
465 /**
466  * Ensure that the HUD is visible or hidden.
467  */
468 void
469 FGBFI::setHUDVisible (bool visible)
470 {
471   globals->get_options()->set_hud_status(visible);
472 }
473
474
475 \f
476 ////////////////////////////////////////////////////////////////////////
477 // Position
478 ////////////////////////////////////////////////////////////////////////
479
480
481 /**
482  * Return the current latitude in degrees (negative for south).
483  */
484 double
485 FGBFI::getLatitude ()
486 {
487   return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
488 }
489
490
491 /**
492  * Set the current latitude in degrees (negative for south).
493  */
494 void
495 FGBFI::setLatitude (double latitude)
496 {
497   current_aircraft.fdm_state->set_Latitude(latitude * DEG_TO_RAD);
498   fgUpdateSkyAndLightingParams();
499   if (_lighting_countdown <= 0)
500     _lighting_countdown = 5;
501 }
502
503
504 /**
505  * Return the current longitude in degrees (negative for west).
506  */
507 double
508 FGBFI::getLongitude ()
509 {
510   return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
511 }
512
513
514 /**
515  * Set the current longitude in degrees (negative for west).
516  */
517 void
518 FGBFI::setLongitude (double longitude)
519 {
520   current_aircraft.fdm_state->set_Longitude(longitude * DEG_TO_RAD);
521   fgUpdateSkyAndLightingParams();
522   if (_lighting_countdown <= 0)
523     _lighting_countdown = 5;
524 }
525
526
527 /**
528  * Return the current altitude in feet.
529  */
530 double
531 FGBFI::getAltitude ()
532 {
533   return current_aircraft.fdm_state->get_Altitude();
534 }
535
536
537
538 /**
539  * Return the current altitude in above the terrain.
540  */
541 double
542 FGBFI::getAGL ()
543 {
544   return current_aircraft.fdm_state->get_Altitude()
545          - (scenery.cur_elev * METER_TO_FEET);
546 }
547
548
549 /**
550  * Set the current altitude in feet.
551  */
552 void
553 FGBFI::setAltitude (double altitude)
554 {
555   current_aircraft.fdm_state->set_Altitude(altitude);
556
557                                 // 2000-12-07
558                                 // This is an ugly kludge around a
559                                 // LaRCsim problem; if the
560                                 // requested altitude cannot be
561                                 // set right away (because it's
562                                 // below the last-calculated ground
563                                 // level), pause FGFS, wait for
564                                 // five frames, and then try again.
565   if (_altitude_countdown <= 0 &&
566       fabs(getAltitude() - altitude) > 5.0) {
567     _altitude_countdown = 5;
568     _requested_altitude = altitude;
569     _saved_freeze = globals->get_freeze();
570     globals->set_freeze(true);
571   }
572 }
573
574
575 \f
576 ////////////////////////////////////////////////////////////////////////
577 // Attitude
578 ////////////////////////////////////////////////////////////////////////
579
580
581 /**
582  * Return the current heading in degrees.
583  */
584 double
585 FGBFI::getHeading ()
586 {
587   return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
588 }
589
590
591 /**
592  * Return the current heading in degrees.
593  */
594 double
595 FGBFI::getHeadingMag ()
596 {
597   return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG - getMagVar();
598 }
599
600
601 /**
602  * Set the current heading in degrees.
603  */
604 void
605 FGBFI::setHeading (double heading)
606 {
607   FGInterface * fdm = current_aircraft.fdm_state;
608   fdm->set_Euler_Angles(fdm->get_Phi(), fdm->get_Theta(),
609                         heading * DEG_TO_RAD);
610 }
611
612
613 /**
614  * Return the current pitch in degrees.
615  */
616 double
617 FGBFI::getPitch ()
618 {
619   return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
620 }
621
622
623 /**
624  * Set the current pitch in degrees.
625  */
626 void
627 FGBFI::setPitch (double pitch)
628 {
629   FGInterface * fdm = current_aircraft.fdm_state;
630   fdm->set_Euler_Angles(fdm->get_Phi(), pitch * DEG_TO_RAD, fdm->get_Psi());
631 }
632
633
634 /**
635  * Return the current roll in degrees.
636  */
637 double
638 FGBFI::getRoll ()
639 {
640   return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
641 }
642
643
644 /**
645  * Set the current roll in degrees.
646  */
647 void
648 FGBFI::setRoll (double roll)
649 {
650   FGInterface * fdm = current_aircraft.fdm_state;
651   fdm->set_Euler_Angles(roll * DEG_TO_RAD, fdm->get_Theta(), fdm->get_Psi());
652 }
653
654
655 /**
656  * Return the current engine0 rpm
657  */
658 double
659 FGBFI::getRPM ()
660 {
661   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
662       return current_aircraft.fdm_state->get_engine(0)->get_RPM();
663   } else {
664       return 0.0;
665   }
666 }
667
668
669 /**
670  * Set the current engine0 rpm
671  */
672 void
673 FGBFI::setRPM (double rpm)
674 {
675     if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
676         if (getRPM() != rpm) {
677             current_aircraft.fdm_state->get_engine(0)->set_RPM( rpm );
678         }
679     }
680 }
681
682
683 /**
684  * Return the current engine0 EGT.
685  */
686 double
687 FGBFI::getEGT ()
688 {
689   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
690       return current_aircraft.fdm_state->get_engine(0)->get_EGT();
691   } else {
692       return 0.0;
693   }
694 }
695
696
697 /**
698  * Return the current engine0 CHT.
699  */
700 double
701 FGBFI::getCHT ()
702 {
703   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
704       return current_aircraft.fdm_state->get_engine(0)->get_CHT();
705   } else {
706       return 0.0;
707   }
708 }
709
710
711 /**
712  * Return the current engine0 CHT.
713  */
714 double
715 FGBFI::getMP ()
716 {
717   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
718       return current_aircraft.fdm_state->get_engine(0)->get_Manifold_Pressure();
719   } else {
720       return 0.0;
721   }
722 }
723
724
725 \f
726 ////////////////////////////////////////////////////////////////////////
727 // Velocities
728 ////////////////////////////////////////////////////////////////////////
729
730
731 /**
732  * Return the current airspeed in knots.
733  */
734 double
735 FGBFI::getAirspeed ()
736 {
737                                 // FIXME: should we add speed-up?
738   return current_aircraft.fdm_state->get_V_calibrated_kts();
739 }
740
741
742 /**
743  * Set the calibrated airspeed in knots.
744  */
745 void
746 FGBFI::setAirspeed (double speed)
747 {
748   current_aircraft.fdm_state->set_V_calibrated_kts(speed);
749 }
750
751
752 /**
753  * Return the current sideslip (FIXME: units unknown).
754  */
755 double
756 FGBFI::getSideSlip ()
757 {
758   return current_aircraft.fdm_state->get_Beta();
759 }
760
761
762 /**
763  * Return the current climb rate in feet/minute
764  */
765 double
766 FGBFI::getVerticalSpeed ()
767 {
768                                 // What about meters?
769   return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
770 }
771
772
773 /**
774  * Get the current north velocity (units??).
775  */
776 double
777 FGBFI::getSpeedNorth ()
778 {
779   return current_aircraft.fdm_state->get_V_north();
780 }
781
782
783 // /**
784 //  * Set the current north velocity (units??).
785 //  */
786 // void
787 // FGBFI::setSpeedNorth (double speed)
788 // {
789 //   FGInterface * fdm = current_aircraft.fdm_state;
790 // //   fdm->set_Velocities_Local(speed, fdm->get_V_east(), fdm->get_V_down());
791 // }
792
793
794 /**
795  * Get the current east velocity (units??).
796  */
797 double
798 FGBFI::getSpeedEast ()
799 {
800   return current_aircraft.fdm_state->get_V_east();
801 }
802
803
804 // /**
805 //  * Set the current east velocity (units??).
806 //  */
807 // void
808 // FGBFI::setSpeedEast (double speed)
809 // {
810 //   FGInterface * fdm = current_aircraft.fdm_state;
811 // //   fdm->set_Velocities_Local(fdm->get_V_north(), speed, fdm->get_V_down());
812 // }
813
814
815 /**
816  * Get the current down velocity (units??).
817  */
818 double
819 FGBFI::getSpeedDown ()
820 {
821   return current_aircraft.fdm_state->get_V_down();
822 }
823
824
825 // /**
826 //  * Set the current down velocity (units??).
827 //  */
828 // void
829 // FGBFI::setSpeedDown (double speed)
830 // {
831 //   FGInterface * fdm = current_aircraft.fdm_state;
832 // //   fdm->set_Velocities_Local(fdm->get_V_north(), fdm->get_V_east(), speed);
833 // }
834
835
836 \f
837 ////////////////////////////////////////////////////////////////////////
838 // Controls
839 ////////////////////////////////////////////////////////////////////////
840
841 #if 0
842
843 /**
844  * Get the throttle setting, from 0.0 (none) to 1.0 (full).
845  */
846 double
847 FGBFI::getThrottle ()
848 {
849                                 // FIXME: add engine selector
850   return controls.get_throttle(0);
851 }
852
853
854 /**
855  * Set the throttle, from 0.0 (none) to 1.0 (full).
856  */
857 void
858 FGBFI::setThrottle (double throttle)
859 {
860                                 // FIXME: allow engine selection
861   controls.set_throttle(0, throttle);
862 }
863
864
865 /**
866  * Get the fuel mixture setting, from 0.0 (none) to 1.0 (full).
867  */
868 double
869 FGBFI::getMixture ()
870 {
871                                 // FIXME: add engine selector
872   return controls.get_mixture(0);
873 }
874
875
876 /**
877  * Set the fuel mixture, from 0.0 (none) to 1.0 (full).
878  */
879 void
880 FGBFI::setMixture (double mixture)
881 {
882                                 // FIXME: allow engine selection
883   controls.set_mixture(0, mixture);
884 }
885
886
887 /**
888  * Get the propellor pitch setting, from 0.0 (none) to 1.0 (full).
889  */
890 double
891 FGBFI::getPropAdvance ()
892 {
893                                 // FIXME: add engine selector
894   return controls.get_prop_advance(0);
895 }
896
897
898 /**
899  * Set the propellor pitch, from 0.0 (none) to 1.0 (full).
900  */
901 void
902 FGBFI::setPropAdvance (double pitch)
903 {
904                                 // FIXME: allow engine selection
905   controls.set_prop_advance(0, pitch);
906 }
907
908
909 /**
910  * Get the flaps setting, from 0.0 (none) to 1.0 (full).
911  */
912 double
913 FGBFI::getFlaps ()
914 {
915   return controls.get_flaps();
916 }
917
918
919 /**
920  * Set the flaps, from 0.0 (none) to 1.0 (full).
921  */
922 void
923 FGBFI::setFlaps (double flaps)
924 {
925                                 // FIXME: clamp?
926   controls.set_flaps(flaps);
927 }
928
929
930 /**
931  * Get the aileron, from -1.0 (left) to 1.0 (right).
932  */
933 double
934 FGBFI::getAileron ()
935 {
936   return controls.get_aileron();
937 }
938
939
940 /**
941  * Set the aileron, from -1.0 (left) to 1.0 (right).
942  */
943 void
944 FGBFI::setAileron (double aileron)
945 {
946                                 // FIXME: clamp?
947   controls.set_aileron(aileron);
948 }
949
950
951 /**
952  * Get the rudder setting, from -1.0 (left) to 1.0 (right).
953  */
954 double
955 FGBFI::getRudder ()
956 {
957   return controls.get_rudder();
958 }
959
960
961 /**
962  * Set the rudder, from -1.0 (left) to 1.0 (right).
963  */
964 void
965 FGBFI::setRudder (double rudder)
966 {
967                                 // FIXME: clamp?
968   controls.set_rudder(rudder);
969 }
970
971
972 /**
973  * Get the elevator setting, from -1.0 (down) to 1.0 (up).
974  */
975 double
976 FGBFI::getElevator ()
977 {
978   return controls.get_elevator();
979 }
980
981
982 /**
983  * Set the elevator, from -1.0 (down) to 1.0 (up).
984  */
985 void
986 FGBFI::setElevator (double elevator)
987 {
988                                 // FIXME: clamp?
989   controls.set_elevator(elevator);
990 }
991
992
993 /**
994  * Get the elevator trim, from -1.0 (down) to 1.0 (up).
995  */
996 double
997 FGBFI::getElevatorTrim ()
998 {
999   return controls.get_elevator_trim();
1000 }
1001
1002
1003 /**
1004  * Set the elevator trim, from -1.0 (down) to 1.0 (up).
1005  */
1006 void
1007 FGBFI::setElevatorTrim (double trim)
1008 {
1009                                 // FIXME: clamp?
1010   controls.set_elevator_trim(trim);
1011 }
1012
1013
1014 /**
1015  * Get the highest brake setting, from 0.0 (none) to 1.0 (full).
1016  */
1017 double
1018 FGBFI::getBrakes ()
1019 {
1020   double b1 = getCenterBrake();
1021   double b2 = getLeftBrake();
1022   double b3 = getRightBrake();
1023   return (b1 > b2 ? (b1 > b3 ? b1 : b3) : (b2 > b3 ? b2 : b3));
1024 }
1025
1026
1027 /**
1028  * Set all brakes, from 0.0 (none) to 1.0 (full).
1029  */
1030 void
1031 FGBFI::setBrakes (double brake)
1032 {
1033   setCenterBrake(brake);
1034   setLeftBrake(brake);
1035   setRightBrake(brake);
1036 }
1037
1038
1039 /**
1040  * Get the center brake, from 0.0 (none) to 1.0 (full).
1041  */
1042 double
1043 FGBFI::getCenterBrake ()
1044 {
1045   return controls.get_brake(2);
1046 }
1047
1048
1049 /**
1050  * Set the center brake, from 0.0 (none) to 1.0 (full).
1051  */
1052 void
1053 FGBFI::setCenterBrake (double brake)
1054 {
1055   controls.set_brake(2, brake);
1056 }
1057
1058
1059 /**
1060  * Get the left brake, from 0.0 (none) to 1.0 (full).
1061  */
1062 double
1063 FGBFI::getLeftBrake ()
1064 {
1065   return controls.get_brake(0);
1066 }
1067
1068
1069 /**
1070  * Set the left brake, from 0.0 (none) to 1.0 (full).
1071  */
1072 void
1073 FGBFI::setLeftBrake (double brake)
1074 {
1075   controls.set_brake(0, brake);
1076 }
1077
1078
1079 /**
1080  * Get the right brake, from 0.0 (none) to 1.0 (full).
1081  */
1082 double
1083 FGBFI::getRightBrake ()
1084 {
1085   return controls.get_brake(1);
1086 }
1087
1088
1089 /**
1090  * Set the right brake, from 0.0 (none) to 1.0 (full).
1091  */
1092 void
1093 FGBFI::setRightBrake (double brake)
1094 {
1095   controls.set_brake(1, brake);
1096 }
1097
1098
1099 #endif
1100
1101 \f
1102 ////////////////////////////////////////////////////////////////////////
1103 // Autopilot
1104 ////////////////////////////////////////////////////////////////////////
1105
1106
1107 /**
1108  * Get the autopilot altitude lock (true=on).
1109  */
1110 bool
1111 FGBFI::getAPAltitudeLock ()
1112 {
1113     return current_autopilot->get_AltitudeEnabled();
1114 }
1115
1116
1117 /**
1118  * Set the autopilot altitude lock (true=on).
1119  */
1120 void
1121 FGBFI::setAPAltitudeLock (bool lock)
1122 {
1123   current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_LOCK);
1124   current_autopilot->set_AltitudeEnabled(lock);
1125 }
1126
1127
1128 /**
1129  * Get the autopilot target altitude in feet.
1130  */
1131 double
1132 FGBFI::getAPAltitude ()
1133 {
1134   return current_autopilot->get_TargetAltitude() * METER_TO_FEET;
1135 }
1136
1137
1138 /**
1139  * Set the autopilot target altitude in feet.
1140  */
1141 void
1142 FGBFI::setAPAltitude (double altitude)
1143 {
1144     current_autopilot->set_TargetAltitude( altitude );
1145 }
1146
1147
1148 /**
1149  * Get the autopilot heading lock (true=on).
1150  */
1151 bool
1152 FGBFI::getAPHeadingLock ()
1153 {
1154     return
1155       (current_autopilot->get_HeadingEnabled() &&
1156        current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_LOCK);
1157 }
1158
1159
1160 /**
1161  * Set the autopilot heading lock (true=on).
1162  */
1163 void
1164 FGBFI::setAPHeadingLock (bool lock)
1165 {
1166   if (lock) {
1167                                 // We need to do this so that
1168                                 // it's possible to lock onto a
1169                                 // heading other than the current
1170                                 // heading.
1171     double heading = getAPHeadingMag();
1172     current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_LOCK);
1173     current_autopilot->set_HeadingEnabled(true);
1174     setAPHeadingMag(heading);
1175   } else if (current_autopilot->get_HeadingMode() ==
1176              FGAutopilot::FG_HEADING_LOCK) {
1177     current_autopilot->set_HeadingEnabled(false);
1178   }
1179 }
1180
1181
1182 /**
1183  * Get the autopilot target heading in degrees.
1184  */
1185 double
1186 FGBFI::getAPHeading ()
1187 {
1188   return current_autopilot->get_TargetHeading();
1189 }
1190
1191
1192 /**
1193  * Set the autopilot target heading in degrees.
1194  */
1195 void
1196 FGBFI::setAPHeading (double heading)
1197 {
1198   current_autopilot->set_TargetHeading( heading );
1199 }
1200
1201
1202 /**
1203  * Get the autopilot target heading in degrees.
1204  */
1205 double
1206 FGBFI::getAPHeadingMag ()
1207 {
1208   return current_autopilot->get_TargetHeading() - getMagVar();
1209 }
1210
1211
1212 /**
1213  * Set the autopilot target heading in degrees.
1214  */
1215 void
1216 FGBFI::setAPHeadingMag (double heading)
1217 {
1218   current_autopilot->set_TargetHeading( heading + getMagVar() );
1219 }
1220
1221
1222 /**
1223  * Return true if the autopilot is locked to NAV1.
1224  */
1225 bool
1226 FGBFI::getAPNAV1Lock ()
1227 {
1228   return
1229     (current_autopilot->get_HeadingEnabled() &&
1230      current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
1231 }
1232
1233
1234 /**
1235  * Set the autopilot NAV1 lock.
1236  */
1237 void
1238 FGBFI::setAPNAV1Lock (bool lock)
1239 {
1240   if (lock) {
1241     current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
1242     current_autopilot->set_HeadingEnabled(true);
1243   } else if (current_autopilot->get_HeadingMode() ==
1244              FGAutopilot::FG_HEADING_NAV1) {
1245     current_autopilot->set_HeadingEnabled(false);
1246   }
1247 }
1248
1249
1250 \f
1251 ////////////////////////////////////////////////////////////////////////
1252 // GPS
1253 ////////////////////////////////////////////////////////////////////////
1254
1255
1256 /**
1257  * Get the autopilot GPS lock (true=on).
1258  */
1259 bool
1260 FGBFI::getGPSLock ()
1261 {
1262   return (current_autopilot->get_HeadingEnabled() &&
1263           (current_autopilot->get_HeadingMode() ==
1264            FGAutopilot::FG_HEADING_WAYPOINT ));
1265 }
1266
1267
1268 /**
1269  * Set the autopilot GPS lock (true=on).
1270  */
1271 void
1272 FGBFI::setGPSLock (bool lock)
1273 {
1274   if (lock) {
1275     current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_WAYPOINT);
1276     current_autopilot->set_HeadingEnabled(true);
1277   } else if (current_autopilot->get_HeadingMode() ==
1278              FGAutopilot::FG_HEADING_WAYPOINT) {
1279     current_autopilot->set_HeadingEnabled(false);
1280   }
1281 }
1282
1283
1284 /**
1285  * Get the GPS target airport code.
1286  */
1287 string 
1288 FGBFI::getTargetAirport ()
1289 {
1290   // FIXME: not thread-safe
1291   static string out;
1292   out = globals->get_options()->get_airport_id();
1293
1294   return out;
1295 }
1296
1297
1298 /**
1299  * Set the GPS target airport code.
1300  */
1301 void
1302 FGBFI::setTargetAirport (string airportId)
1303 {
1304   globals->get_options()->set_airport_id(airportId);
1305 }
1306
1307
1308 /**
1309  * Get the GPS target latitude in degrees (negative for south).
1310  */
1311 double
1312 FGBFI::getGPSTargetLatitude ()
1313 {
1314     return current_autopilot->get_TargetLatitude();
1315 }
1316
1317
1318 /**
1319  * Get the GPS target longitude in degrees (negative for west).
1320  */
1321 double
1322 FGBFI::getGPSTargetLongitude ()
1323 {
1324   return current_autopilot->get_TargetLongitude();
1325 }
1326
1327 #if 0
1328 /**
1329  * Set the GPS target longitude in degrees (negative for west).
1330  */
1331 void
1332 FGBFI::setGPSTargetLongitude (double longitude)
1333 {
1334   current_autopilot->set_TargetLongitude( longitude );
1335 }
1336 #endif
1337
1338
1339 \f
1340 ////////////////////////////////////////////////////////////////////////
1341 // Weather
1342 ////////////////////////////////////////////////////////////////////////
1343
1344
1345 /**
1346  * Get the current visibility (meters).
1347  */
1348 double
1349 FGBFI::getVisibility ()
1350 {
1351 #ifndef FG_OLD_WEATHER
1352   return WeatherDatabase->getWeatherVisibility();
1353 #else
1354   return current_weather.get_visibility();
1355 #endif
1356 }
1357
1358
1359 /**
1360  * Set the current visibility (meters).
1361  */
1362 void
1363 FGBFI::setVisibility (double visibility)
1364 {
1365 #ifndef FG_OLD_WEATHER
1366   WeatherDatabase->setWeatherVisibility(visibility);
1367 #else
1368   current_weather.set_visibility(visibility);
1369 #endif
1370 }
1371
1372
1373 /**
1374  * Get the current wind north velocity (feet/second).
1375  */
1376 double
1377 FGBFI::getWindNorth ()
1378 {
1379   return current_aircraft.fdm_state->get_V_north_airmass();
1380 }
1381
1382
1383 /**
1384  * Set the current wind north velocity (feet/second).
1385  */
1386 void
1387 FGBFI::setWindNorth (double speed)
1388 {
1389   current_aircraft.fdm_state->set_Velocities_Local_Airmass(speed,
1390                                                            getWindEast(),
1391                                                            getWindDown());
1392 }
1393
1394
1395 /**
1396  * Get the current wind east velocity (feet/second).
1397  */
1398 double
1399 FGBFI::getWindEast ()
1400 {
1401   return current_aircraft.fdm_state->get_V_east_airmass();
1402 }
1403
1404
1405 /**
1406  * Set the current wind east velocity (feet/second).
1407  */
1408 void
1409 FGBFI::setWindEast (double speed)
1410 {
1411   cout << "Set wind-east to " << speed << endl;
1412   current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
1413                                                            speed,
1414                                                            getWindDown());
1415 }
1416
1417
1418 /**
1419  * Get the current wind down velocity (feet/second).
1420  */
1421 double
1422 FGBFI::getWindDown ()
1423 {
1424   return current_aircraft.fdm_state->get_V_down_airmass();
1425 }
1426
1427
1428 /**
1429  * Set the current wind down velocity (feet/second).
1430  */
1431 void
1432 FGBFI::setWindDown (double speed)
1433 {
1434   current_aircraft.fdm_state->set_Velocities_Local_Airmass(getWindNorth(),
1435                                                            getWindEast(),
1436                                                            speed);
1437 }
1438
1439
1440 \f
1441 ////////////////////////////////////////////////////////////////////////
1442 // View.
1443 ////////////////////////////////////////////////////////////////////////
1444
1445 void
1446 FGBFI::setViewAxisLong (double axis)
1447 {
1448   axisLong = axis;
1449 }
1450
1451 void
1452 FGBFI::setViewAxisLat (double axis)
1453 {
1454   axisLat = axis;
1455 }
1456
1457 \f
1458 ////////////////////////////////////////////////////////////////////////
1459 // Time
1460 ////////////////////////////////////////////////////////////////////////
1461
1462 /**
1463  * Return the magnetic variation
1464  */
1465 double
1466 FGBFI::getMagVar ()
1467 {
1468   return globals->get_mag()->get_magvar() * RAD_TO_DEG;
1469 }
1470
1471
1472 /**
1473  * Return the magnetic variation
1474  */
1475 double
1476 FGBFI::getMagDip ()
1477 {
1478   return globals->get_mag()->get_magdip() * RAD_TO_DEG;
1479 }
1480
1481
1482 // end of bfi.cxx
1483