1 // autopilot.cxx -- autopilot subsystem
3 // Written by Jeff Goeke-Smith, started April 1998.
5 // Copyright (C) 1998 Jeff Goeke-Smith, jgoeke@voyager.net
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.
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.
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.
31 #include <Scenery/scenery.hxx>
33 #include "autopilot.hxx"
35 #include <Include/fg_constants.h>
36 #include <Debug/logstream.hxx>
37 #include <Airports/simple.hxx>
39 #include <Main/fg_init.hxx>
40 #include <Main/options.hxx>
41 #include <Time/fg_time.hxx>
44 #define mySlider puSlider
46 /// These statics will eventually go into the class
47 /// they are just here while I am experimenting -- NHV :-)
48 // AutoPilot Gain Adjuster members
49 static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
50 static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
51 static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
52 static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
54 static float MaxRollValue; // 0.1 -> 1.0
55 static float RollOutValue;
56 static float MaxAileronValue;
57 static float RollOutSmoothValue;
59 static float TmpMaxRollValue; // for cancel operation
60 static float TmpRollOutValue;
61 static float TmpMaxAileronValue;
62 static float TmpRollOutSmoothValue;
64 static puDialogBox *APAdjustDialog;
65 static puFrame *APAdjustFrame;
66 static puText *APAdjustDialogMessage;
67 static puFont APAdjustLegendFont;
68 static puFont APAdjustLabelFont;
70 static puOneShot *APAdjustOkButton;
71 static puOneShot *APAdjustResetButton;
72 static puOneShot *APAdjustCancelButton;
74 //static puButton *APAdjustDragButton;
76 static puText *APAdjustMaxRollTitle;
77 static puText *APAdjustRollOutTitle;
78 static puText *APAdjustMaxAileronTitle;
79 static puText *APAdjustRollOutSmoothTitle;
81 static puText *APAdjustMaxAileronText;
82 static puText *APAdjustMaxRollText;
83 static puText *APAdjustRollOutText;
84 static puText *APAdjustRollOutSmoothText;
86 static mySlider *APAdjustHS0;
87 static mySlider *APAdjustHS1;
88 static mySlider *APAdjustHS2;
89 static mySlider *APAdjustHS3;
91 static char SliderText[ 4 ][ 8 ];
93 ///////// AutoPilot New Heading Dialog
95 static puDialogBox *ApHeadingDialog;
96 static puFrame *ApHeadingDialogFrame;
97 static puText *ApHeadingDialogMessage;
98 static puInput *ApHeadingDialogInput;
99 static puOneShot *ApHeadingDialogOkButton;
100 static puOneShot *ApHeadingDialogCancelButton;
103 ///////// AutoPilot New Altitude Dialog
105 static puDialogBox *ApAltitudeDialog = 0;
106 static puFrame *ApAltitudeDialogFrame = 0;
107 static puText *ApAltitudeDialogMessage = 0;
108 static puInput *ApAltitudeDialogInput = 0;
110 static puOneShot *ApAltitudeDialogOkButton = 0;
111 static puOneShot *ApAltitudeDialogCancelButton = 0;
114 /// The beginnings of Lock AutoPilot to target location :-)
115 // Needs cleaning up but works
116 // These statics should disapear when this is a class
117 static puDialogBox *TgtAptDialog = 0;
118 static puFrame *TgtAptDialogFrame = 0;
119 static puText *TgtAptDialogMessage = 0;
120 static puInput *TgtAptDialogInput = 0;
122 static char NewTgtAirportId[16];
123 static char NewTgtAirportLabel[] = "Enter New TgtAirport ID";
125 static puOneShot *TgtAptDialogOkButton = 0;
126 static puOneShot *TgtAptDialogCancelButton = 0;
127 static puOneShot *TgtAptDialogResetButton = 0;
130 // global variable holding the AP info
131 // I want this gone. Data should be in aircraft structure
132 fgAPDataPtr APDataGlobal;
134 // Local Prototype section
135 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 );
136 static double NormalizeDegrees( double Input );
137 // End Local ProtoTypes
139 extern char *coord_format_lat(float);
140 extern char *coord_format_lon(float);
143 // The below routines were copied right from hud.c ( I hate reinventing
144 // the wheel more than necessary)
146 // The following routines obtain information concerntin the aircraft's
147 // current state and return it to calling instrument display routines.
148 // They should eventually be member functions of the aircraft.
150 static void get_control_values( void ) {
152 APData = APDataGlobal;
153 APData->old_aileron = controls.get_aileron();
154 APData->old_elevator = controls.get_elevator();
155 APData->old_elevator_trim = controls.get_elevator_trim();
156 APData->old_rudder = controls.get_rudder();
159 static void MakeTargetHeadingStr( fgAPDataPtr APData, double bearing ) {
162 else if(bearing > 360. )
164 sprintf(APData->TargetHeadingStr, "APHeading %6.1f", bearing);
167 static inline void MakeTargetDistanceStr( fgAPDataPtr APData, double distance ) {
168 sprintf(APData->TargetDistanceStr, "APDistance %.2f NM", distance*METER_TO_NM);
171 static inline void MakeTargetAltitudeStr( fgAPDataPtr APData, double altitude ) {
172 sprintf(APData->TargetAltitudeStr, "APAltitude %6.0f", altitude);
175 static inline void MakeTargetLatLonStr( fgAPDataPtr APData, double lat, double lon ) {
177 tmp = APData->TargetLatitude;
178 sprintf( APData->TargetLatitudeStr , "%s", coord_format_lat(tmp) );
179 tmp = APData->TargetLongitude;
180 sprintf( APData->TargetLongitudeStr, "%s", coord_format_lon(tmp) );
182 sprintf(APData->TargetLatLonStr, "%s %s",
183 APData->TargetLatitudeStr,
184 APData->TargetLongitudeStr );
187 static inline double get_speed( void ) {
188 return( current_aircraft.fdm_state->get_V_equiv_kts() );
191 static inline double get_aoa( void ) {
192 return( current_aircraft.fdm_state->get_Gamma_vert_rad() * RAD_TO_DEG );
195 static inline double fgAPget_latitude( void ) {
196 return( current_aircraft.fdm_state->get_Latitude() * RAD_TO_DEG );
199 static inline double fgAPget_longitude( void ) {
200 return( current_aircraft.fdm_state->get_Longitude() * RAD_TO_DEG );
203 static inline double fgAPget_roll( void ) {
204 return( current_aircraft.fdm_state->get_Phi() * RAD_TO_DEG );
207 static inline double get_pitch( void ) {
208 return( current_aircraft.fdm_state->get_Theta() );
211 double fgAPget_heading( void ) {
212 return( current_aircraft.fdm_state->get_Psi() * RAD_TO_DEG );
215 static inline double fgAPget_altitude( void ) {
216 return( current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER );
219 static inline double fgAPget_climb( void ) {
220 // return in meters per minute
221 return( current_aircraft.fdm_state->get_Climb_Rate() * FEET_TO_METER * 60 );
224 static inline double get_sideslip( void ) {
225 return( current_aircraft.fdm_state->get_Beta() );
228 static inline double fgAPget_agl( void ) {
231 agl = current_aircraft.fdm_state->get_Altitude() * FEET_TO_METER
237 // End of copied section. ( thanks for the wheel :-)
238 double fgAPget_TargetLatitude( void ) {
239 fgAPDataPtr APData = APDataGlobal;
240 return APData->TargetLatitude;
243 double fgAPget_TargetLongitude( void ) {
244 fgAPDataPtr APData = APDataGlobal;
245 return APData->TargetLongitude;
248 double fgAPget_TargetHeading( void ) {
249 fgAPDataPtr APData = APDataGlobal;
250 return APData->TargetHeading;
253 double fgAPget_TargetDistance( void ) {
254 fgAPDataPtr APData = APDataGlobal;
255 return APData->TargetDistance;
258 double fgAPget_TargetAltitude( void ) {
259 fgAPDataPtr APData = APDataGlobal;
260 return APData->TargetAltitude;
263 char *fgAPget_TargetLatitudeStr( void ) {
264 fgAPDataPtr APData = APDataGlobal;
265 return APData->TargetLatitudeStr;
268 char *fgAPget_TargetLongitudeStr( void ) {
269 fgAPDataPtr APData = APDataGlobal;
270 return APData->TargetLongitudeStr;
273 char *fgAPget_TargetDistanceStr( void ) {
274 fgAPDataPtr APData = APDataGlobal;
275 return APData->TargetDistanceStr;
278 char *fgAPget_TargetHeadingStr( void ) {
279 fgAPDataPtr APData = APDataGlobal;
280 return APData->TargetHeadingStr;
283 char *fgAPget_TargetAltitudeStr( void ) {
284 fgAPDataPtr APData = APDataGlobal;
285 return APData->TargetAltitudeStr;
288 char *fgAPget_TargetLatLonStr( void ) {
289 fgAPDataPtr APData = APDataGlobal;
290 return APData->TargetLatLonStr;
293 bool fgAPWayPointEnabled( void )
297 APData = APDataGlobal;
299 // heading hold enabled?
300 return APData->waypoint_hold;
304 bool fgAPHeadingEnabled( void )
308 APData = APDataGlobal;
310 // heading hold enabled?
311 return APData->heading_hold;
314 bool fgAPAltitudeEnabled( void )
318 APData = APDataGlobal;
320 // altitude hold or terrain follow enabled?
321 return APData->altitude_hold;
324 bool fgAPTerrainFollowEnabled( void )
328 APData = APDataGlobal;
330 // altitude hold or terrain follow enabled?
331 return APData->terrain_follow ;
334 bool fgAPAutoThrottleEnabled( void )
338 APData = APDataGlobal;
340 // autothrottle enabled?
341 return APData->auto_throttle;
344 void fgAPAltitudeAdjust( double inc )
346 // Remove at a later date
347 fgAPDataPtr APData = APDataGlobal;
350 double target_alt, target_agl;
352 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
353 target_alt = APData->TargetAltitude * METER_TO_FEET;
354 target_agl = APData->TargetAGL * METER_TO_FEET;
356 target_alt = APData->TargetAltitude;
357 target_agl = APData->TargetAGL;
360 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
361 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
363 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
364 target_alt *= FEET_TO_METER;
365 target_agl *= FEET_TO_METER;
368 APData->TargetAltitude = target_alt;
369 APData->TargetAGL = target_agl;
371 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
372 target_alt *= METER_TO_FEET;
373 ApAltitudeDialogInput->setValue((float)target_alt);
374 MakeTargetAltitudeStr( APData, target_alt);
376 get_control_values();
379 void fgAPAltitudeSet( double new_altitude ) {
380 // Remove at a later date
381 fgAPDataPtr APData = APDataGlobal;
383 double target_alt = new_altitude;
385 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
386 target_alt = new_altitude * FEET_TO_METER;
388 if( target_alt < scenery.cur_elev )
389 target_alt = scenery.cur_elev;
391 APData->TargetAltitude = target_alt;
393 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
394 target_alt *= METER_TO_FEET;
395 ApAltitudeDialogInput->setValue((float)target_alt);
396 MakeTargetAltitudeStr( APData, target_alt);
398 get_control_values();
401 void fgAPHeadingAdjust( double inc ) {
402 fgAPDataPtr APData = APDataGlobal;
404 if ( APData->waypoint_hold )
405 APData->waypoint_hold = false;
407 double target = ( int ) ( APData->TargetHeading / inc ) * inc + inc;
409 APData->TargetHeading = NormalizeDegrees( target );
410 // following cast needed ambiguous plib
411 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
412 MakeTargetHeadingStr( APData, APData->TargetHeading );
413 get_control_values();
416 void fgAPHeadingSet( double new_heading ) {
417 fgAPDataPtr APData = APDataGlobal;
419 if ( APData->waypoint_hold )
420 APData->waypoint_hold = false;
422 new_heading = NormalizeDegrees( new_heading );
423 APData->TargetHeading = new_heading;
424 // following cast needed ambiguous plib
425 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
426 MakeTargetHeadingStr( APData, APData->TargetHeading );
427 get_control_values();
430 void fgAPAutoThrottleAdjust( double inc ) {
431 fgAPDataPtr APData = APDataGlobal;
433 double target = ( int ) ( APData->TargetSpeed / inc ) * inc + inc;
435 APData->TargetSpeed = target;
438 // THIS NEEDS IMPROVEMENT !!!!!!!!!!!!!
439 static int scan_number(char *s, double *new_value)
444 char *WordBufPtr = WordBuf;
449 *WordBufPtr++ = *cptr++;
451 while (isdigit(*cptr) ) {
452 *WordBufPtr++ = *cptr++;
456 *WordBufPtr++ = *cptr++; // put the '.' into the string
457 while (isdigit(*cptr)) {
458 *WordBufPtr++ = *cptr++;
463 sscanf(WordBuf, "%lf", new_value);
470 void ApHeadingDialog_Cancel(puObject *)
472 ApHeadingDialogInput->rejectInput();
473 FG_POP_PUI_DIALOG( ApHeadingDialog );
476 void ApHeadingDialog_OK (puObject *me)
481 ApHeadingDialogInput -> getValue( &c );
485 if( scan_number( c, &NewHeading ) )
487 if(!fgAPHeadingEnabled())
489 fgAPHeadingSet( NewHeading );
493 s += " is not a valid number.";
496 ApHeadingDialog_Cancel(me);
497 if( error ) mkDialog(s.c_str());
500 void NewHeading(puObject *cb)
502 // string ApHeadingLabel( "Enter New Heading" );
503 // ApHeadingDialogMessage -> setLabel(ApHeadingLabel.c_str());
504 ApHeadingDialogInput -> acceptInput();
505 FG_PUSH_PUI_DIALOG( ApHeadingDialog );
508 void NewHeadingInit(void)
510 // printf("NewHeadingInit\n");
511 char NewHeadingLabel[] = "Enter New Heading";
514 float heading = fgAPget_heading();
516 (puGetStringWidth( puGetDefaultLabelFont(), NewHeadingLabel ) /2 );
518 ApHeadingDialog = new puDialogBox (150, 50);
520 ApHeadingDialogFrame = new puFrame (0, 0, 260, 150);
522 ApHeadingDialogMessage = new puText (len, 110);
523 ApHeadingDialogMessage -> setDefaultValue (NewHeadingLabel);
524 ApHeadingDialogMessage -> getDefaultValue (&s);
525 ApHeadingDialogMessage -> setLabel (s);
527 ApHeadingDialogInput = new puInput ( 50, 70, 210, 100 );
528 ApHeadingDialogInput -> setValue ( heading );
530 ApHeadingDialogOkButton = new puOneShot (50, 10, 110, 50);
531 ApHeadingDialogOkButton -> setLegend (gui_msg_OK);
532 ApHeadingDialogOkButton -> makeReturnDefault (TRUE);
533 ApHeadingDialogOkButton -> setCallback (ApHeadingDialog_OK);
535 ApHeadingDialogCancelButton = new puOneShot (140, 10, 210, 50);
536 ApHeadingDialogCancelButton -> setLegend (gui_msg_CANCEL);
537 ApHeadingDialogCancelButton -> setCallback (ApHeadingDialog_Cancel);
540 FG_FINALIZE_PUI_DIALOG( ApHeadingDialog );
543 void ApAltitudeDialog_Cancel(puObject *)
545 ApAltitudeDialogInput -> rejectInput();
546 FG_POP_PUI_DIALOG( ApAltitudeDialog );
549 void ApAltitudeDialog_OK (puObject *me)
554 ApAltitudeDialogInput->getValue( &c );
558 if( scan_number( c, &NewAltitude) )
560 if(!(fgAPAltitudeEnabled()))
561 fgAPToggleAltitude();
562 fgAPAltitudeSet( NewAltitude );
566 s += " is not a valid number.";
569 ApAltitudeDialog_Cancel(me);
570 if( error ) mkDialog(s.c_str());
571 get_control_values();
574 void NewAltitude(puObject *cb)
576 ApAltitudeDialogInput -> acceptInput();
577 FG_PUSH_PUI_DIALOG( ApAltitudeDialog );
580 void NewAltitudeInit(void)
582 // printf("NewAltitudeInit\n");
583 char NewAltitudeLabel[] = "Enter New Altitude";
586 float alt = current_aircraft.fdm_state->get_Altitude();
588 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_METERS) {
589 alt *= FEET_TO_METER;
593 (puGetStringWidth( puGetDefaultLabelFont(), NewAltitudeLabel )/2);
595 // ApAltitudeDialog = new puDialogBox (150, 50);
596 ApAltitudeDialog = new puDialogBox (150, 200);
598 ApAltitudeDialogFrame = new puFrame (0, 0, 260, 150);
599 ApAltitudeDialogMessage = new puText (len, 110);
600 ApAltitudeDialogMessage -> setDefaultValue (NewAltitudeLabel);
601 ApAltitudeDialogMessage -> getDefaultValue (&s);
602 ApAltitudeDialogMessage -> setLabel (s);
604 ApAltitudeDialogInput = new puInput ( 50, 70, 210, 100 );
605 ApAltitudeDialogInput -> setValue ( alt );
606 // Uncomment the next line to have input active on startup
607 // ApAltitudeDialogInput -> acceptInput ( );
608 // cursor at begining or end of line ?
611 // ApAltitudeDialogInput -> setCursor ( len );
612 // ApAltitudeDialogInput -> setSelectRegion ( 5, 9 );
614 ApAltitudeDialogOkButton = new puOneShot (50, 10, 110, 50);
615 ApAltitudeDialogOkButton -> setLegend (gui_msg_OK);
616 ApAltitudeDialogOkButton -> makeReturnDefault (TRUE);
617 ApAltitudeDialogOkButton -> setCallback (ApAltitudeDialog_OK);
619 ApAltitudeDialogCancelButton = new puOneShot (140, 10, 210, 50);
620 ApAltitudeDialogCancelButton -> setLegend (gui_msg_CANCEL);
621 ApAltitudeDialogCancelButton -> setCallback (ApAltitudeDialog_Cancel);
624 FG_FINALIZE_PUI_DIALOG( ApAltitudeDialog );
627 /////// simple AutoPilot GAIN / LIMITS ADJUSTER
629 #define fgAP_CLAMP(val,min,max) \
630 ( (val) = (val) > (max) ? (max) : (val) < (min) ? (min) : (val) )
632 static void maxroll_adj( puObject *hs ) {
634 fgAPDataPtr APData = APDataGlobal;
636 hs-> getValue ( &val ) ;
637 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
638 // printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
639 APData-> MaxRoll = MaxRollAdjust * val;
640 sprintf( SliderText[ 0 ], "%05.2f", APData->MaxRoll );
641 APAdjustMaxRollText -> setLabel ( SliderText[ 0 ] ) ;
644 static void rollout_adj( puObject *hs ) {
646 fgAPDataPtr APData = APDataGlobal;
648 hs-> getValue ( &val ) ;
649 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
650 // printf ( "rollout_adj( %p ) %f %f\n", hs, val, RollOutAdjust * val ) ;
651 APData-> RollOut = RollOutAdjust * val;
652 sprintf( SliderText[ 1 ], "%05.2f", APData->RollOut );
653 APAdjustRollOutText -> setLabel ( SliderText[ 1 ] );
656 static void maxaileron_adj( puObject *hs ) {
659 APData = APDataGlobal;
661 hs-> getValue ( &val ) ;
662 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
663 // printf ( "maxaileron_adj( %p ) %f %f\n", hs, val, MaxAileronAdjust * val ) ;
664 APData-> MaxAileron = MaxAileronAdjust * val;
665 sprintf( SliderText[ 3 ], "%05.2f", APData->MaxAileron );
666 APAdjustMaxAileronText -> setLabel ( SliderText[ 3 ] );
669 static void rolloutsmooth_adj( puObject *hs ) {
671 fgAPDataPtr APData = APDataGlobal;
673 hs -> getValue ( &val ) ;
674 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
675 // printf ( "rolloutsmooth_adj( %p ) %f %f\n", hs, val, RollOutSmoothAdjust * val ) ;
676 APData->RollOutSmooth = RollOutSmoothAdjust * val;
677 sprintf( SliderText[ 2 ], "%5.2f", APData-> RollOutSmooth );
678 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
682 static void goAwayAPAdjust (puObject *)
684 FG_POP_PUI_DIALOG( APAdjustDialog );
687 void cancelAPAdjust( puObject *self ) {
688 fgAPDataPtr APData = APDataGlobal;
690 APData-> MaxRoll = TmpMaxRollValue;
691 APData-> RollOut = TmpRollOutValue;
692 APData-> MaxAileron = TmpMaxAileronValue;
693 APData-> RollOutSmooth = TmpRollOutSmoothValue;
695 goAwayAPAdjust(self);
698 void resetAPAdjust( puObject *self ) {
699 fgAPDataPtr APData = APDataGlobal;
701 APData-> MaxRoll = MaxRollAdjust / 2;
702 APData-> RollOut = RollOutAdjust / 2;
703 APData-> MaxAileron = MaxAileronAdjust / 2;
704 APData-> RollOutSmooth = RollOutSmoothAdjust / 2;
706 FG_POP_PUI_DIALOG( APAdjustDialog );
711 void fgAPAdjust( puObject * ) {
712 fgAPDataPtr APData = APDataGlobal;
714 TmpMaxRollValue = APData-> MaxRoll;
715 TmpRollOutValue = APData-> RollOut;
716 TmpMaxAileronValue = APData-> MaxAileron;
717 TmpRollOutSmoothValue = APData-> RollOutSmooth;
719 MaxRollValue = APData-> MaxRoll / MaxRollAdjust;
720 RollOutValue = APData-> RollOut / RollOutAdjust;
721 MaxAileronValue = APData-> MaxAileron / MaxAileronAdjust;
722 RollOutSmoothValue = APData-> RollOutSmooth / RollOutSmoothAdjust;
724 APAdjustHS0-> setValue ( MaxRollValue ) ;
725 APAdjustHS1-> setValue ( RollOutValue ) ;
726 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
727 APAdjustHS3-> setValue ( MaxAileronValue ) ;
729 FG_PUSH_PUI_DIALOG( APAdjustDialog );
732 // Done once at system initialization
733 void fgAPAdjustInit( void ) {
735 // printf("fgAPAdjustInit\n");
736 #define HORIZONTAL FALSE
740 int DialogWidth = 230;
742 char Label[] = "AutoPilot Adjust";
745 fgAPDataPtr APData = APDataGlobal;
747 int labelX = (DialogWidth / 2) -
748 (puGetStringWidth( puGetDefaultLabelFont(), Label ) / 2);
749 labelX -= 30; // KLUDGEY
754 int slider_width = 210;
755 int slider_title_x = 15;
756 int slider_value_x = 160;
757 float slider_delta = 0.1f;
759 TmpMaxRollValue = APData-> MaxRoll;
760 TmpRollOutValue = APData-> RollOut;
761 TmpMaxAileronValue = APData-> MaxAileron;
762 TmpRollOutSmoothValue = APData-> RollOutSmooth;
764 MaxRollValue = APData-> MaxRoll / MaxRollAdjust;
765 RollOutValue = APData-> RollOut / RollOutAdjust;
766 MaxAileronValue = APData-> MaxAileron / MaxAileronAdjust;
767 RollOutSmoothValue = APData-> RollOutSmooth / RollOutSmoothAdjust;
769 puGetDefaultFonts ( &APAdjustLegendFont, &APAdjustLabelFont );
770 APAdjustDialog = new puDialogBox ( DialogX, DialogY ); {
771 int horiz_slider_height = puGetStringHeight (APAdjustLabelFont) +
772 puGetStringDescender (APAdjustLabelFont) +
773 PUSTR_TGAP + PUSTR_BGAP + 5;
775 APAdjustFrame = new puFrame ( 0, 0,
776 DialogWidth, 85 + nSliders * horiz_slider_height );
778 APAdjustDialogMessage = new puText ( labelX, 52 + nSliders * horiz_slider_height );
779 APAdjustDialogMessage -> setDefaultValue ( Label );
780 APAdjustDialogMessage -> getDefaultValue ( &s );
781 APAdjustDialogMessage -> setLabel ( s );
783 APAdjustHS0 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
784 APAdjustHS0-> setDelta ( slider_delta ) ;
785 APAdjustHS0-> setValue ( MaxRollValue ) ;
786 APAdjustHS0-> setCBMode ( PUSLIDER_DELTA ) ;
787 APAdjustHS0-> setCallback ( maxroll_adj ) ;
789 sprintf( SliderText[ 0 ], "%05.2f", APData->MaxRoll );
790 APAdjustMaxRollTitle = new puText ( slider_title_x, slider_y ) ;
791 APAdjustMaxRollTitle-> setDefaultValue ( "MaxRoll" ) ;
792 APAdjustMaxRollTitle-> getDefaultValue ( &s ) ;
793 APAdjustMaxRollTitle-> setLabel ( s ) ;
794 APAdjustMaxRollText = new puText ( slider_value_x, slider_y ) ;
795 APAdjustMaxRollText-> setLabel ( SliderText[ 0 ] ) ;
797 slider_y += horiz_slider_height;
799 APAdjustHS1 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
800 APAdjustHS1-> setDelta ( slider_delta ) ;
801 APAdjustHS1-> setValue ( RollOutValue ) ;
802 APAdjustHS1-> setCBMode ( PUSLIDER_DELTA ) ;
803 APAdjustHS1-> setCallback ( rollout_adj ) ;
805 sprintf( SliderText[ 1 ], "%05.2f", APData->RollOut );
806 APAdjustRollOutTitle = new puText ( slider_title_x, slider_y ) ;
807 APAdjustRollOutTitle-> setDefaultValue ( "AdjustRollOut" ) ;
808 APAdjustRollOutTitle-> getDefaultValue ( &s ) ;
809 APAdjustRollOutTitle-> setLabel ( s ) ;
810 APAdjustRollOutText = new puText ( slider_value_x, slider_y ) ;
811 APAdjustRollOutText-> setLabel ( SliderText[ 1 ] );
813 slider_y += horiz_slider_height;
815 APAdjustHS2 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
816 APAdjustHS2-> setDelta ( slider_delta ) ;
817 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
818 APAdjustHS2-> setCBMode ( PUSLIDER_DELTA ) ;
819 APAdjustHS2-> setCallback ( rolloutsmooth_adj ) ;
821 sprintf( SliderText[ 2 ], "%5.2f", APData->RollOutSmooth );
822 APAdjustRollOutSmoothTitle = new puText ( slider_title_x, slider_y ) ;
823 APAdjustRollOutSmoothTitle-> setDefaultValue ( "RollOutSmooth" ) ;
824 APAdjustRollOutSmoothTitle-> getDefaultValue ( &s ) ;
825 APAdjustRollOutSmoothTitle-> setLabel ( s ) ;
826 APAdjustRollOutSmoothText = new puText ( slider_value_x, slider_y ) ;
827 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
829 slider_y += horiz_slider_height;
831 APAdjustHS3 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
832 APAdjustHS3-> setDelta ( slider_delta ) ;
833 APAdjustHS3-> setValue ( MaxAileronValue ) ;
834 APAdjustHS3-> setCBMode ( PUSLIDER_DELTA ) ;
835 APAdjustHS3-> setCallback ( maxaileron_adj ) ;
837 sprintf( SliderText[ 3 ], "%05.2f", APData->MaxAileron );
838 APAdjustMaxAileronTitle = new puText ( slider_title_x, slider_y ) ;
839 APAdjustMaxAileronTitle-> setDefaultValue ( "MaxAileron" ) ;
840 APAdjustMaxAileronTitle-> getDefaultValue ( &s ) ;
841 APAdjustMaxAileronTitle-> setLabel ( s ) ;
842 APAdjustMaxAileronText = new puText ( slider_value_x, slider_y ) ;
843 APAdjustMaxAileronText-> setLabel ( SliderText[ 3 ] );
845 APAdjustOkButton = new puOneShot ( 10, 10, 60, 50 );
846 APAdjustOkButton-> setLegend ( gui_msg_OK );
847 APAdjustOkButton-> makeReturnDefault ( TRUE );
848 APAdjustOkButton-> setCallback ( goAwayAPAdjust );
850 APAdjustCancelButton = new puOneShot ( 70, 10, 150, 50 );
851 APAdjustCancelButton-> setLegend ( gui_msg_CANCEL );
852 APAdjustCancelButton-> setCallback ( cancelAPAdjust );
854 APAdjustResetButton = new puOneShot ( 160, 10, 220, 50 );
855 APAdjustResetButton-> setLegend ( gui_msg_RESET );
856 APAdjustResetButton-> setCallback ( resetAPAdjust );
858 FG_FINALIZE_PUI_DIALOG( APAdjustDialog );
863 // Simple Dialog to input Target Airport
864 void TgtAptDialog_Cancel(puObject *)
866 FG_POP_PUI_DIALOG( TgtAptDialog );
869 void TgtAptDialog_OK (puObject *)
872 APData = APDataGlobal;
875 // FGTime *t = FGTime::cur_time_params;
876 // int PauseMode = t->getPause();
878 // t->togglePauseMode();
881 TgtAptDialogInput->getValue(&s);
884 TgtAptDialog_Cancel( NULL );
886 if ( TgtAptId.length() ) {
887 // set initial position from TgtAirport id
892 FG_LOG( FG_GENERAL, FG_INFO,
893 "Attempting to set starting position from airport code "
896 airports.load("apt_simple");
897 if ( airports.search( TgtAptId, &a ) )
899 double course, reverse, distance;
900 // fgAPset_tgt_airport_id( TgtAptId.c_str() );
901 current_options.set_airport_id( TgtAptId.c_str() );
902 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
904 APData->TargetLatitude = a.latitude; // * DEG_TO_RAD;
905 APData->TargetLongitude = a.longitude; // * DEG_TO_RAD;
906 MakeTargetLatLonStr( APData,
907 APData->TargetLatitude,
908 APData->TargetLongitude);
910 APData->old_lat = fgAPget_latitude();
911 APData->old_lon = fgAPget_longitude();
913 // need to test for iter
914 if( ! geo_inverse_wgs_84( fgAPget_altitude(),
917 APData->TargetLatitude,
918 APData->TargetLongitude,
922 APData->TargetHeading = course;
923 MakeTargetHeadingStr( APData, APData->TargetHeading );
924 APData->TargetDistance = distance;
925 MakeTargetDistanceStr( APData, distance );
926 // This changes the AutoPilot Heading
927 // following cast needed
928 ApHeadingDialogInput->setValue ((float)APData->TargetHeading );
930 APData->waypoint_hold = true ;
931 APData->heading_hold = true;
934 TgtAptId += " not in database.";
935 mkDialog(TgtAptId.c_str());
938 get_control_values();
939 // if( PauseMode != t->getPause() )
940 // t->togglePauseMode();
943 void TgtAptDialog_Reset(puObject *)
945 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
946 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
947 TgtAptDialogInput->setValue ( NewTgtAirportId );
948 TgtAptDialogInput->setCursor( 0 ) ;
951 void NewTgtAirport(puObject *cb)
953 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
954 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
955 TgtAptDialogInput->setValue( NewTgtAirportId );
957 FG_PUSH_PUI_DIALOG( TgtAptDialog );
960 void NewTgtAirportInit(void)
962 FG_LOG( FG_AUTOPILOT, FG_INFO, " enter NewTgtAirportInit()" );
963 // fgAPset_tgt_airport_id( current_options.get_airport_id() );
964 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
965 FG_LOG( FG_AUTOPILOT, FG_INFO, " NewTgtAirportId " << NewTgtAirportId );
966 // printf(" NewTgtAirportId %s\n", NewTgtAirportId);
967 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
968 NewTgtAirportLabel ) / 2;
970 TgtAptDialog = new puDialogBox (150, 50);
972 TgtAptDialogFrame = new puFrame (0,0,350, 150);
973 TgtAptDialogMessage = new puText (len, 110);
974 TgtAptDialogMessage -> setLabel (NewTgtAirportLabel);
976 TgtAptDialogInput = new puInput (50, 70, 300, 100);
977 TgtAptDialogInput -> setValue (NewTgtAirportId);
978 TgtAptDialogInput -> acceptInput();
980 TgtAptDialogOkButton = new puOneShot (50, 10, 110, 50);
981 TgtAptDialogOkButton -> setLegend (gui_msg_OK);
982 TgtAptDialogOkButton -> setCallback (TgtAptDialog_OK);
983 TgtAptDialogOkButton -> makeReturnDefault(TRUE);
985 TgtAptDialogCancelButton = new puOneShot (140, 10, 210, 50);
986 TgtAptDialogCancelButton -> setLegend (gui_msg_CANCEL);
987 TgtAptDialogCancelButton -> setCallback (TgtAptDialog_Cancel);
989 TgtAptDialogResetButton = new puOneShot (240, 10, 300, 50);
990 TgtAptDialogResetButton -> setLegend (gui_msg_RESET);
991 TgtAptDialogResetButton -> setCallback (TgtAptDialog_Reset);
993 FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
994 printf("leave NewTgtAirportInit()");
998 // Finally actual guts of AutoPilot
999 void fgAPInit( fgAIRCRAFT *current_aircraft ) {
1002 FG_LOG( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem" );
1004 APData = ( fgAPDataPtr ) calloc( sizeof( fgAPData ), 1 );
1006 if ( APData == NULL ) {
1007 // I couldn't get the mem. Dying
1008 FG_LOG( FG_AUTOPILOT, FG_ALERT, "No ram for Autopilot. Dying." );
1012 FG_LOG( FG_AUTOPILOT, FG_INFO, " Autopilot allocated " );
1014 APData->waypoint_hold = false ; // turn the target hold off
1015 APData->heading_hold = false ; // turn the heading hold off
1016 APData->altitude_hold = false ; // turn the altitude hold off
1018 // Initialize target location to startup location
1019 // FG_LOG( FG_AUTOPILOT, FG_INFO, " Autopilot setting startup location" );
1021 APData->TargetLatitude = fgAPget_latitude();
1023 APData->TargetLongitude = fgAPget_longitude();
1025 // FG_LOG( FG_AUTOPILOT, FG_INFO, " Autopilot setting TargetLatitudeStr" );
1026 MakeTargetLatLonStr( APData, APData->TargetLatitude, APData->TargetLongitude);
1028 APData->TargetHeading = 0.0; // default direction, due north
1029 APData->TargetAltitude = 3000; // default altitude in meters
1030 APData->alt_error_accum = 0.0;
1032 MakeTargetAltitudeStr( APData, 3000.0);
1033 MakeTargetHeadingStr( APData, 0.0 );
1035 // These eventually need to be read from current_aircaft somehow.
1039 // the maximum roll, in Deg
1040 APData->MaxRoll = 7;
1041 // the deg from heading to start rolling out at, in Deg
1042 APData->RollOut = 30;
1043 // how far can I move the aleron from center.
1044 APData->MaxAileron = .1;
1045 // Smoothing distance for alerion control
1046 APData->RollOutSmooth = 10;
1049 // the maximum roll, in Deg
1050 APData->MaxRoll = 20;
1052 // the deg from heading to start rolling out at, in Deg
1053 APData->RollOut = 20;
1055 // how far can I move the aleron from center.
1056 APData->MaxAileron = .2;
1058 // Smoothing distance for alerion control
1059 APData->RollOutSmooth = 10;
1061 //Remove at a later date
1062 APDataGlobal = APData;
1064 // Hardwired for now should be in options
1065 // 25% max control variablilty 0.5 / 2.0
1066 APData->disengage_threshold = 1.0;
1068 #if !defined( USING_SLIDER_CLASS )
1069 MaxRollAdjust = 2 * APData->MaxRoll;
1070 RollOutAdjust = 2 * APData->RollOut;
1071 MaxAileronAdjust = 2 * APData->MaxAileron;
1072 RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
1073 #endif // !defined( USING_SLIDER_CLASS )
1075 get_control_values();
1077 // FG_LOG( FG_AUTOPILOT, FG_INFO, " calling NewTgtAirportInit" );
1078 NewTgtAirportInit();
1085 void fgAPReset( void ) {
1086 fgAPDataPtr APData = APDataGlobal;
1088 if ( fgAPTerrainFollowEnabled() )
1089 fgAPToggleTerrainFollow( );
1091 if ( fgAPAltitudeEnabled() )
1092 fgAPToggleAltitude();
1094 if ( fgAPHeadingEnabled() )
1095 fgAPToggleHeading();
1097 if ( fgAPAutoThrottleEnabled() )
1098 fgAPToggleAutoThrottle();
1100 APData->TargetHeading = 0.0; // default direction, due north
1101 MakeTargetHeadingStr( APData, APData->TargetHeading );
1103 APData->TargetAltitude = 3000; // default altitude in meters
1104 MakeTargetAltitudeStr( APData, 3000);
1106 APData->alt_error_accum = 0.0;
1109 get_control_values();
1111 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
1113 APData->TargetLatitude = fgAPget_latitude();
1114 APData->TargetLongitude = fgAPget_longitude();
1115 MakeTargetLatLonStr( APData,
1116 APData->TargetLatitude,
1117 APData->TargetLongitude);
1121 int fgAPRun( void ) {
1122 // Remove the following lines when the calling funcitons start
1123 // passing in the data pointer
1127 APData = APDataGlobal;
1130 // get control settings
1131 double aileron = controls.get_aileron();
1132 double elevator = controls.get_elevator();
1133 double elevator_trim = controls.get_elevator_trim();
1134 double rudder = controls.get_rudder();
1136 double lat = fgAPget_latitude();
1137 double lon = fgAPget_longitude();
1139 // see if somebody else has changed them
1140 if( fabs(aileron - APData->old_aileron) > APData->disengage_threshold ||
1141 fabs(elevator - APData->old_elevator) > APData->disengage_threshold ||
1142 fabs(elevator_trim - APData->old_elevator_trim) > APData->disengage_threshold ||
1143 fabs(rudder - APData->old_rudder) > APData->disengage_threshold )
1145 // if controls changed externally turn autopilot off
1146 APData->waypoint_hold = false ; // turn the target hold off
1147 APData->heading_hold = false ; // turn the heading hold off
1148 APData->altitude_hold = false ; // turn the altitude hold off
1149 APData->terrain_follow = false; // turn the terrain_follow hold off
1150 // APData->auto_throttle = false; // turn the auto_throttle off
1152 // stash this runs control settings
1153 APData->old_aileron = aileron;
1154 APData->old_elevator = elevator;
1155 APData->old_elevator_trim = elevator_trim;
1156 APData->old_rudder = rudder;
1161 // waypoint hold enabled?
1162 if ( APData->waypoint_hold == true )
1164 double wp_course, wp_reverse, wp_distance;
1166 #ifdef DO_fgAP_CORRECTED_COURSE
1167 // compute course made good
1168 // this needs lots of special casing before use
1169 double course, reverse, distance, corrected_course;
1170 // need to test for iter
1171 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
1179 #endif // DO_fgAP_CORRECTED_COURSE
1181 // compute course to way_point
1182 // need to test for iter
1183 if( ! geo_inverse_wgs_84( 0, //fgAPget_altitude(),
1186 APData->TargetLatitude,
1187 APData->TargetLongitude,
1192 #ifdef DO_fgAP_CORRECTED_COURSE
1193 corrected_course = course - wp_course;
1194 if( fabs(corrected_course) > 0.1 )
1195 printf("fgAP: course %f wp_course %f %f %f\n",
1196 course, wp_course, fabs(corrected_course), distance );
1197 #endif // DO_fgAP_CORRECTED_COURSE
1199 if ( wp_distance > 100 ) {
1200 // corrected_course = course - wp_course;
1201 APData->TargetHeading = NormalizeDegrees(wp_course);
1203 printf("APData->distance(%f) to close\n", wp_distance);
1204 // Real Close -- set heading hold to current heading
1205 // and Ring the arival bell !!
1206 NewTgtAirport(NULL);
1207 APData->waypoint_hold = false;
1209 APData->TargetHeading = fgAPget_heading();
1211 MakeTargetHeadingStr( APData, APData->TargetHeading );
1212 // Force this just in case
1213 APData->TargetDistance = wp_distance;
1214 MakeTargetDistanceStr( APData, wp_distance );
1215 // This changes the AutoPilot Heading Read Out
1216 // following cast needed
1217 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1219 APData->heading_hold = true;
1222 // heading hold enabled?
1223 if ( APData->heading_hold == true ) {
1230 NormalizeDegrees( APData->TargetHeading - fgAPget_heading() );
1231 // figure out how far off we are from desired heading
1233 // Now it is time to deterime how far we should be rolled.
1234 FG_LOG( FG_AUTOPILOT, FG_DEBUG, "RelHeading: " << RelHeading );
1237 // Check if we are further from heading than the roll out point
1238 if ( fabs( RelHeading ) > APData->RollOut ) {
1239 // set Target Roll to Max in desired direction
1240 if ( RelHeading < 0 ) {
1241 TargetRoll = 0 - APData->MaxRoll;
1243 TargetRoll = APData->MaxRoll;
1246 // We have to calculate the Target roll
1248 // This calculation engine thinks that the Target roll
1249 // should be a line from (RollOut,MaxRoll) to (-RollOut,
1250 // -MaxRoll) I hope this works well. If I get ambitious
1251 // some day this might become a fancier curve or
1254 TargetRoll = LinearExtrapolate( RelHeading, -APData->RollOut,
1255 -APData->MaxRoll, APData->RollOut,
1259 // Target Roll has now been Found.
1261 // Compare Target roll to Current Roll, Generate Rel Roll
1263 FG_LOG( FG_COCKPIT, FG_BULK, "TargetRoll: " << TargetRoll );
1265 RelRoll = NormalizeDegrees( TargetRoll - fgAPget_roll() );
1267 // Check if we are further from heading than the roll out smooth point
1268 if ( fabs( RelRoll ) > APData->RollOutSmooth ) {
1269 // set Target Roll to Max in desired direction
1270 if ( RelRoll < 0 ) {
1271 AileronSet = 0 - APData->MaxAileron;
1273 AileronSet = APData->MaxAileron;
1276 AileronSet = LinearExtrapolate( RelRoll, -APData->RollOutSmooth,
1277 -APData->MaxAileron,
1278 APData->RollOutSmooth,
1279 APData->MaxAileron );
1282 controls.set_aileron( AileronSet );
1283 controls.set_rudder( AileronSet / 2.0 );
1284 // controls.set_rudder( 0.0 );
1287 // altitude hold or terrain follow enabled?
1288 if ( APData->altitude_hold || APData->terrain_follow ) {
1289 double speed, max_climb, error;
1290 double prop_error, int_error;
1291 double prop_adj, int_adj, total_adj;
1293 if ( APData->altitude_hold ) {
1294 // normal altitude hold
1295 APData->TargetClimbRate =
1296 ( APData->TargetAltitude - fgAPget_altitude() ) * 8.0;
1297 } else if ( APData->terrain_follow ) {
1298 // brain dead ground hugging with no look ahead
1299 APData->TargetClimbRate =
1300 ( APData->TargetAGL - fgAPget_agl() ) * 16.0;
1302 // just try to zero out rate of climb ...
1303 APData->TargetClimbRate = 0.0;
1306 speed = get_speed();
1308 if ( speed < 90.0 ) {
1310 } else if ( speed < 100.0 ) {
1311 max_climb = ( speed - 90.0 ) * 20;
1312 // } else if ( speed < 150.0 ) {
1314 max_climb = ( speed - 100.0 ) * 4.0 + 200.0;
1315 } //else { // this is NHV hack
1316 // max_climb = ( speed - 150.0 ) * 6.0 + 300.0;
1319 if ( APData->TargetClimbRate > max_climb ) {
1320 APData->TargetClimbRate = max_climb;
1323 else if ( APData->TargetClimbRate < -400.0 ) {
1324 APData->TargetClimbRate = -400.0;
1327 error = fgAPget_climb() - APData->TargetClimbRate;
1329 // accumulate the error under the curve ... this really should
1331 APData->alt_error_accum += error;
1333 // calculate integral error, and adjustment amount
1334 int_error = APData->alt_error_accum;
1335 // printf("error = %.2f int_error = %.2f\n", error, int_error);
1336 int_adj = int_error / 8000.0;
1338 // caclulate proportional error
1340 prop_adj = prop_error / 2000.0;
1342 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
1343 if ( total_adj > 0.6 ) {
1346 else if ( total_adj < -0.2 ) {
1350 controls.set_elevator( total_adj );
1353 // auto throttle enabled?
1354 if ( APData->auto_throttle ) {
1356 double prop_error, int_error;
1357 double prop_adj, int_adj, total_adj;
1359 error = APData->TargetSpeed - get_speed();
1361 // accumulate the error under the curve ... this really should
1363 APData->speed_error_accum += error;
1364 if ( APData->speed_error_accum > 2000.0 ) {
1365 APData->speed_error_accum = 2000.0;
1367 else if ( APData->speed_error_accum < -2000.0 ) {
1368 APData->speed_error_accum = -2000.0;
1371 // calculate integral error, and adjustment amount
1372 int_error = APData->speed_error_accum;
1374 // printf("error = %.2f int_error = %.2f\n", error, int_error);
1375 int_adj = int_error / 200.0;
1377 // caclulate proportional error
1379 prop_adj = 0.5 + prop_error / 50.0;
1381 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
1382 if ( total_adj > 1.0 ) {
1385 else if ( total_adj < 0.0 ) {
1389 controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
1392 #ifdef THIS_CODE_IS_NOT_USED
1393 if (APData->Mode == 2) // Glide slope hold
1398 // First, calculate Relative slope and normalize it
1399 RelSlope = NormalizeDegrees( APData->TargetSlope - get_pitch());
1401 // Now calculate the elevator offset from current angle
1402 if ( abs(RelSlope) > APData->SlopeSmooth )
1404 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
1405 RelElevator = -APData->MaxElevator;
1407 RelElevator = APData->MaxElevator;
1411 RelElevator = LinearExtrapolate(RelSlope,-APData->SlopeSmooth,-APData->MaxElevator,APData->SlopeSmooth,APData->MaxElevator);
1414 fgElevMove(RelElevator);
1417 #endif // THIS_CODE_IS_NOT_USED
1419 // stash this runs control settings
1420 // get_control_values();
1421 APData->old_aileron = controls.get_aileron();
1422 APData->old_elevator = controls.get_elevator();
1423 APData->old_elevator_trim = controls.get_elevator_trim();
1424 APData->old_rudder = controls.get_rudder();
1426 // for cross track error
1427 APData->old_lat = lat;
1428 APData->old_lon = lon;
1435 void fgAPSetMode( int mode)
1437 //Remove the following line when the calling funcitons start passing in the data pointer
1440 APData = APDataGlobal;
1443 fgPrintf( FG_COCKPIT, FG_INFO, "APSetMode : %d\n", mode );
1445 APData->Mode = mode; // set the new mode
1449 void fgAPset_tgt_airport_id( const string id ) {
1450 FG_LOG( FG_AUTOPILOT, FG_INFO, "entering fgAPset_tgt_airport_id " << id );
1452 APData = APDataGlobal;
1453 APData->tgt_airport_id = id;
1454 FG_LOG( FG_AUTOPILOT, FG_INFO, "leaving fgAPset_tgt_airport_id "
1455 << APData->tgt_airport_id );
1458 string fgAPget_tgt_airport_id( void ) {
1459 fgAPDataPtr APData = APDataGlobal;
1460 return APData->tgt_airport_id;
1464 void fgAPToggleHeading( void ) {
1465 // Remove at a later date
1468 APData = APDataGlobal;
1471 if ( APData->heading_hold || APData->waypoint_hold ) {
1472 // turn off heading hold
1473 APData->heading_hold = false;
1474 APData->waypoint_hold = false;
1476 // turn on heading hold, lock at current heading
1477 APData->heading_hold = true;
1478 APData->TargetHeading = fgAPget_heading();
1479 MakeTargetHeadingStr( APData, APData->TargetHeading );
1480 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1483 get_control_values();
1484 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetHeading: ("
1485 << APData->heading_hold << ") " << APData->TargetHeading );
1488 void fgAPToggleWayPoint( void ) {
1489 // Remove at a later date
1492 APData = APDataGlobal;
1495 if ( APData->waypoint_hold ) {
1496 // turn off location hold
1497 APData->waypoint_hold = false;
1498 // set heading hold to current heading
1499 // APData->heading_hold = true;
1500 APData->TargetHeading = fgAPget_heading();
1502 double course, reverse, distance;
1503 // turn on location hold
1504 // turn on heading hold
1505 APData->old_lat = fgAPget_latitude();
1506 APData->old_lon = fgAPget_longitude();
1508 // need to test for iter
1509 if(!geo_inverse_wgs_84( fgAPget_altitude(),
1511 fgAPget_longitude(),
1512 APData->TargetLatitude,
1513 APData->TargetLongitude,
1517 APData->TargetHeading = course;
1518 APData->TargetDistance = distance;
1519 MakeTargetDistanceStr( APData, distance );
1522 APData->waypoint_hold = true;
1523 APData->heading_hold = true;
1526 // This changes the AutoPilot Heading
1527 // following cast needed
1528 ApHeadingDialogInput->setValue ((float)APData->TargetHeading );
1529 MakeTargetHeadingStr( APData, APData->TargetHeading );
1531 get_control_values();
1533 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetLocation: ( "
1534 << APData->waypoint_hold << " "
1535 << APData->TargetLatitude << " "
1536 << APData->TargetLongitude << " ) "
1541 void fgAPToggleAltitude( void ) {
1542 // Remove at a later date
1545 APData = APDataGlobal;
1548 if ( APData->altitude_hold ) {
1549 // turn off altitude hold
1550 APData->altitude_hold = false;
1552 // turn on altitude hold, lock at current altitude
1553 APData->altitude_hold = true;
1554 APData->terrain_follow = false;
1555 APData->TargetAltitude = fgAPget_altitude();
1556 APData->alt_error_accum = 0.0;
1557 // alt_error_queue.erase( alt_error_queue.begin(),
1558 // alt_error_queue.end() );
1559 float target_alt = APData->TargetAltitude;
1560 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
1561 target_alt *= METER_TO_FEET;
1563 ApAltitudeDialogInput->setValue(target_alt);
1564 MakeTargetAltitudeStr( APData, target_alt);
1567 get_control_values();
1568 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAltitude: ("
1569 << APData->altitude_hold << ") " << APData->TargetAltitude );
1573 void fgAPToggleAutoThrottle ( void ) {
1574 // Remove at a later date
1577 APData = APDataGlobal;
1580 if ( APData->auto_throttle ) {
1581 // turn off altitude hold
1582 APData->auto_throttle = false;
1584 // turn on terrain follow, lock at current agl
1585 APData->auto_throttle = true;
1586 APData->TargetSpeed = get_speed();
1587 APData->speed_error_accum = 0.0;
1590 get_control_values();
1591 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: ("
1592 << APData->auto_throttle << ") " << APData->TargetSpeed );
1595 void fgAPToggleTerrainFollow( void ) {
1596 // Remove at a later date
1599 APData = APDataGlobal;
1602 if ( APData->terrain_follow ) {
1603 // turn off altitude hold
1604 APData->terrain_follow = false;
1606 // turn on terrain follow, lock at current agl
1607 APData->terrain_follow = true;
1608 APData->altitude_hold = false;
1609 APData->TargetAGL = fgAPget_agl();
1610 APData->alt_error_accum = 0.0;
1612 get_control_values();
1614 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetTerrainFollow: ("
1615 << APData->terrain_follow << ") " << APData->TargetAGL );
1618 static double NormalizeDegrees( double Input ) {
1619 // normalize the input to the range (-180,180]
1620 // Input should not be greater than -360 to 360.
1621 // Current rules send the output to an undefined state.
1625 else if ( Input <= -180 )
1626 while ( Input <= -180 )
1631 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
1632 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
1633 //assert(x1 != x2); // Divide by zero error. Cold abort for now
1636 // static double y = 0.0;
1637 // double dx = x2 -x1;
1638 // if( (dx < -FG_EPSILON ) || ( dx > FG_EPSILON ) )
1641 double m, b, y; // the constants to find in y=mx+b
1644 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
1646 b = y1 - m * x1; // calculate the b
1648 y = m * x + b; // the final calculation
1657 /* Direct and inverse distance functions */
1658 /** Proceedings of the 7th International Symposium on Geodetic
1660 "The Nested Coefficient Method for Accurate Solutions of Direct
1662 Inverse Geodetic Problems With Any Length"
1666 /* modified for FlightGear to use WGS84 only Norman Vine */
1668 //#include "dstazfns.h"
1670 #define GEOD_INV_PI (3.14159265358979323846)
1675 /* for WGS_84 a = 6378137.000, rf = 298.257223563; */
1677 static double M0( double e2 )
1678 { //double e4 = e2*e2;
1679 return GEOD_INV_PI*(1.0 - e2*( 1.0/4.0 + e2*( 3.0/64.0 + e2*(5.0/256.0) )))/2.0;
1682 int geo_direct_wgs_84 ( double alt, double lat1, double lon1, double az1, double s,
1683 double *lat2, double *lon2, double *az2 )
1685 double a = 6378137.000, rf = 298.257223563;
1686 double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
1687 double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
1688 double b = a*(1.0-f), e2 = f*(2.0-f);
1689 double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
1690 double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
1691 double azm1 = az1*RADDEG;
1692 double sinaz1 = sin(azm1), cosaz1 = cos(azm1);
1695 if( fabs(s) < 0.01 ) /* distance < centimeter => congruency */
1699 if( *az2 > 360.0 ) *az2 -= 360.0;
1703 if( cosphi1 ) /* non-polar origin */
1704 { /* u1 is reduced latitude */
1705 double tanu1 = sqrt(1.0-e2)*sinphi1/cosphi1;
1706 double sig1 = atan2(tanu1,cosaz1);
1707 double cosu1 = 1.0/sqrt( 1.0 + tanu1*tanu1 ), sinu1 = tanu1*cosu1;
1708 double sinaz = cosu1*sinaz1, cos2saz = 1.0-sinaz*sinaz;
1709 double us = cos2saz*e2/(1.0-e2);
1711 double ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/16384.0,
1712 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0,
1714 /* FIRST ESTIMATE OF SIGMA (SIG) */
1715 double first = s/(b*ta); /* !!*/
1717 double c2sigm, sinsig,cossig, temp,denom,rnumer, dlams, dlam;
1719 { c2sigm = cos(2.0*sig1+sig);
1720 sinsig = sin(sig); cossig = cos(sig);
1723 tb*sinsig*(c2sigm+tb*(cossig*(-1.0+2.0*pow(c2sigm,2.0)) -
1724 tb*c2sigm*(-3.0+4.0*pow(sinsig,2.0))*(-3.0+4.0*pow(c2sigm,2.0))/6.0)/4.0);
1726 while( fabs(sig-temp) > testv);
1727 /* LATITUDE OF POINT 2 */
1728 /* DENOMINATOR IN 2 PARTS (TEMP ALSO USED LATER) */
1729 temp = sinu1*sinsig-cosu1*cossig*cosaz1;
1730 denom = (1.0-f)*sqrt(sinaz*sinaz+temp*temp);
1732 rnumer = sinu1*cossig+cosu1*sinsig*cosaz1;
1733 *lat2 = atan2(rnumer,denom)/RADDEG;
1734 /* DIFFERENCE IN LONGITUDE ON AUXILARY SPHERE (DLAMS ) */
1735 rnumer = sinsig*sinaz1;
1736 denom = cosu1*cossig-sinu1*sinsig*cosaz1;
1737 dlams = atan2(rnumer,denom);
1739 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
1740 /* DIFFERENCE IN LONGITUDE */
1741 dlam = dlams-(1.0-tc)*f*sinaz*(sig+tc*sinsig*(c2sigm+tc*cossig*(-1.0+2.0*
1743 *lon2 = (lam1+dlam)/RADDEG;
1744 if(*lon2 > 180.0 ) *lon2 -= 360.0;
1745 if(*lon2 < -180.0 ) *lon2 += 360.0;
1746 /* AZIMUTH - FROM NORTH */
1747 *az2 = atan2(-sinaz,temp)/RADDEG;
1748 if( fabs(*az2) < testv ) *az2 = 0.0;
1749 if( *az2 < 0.0) *az2 += 360.0;
1752 else /* phi1 == 90 degrees, polar origin */
1753 { double dM = a*M0(e2) - s;
1754 double paz = ( phi1 < 0.0 ? 180.0 : 0.0 );
1755 return geo_direct_wgs_84( alt, 0.0, lon1, paz, dM,lat2,lon2,az2 );
1759 int geo_inverse_wgs_84( double alt, double lat1, double lon1, double lat2,
1760 double lon2, double *az1, double *az2, double *s )
1762 double a = 6378137.000, rf = 298.257223563;
1764 double RADDEG = (GEOD_INV_PI)/180.0, testv = 1.0E-10;
1765 double f = ( rf > 0.0 ? 1.0/rf : 0.0 );
1766 double b = a*(1.0-f), e2 = f*(2.0-f);
1767 double phi1 = lat1*RADDEG, lam1 = lon1*RADDEG;
1768 double sinphi1 = sin(phi1), cosphi1 = cos(phi1);
1769 double phi2 = lat2*RADDEG, lam2 = lon2*RADDEG;
1770 double sinphi2 = sin(phi2), cosphi2 = cos(phi2);
1772 if( (fabs(lat1-lat2) < testv &&
1773 ( fabs(lon1-lon2) < testv) || fabs(lat1-90.0) < testv ) )
1774 { /* TWO STATIONS ARE IDENTICAL : SET DISTANCE & AZIMUTHS TO ZERO */
1775 *az1 = 0.0; *az2 = 0.0; *s = 0.0;
1778 if( fabs(cosphi1) < testv ) /* initial point is polar */
1780 int k = geo_inverse_wgs_84( alt, lat2,lon2,lat1,lon1, az1,az2,s );
1781 b = *az1; *az1 = *az2; *az2 = b;
1784 if( fabs(cosphi2) < testv ) /* terminal point is polar */
1786 int k = geo_inverse_wgs_84( alt, lat1,lon1,lat1,lon1+180.0,
1789 *az2 = *az1 + 180.0;
1790 if( *az2 > 360.0 ) *az2 -= 360.0;
1792 } else /* Geodesic passes through the pole (antipodal) */
1793 if( (fabs( fabs(lon1-lon2) - 180 ) < testv) &&
1794 (fabs(lat1+lat2) < testv) )
1797 geo_inverse_wgs_84( alt, lat1,lon1, lat1,lon2, az1,az2, &s1 );
1798 geo_inverse_wgs_84( alt, lat2,lon2, lat1,lon2, az1,az2, &s2 );
1802 } else /* antipodal and polar points don't get here */
1804 double dlam = lam2 - lam1, dlams = dlam;
1805 double sdlams,cdlams, sig,sinsig,cossig, sinaz,
1807 double tc,temp, us,rnumer,denom, ta,tb;
1808 double cosu1,sinu1, sinu2,cosu2;
1809 /* Reduced latitudes */
1810 temp = (1.0-f)*sinphi1/cosphi1;
1811 cosu1 = 1.0/sqrt(1.0+temp*temp);
1813 temp = (1.0-f)*sinphi2/cosphi2;
1814 cosu2 = 1.0/sqrt(1.0+temp*temp);
1818 sdlams = sin(dlams), cdlams = cos(dlams);
1819 sinsig = sqrt(pow(cosu2*sdlams,2.0)+
1820 pow(cosu1*sinu2-sinu1*cosu2*cdlams,2.0));
1821 cossig = sinu1*sinu2+cosu1*cosu2*cdlams;
1823 sig = atan2(sinsig,cossig);
1824 sinaz = cosu1*cosu2*sdlams/sinsig;
1825 cos2saz = 1.0-sinaz*sinaz;
1826 c2sigm = (sinu1 == 0.0 || sinu2 == 0.0 ? cossig :
1827 cossig-2.0*sinu1*sinu2/cos2saz);
1828 tc = f*cos2saz*(4.0+f*(4.0-3.0*cos2saz))/16.0;
1830 dlams = dlam+(1.0-tc)*f*sinaz*
1832 (c2sigm+tc*cossig*(-1.0+2.0*pow(c2sigm,2.0))));
1833 if (fabs(dlams) > GEOD_INV_PI && iter++ > 50)
1835 } while ( fabs(temp-dlams) > testv);
1837 us = cos2saz*(pow(a,2.0)-pow(b,2.0))/pow(b,2.0); /* !! */
1838 /* BACK AZIMUTH FROM NORTH */
1839 rnumer = -(cosu1*sdlams);
1840 denom = sinu1*cosu2-cosu1*sinu2*cdlams;
1841 *az2 = atan2(rnumer,denom)/RADDEG;
1842 if( fabs(*az2) < testv ) *az2 = 0.0;
1843 if(*az2 < 0.0) *az2 += 360.0;
1844 /* FORWARD AZIMUTH FROM NORTH */
1845 rnumer = cosu2*sdlams;
1846 denom = cosu1*sinu2-sinu1*cosu2*cdlams;
1847 *az1 = atan2(rnumer,denom)/RADDEG;
1848 if( fabs(*az1) < testv ) *az1 = 0.0;
1849 if(*az1 < 0.0) *az1 += 360.0;
1851 ta = 1.0+us*(4096.0+us*(-768.0+us*(320.0-175.0*us)))/
1853 tb = us*(256.0+us*(-128.0+us*(74.0-47.0*us)))/1024.0;
1854 /* GEODETIC DISTANCE */
1855 *s = b*ta*(sig-tb*sinsig*
1856 (c2sigm+tb*(cossig*(-1.0+2.0*pow(c2sigm,2.0))-tb*
1857 c2sigm*(-3.0+4.0*pow(sinsig,2.0))*
1858 (-3.0+4.0*pow(c2sigm,2.0))/6.0)/