]> git.mxchange.org Git - flightgear.git/blob - src/Main/bfi.cxx
13f1b18331bcad12581f576f3f25fa226041ab11
[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/math/fg_types.hxx>
36
37 #include <Aircraft/aircraft.hxx>
38 #include <Controls/controls.hxx>
39 #include <Autopilot/autopilot.hxx>
40 #include <Time/fg_time.hxx>
41 #include <Time/light.hxx>
42 #include <Cockpit/radiostack.hxx>
43 #ifndef FG_OLD_WEATHER
44 #  include <WeatherCM/FGLocalWeatherDatabase.h>
45 #else
46 #  include <Weather/weather.hxx>
47 #endif
48
49 #include "options.hxx"
50 #include "save.hxx"
51 #include "fg_init.hxx"
52
53 FG_USING_NAMESPACE(std);
54
55                                 // FIXME: these are not part of the
56                                 // published interface!!!
57 extern fgAPDataPtr APDataGlobal;
58 extern void fgAPAltitudeSet (double new_altitude);
59 extern void fgAPHeadingSet (double new_heading);
60
61
62 #include "bfi.hxx"
63
64
65 \f
66 ////////////////////////////////////////////////////////////////////////
67 // Static variables.
68 ////////////////////////////////////////////////////////////////////////
69
70 bool FGBFI::_needReinit = false;
71
72
73 \f
74 ////////////////////////////////////////////////////////////////////////
75 // Local functions
76 ////////////////////////////////////////////////////////////////////////
77
78
79 /**
80  * Reinitialize FGFS if required.
81  *
82  * Some changes (especially those in aircraft position) require that
83  * FGFS be reinitialized afterwards.  Rather than reinitialize after
84  * every change, the setter methods simply set a flag so that there
85  * can be a single reinit at the end of the frame.
86  */
87 void
88 FGBFI::update ()
89 {
90   if (_needReinit) {
91     reinit();
92   }
93 }
94
95
96 /**
97  * Reinitialize FGFS to use the new BFI settings.
98  */
99 void
100 FGBFI::reinit ()
101 {
102                                 // Save the state of everything
103                                 // that's going to get clobbered
104                                 // when we reinit the subsystems.
105
106                                 // TODO: add more AP stuff
107   double elevator = getElevator();
108   double aileron = getAileron();
109   double rudder = getRudder();
110   double throttle = getThrottle();
111   double elevator_trim = getElevatorTrim();
112   double flaps = getFlaps();
113   double brake = getBrake();
114   bool apHeadingLock = getAPHeadingLock();
115   double apHeading = getAPHeading();
116   bool apAltitudeLock = getAPAltitudeLock();
117   double apAltitude = getAPAltitude();
118   const string &targetAirport = getTargetAirport();
119   bool gpsLock = getGPSLock();
120   double gpsLatitude = getGPSTargetLatitude();
121   double gpsLongitude = getGPSTargetLongitude();
122
123   fgReInitSubsystems();
124   // solarSystemRebuild();
125   cur_light_params.Update();
126
127                                 // Restore all of the old states.
128   setElevator(elevator);
129   setAileron(aileron);
130   setRudder(rudder);
131   setThrottle(throttle);
132   setElevatorTrim(elevator_trim);
133   setFlaps(flaps);
134   setBrake(brake);
135   setAPHeadingLock(apHeadingLock);
136   setAPHeading(apHeading);
137   setAPAltitudeLock(apAltitudeLock);
138   setAPAltitude(apAltitude);
139   setTargetAirport(targetAirport);
140   setGPSLock(gpsLock);
141   setGPSTargetLatitude(gpsLatitude);
142   setGPSTargetLongitude(gpsLongitude);
143
144   _needReinit = false;
145 }
146
147
148 \f
149 ////////////////////////////////////////////////////////////////////////
150 // Simulation.
151 ////////////////////////////////////////////////////////////////////////
152
153
154 /**
155  * Return the flight model as an integer.
156  *
157  * TODO: use a string instead.
158  */
159 int
160 FGBFI::getFlightModel ()
161 {
162   return current_options.get_flight_model();
163 }
164
165
166 /**
167  * Set the flight model as an integer.
168  *
169  * TODO: use a string instead.
170  */
171 void
172 FGBFI::setFlightModel (int model)
173 {
174   current_options.set_flight_model(model);
175   needReinit();
176 }
177
178
179 /**
180  * Return the current Zulu time.
181  */
182 time_t
183 FGBFI::getTimeGMT ()
184 {
185                                 // FIXME: inefficient
186   return mktime(FGTime::cur_time_params->getGmt());
187 }
188
189
190 /**
191  * Set the current Zulu time.
192  */
193 void
194 FGBFI::setTimeGMT (time_t time)
195 {
196                                 // FIXME: need to update lighting
197                                 // and solar system
198   current_options.set_time_offset(time);
199   current_options.set_time_offset_type(fgOPTIONS::FG_TIME_GMT_ABSOLUTE);
200   FGTime::cur_time_params->init( cur_fdm_state->get_Longitude(),
201                                  cur_fdm_state->get_Latitude() );
202   FGTime::cur_time_params->update( cur_fdm_state->get_Longitude(),
203                                    cur_fdm_state->get_Latitude(),
204                                    cur_fdm_state->get_Altitude()
205                                    * FEET_TO_METER );
206   needReinit();
207 }
208
209
210 /**
211  * Return true if the HUD is visible.
212  */
213 bool
214 FGBFI::getHUDVisible ()
215 {
216   return current_options.get_hud_status();
217 }
218
219
220 /**
221  * Ensure that the HUD is visible or hidden.
222  */
223 void
224 FGBFI::setHUDVisible (bool visible)
225 {
226   current_options.set_hud_status(visible);
227 }
228
229
230 /**
231  * Return true if the 2D panel is visible.
232  */
233 bool
234 FGBFI::getPanelVisible ()
235 {
236   return current_options.get_panel_status();
237 }
238
239
240 /**
241  * Ensure that the 2D panel is visible or hidden.
242  */
243 void
244 FGBFI::setPanelVisible (bool visible)
245 {
246   if (current_options.get_panel_status() != visible) {
247     current_options.toggle_panel();
248   }
249 }
250
251
252 \f
253 ////////////////////////////////////////////////////////////////////////
254 // Position
255 ////////////////////////////////////////////////////////////////////////
256
257
258 /**
259  * Return the current latitude in degrees (negative for south).
260  */
261 double
262 FGBFI::getLatitude ()
263 {
264   return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
265 }
266
267
268 /**
269  * Set the current latitude in degrees (negative for south).
270  */
271 void
272 FGBFI::setLatitude (double latitude)
273 {
274   current_options.set_lat(latitude);
275   needReinit();
276 }
277
278
279 /**
280  * Return the current longitude in degrees (negative for west).
281  */
282 double
283 FGBFI::getLongitude ()
284 {
285   return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
286 }
287
288
289 /**
290  * Set the current longitude in degrees (negative for west).
291  */
292 void
293 FGBFI::setLongitude (double longitude)
294 {
295   current_options.set_lon(longitude);
296   needReinit();
297 }
298
299
300 /**
301  * Return the current altitude in feet.
302  */
303 double
304 FGBFI::getAltitude ()
305 {
306   return current_aircraft.fdm_state->get_Altitude();
307 }
308
309
310 /**
311  * Set the current altitude in feet.
312  */
313 void
314 FGBFI::setAltitude (double altitude)
315 {
316   current_options.set_altitude(altitude * FEET_TO_METER);
317   needReinit();
318 }
319
320
321 \f
322 ////////////////////////////////////////////////////////////////////////
323 // Attitude
324 ////////////////////////////////////////////////////////////////////////
325
326
327 /**
328  * Return the current heading in degrees.
329  */
330 double
331 FGBFI::getHeading ()
332 {
333   return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
334 }
335
336
337 /**
338  * Set the current heading in degrees.
339  */
340 void
341 FGBFI::setHeading (double heading)
342 {
343   current_options.set_heading(heading);
344   needReinit();
345 }
346
347
348 /**
349  * Return the current pitch in degrees.
350  */
351 double
352 FGBFI::getPitch ()
353 {
354   return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
355 }
356
357
358 /**
359  * Set the current pitch in degrees.
360  */
361 void
362 FGBFI::setPitch (double pitch)
363 {
364
365   current_options.set_pitch(pitch);
366   needReinit();
367 }
368
369
370 /**
371  * Return the current roll in degrees.
372  */
373 double
374 FGBFI::getRoll ()
375 {
376   return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
377 }
378
379
380 /**
381  * Set the current roll in degrees.
382  */
383 void
384 FGBFI::setRoll (double roll)
385 {
386   current_options.set_roll(roll);
387   needReinit();
388 }
389
390
391 \f
392 ////////////////////////////////////////////////////////////////////////
393 // Velocities
394 ////////////////////////////////////////////////////////////////////////
395
396
397 /**
398  * Return the current airspeed in knots.
399  */
400 double
401 FGBFI::getAirspeed ()
402 {
403                                 // FIXME: should we add speed-up?
404   return current_aircraft.fdm_state->get_V_calibrated_kts();
405 }
406
407
408 /**
409  * Return the current sideslip (FIXME: units unknown).
410  */
411 double
412 FGBFI::getSideSlip ()
413 {
414   return current_aircraft.fdm_state->get_Beta();
415 }
416
417
418 /**
419  * Return the current climb rate in feet/second (FIXME: verify).
420  */
421 double
422 FGBFI::getVerticalSpeed ()
423 {
424                                 // What about meters?
425   return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
426 }
427
428
429 /**
430  * Get the current north velocity (units??).
431  */
432 double
433 FGBFI::getSpeedNorth ()
434 {
435   return current_aircraft.fdm_state->get_V_north();
436 }
437
438
439 /**
440  * Set the current north velocity (units??).
441  */
442 void
443 FGBFI::setSpeedNorth (double speed)
444 {
445   current_options.set_uBody(speed);
446   needReinit();
447 }
448
449
450 /**
451  * Get the current east velocity (units??).
452  */
453 double
454 FGBFI::getSpeedEast ()
455 {
456   return current_aircraft.fdm_state->get_V_east();
457 }
458
459
460 /**
461  * Set the current east velocity (units??).
462  */
463 void
464 FGBFI::setSpeedEast (double speed)
465 {
466   current_options.set_vBody(speed);
467   needReinit();
468 }
469
470
471 /**
472  * Get the current down velocity (units??).
473  */
474 double
475 FGBFI::getSpeedDown ()
476 {
477   return current_aircraft.fdm_state->get_V_down();
478 }
479
480
481 /**
482  * Set the current down velocity (units??).
483  */
484 void
485 FGBFI::setSpeedDown (double speed)
486 {
487   current_options.set_wBody(speed);
488   needReinit();
489 }
490
491
492 \f
493 ////////////////////////////////////////////////////////////////////////
494 // Controls
495 ////////////////////////////////////////////////////////////////////////
496
497
498 /**
499  * Get the throttle setting, from 0.0 (none) to 1.0 (full).
500  */
501 double
502 FGBFI::getThrottle ()
503 {
504                                 // FIXME: add throttle selector
505   return controls.get_throttle(0);
506 }
507
508
509 /**
510  * Set the throttle, from 0.0 (none) to 1.0 (full).
511  */
512 void
513 FGBFI::setThrottle (double throttle)
514 {
515                                 // FIXME: allow throttle selection
516                                 // FIXME: clamp?
517   controls.set_throttle(0, throttle);
518 }
519
520
521 /**
522  * Get the flaps setting, from 0.0 (none) to 1.0 (full).
523  */
524 double
525 FGBFI::getFlaps ()
526 {
527   return controls.get_flaps();
528 }
529
530
531 /**
532  * Set the flaps, from 0.0 (none) to 1.0 (full).
533  */
534 void
535 FGBFI::setFlaps (double flaps)
536 {
537                                 // FIXME: clamp?
538   controls.set_flaps(flaps);
539 }
540
541
542 /**
543  * Get the aileron, from -1.0 (left) to 1.0 (right).
544  */
545 double
546 FGBFI::getAileron ()
547 {
548   return controls.get_aileron();
549 }
550
551
552 /**
553  * Set the aileron, from -1.0 (left) to 1.0 (right).
554  */
555 void
556 FGBFI::setAileron (double aileron)
557 {
558                                 // FIXME: clamp?
559   controls.set_aileron(aileron);
560 }
561
562
563 /**
564  * Get the rudder setting, from -1.0 (left) to 1.0 (right).
565  */
566 double
567 FGBFI::getRudder ()
568 {
569   return controls.get_rudder();
570 }
571
572
573 /**
574  * Set the rudder, from -1.0 (left) to 1.0 (right).
575  */
576 void
577 FGBFI::setRudder (double rudder)
578 {
579                                 // FIXME: clamp?
580   controls.set_rudder(rudder);
581 }
582
583
584 /**
585  * Get the elevator setting, from -1.0 (down) to 1.0 (up).
586  */
587 double
588 FGBFI::getElevator ()
589 {
590   return controls.get_elevator();
591 }
592
593
594 /**
595  * Set the elevator, from -1.0 (down) to 1.0 (up).
596  */
597 void
598 FGBFI::setElevator (double elevator)
599 {
600                                 // FIXME: clamp?
601   controls.set_elevator(elevator);
602 }
603
604
605 /**
606  * Get the elevator trim, from -1.0 (down) to 1.0 (up).
607  */
608 double
609 FGBFI::getElevatorTrim ()
610 {
611   return controls.get_elevator_trim();
612 }
613
614
615 /**
616  * Set the elevator trim, from -1.0 (down) to 1.0 (up).
617  */
618 void
619 FGBFI::setElevatorTrim (double trim)
620 {
621                                 // FIXME: clamp?
622   controls.set_elevator_trim(trim);
623 }
624
625
626 /**
627  * Get the brake setting, from 0.0 (none) to 1.0 (full).
628  */
629 double
630 FGBFI::getBrake ()
631 {
632                                 // FIXME: add brake selector
633   return controls.get_brake(0);
634 }
635
636
637 /**
638  * Set the brake, from 0.0 (none) to 1.0 (full).
639  */
640 void
641 FGBFI::setBrake (double brake)
642 {
643                                 // FIXME: clamp?
644                                 // FIXME: allow brake selection
645   controls.set_brake(0, brake);
646 }
647
648
649 \f
650 ////////////////////////////////////////////////////////////////////////
651 // Autopilot
652 ////////////////////////////////////////////////////////////////////////
653
654
655 /**
656  * Get the autopilot altitude lock (true=on).
657  */
658 bool
659 FGBFI::getAPAltitudeLock ()
660 {
661   return fgAPAltitudeEnabled();
662 }
663
664
665 /**
666  * Set the autopilot altitude lock (true=on).
667  */
668 void
669 FGBFI::setAPAltitudeLock (bool lock)
670 {
671   APDataGlobal->altitude_hold = lock;
672 }
673
674
675 /**
676  * Get the autopilot target altitude in feet.
677  */
678 double
679 FGBFI::getAPAltitude ()
680 {
681   return fgAPget_TargetAltitude() * METER_TO_FEET;
682 }
683
684
685 /**
686  * Set the autopilot target altitude in feet.
687  */
688 void
689 FGBFI::setAPAltitude (double altitude)
690 {
691   fgAPAltitudeSet(altitude);
692 }
693
694
695 /**
696  * Get the autopilot heading lock (true=on).
697  */
698 bool
699 FGBFI::getAPHeadingLock ()
700 {
701   return fgAPHeadingEnabled();
702 }
703
704
705 /**
706  * Set the autopilot heading lock (true=on).
707  */
708 void
709 FGBFI::setAPHeadingLock (bool lock)
710 {
711   APDataGlobal->heading_hold = lock;
712 }
713
714
715 /**
716  * Get the autopilot target heading in degrees.
717  */
718 double
719 FGBFI::getAPHeading ()
720 {
721   return fgAPget_TargetHeading();
722 }
723
724
725 /**
726  * Set the autopilot target heading in degrees.
727  */
728 void
729 FGBFI::setAPHeading (double heading)
730 {
731   fgAPHeadingSet(heading);
732 }
733
734
735 \f
736 ////////////////////////////////////////////////////////////////////////
737 // Radio navigation.
738 ////////////////////////////////////////////////////////////////////////
739
740 double
741 FGBFI::getNAV1Freq ()
742 {
743   return current_radiostack->get_nav1_freq();
744 }
745
746 double
747 FGBFI::getNAV1AltFreq ()
748 {
749   return current_radiostack->get_nav1_alt_freq();
750 }
751
752 double
753 FGBFI::getNAV1SelRadial ()
754 {
755   return current_radiostack->get_nav1_sel_radial();
756 }
757
758 double
759 FGBFI::getNAV1Radial ()
760 {
761   return current_radiostack->get_nav1_radial();
762 }
763
764 double
765 FGBFI::getNAV2Freq ()
766 {
767   return current_radiostack->get_nav2_freq();
768 }
769
770 double
771 FGBFI::getNAV2AltFreq ()
772 {
773   return current_radiostack->get_nav2_alt_freq();
774 }
775
776 double
777 FGBFI::getNAV2SelRadial ()
778 {
779   return current_radiostack->get_nav2_sel_radial();
780 }
781
782 double
783 FGBFI::getNAV2Radial ()
784 {
785   return current_radiostack->get_nav2_radial();
786 }
787
788 double
789 FGBFI::getADFFreq ()
790 {
791   return current_radiostack->get_adf_freq();
792 }
793
794 double
795 FGBFI::getADFAltFreq ()
796 {
797   return current_radiostack->get_adf_alt_freq();
798 }
799
800 double
801 FGBFI::getADFRotation ()
802 {
803   return current_radiostack->get_adf_rotation();
804 }
805
806 void
807 FGBFI::setNAV1Freq (double freq)
808 {
809   current_radiostack->set_nav1_freq(freq);
810 }
811
812 void
813 FGBFI::setNAV1AltFreq (double freq)
814 {
815   current_radiostack->set_nav1_alt_freq(freq);
816 }
817
818 void
819 FGBFI::setNAV1SelRadial (double radial)
820 {
821   current_radiostack->set_nav1_sel_radial(radial);
822 }
823
824 void
825 FGBFI::setNAV2Freq (double freq)
826 {
827   current_radiostack->set_nav2_freq(freq);
828 }
829
830 void
831 FGBFI::setNAV2AltFreq (double freq)
832 {
833   current_radiostack->set_nav2_alt_freq(freq);
834 }
835
836 void
837 FGBFI::setNAV2SelRadial (double radial)
838 {
839   current_radiostack->set_nav2_sel_radial(radial);
840 }
841
842 void
843 FGBFI::setADFFreq (double freq)
844 {
845   current_radiostack->set_adf_freq(freq);
846 }
847
848 void
849 FGBFI::setADFAltFreq (double freq)
850 {
851   current_radiostack->set_adf_alt_freq(freq);
852 }
853
854 void
855 FGBFI::setADFRotation (double rot)
856 {
857   current_radiostack->set_adf_rotation(rot);
858 }
859
860
861 \f
862 ////////////////////////////////////////////////////////////////////////
863 // GPS
864 ////////////////////////////////////////////////////////////////////////
865
866
867 /**
868  * Get the autopilot GPS lock (true=on).
869  */
870 bool
871 FGBFI::getGPSLock ()
872 {
873   return fgAPWayPointEnabled();
874 }
875
876
877 /**
878  * Set the autopilot GPS lock (true=on).
879  */
880 void
881 FGBFI::setGPSLock (bool lock)
882 {
883   APDataGlobal->waypoint_hold = lock;
884 }
885
886
887 /**
888  * Get the GPS target airport code.
889  */
890 const string
891 FGBFI::getTargetAirport ()
892 {
893   return current_options.get_airport_id();
894 }
895
896
897 /**
898  * Set the GPS target airport code.
899  */
900 void
901 FGBFI::setTargetAirport (const string &airportId)
902 {
903   current_options.set_airport_id(airportId);
904 }
905
906
907 /**
908  * Get the GPS target latitude in degrees (negative for south).
909  */
910 double
911 FGBFI::getGPSTargetLatitude ()
912 {
913   return fgAPget_TargetLatitude();
914 }
915
916
917 /**
918  * Set the GPS target latitude in degrees (negative for south).
919  */
920 void
921 FGBFI::setGPSTargetLatitude (double latitude)
922 {
923   APDataGlobal->TargetLatitude = latitude;
924 }
925
926
927 /**
928  * Get the GPS target longitude in degrees (negative for west).
929  */
930 double
931 FGBFI::getGPSTargetLongitude ()
932 {
933   return fgAPget_TargetLongitude();
934 }
935
936
937 /**
938  * Set the GPS target longitude in degrees (negative for west).
939  */
940 void
941 FGBFI::setGPSTargetLongitude (double longitude)
942 {
943   APDataGlobal->TargetLongitude = longitude;
944 }
945
946
947 \f
948 ////////////////////////////////////////////////////////////////////////
949 // Weather
950 ////////////////////////////////////////////////////////////////////////
951
952
953 /**
954  * Get the current visible (units??).
955  */
956 double
957 FGBFI::getVisibility ()
958 {
959 #ifndef FG_OLD_WEATHER
960   return WeatherDatabase->getWeatherVisibility();
961 #else
962   return current_weather.get_visibility();
963 #endif
964 }
965
966
967 /**
968  * Set the current visibility (units??).
969  */
970 void
971 FGBFI::setVisibility (double visibility)
972 {
973 #ifndef FG_OLD_WEATHER
974   WeatherDatabase->setWeatherVisibility(visibility);
975 #else
976   current_weather.set_visibility(visibility);
977 #endif
978 }
979
980
981 \f
982 ////////////////////////////////////////////////////////////////////////
983 // Time
984 ////////////////////////////////////////////////////////////////////////
985
986 /**
987  * Return the magnetic variation
988  */
989 double
990 FGBFI::getMagVar ()
991 {
992   return FGTime::cur_time_params->getMagVar() * RAD_TO_DEG;
993 }
994
995
996 /**
997  * Return the magnetic variation
998  */
999 double
1000 FGBFI::getMagDip ()
1001 {
1002   return FGTime::cur_time_params->getMagDip() * RAD_TO_DEG;
1003 }
1004
1005
1006 // end of bfi.cxx
1007