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