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