]> git.mxchange.org Git - flightgear.git/blob - src/Main/bfi.cxx
59a5e68e80a0a7042a6483051402dcf65d178d1b
[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 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 #ifdef HAVE_CONFIG_H
25 #  include <config.h>
26 #endif
27
28 #if defined( FG_HAVE_NATIVE_SGI_COMPILERS )
29 #  include <iostream.h>
30 #else
31 #  include <iostream>
32 #endif
33
34 #include <simgear/constants.h>
35 #include <simgear/debug/logstream.hxx>
36 #include <simgear/ephemeris/ephemeris.hxx>
37 #include <simgear/math/sg_types.hxx>
38 #include <simgear/misc/props.hxx>
39 #include <simgear/timing/sg_time.hxx>
40
41 #include <Aircraft/aircraft.hxx>
42 #include <FDM/UIUCModel/uiuc_aircraftdir.h>
43 #include <Controls/controls.hxx>
44 #include <Autopilot/newauto.hxx>
45 #include <Scenery/scenery.hxx>
46 #include <Time/light.hxx>
47 #include <Time/event.hxx>
48 #include <Time/sunpos.hxx>
49 #include <Time/tmp.hxx>
50 #include <Cockpit/radiostack.hxx>
51 #ifndef FG_OLD_WEATHER
52 #  include <WeatherCM/FGLocalWeatherDatabase.h>
53 #else
54 #  include <Weather/weather.hxx>
55 #endif
56
57 #include "globals.hxx"
58 #include "save.hxx"
59 #include "fg_init.hxx"
60 #include <simgear/misc/props.hxx>
61
62 FG_USING_NAMESPACE(std);
63
64
65 #include "bfi.hxx"
66
67
68 \f
69 ////////////////////////////////////////////////////////////////////////
70 // Static variables.
71 ////////////////////////////////////////////////////////////////////////
72
73 bool FGBFI::_needReinit = false;
74
75
76 \f
77 ////////////////////////////////////////////////////////////////////////
78 // Local functions
79 ////////////////////////////////////////////////////////////////////////
80
81
82 /**
83  * Initialize the BFI by binding its functions to properties.
84  *
85  * TODO: perhaps these should migrate into the individual modules
86  * (i.e. they should register themselves).
87  */
88 void
89 FGBFI::init ()
90 {
91   FG_LOG(FG_GENERAL, FG_INFO, "Starting BFI init");
92                                 // Simulation
93   current_properties.tieInt("/sim/flight-model",
94                             getFlightModel, setFlightModel);
95 //   current_properties.tieString("/sim/aircraft",
96 //                             getAircraft, setAircraft);
97   // TODO: timeGMT
98   current_properties.tieString("/sim/time/gmt-string",
99                                getGMTString, 0);
100   current_properties.tieBool("/sim/hud/visibility",
101                              getHUDVisible, setHUDVisible);
102   current_properties.tieBool("/sim/panel/visibility",
103                              getPanelVisible, setPanelVisible);
104
105                                 // Position
106   current_properties.tieString("/position/airport-id",
107                                 getTargetAirport, setTargetAirport);
108   current_properties.tieDouble("/position/latitude",
109                                 getLatitude, setLatitude);
110   current_properties.tieDouble("/position/longitude",
111                                getLongitude, setLongitude);
112   current_properties.tieDouble("/position/altitude",
113                                // getAltitude, setAltitude);
114                                getAltitude, setAltitude, false);
115   current_properties.tieDouble("/position/altitude-agl",
116                                getAGL, 0);
117
118                                 // Orientation
119   current_properties.tieDouble("/orientation/heading",
120                                getHeading, setHeading);
121   current_properties.tieDouble("/orientation/heading-magnetic",
122                                getHeadingMag, 0);
123   current_properties.tieDouble("/orientation/pitch",
124                                getPitch, setPitch);
125   current_properties.tieDouble("/orientation/roll",
126                                getRoll, setRoll);
127
128                                 // Engine
129   current_properties.tieDouble("/engines/engine0/rpm",
130                                getRPM, 0);
131   current_properties.tieDouble("/engines/engine0/egt",
132                                getEGT, 0);
133   current_properties.tieDouble("/engines/engine0/cht",
134                                getCHT, 0);
135
136                                 // Velocities
137   current_properties.tieDouble("/velocities/airspeed",
138                                getAirspeed, 0);
139   current_properties.tieDouble("/velocities/side-slip",
140                                getSideSlip, 0);
141   current_properties.tieDouble("/velocities/vertical-speed",
142                                getVerticalSpeed, 0);
143   current_properties.tieDouble("/velocities/speed-north",
144                                getSpeedNorth, setSpeedNorth);
145   current_properties.tieDouble("/velocities/speed-east",
146                                getSpeedEast, setSpeedEast);
147   current_properties.tieDouble("/velocities/speed-down",
148                                getSpeedDown, setSpeedDown);
149
150                                 // Controls
151   current_properties.tieDouble("/controls/throttle",
152                                getThrottle, setThrottle);
153   current_properties.tieDouble("/controls/mixture",
154                                getMixture, setMixture);
155   current_properties.tieDouble("/controls/propellor-pitch",
156                                getPropAdvance, setPropAdvance);
157   current_properties.tieDouble("/controls/flaps",
158                                getFlaps, setFlaps);
159   current_properties.tieDouble("/controls/aileron",
160                                getAileron, setAileron);
161   current_properties.tieDouble("/controls/rudder",
162                                getRudder, setRudder);
163   current_properties.tieDouble("/controls/elevator",
164                                getElevator, setElevator);
165   current_properties.tieDouble("/controls/elevator-trim",
166                                getElevatorTrim, setElevatorTrim);
167   current_properties.tieDouble("/controls/brakes/all",
168                                getBrakes, setBrakes);
169   current_properties.tieDouble("/controls/brakes/left",
170                                getLeftBrake, setLeftBrake);
171   current_properties.tieDouble("/controls/brakes/right",
172                                getRightBrake, setRightBrake);
173   current_properties.tieDouble("/controls/brakes/center",
174                                getRightBrake, setCenterBrake);
175
176                                 // Autopilot
177   current_properties.tieBool("/autopilot/locks/altitude",
178                              getAPAltitudeLock, setAPAltitudeLock);
179   current_properties.tieDouble("/autopilot/settings/altitude",
180                                getAPAltitude, setAPAltitude);
181   current_properties.tieBool("/autopilot/locks/heading",
182                              getAPHeadingLock, setAPHeadingLock);
183   current_properties.tieDouble("/autopilot/settings/heading",
184                                getAPHeading, setAPHeading);
185   current_properties.tieDouble("/autopilot/settings/heading-magnetic",
186                                getAPHeadingMag, setAPHeadingMag);
187   current_properties.tieBool("/autopilot/locks/nav1",
188                              getAPNAV1Lock, setAPNAV1Lock);
189
190                                 // Radio navigation
191   current_properties.tieDouble("/radios/nav1/frequencies/selected",
192                                getNAV1Freq, setNAV1Freq);
193   current_properties.tieDouble("/radios/nav1/frequencies/standby",
194                                getNAV1AltFreq, setNAV1AltFreq);
195   current_properties.tieDouble("/radios/nav1/radials/actual",
196                                getNAV1Radial, 0);
197   current_properties.tieDouble("/radios/nav1/radials/selected",
198                                getNAV1SelRadial, setNAV1SelRadial);
199   current_properties.tieDouble("/radios/nav1/dme/distance",
200                                getNAV1DistDME, 0);
201   current_properties.tieBool("/radios/nav1/to-flag",
202                              getNAV1TO, 0);
203   current_properties.tieBool("/radios/nav1/from-flag",
204                              getNAV1FROM, 0);
205   current_properties.tieBool("/radios/nav1/in-range",
206                              getNAV1InRange, 0);
207   current_properties.tieBool("/radios/nav1/dme/in-range",
208                              getNAV1DMEInRange, 0);
209                                
210   current_properties.tieDouble("/radios/nav2/frequencies/selected",
211                                getNAV2Freq, setNAV2Freq);
212   current_properties.tieDouble("/radios/nav2/frequencies/standby",
213                                getNAV2AltFreq, setNAV2AltFreq);
214   current_properties.tieDouble("/radios/nav2/radials/actual",
215                                getNAV2Radial, 0);
216   current_properties.tieDouble("/radios/nav2/radials/selected",
217                                getNAV2SelRadial, setNAV2SelRadial);
218   current_properties.tieDouble("/radios/nav2/dme/distance",
219                                getNAV2DistDME, 0);
220   current_properties.tieBool("/radios/nav2/to-flag",
221                              getNAV2TO, 0);
222   current_properties.tieBool("/radios/nav2/from-flag",
223                              getNAV2FROM, 0);
224   current_properties.tieBool("/radios/nav2/in-range",
225                              getNAV2InRange, 0);
226   current_properties.tieBool("/radios/nav2/dme/in-range",
227                              getNAV2DMEInRange, 0);
228
229   current_properties.tieDouble("/radios/adf/frequencies/selected",
230                                getADFFreq, setADFFreq);
231   current_properties.tieDouble("/radios/adf/frequencies/standby",
232                                getADFAltFreq, setADFAltFreq);
233   current_properties.tieDouble("/radios/adf/rotation",
234                                getADFRotation, setADFRotation);
235
236   current_properties.tieDouble("/environment/visibility",
237                                getVisibility, setVisibility);
238
239   _needReinit = false;
240
241   FG_LOG(FG_GENERAL, FG_INFO, "Ending BFI init");
242 }
243
244
245 /**
246  * Reinitialize FGFS if required.
247  *
248  * Some changes (especially those in aircraft position) require that
249  * FGFS be reinitialized afterwards.  Rather than reinitialize after
250  * every change, the setter methods simply set a flag so that there
251  * can be a single reinit at the end of the frame.
252  */
253 void
254 FGBFI::update ()
255 {
256   if (_needReinit) {
257     reinit();
258   }
259 }
260
261
262 /**
263  * Reinitialize FGFS to use the new BFI settings.
264  */
265 void
266 FGBFI::reinit ()
267 {
268                                 // Save the state of everything
269                                 // that's going to get clobbered
270                                 // when we reinit the subsystems.
271
272   cout << "BFI: start reinit\n";
273
274                                 // TODO: add more AP stuff
275   double elevator = getElevator();
276   double aileron = getAileron();
277   double rudder = getRudder();
278   double throttle = getThrottle();
279   double elevator_trim = getElevatorTrim();
280   double flaps = getFlaps();
281   double brake = getBrakes();
282   bool apHeadingLock = getAPHeadingLock();
283   double apHeadingMag = getAPHeadingMag();
284   bool apAltitudeLock = getAPAltitudeLock();
285   double apAltitude = getAPAltitude();
286   const string &targetAirport = getTargetAirport();
287   bool gpsLock = getGPSLock();
288   double gpsLatitude = getGPSTargetLatitude();
289   double gpsLongitude = getGPSTargetLongitude();
290
291   setTargetAirport("");
292   cout << "Target airport is " << globals->get_options()->get_airport_id() << endl;
293
294   fgReInitSubsystems();
295
296                                 // FIXME: this is wrong.
297                                 // All of these are scheduled events,
298                                 // and it should be possible to force
299                                 // them all to run once.
300   fgUpdateSunPos();
301   fgUpdateMoonPos();
302   cur_light_params.Update();
303   fgUpdateLocalTime();
304   fgUpdateWeatherDatabase();
305   fgRadioSearch();
306
307                                 // Restore all of the old states.
308   setElevator(elevator);
309   setAileron(aileron);
310   setRudder(rudder);
311   setThrottle(throttle);
312   setElevatorTrim(elevator_trim);
313   setFlaps(flaps);
314   setBrakes(brake);
315   setAPHeadingLock(apHeadingLock);
316   setAPHeadingMag(apHeadingMag);
317   setAPAltitudeLock(apAltitudeLock);
318   setAPAltitude(apAltitude);
319   setTargetAirport(targetAirport);
320   setGPSLock(gpsLock);
321
322   _needReinit = false;
323
324   cout << "BFI: end reinit\n";
325 }
326
327
328 \f
329 ////////////////////////////////////////////////////////////////////////
330 // Simulation.
331 ////////////////////////////////////////////////////////////////////////
332
333
334 /**
335  * Return the flight model as an integer.
336  *
337  * TODO: use a string instead.
338  */
339 int
340 FGBFI::getFlightModel ()
341 {
342   return globals->get_options()->get_flight_model();
343 }
344
345
346 /**
347  * Return the current aircraft as a string.
348  */
349 const string
350 FGBFI::getAircraft ()
351 {
352   return globals->get_options()->get_aircraft();
353 }
354
355
356 /**
357  * Return the current aircraft directory (UIUC) as a string.
358  */
359 const string
360 FGBFI::getAircraftDir ()
361 {
362   return aircraft_dir;
363 }
364
365
366 /**
367  * Set the flight model as an integer.
368  *
369  * TODO: use a string instead.
370  */
371 void
372 FGBFI::setFlightModel (int model)
373 {
374   if (getFlightModel() != model) {
375     globals->get_options()->set_flight_model(model);
376     needReinit();
377   }
378 }
379
380
381 /**
382  * Set the current aircraft.
383  */
384 void
385 FGBFI::setAircraft (const string &aircraft)
386 {
387   if (getAircraft() != aircraft) {
388     globals->get_options()->set_aircraft(aircraft);
389     needReinit();
390   }
391 }
392
393
394 /**
395  * Set the current aircraft directory (UIUC).
396  */
397 void
398 FGBFI::setAircraftDir (const string &dir)
399 {
400   if (getAircraftDir() != dir) {
401     aircraft_dir = dir;
402     needReinit();
403   }
404 }
405
406
407 /**
408  * Return the current Zulu time.
409  */
410 time_t
411 FGBFI::getTimeGMT ()
412 {
413   return globals->get_time_params()->get_cur_time();
414 }
415
416
417 /**
418  * Set the current Zulu time.
419  */
420 void
421 FGBFI::setTimeGMT (time_t time)
422 {
423   if (getTimeGMT() != time) {
424                                 // FIXME: need to update lighting
425                                 // and solar system
426     globals->get_options()->set_time_offset(time);
427     globals->get_options()->set_time_offset_type(FGOptions::FG_TIME_GMT_ABSOLUTE);
428     globals->get_time_params()->update( cur_fdm_state->get_Longitude(),
429                                         cur_fdm_state->get_Latitude(),
430                                         globals->get_warp() );
431     needReinit();
432   }
433 }
434
435
436 /**
437  * Return the GMT as a string.
438  */
439 const string &
440 FGBFI::getGMTString ()
441 {
442   static string out;            // FIXME: not thread-safe
443   char buf[16];
444   struct tm * t = globals->get_time_params()->getGmt();
445   sprintf(buf, " %.2d:%.2d:%.2d",
446           t->tm_hour, t->tm_min, t->tm_sec);
447   out = buf;
448   return out;
449 }
450
451
452 /**
453  * Return true if the HUD is visible.
454  */
455 bool
456 FGBFI::getHUDVisible ()
457 {
458   return globals->get_options()->get_hud_status();
459 }
460
461
462 /**
463  * Ensure that the HUD is visible or hidden.
464  */
465 void
466 FGBFI::setHUDVisible (bool visible)
467 {
468   globals->get_options()->set_hud_status(visible);
469 }
470
471
472 /**
473  * Return true if the 2D panel is visible.
474  */
475 bool
476 FGBFI::getPanelVisible ()
477 {
478   return globals->get_options()->get_panel_status();
479 }
480
481
482 /**
483  * Ensure that the 2D panel is visible or hidden.
484  */
485 void
486 FGBFI::setPanelVisible (bool visible)
487 {
488   if (globals->get_options()->get_panel_status() != visible) {
489     globals->get_options()->toggle_panel();
490   }
491 }
492
493
494 \f
495 ////////////////////////////////////////////////////////////////////////
496 // Position
497 ////////////////////////////////////////////////////////////////////////
498
499
500 /**
501  * Return the current latitude in degrees (negative for south).
502  */
503 double
504 FGBFI::getLatitude ()
505 {
506   return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
507 }
508
509
510 /**
511  * Set the current latitude in degrees (negative for south).
512  */
513 void
514 FGBFI::setLatitude (double latitude)
515 {
516   if (getLatitude() != latitude) {
517     globals->get_options()->set_lat(latitude);
518     current_aircraft.fdm_state->set_Latitude(latitude * DEG_TO_RAD);
519     needReinit();
520   }
521 }
522
523
524 /**
525  * Return the current longitude in degrees (negative for west).
526  */
527 double
528 FGBFI::getLongitude ()
529 {
530   return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
531 }
532
533
534 /**
535  * Set the current longitude in degrees (negative for west).
536  */
537 void
538 FGBFI::setLongitude (double longitude)
539 {
540   if (getLongitude() != longitude) {
541     globals->get_options()->set_lon(longitude);
542     current_aircraft.fdm_state->set_Longitude(longitude * DEG_TO_RAD);
543     needReinit();
544   }
545 }
546
547
548 /**
549  * Return the current altitude in feet.
550  */
551 double
552 FGBFI::getAltitude ()
553 {
554   return current_aircraft.fdm_state->get_Altitude();
555 }
556
557
558
559 /**
560  * Return the current altitude in above the terrain.
561  */
562 double
563 FGBFI::getAGL ()
564 {
565   return current_aircraft.fdm_state->get_Altitude()
566          - (scenery.cur_elev * METER_TO_FEET);
567 }
568
569
570 /**
571  * Set the current altitude in feet.
572  */
573 void
574 FGBFI::setAltitude (double altitude)
575 {
576   if (getAltitude() != altitude) {
577     fgFDMForceAltitude(getFlightModel(), altitude);
578     globals->get_options()->set_altitude(altitude);
579     current_aircraft.fdm_state->set_Altitude(altitude);
580   }
581 }
582
583
584 \f
585 ////////////////////////////////////////////////////////////////////////
586 // Attitude
587 ////////////////////////////////////////////////////////////////////////
588
589
590 /**
591  * Return the current heading in degrees.
592  */
593 double
594 FGBFI::getHeading ()
595 {
596   return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
597 }
598
599
600 /**
601  * Return the current heading in degrees.
602  */
603 double
604 FGBFI::getHeadingMag ()
605 {
606   return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG - getMagVar();
607 }
608
609
610 /**
611  * Set the current heading in degrees.
612  */
613 void
614 FGBFI::setHeading (double heading)
615 {
616   if (getHeading() != heading) {
617     globals->get_options()->set_heading(heading);
618     current_aircraft.fdm_state->set_Euler_Angles(getRoll() * DEG_TO_RAD,
619                                                  getPitch() * DEG_TO_RAD,
620                                                  heading * DEG_TO_RAD);
621     needReinit();
622   }
623 }
624
625
626 /**
627  * Return the current pitch in degrees.
628  */
629 double
630 FGBFI::getPitch ()
631 {
632   return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
633 }
634
635
636 /**
637  * Set the current pitch in degrees.
638  */
639 void
640 FGBFI::setPitch (double pitch)
641 {
642   if (getPitch() != pitch) {
643     globals->get_options()->set_pitch(pitch);
644     current_aircraft.fdm_state->set_Euler_Angles(getRoll() * DEG_TO_RAD,
645                                                  pitch * DEG_TO_RAD,
646                                                  getHeading() * DEG_TO_RAD);
647     needReinit();
648   }
649 }
650
651
652 /**
653  * Return the current roll in degrees.
654  */
655 double
656 FGBFI::getRoll ()
657 {
658   return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
659 }
660
661
662 /**
663  * Set the current roll in degrees.
664  */
665 void
666 FGBFI::setRoll (double roll)
667 {
668   if (getRoll() != roll) {
669     globals->get_options()->set_roll(roll);
670     current_aircraft.fdm_state->set_Euler_Angles(roll * DEG_TO_RAD,
671                                                  getPitch() * DEG_TO_RAD,
672                                                  getHeading() * DEG_TO_RAD);
673     needReinit();
674   }
675 }
676
677
678 /**
679  * Return the current engine0 rpm
680  */
681 double
682 FGBFI::getRPM ()
683 {
684   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
685       return current_aircraft.fdm_state->get_engine(0)->get_RPM();
686   } else {
687       return 0.0;
688   }
689 }
690
691
692 /**
693  * Set the current engine0 rpm
694  */
695 void
696 FGBFI::setRPM (double rpm)
697 {
698     if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
699         if (getRPM() != rpm) {
700             current_aircraft.fdm_state->get_engine(0)->set_RPM( rpm );
701         }
702     }
703 }
704
705
706 /**
707  * Return the current engine0 EGT.
708  */
709 double
710 FGBFI::getEGT ()
711 {
712   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
713       return current_aircraft.fdm_state->get_engine(0)->get_EGT();
714   }
715 }
716
717
718 /**
719  * Return the current engine0 CHT.
720  */
721 double
722 FGBFI::getCHT ()
723 {
724   if ( current_aircraft.fdm_state->get_engine(0) != NULL ) {
725       return current_aircraft.fdm_state->get_engine(0)->get_CHT();
726   }
727 }
728
729
730 \f
731 ////////////////////////////////////////////////////////////////////////
732 // Velocities
733 ////////////////////////////////////////////////////////////////////////
734
735
736 /**
737  * Return the current airspeed in knots.
738  */
739 double
740 FGBFI::getAirspeed ()
741 {
742                                 // FIXME: should we add speed-up?
743   return current_aircraft.fdm_state->get_V_calibrated_kts();
744 }
745
746
747 /**
748  * Return the current sideslip (FIXME: units unknown).
749  */
750 double
751 FGBFI::getSideSlip ()
752 {
753   return current_aircraft.fdm_state->get_Beta();
754 }
755
756
757 /**
758  * Return the current climb rate in feet/minute
759  */
760 double
761 FGBFI::getVerticalSpeed ()
762 {
763                                 // What about meters?
764   return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
765 }
766
767
768 /**
769  * Get the current north velocity (units??).
770  */
771 double
772 FGBFI::getSpeedNorth ()
773 {
774   return current_aircraft.fdm_state->get_V_north();
775 }
776
777
778 /**
779  * Set the current north velocity (units??).
780  */
781 void
782 FGBFI::setSpeedNorth (double speed)
783 {
784   if (getSpeedNorth() != speed) {
785     current_aircraft.fdm_state->set_Velocities_Local(speed,
786                                                      getSpeedEast(),
787                                                      getSpeedDown());
788     needReinit();
789   }
790 }
791
792
793 /**
794  * Get the current east velocity (units??).
795  */
796 double
797 FGBFI::getSpeedEast ()
798 {
799   return current_aircraft.fdm_state->get_V_east();
800 }
801
802
803 /**
804  * Set the current east velocity (units??).
805  */
806 void
807 FGBFI::setSpeedEast (double speed)
808 {
809   if (getSpeedEast() != speed) {
810     current_aircraft.fdm_state->set_Velocities_Local(getSpeedNorth(),
811                                                      speed,
812                                                      getSpeedDown());
813     needReinit();
814   }
815 }
816
817
818 /**
819  * Get the current down velocity (units??).
820  */
821 double
822 FGBFI::getSpeedDown ()
823 {
824   return current_aircraft.fdm_state->get_V_down();
825 }
826
827
828 /**
829  * Set the current down velocity (units??).
830  */
831 void
832 FGBFI::setSpeedDown (double speed)
833 {
834   if (getSpeedDown() != speed) {
835     current_aircraft.fdm_state->set_Velocities_Local(getSpeedNorth(),
836                                                      getSpeedEast(),
837                                                      speed);
838     needReinit();
839   }
840 }
841
842
843 \f
844 ////////////////////////////////////////////////////////////////////////
845 // Controls
846 ////////////////////////////////////////////////////////////////////////
847
848
849 /**
850  * Get the throttle setting, from 0.0 (none) to 1.0 (full).
851  */
852 double
853 FGBFI::getThrottle ()
854 {
855                                 // FIXME: add engine selector
856   return controls.get_throttle(0);
857 }
858
859
860 /**
861  * Set the throttle, from 0.0 (none) to 1.0 (full).
862  */
863 void
864 FGBFI::setThrottle (double throttle)
865 {
866                                 // FIXME: allow engine selection
867   controls.set_throttle(0, throttle);
868 }
869
870
871 /**
872  * Get the fuel mixture setting, from 0.0 (none) to 1.0 (full).
873  */
874 double
875 FGBFI::getMixture ()
876 {
877                                 // FIXME: add engine selector
878   return controls.get_mixture(0);
879 }
880
881
882 /**
883  * Set the fuel mixture, from 0.0 (none) to 1.0 (full).
884  */
885 void
886 FGBFI::setMixture (double mixture)
887 {
888                                 // FIXME: allow engine selection
889   controls.set_mixture(0, mixture);
890 }
891
892
893 /**
894  * Get the propellor pitch setting, from 0.0 (none) to 1.0 (full).
895  */
896 double
897 FGBFI::getPropAdvance ()
898 {
899                                 // FIXME: add engine selector
900   return controls.get_prop_advance(0);
901 }
902
903
904 /**
905  * Set the propellor pitch, from 0.0 (none) to 1.0 (full).
906  */
907 void
908 FGBFI::setPropAdvance (double pitch)
909 {
910                                 // FIXME: allow engine selection
911   controls.set_prop_advance(0, pitch);
912 }
913
914
915 /**
916  * Get the flaps setting, from 0.0 (none) to 1.0 (full).
917  */
918 double
919 FGBFI::getFlaps ()
920 {
921   return controls.get_flaps();
922 }
923
924
925 /**
926  * Set the flaps, from 0.0 (none) to 1.0 (full).
927  */
928 void
929 FGBFI::setFlaps (double flaps)
930 {
931                                 // FIXME: clamp?
932   controls.set_flaps(flaps);
933 }
934
935
936 /**
937  * Get the aileron, from -1.0 (left) to 1.0 (right).
938  */
939 double
940 FGBFI::getAileron ()
941 {
942   return controls.get_aileron();
943 }
944
945
946 /**
947  * Set the aileron, from -1.0 (left) to 1.0 (right).
948  */
949 void
950 FGBFI::setAileron (double aileron)
951 {
952                                 // FIXME: clamp?
953   controls.set_aileron(aileron);
954 }
955
956
957 /**
958  * Get the rudder setting, from -1.0 (left) to 1.0 (right).
959  */
960 double
961 FGBFI::getRudder ()
962 {
963   return controls.get_rudder();
964 }
965
966
967 /**
968  * Set the rudder, from -1.0 (left) to 1.0 (right).
969  */
970 void
971 FGBFI::setRudder (double rudder)
972 {
973                                 // FIXME: clamp?
974   controls.set_rudder(rudder);
975 }
976
977
978 /**
979  * Get the elevator setting, from -1.0 (down) to 1.0 (up).
980  */
981 double
982 FGBFI::getElevator ()
983 {
984   return controls.get_elevator();
985 }
986
987
988 /**
989  * Set the elevator, from -1.0 (down) to 1.0 (up).
990  */
991 void
992 FGBFI::setElevator (double elevator)
993 {
994                                 // FIXME: clamp?
995   controls.set_elevator(elevator);
996 }
997
998
999 /**
1000  * Get the elevator trim, from -1.0 (down) to 1.0 (up).
1001  */
1002 double
1003 FGBFI::getElevatorTrim ()
1004 {
1005   return controls.get_elevator_trim();
1006 }
1007
1008
1009 /**
1010  * Set the elevator trim, from -1.0 (down) to 1.0 (up).
1011  */
1012 void
1013 FGBFI::setElevatorTrim (double trim)
1014 {
1015                                 // FIXME: clamp?
1016   controls.set_elevator_trim(trim);
1017 }
1018
1019
1020 /**
1021  * Get the highest brake setting, from 0.0 (none) to 1.0 (full).
1022  */
1023 double
1024 FGBFI::getBrakes ()
1025 {
1026   double b1 = getCenterBrake();
1027   double b2 = getLeftBrake();
1028   double b3 = getRightBrake();
1029   return (b1 > b2 ? (b1 > b3 ? b1 : b3) : (b2 > b3 ? b2 : b3));
1030 }
1031
1032
1033 /**
1034  * Set all brakes, from 0.0 (none) to 1.0 (full).
1035  */
1036 void
1037 FGBFI::setBrakes (double brake)
1038 {
1039   setCenterBrake(brake);
1040   setLeftBrake(brake);
1041   setRightBrake(brake);
1042 }
1043
1044
1045 /**
1046  * Get the center brake, from 0.0 (none) to 1.0 (full).
1047  */
1048 double
1049 FGBFI::getCenterBrake ()
1050 {
1051   return controls.get_brake(2);
1052 }
1053
1054
1055 /**
1056  * Set the center brake, from 0.0 (none) to 1.0 (full).
1057  */
1058 void
1059 FGBFI::setCenterBrake (double brake)
1060 {
1061   controls.set_brake(2, brake);
1062 }
1063
1064
1065 /**
1066  * Get the left brake, from 0.0 (none) to 1.0 (full).
1067  */
1068 double
1069 FGBFI::getLeftBrake ()
1070 {
1071   return controls.get_brake(0);
1072 }
1073
1074
1075 /**
1076  * Set the left brake, from 0.0 (none) to 1.0 (full).
1077  */
1078 void
1079 FGBFI::setLeftBrake (double brake)
1080 {
1081   controls.set_brake(0, brake);
1082 }
1083
1084
1085 /**
1086  * Get the right brake, from 0.0 (none) to 1.0 (full).
1087  */
1088 double
1089 FGBFI::getRightBrake ()
1090 {
1091   return controls.get_brake(1);
1092 }
1093
1094
1095 /**
1096  * Set the right brake, from 0.0 (none) to 1.0 (full).
1097  */
1098 void
1099 FGBFI::setRightBrake (double brake)
1100 {
1101   controls.set_brake(1, brake);
1102 }
1103
1104
1105
1106
1107 \f
1108 ////////////////////////////////////////////////////////////////////////
1109 // Autopilot
1110 ////////////////////////////////////////////////////////////////////////
1111
1112
1113 /**
1114  * Get the autopilot altitude lock (true=on).
1115  */
1116 bool
1117 FGBFI::getAPAltitudeLock ()
1118 {
1119     return current_autopilot->get_AltitudeEnabled();
1120 }
1121
1122
1123 /**
1124  * Set the autopilot altitude lock (true=on).
1125  */
1126 void
1127 FGBFI::setAPAltitudeLock (bool lock)
1128 {
1129   current_autopilot->set_AltitudeMode(FGAutopilot::FG_ALTITUDE_LOCK);
1130   current_autopilot->set_AltitudeEnabled(lock);
1131 }
1132
1133
1134 /**
1135  * Get the autopilot target altitude in feet.
1136  */
1137 double
1138 FGBFI::getAPAltitude ()
1139 {
1140   return current_autopilot->get_TargetAltitude() * METER_TO_FEET;
1141 }
1142
1143
1144 /**
1145  * Set the autopilot target altitude in feet.
1146  */
1147 void
1148 FGBFI::setAPAltitude (double altitude)
1149 {
1150     current_autopilot->set_TargetAltitude( altitude );
1151 }
1152
1153
1154 /**
1155  * Get the autopilot heading lock (true=on).
1156  */
1157 bool
1158 FGBFI::getAPHeadingLock ()
1159 {
1160     return
1161       (current_autopilot->get_HeadingEnabled() &&
1162        current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_LOCK);
1163 }
1164
1165
1166 /**
1167  * Set the autopilot heading lock (true=on).
1168  */
1169 void
1170 FGBFI::setAPHeadingLock (bool lock)
1171 {
1172   if (lock) {
1173                                 // We need to do this so that
1174                                 // it's possible to lock onto a
1175                                 // heading other than the current
1176                                 // heading.
1177     double heading = getAPHeadingMag();
1178     current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_LOCK);
1179     current_autopilot->set_HeadingEnabled(true);
1180     setAPHeadingMag(heading);
1181   } else if (current_autopilot->get_HeadingMode() ==
1182              FGAutopilot::FG_HEADING_LOCK) {
1183     current_autopilot->set_HeadingEnabled(false);
1184   }
1185 }
1186
1187
1188 /**
1189  * Get the autopilot target heading in degrees.
1190  */
1191 double
1192 FGBFI::getAPHeading ()
1193 {
1194   return current_autopilot->get_TargetHeading();
1195 }
1196
1197
1198 /**
1199  * Set the autopilot target heading in degrees.
1200  */
1201 void
1202 FGBFI::setAPHeading (double heading)
1203 {
1204   current_autopilot->set_TargetHeading( heading );
1205 }
1206
1207
1208 /**
1209  * Get the autopilot target heading in degrees.
1210  */
1211 double
1212 FGBFI::getAPHeadingMag ()
1213 {
1214   return current_autopilot->get_TargetHeading() - getMagVar();
1215 }
1216
1217
1218 /**
1219  * Set the autopilot target heading in degrees.
1220  */
1221 void
1222 FGBFI::setAPHeadingMag (double heading)
1223 {
1224   current_autopilot->set_TargetHeading( heading + getMagVar() );
1225 }
1226
1227
1228 /**
1229  * Return true if the autopilot is locked to NAV1.
1230  */
1231 bool
1232 FGBFI::getAPNAV1Lock ()
1233 {
1234   return
1235     (current_autopilot->get_HeadingEnabled() &&
1236      current_autopilot->get_HeadingMode() == FGAutopilot::FG_HEADING_NAV1);
1237 }
1238
1239
1240 /**
1241  * Set the autopilot NAV1 lock.
1242  */
1243 void
1244 FGBFI::setAPNAV1Lock (bool lock)
1245 {
1246   if (lock) {
1247     current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_NAV1);
1248     current_autopilot->set_HeadingEnabled(true);
1249   } else if (current_autopilot->get_HeadingMode() ==
1250              FGAutopilot::FG_HEADING_NAV1) {
1251     current_autopilot->set_HeadingEnabled(false);
1252   }
1253 }
1254
1255
1256 \f
1257 ////////////////////////////////////////////////////////////////////////
1258 // Radio navigation.
1259 ////////////////////////////////////////////////////////////////////////
1260
1261 double
1262 FGBFI::getNAV1Freq ()
1263 {
1264   return current_radiostack->get_nav1_freq();
1265 }
1266
1267 double
1268 FGBFI::getNAV1AltFreq ()
1269 {
1270   return current_radiostack->get_nav1_alt_freq();
1271 }
1272
1273 double
1274 FGBFI::getNAV1Radial ()
1275 {
1276   return current_radiostack->get_nav1_radial();
1277 }
1278
1279 double
1280 FGBFI::getNAV1SelRadial ()
1281 {
1282   return current_radiostack->get_nav1_sel_radial();
1283 }
1284
1285 double
1286 FGBFI::getNAV1DistDME ()
1287 {
1288   return current_radiostack->get_nav1_dme_dist();
1289 }
1290
1291 bool 
1292 FGBFI::getNAV1TO ()
1293 {
1294   if (current_radiostack->get_nav1_inrange()) {
1295     double heading = current_radiostack->get_nav1_heading();
1296     double radial = current_radiostack->get_nav1_radial();
1297     double var = FGBFI::getMagVar();
1298     if (current_radiostack->get_nav1_loc()) {
1299       double offset = fabs(heading - radial);
1300       return (offset<= 8.0 || offset >= 352.0);
1301     } else {
1302       double offset =
1303         fabs(heading - var - radial);
1304       return (offset <= 20.0 || offset >= 340.0);
1305     }
1306   } else {
1307     return false;
1308   }
1309 }
1310
1311 bool
1312 FGBFI::getNAV1FROM ()
1313 {
1314   if (current_radiostack->get_nav1_inrange()) {
1315     double heading = current_radiostack->get_nav1_heading();
1316     double radial = current_radiostack->get_nav1_radial();
1317     double var = FGBFI::getMagVar();
1318     if (current_radiostack->get_nav1_loc()) {
1319       double offset = fabs(heading - radial);
1320       return (offset >= 172.0 && offset<= 188.0);
1321     } else {
1322       double offset =
1323         fabs(heading - var - radial);
1324       return (offset >= 160.0 && offset <= 200.0);
1325     }
1326   } else {
1327     return false;
1328   }
1329 }
1330
1331 bool
1332 FGBFI::getNAV1InRange ()
1333 {
1334   return current_radiostack->get_nav1_inrange();
1335 }
1336
1337 bool
1338 FGBFI::getNAV1DMEInRange ()
1339 {
1340   return (current_radiostack->get_nav1_inrange() &&
1341           current_radiostack->get_nav1_has_dme());
1342 }
1343
1344 double
1345 FGBFI::getNAV2Freq ()
1346 {
1347   return current_radiostack->get_nav2_freq();
1348 }
1349
1350 double
1351 FGBFI::getNAV2AltFreq ()
1352 {
1353   return current_radiostack->get_nav2_alt_freq();
1354 }
1355
1356 double
1357 FGBFI::getNAV2Radial ()
1358 {
1359   return current_radiostack->get_nav2_radial();
1360 }
1361
1362 double
1363 FGBFI::getNAV2SelRadial ()
1364 {
1365   return current_radiostack->get_nav2_sel_radial();
1366 }
1367
1368 double
1369 FGBFI::getNAV2DistDME ()
1370 {
1371   return current_radiostack->get_nav2_dme_dist();
1372 }
1373
1374 bool 
1375 FGBFI::getNAV2TO ()
1376 {
1377   if (current_radiostack->get_nav2_inrange()) {
1378     double heading = current_radiostack->get_nav2_heading();
1379     double radial = current_radiostack->get_nav2_radial();
1380     double var = FGBFI::getMagVar();
1381     if (current_radiostack->get_nav2_loc()) {
1382       double offset = fabs(heading - radial);
1383       return (offset<= 8.0 || offset >= 352.0);
1384     } else {
1385       double offset =
1386         fabs(heading - var - radial);
1387       return (offset <= 20.0 || offset >= 340.0);
1388     }
1389   } else {
1390     return false;
1391   }
1392 }
1393
1394 bool 
1395 FGBFI::getNAV2FROM ()
1396 {
1397   if (current_radiostack->get_nav2_inrange()) {
1398     double heading = current_radiostack->get_nav2_heading();
1399     double radial = current_radiostack->get_nav2_radial();
1400     double var = FGBFI::getMagVar();
1401     if (current_radiostack->get_nav2_loc()) {
1402       double offset = fabs(heading - radial);
1403       return (offset >= 172.0 && offset<= 188.0);
1404     } else {
1405       double offset =
1406         fabs(heading - var - radial);
1407       return (offset >= 160.0 && offset <= 200.0);
1408     }
1409   } else {
1410     return false;
1411   }
1412 }
1413
1414
1415 bool
1416 FGBFI::getNAV2InRange ()
1417 {
1418   return current_radiostack->get_nav2_inrange();
1419 }
1420
1421 bool
1422 FGBFI::getNAV2DMEInRange ()
1423 {
1424   return (current_radiostack->get_nav2_inrange() &&
1425           current_radiostack->get_nav2_has_dme());
1426 }
1427
1428 double
1429 FGBFI::getADFFreq ()
1430 {
1431   return current_radiostack->get_adf_freq();
1432 }
1433
1434 double
1435 FGBFI::getADFAltFreq ()
1436 {
1437   return current_radiostack->get_adf_alt_freq();
1438 }
1439
1440 double
1441 FGBFI::getADFRotation ()
1442 {
1443   return current_radiostack->get_adf_rotation();
1444 }
1445
1446 void
1447 FGBFI::setNAV1Freq (double freq)
1448 {
1449   current_radiostack->set_nav1_freq(freq);
1450 }
1451
1452 void
1453 FGBFI::setNAV1AltFreq (double freq)
1454 {
1455   current_radiostack->set_nav1_alt_freq(freq);
1456 }
1457
1458 void
1459 FGBFI::setNAV1SelRadial (double radial)
1460 {
1461   current_radiostack->set_nav1_sel_radial(radial);
1462 }
1463
1464 void
1465 FGBFI::setNAV2Freq (double freq)
1466 {
1467   current_radiostack->set_nav2_freq(freq);
1468 }
1469
1470 void
1471 FGBFI::setNAV2AltFreq (double freq)
1472 {
1473   current_radiostack->set_nav2_alt_freq(freq);
1474 }
1475
1476 void
1477 FGBFI::setNAV2SelRadial (double radial)
1478 {
1479   current_radiostack->set_nav2_sel_radial(radial);
1480 }
1481
1482 void
1483 FGBFI::setADFFreq (double freq)
1484 {
1485   current_radiostack->set_adf_freq(freq);
1486 }
1487
1488 void
1489 FGBFI::setADFAltFreq (double freq)
1490 {
1491   current_radiostack->set_adf_alt_freq(freq);
1492 }
1493
1494 void
1495 FGBFI::setADFRotation (double rot)
1496 {
1497   current_radiostack->set_adf_rotation(rot);
1498 }
1499
1500
1501 \f
1502 ////////////////////////////////////////////////////////////////////////
1503 // GPS
1504 ////////////////////////////////////////////////////////////////////////
1505
1506
1507 /**
1508  * Get the autopilot GPS lock (true=on).
1509  */
1510 bool
1511 FGBFI::getGPSLock ()
1512 {
1513   return (current_autopilot->get_HeadingEnabled() &&
1514           (current_autopilot->get_HeadingMode() ==
1515            FGAutopilot::FG_HEADING_WAYPOINT ));
1516 }
1517
1518
1519 /**
1520  * Set the autopilot GPS lock (true=on).
1521  */
1522 void
1523 FGBFI::setGPSLock (bool lock)
1524 {
1525   if (lock) {
1526     current_autopilot->set_HeadingMode(FGAutopilot::FG_HEADING_WAYPOINT);
1527     current_autopilot->set_HeadingEnabled(true);
1528   } else if (current_autopilot->get_HeadingMode() ==
1529              FGAutopilot::FG_HEADING_WAYPOINT) {
1530     current_autopilot->set_HeadingEnabled(false);
1531   }
1532 }
1533
1534
1535 /**
1536  * Get the GPS target airport code.
1537  */
1538 const string &
1539 FGBFI::getTargetAirport ()
1540 {
1541   // FIXME: not thread-safe
1542   static string out;
1543   out = globals->get_options()->get_airport_id();
1544
1545   return out;
1546 }
1547
1548
1549 /**
1550  * Set the GPS target airport code.
1551  */
1552 void
1553 FGBFI::setTargetAirport (const string &airportId)
1554 {
1555   // cout << "setting target airport id = " << airportId << endl;
1556   globals->get_options()->set_airport_id(airportId);
1557 }
1558
1559
1560 /**
1561  * Get the GPS target latitude in degrees (negative for south).
1562  */
1563 double
1564 FGBFI::getGPSTargetLatitude ()
1565 {
1566     return current_autopilot->get_TargetLatitude();
1567 }
1568
1569
1570 /**
1571  * Get the GPS target longitude in degrees (negative for west).
1572  */
1573 double
1574 FGBFI::getGPSTargetLongitude ()
1575 {
1576   return current_autopilot->get_TargetLongitude();
1577 }
1578
1579 #if 0
1580 /**
1581  * Set the GPS target longitude in degrees (negative for west).
1582  */
1583 void
1584 FGBFI::setGPSTargetLongitude (double longitude)
1585 {
1586   current_autopilot->set_TargetLongitude( longitude );
1587 }
1588 #endif
1589
1590
1591 \f
1592 ////////////////////////////////////////////////////////////////////////
1593 // Weather
1594 ////////////////////////////////////////////////////////////////////////
1595
1596
1597 /**
1598  * Get the current visible (units??).
1599  */
1600 double
1601 FGBFI::getVisibility ()
1602 {
1603 #ifndef FG_OLD_WEATHER
1604   return WeatherDatabase->getWeatherVisibility();
1605 #else
1606   return current_weather.get_visibility();
1607 #endif
1608 }
1609
1610
1611 /**
1612  * Check whether clouds are enabled.
1613  */
1614 bool
1615 FGBFI::getClouds ()
1616 {
1617   return globals->get_options()->get_clouds();
1618 }
1619
1620
1621 /**
1622  * Check the height of the clouds ASL (units?).
1623  */
1624 double
1625 FGBFI::getCloudsASL ()
1626 {
1627   return globals->get_options()->get_clouds_asl();
1628 }
1629
1630
1631 /**
1632  * Set the current visibility (units??).
1633  */
1634 void
1635 FGBFI::setVisibility (double visibility)
1636 {
1637 #ifndef FG_OLD_WEATHER
1638   WeatherDatabase->setWeatherVisibility(visibility);
1639 #else
1640   current_weather.set_visibility(visibility);
1641 #endif
1642 }
1643
1644
1645 /**
1646  * Switch clouds on or off.
1647  */
1648 void
1649 FGBFI::setClouds (bool clouds)
1650 {
1651   if (getClouds() != clouds) {
1652     cout << "Set clouds to " << clouds << endl;
1653     globals->get_options()->set_clouds(clouds);
1654     needReinit();
1655   }
1656 }
1657
1658
1659 /**
1660  * Set the cloud height.
1661  */
1662 void
1663 FGBFI::setCloudsASL (double cloudsASL)
1664 {
1665   if (getCloudsASL() != cloudsASL) {
1666     globals->get_options()->set_clouds_asl(cloudsASL);
1667     needReinit();
1668   }
1669 }
1670
1671
1672 \f
1673 ////////////////////////////////////////////////////////////////////////
1674 // Time
1675 ////////////////////////////////////////////////////////////////////////
1676
1677 /**
1678  * Return the magnetic variation
1679  */
1680 double
1681 FGBFI::getMagVar ()
1682 {
1683   return globals->get_mag()->get_magvar() * RAD_TO_DEG;
1684 }
1685
1686
1687 /**
1688  * Return the magnetic variation
1689  */
1690 double
1691 FGBFI::getMagDip ()
1692 {
1693   return globals->get_mag()->get_magdip() * RAD_TO_DEG;
1694 }
1695
1696
1697 // end of bfi.cxx
1698