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