]> git.mxchange.org Git - flightgear.git/blob - src/Main/bfi.cxx
10978d7912eac7f46d57644bdebe3da92aacee56
[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);
200   FGTime::cur_time_params->update(*cur_fdm_state);
201   needReinit();
202 }
203
204
205 /**
206  * Return true if the HUD is visible.
207  */
208 bool
209 FGBFI::getHUDVisible ()
210 {
211   return current_options.get_hud_status();
212 }
213
214
215 /**
216  * Ensure that the HUD is visible or hidden.
217  */
218 void
219 FGBFI::setHUDVisible (bool visible)
220 {
221   current_options.set_hud_status(visible);
222 }
223
224
225 /**
226  * Return true if the 2D panel is visible.
227  */
228 bool
229 FGBFI::getPanelVisible ()
230 {
231   return current_options.get_panel_status();
232 }
233
234
235 /**
236  * Ensure that the 2D panel is visible or hidden.
237  */
238 void
239 FGBFI::setPanelVisible (bool visible)
240 {
241   if (current_options.get_panel_status() != visible) {
242     current_options.toggle_panel();
243   }
244 }
245
246
247 \f
248 ////////////////////////////////////////////////////////////////////////
249 // Position
250 ////////////////////////////////////////////////////////////////////////
251
252
253 /**
254  * Return the current latitude in degrees (negative for south).
255  */
256 double
257 FGBFI::getLatitude ()
258 {
259   return current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG;
260 }
261
262
263 /**
264  * Set the current latitude in degrees (negative for south).
265  */
266 void
267 FGBFI::setLatitude (double latitude)
268 {
269   current_options.set_lat(latitude);
270   needReinit();
271 }
272
273
274 /**
275  * Return the current longitude in degrees (negative for west).
276  */
277 double
278 FGBFI::getLongitude ()
279 {
280   return current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG;
281 }
282
283
284 /**
285  * Set the current longitude in degrees (negative for west).
286  */
287 void
288 FGBFI::setLongitude (double longitude)
289 {
290   current_options.set_lon(longitude);
291   needReinit();
292 }
293
294
295 /**
296  * Return the current altitude in feet.
297  */
298 double
299 FGBFI::getAltitude ()
300 {
301   return current_aircraft.fdm_state->get_Altitude();
302 }
303
304
305 /**
306  * Set the current altitude in feet.
307  */
308 void
309 FGBFI::setAltitude (double altitude)
310 {
311   current_options.set_altitude(altitude * FEET_TO_METER);
312   needReinit();
313 }
314
315
316 \f
317 ////////////////////////////////////////////////////////////////////////
318 // Attitude
319 ////////////////////////////////////////////////////////////////////////
320
321
322 /**
323  * Return the current heading in degrees.
324  */
325 double
326 FGBFI::getHeading ()
327 {
328   return current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG;
329 }
330
331
332 /**
333  * Set the current heading in degrees.
334  */
335 void
336 FGBFI::setHeading (double heading)
337 {
338   current_options.set_heading(heading);
339   needReinit();
340 }
341
342
343 /**
344  * Return the current pitch in degrees.
345  */
346 double
347 FGBFI::getPitch ()
348 {
349   return current_aircraft.fdm_state->get_Theta() * RAD_TO_DEG;
350 }
351
352
353 /**
354  * Set the current pitch in degrees.
355  */
356 void
357 FGBFI::setPitch (double pitch)
358 {
359
360   current_options.set_pitch(pitch);
361   needReinit();
362 }
363
364
365 /**
366  * Return the current roll in degrees.
367  */
368 double
369 FGBFI::getRoll ()
370 {
371   return current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG;
372 }
373
374
375 /**
376  * Set the current roll in degrees.
377  */
378 void
379 FGBFI::setRoll (double roll)
380 {
381   current_options.set_roll(roll);
382   needReinit();
383 }
384
385
386 \f
387 ////////////////////////////////////////////////////////////////////////
388 // Velocities
389 ////////////////////////////////////////////////////////////////////////
390
391
392 /**
393  * Return the current airspeed in knots.
394  */
395 double
396 FGBFI::getAirspeed ()
397 {
398                                 // FIXME: should we add speed-up?
399   return current_aircraft.fdm_state->get_V_calibrated_kts();
400 }
401
402
403 /**
404  * Return the current sideslip (FIXME: units unknown).
405  */
406 double
407 FGBFI::getSideSlip ()
408 {
409   return current_aircraft.fdm_state->get_Beta();
410 }
411
412
413 /**
414  * Return the current climb rate in feet/second (FIXME: verify).
415  */
416 double
417 FGBFI::getVerticalSpeed ()
418 {
419                                 // What about meters?
420   return current_aircraft.fdm_state->get_Climb_Rate() * 60.0;
421 }
422
423
424 /**
425  * Get the current north velocity (units??).
426  */
427 double
428 FGBFI::getSpeedNorth ()
429 {
430   return current_aircraft.fdm_state->get_V_north();
431 }
432
433
434 /**
435  * Set the current north velocity (units??).
436  */
437 void
438 FGBFI::setSpeedNorth (double speed)
439 {
440   current_options.set_uBody(speed);
441   needReinit();
442 }
443
444
445 /**
446  * Get the current east velocity (units??).
447  */
448 double
449 FGBFI::getSpeedEast ()
450 {
451   return current_aircraft.fdm_state->get_V_east();
452 }
453
454
455 /**
456  * Set the current east velocity (units??).
457  */
458 void
459 FGBFI::setSpeedEast (double speed)
460 {
461   current_options.set_vBody(speed);
462   needReinit();
463 }
464
465
466 /**
467  * Get the current down velocity (units??).
468  */
469 double
470 FGBFI::getSpeedDown ()
471 {
472   return current_aircraft.fdm_state->get_V_down();
473 }
474
475
476 /**
477  * Set the current down velocity (units??).
478  */
479 void
480 FGBFI::setSpeedDown (double speed)
481 {
482   current_options.set_wBody(speed);
483   needReinit();
484 }
485
486
487 \f
488 ////////////////////////////////////////////////////////////////////////
489 // Controls
490 ////////////////////////////////////////////////////////////////////////
491
492
493 /**
494  * Get the throttle setting, from 0.0 (none) to 1.0 (full).
495  */
496 double
497 FGBFI::getThrottle ()
498 {
499                                 // FIXME: add throttle selector
500   return controls.get_throttle(0);
501 }
502
503
504 /**
505  * Set the throttle, from 0.0 (none) to 1.0 (full).
506  */
507 void
508 FGBFI::setThrottle (double throttle)
509 {
510                                 // FIXME: allow throttle selection
511                                 // FIXME: clamp?
512   controls.set_throttle(0, throttle);
513 }
514
515
516 /**
517  * Get the flaps setting, from 0.0 (none) to 1.0 (full).
518  */
519 double
520 FGBFI::getFlaps ()
521 {
522   return controls.get_flaps();
523 }
524
525
526 /**
527  * Set the flaps, from 0.0 (none) to 1.0 (full).
528  */
529 void
530 FGBFI::setFlaps (double flaps)
531 {
532                                 // FIXME: clamp?
533   controls.set_flaps(flaps);
534 }
535
536
537 /**
538  * Get the aileron, from -1.0 (left) to 1.0 (right).
539  */
540 double
541 FGBFI::getAileron ()
542 {
543   return controls.get_aileron();
544 }
545
546
547 /**
548  * Set the aileron, from -1.0 (left) to 1.0 (right).
549  */
550 void
551 FGBFI::setAileron (double aileron)
552 {
553                                 // FIXME: clamp?
554   controls.set_aileron(aileron);
555 }
556
557
558 /**
559  * Get the rudder setting, from -1.0 (left) to 1.0 (right).
560  */
561 double
562 FGBFI::getRudder ()
563 {
564   return controls.get_rudder();
565 }
566
567
568 /**
569  * Set the rudder, from -1.0 (left) to 1.0 (right).
570  */
571 void
572 FGBFI::setRudder (double rudder)
573 {
574                                 // FIXME: clamp?
575   controls.set_rudder(rudder);
576 }
577
578
579 /**
580  * Get the elevator setting, from -1.0 (down) to 1.0 (up).
581  */
582 double
583 FGBFI::getElevator ()
584 {
585   return controls.get_elevator();
586 }
587
588
589 /**
590  * Set the elevator, from -1.0 (down) to 1.0 (up).
591  */
592 void
593 FGBFI::setElevator (double elevator)
594 {
595                                 // FIXME: clamp?
596   controls.set_elevator(elevator);
597 }
598
599
600 /**
601  * Get the elevator trim, from -1.0 (down) to 1.0 (up).
602  */
603 double
604 FGBFI::getElevatorTrim ()
605 {
606   return controls.get_elevator_trim();
607 }
608
609
610 /**
611  * Set the elevator trim, from -1.0 (down) to 1.0 (up).
612  */
613 void
614 FGBFI::setElevatorTrim (double trim)
615 {
616                                 // FIXME: clamp?
617   controls.set_elevator_trim(trim);
618 }
619
620
621 /**
622  * Get the brake setting, from 0.0 (none) to 1.0 (full).
623  */
624 double
625 FGBFI::getBrake ()
626 {
627                                 // FIXME: add brake selector
628   return controls.get_brake(0);
629 }
630
631
632 /**
633  * Set the brake, from 0.0 (none) to 1.0 (full).
634  */
635 void
636 FGBFI::setBrake (double brake)
637 {
638                                 // FIXME: clamp?
639                                 // FIXME: allow brake selection
640   controls.set_brake(0, brake);
641 }
642
643
644 \f
645 ////////////////////////////////////////////////////////////////////////
646 // Autopilot
647 ////////////////////////////////////////////////////////////////////////
648
649
650 /**
651  * Get the autopilot altitude lock (true=on).
652  */
653 bool
654 FGBFI::getAPAltitudeLock ()
655 {
656   return fgAPAltitudeEnabled();
657 }
658
659
660 /**
661  * Set the autopilot altitude lock (true=on).
662  */
663 void
664 FGBFI::setAPAltitudeLock (bool lock)
665 {
666   APDataGlobal->altitude_hold = lock;
667 }
668
669
670 /**
671  * Get the autopilot target altitude in feet.
672  */
673 double
674 FGBFI::getAPAltitude ()
675 {
676   return fgAPget_TargetAltitude() * METER_TO_FEET;
677 }
678
679
680 /**
681  * Set the autopilot target altitude in feet.
682  */
683 void
684 FGBFI::setAPAltitude (double altitude)
685 {
686   fgAPAltitudeSet(altitude);
687 }
688
689
690 /**
691  * Get the autopilot heading lock (true=on).
692  */
693 bool
694 FGBFI::getAPHeadingLock ()
695 {
696   return fgAPHeadingEnabled();
697 }
698
699
700 /**
701  * Set the autopilot heading lock (true=on).
702  */
703 void
704 FGBFI::setAPHeadingLock (bool lock)
705 {
706   APDataGlobal->heading_hold = lock;
707 }
708
709
710 /**
711  * Get the autopilot target heading in degrees.
712  */
713 double
714 FGBFI::getAPHeading ()
715 {
716   return fgAPget_TargetHeading();
717 }
718
719
720 /**
721  * Set the autopilot target heading in degrees.
722  */
723 void
724 FGBFI::setAPHeading (double heading)
725 {
726   fgAPHeadingSet(heading);
727 }
728
729
730 \f
731 ////////////////////////////////////////////////////////////////////////
732 // GPS
733 ////////////////////////////////////////////////////////////////////////
734
735
736 /**
737  * Get the autopilot GPS lock (true=on).
738  */
739 bool
740 FGBFI::getGPSLock ()
741 {
742   return fgAPWayPointEnabled();
743 }
744
745
746 /**
747  * Set the autopilot GPS lock (true=on).
748  */
749 void
750 FGBFI::setGPSLock (bool lock)
751 {
752   APDataGlobal->waypoint_hold = lock;
753 }
754
755
756 /**
757  * Get the GPS target airport code.
758  */
759 const string
760 FGBFI::getTargetAirport ()
761 {
762   return current_options.get_airport_id();
763 }
764
765
766 /**
767  * Set the GPS target airport code.
768  */
769 void
770 FGBFI::setTargetAirport (const string &airportId)
771 {
772   current_options.set_airport_id(airportId);
773 }
774
775
776 /**
777  * Get the GPS target latitude in degrees (negative for south).
778  */
779 double
780 FGBFI::getGPSTargetLatitude ()
781 {
782   return fgAPget_TargetLatitude();
783 }
784
785
786 /**
787  * Set the GPS target latitude in degrees (negative for south).
788  */
789 void
790 FGBFI::setGPSTargetLatitude (double latitude)
791 {
792   APDataGlobal->TargetLatitude = latitude;
793 }
794
795
796 /**
797  * Get the GPS target longitude in degrees (negative for west).
798  */
799 double
800 FGBFI::getGPSTargetLongitude ()
801 {
802   return fgAPget_TargetLongitude();
803 }
804
805
806 /**
807  * Set the GPS target longitude in degrees (negative for west).
808  */
809 void
810 FGBFI::setGPSTargetLongitude (double longitude)
811 {
812   APDataGlobal->TargetLongitude = longitude;
813 }
814
815
816 \f
817 ////////////////////////////////////////////////////////////////////////
818 // Weather
819 ////////////////////////////////////////////////////////////////////////
820
821
822 /**
823  * Get the current visible (units??).
824  */
825 double
826 FGBFI::getVisibility ()
827 {
828 #ifndef FG_OLD_WEATHER
829   return WeatherDatabase->getWeatherVisibility();
830 #else
831   return current_weather.get_visibility();
832 #endif
833 }
834
835
836 /**
837  * Set the current visibility (units??).
838  */
839 void
840 FGBFI::setVisibility (double visibility)
841 {
842 #ifndef FG_OLD_WEATHER
843   WeatherDatabase->setWeatherVisibility(visibility);
844 #else
845   current_weather.set_visibility(visibility);
846 #endif
847 }
848
849
850 // end of bfi.cxx