1 // autopilot.cxx -- autopilot subsystem
3 // Started by Jeff Goeke-Smith, started April 1998.
5 // Copyright (C) 1998 Jeff Goeke-Smith, jgoeke@voyager.net
7 // Heavy modifications and additions by Norman Vine and few by Curtis
10 // This program is free software; you can redistribute it and/or
11 // modify it under the terms of the GNU General Public License as
12 // published by the Free Software Foundation; either version 2 of the
13 // License, or (at your option) any later version.
15 // This program is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 // General Public License for more details.
20 // You should have received a copy of the GNU General Public License
21 // along with this program; if not, write to the Free Software
22 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include <Scenery/scenery.hxx>
36 #include "autopilot.hxx"
38 #include <Include/fg_constants.h>
39 #include <Debug/logstream.hxx>
40 #include <Airports/simple.hxx>
42 #include <Main/fg_init.hxx>
43 #include <Main/options.hxx>
44 #include <Time/fg_time.hxx>
47 #define mySlider puSlider
49 /// These statics will eventually go into the class
50 /// they are just here while I am experimenting -- NHV :-)
51 // AutoPilot Gain Adjuster members
52 static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
53 static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
54 static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
55 static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
57 static float MaxRollValue; // 0.1 -> 1.0
58 static float RollOutValue;
59 static float MaxAileronValue;
60 static float RollOutSmoothValue;
62 static float TmpMaxRollValue; // for cancel operation
63 static float TmpRollOutValue;
64 static float TmpMaxAileronValue;
65 static float TmpRollOutSmoothValue;
67 static puDialogBox *APAdjustDialog;
68 static puFrame *APAdjustFrame;
69 static puText *APAdjustDialogMessage;
70 static puFont APAdjustLegendFont;
71 static puFont APAdjustLabelFont;
73 static puOneShot *APAdjustOkButton;
74 static puOneShot *APAdjustResetButton;
75 static puOneShot *APAdjustCancelButton;
77 //static puButton *APAdjustDragButton;
79 static puText *APAdjustMaxRollTitle;
80 static puText *APAdjustRollOutTitle;
81 static puText *APAdjustMaxAileronTitle;
82 static puText *APAdjustRollOutSmoothTitle;
84 static puText *APAdjustMaxAileronText;
85 static puText *APAdjustMaxRollText;
86 static puText *APAdjustRollOutText;
87 static puText *APAdjustRollOutSmoothText;
89 static mySlider *APAdjustHS0;
90 static mySlider *APAdjustHS1;
91 static mySlider *APAdjustHS2;
92 static mySlider *APAdjustHS3;
94 static char SliderText[ 4 ][ 8 ];
96 ///////// AutoPilot New Heading Dialog
98 static puDialogBox *ApHeadingDialog;
99 static puFrame *ApHeadingDialogFrame;
100 static puText *ApHeadingDialogMessage;
101 static puInput *ApHeadingDialogInput;
102 static puOneShot *ApHeadingDialogOkButton;
103 static puOneShot *ApHeadingDialogCancelButton;
106 ///////// AutoPilot New Altitude Dialog
108 static puDialogBox *ApAltitudeDialog = 0;
109 static puFrame *ApAltitudeDialogFrame = 0;
110 static puText *ApAltitudeDialogMessage = 0;
111 static puInput *ApAltitudeDialogInput = 0;
113 static puOneShot *ApAltitudeDialogOkButton = 0;
114 static puOneShot *ApAltitudeDialogCancelButton = 0;
117 /// The beginnings of Lock AutoPilot to target location :-)
118 // Needs cleaning up but works
119 // These statics should disapear when this is a class
120 static puDialogBox *TgtAptDialog = 0;
121 static puFrame *TgtAptDialogFrame = 0;
122 static puText *TgtAptDialogMessage = 0;
123 static puInput *TgtAptDialogInput = 0;
125 static char NewTgtAirportId[16];
126 static char NewTgtAirportLabel[] = "Enter New TgtAirport ID";
128 static puOneShot *TgtAptDialogOkButton = 0;
129 static puOneShot *TgtAptDialogCancelButton = 0;
130 static puOneShot *TgtAptDialogResetButton = 0;
133 // global variable holding the AP info
134 // I want this gone. Data should be in aircraft structure
135 fgAPDataPtr APDataGlobal;
137 // Local Prototype section
138 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 );
139 static double NormalizeDegrees( double Input );
140 // End Local ProtoTypes
142 extern char *coord_format_lat(float);
143 extern char *coord_format_lon(float);
146 static inline double get_ground_speed( void ) {
147 // starts in ft/s so we convert to kts
148 double ft_s = cur_fdm_state->get_V_ground_speed()
149 * current_options.get_speed_up();;
150 double kts = ft_s * FEET_TO_METER * 3600 * METER_TO_NM;
154 // The below routines were copied right from hud.c ( I hate reinventing
155 // the wheel more than necessary)
157 // The following routines obtain information concerntin the aircraft's
158 // current state and return it to calling instrument display routines.
159 // They should eventually be member functions of the aircraft.
161 static void get_control_values( void ) {
163 APData = APDataGlobal;
164 APData->old_aileron = controls.get_aileron();
165 APData->old_elevator = controls.get_elevator();
166 APData->old_elevator_trim = controls.get_elevator_trim();
167 APData->old_rudder = controls.get_rudder();
170 static void MakeTargetHeadingStr( fgAPDataPtr APData, double bearing ) {
173 else if(bearing > 360. )
175 sprintf(APData->TargetHeadingStr, "APHeading %6.1f", bearing);
178 static inline void MakeTargetDistanceStr( fgAPDataPtr APData, double distance ) {
179 double eta = distance*METER_TO_NM / get_ground_speed();
180 if ( eta >= 100.0 ) { eta = 99.999; }
182 if ( eta < (1.0/6.0) ) {
183 // within 10 minutes, bump up to min/secs
187 minor = (int)((eta - (int)eta) * 60.0);
188 sprintf(APData->TargetDistanceStr, "APDistance %.2f NM ETA %d:%02d",
189 distance*METER_TO_NM, major, minor);
190 // cout << "distance = " << distance*METER_TO_NM
191 // << " gndsp = " << get_ground_speed()
192 // << " time = " << eta
193 // << " major = " << major
194 // << " minor = " << minor
198 static inline void MakeTargetAltitudeStr( fgAPDataPtr APData, double altitude ) {
199 sprintf(APData->TargetAltitudeStr, "APAltitude %6.0f", altitude);
202 static inline void MakeTargetLatLonStr( fgAPDataPtr APData, double lat, double lon ) {
204 tmp = APData->TargetLatitude;
205 sprintf( APData->TargetLatitudeStr , "%s", coord_format_lat(tmp) );
206 tmp = APData->TargetLongitude;
207 sprintf( APData->TargetLongitudeStr, "%s", coord_format_lon(tmp) );
209 sprintf(APData->TargetLatLonStr, "%s %s",
210 APData->TargetLatitudeStr,
211 APData->TargetLongitudeStr );
214 static inline double get_speed( void ) {
215 return( cur_fdm_state->get_V_equiv_kts() );
218 static inline double get_aoa( void ) {
219 return( cur_fdm_state->get_Gamma_vert_rad() * RAD_TO_DEG );
222 static inline double fgAPget_latitude( void ) {
223 return( cur_fdm_state->get_Latitude() * RAD_TO_DEG );
226 static inline double fgAPget_longitude( void ) {
227 return( cur_fdm_state->get_Longitude() * RAD_TO_DEG );
230 static inline double fgAPget_roll( void ) {
231 return( cur_fdm_state->get_Phi() * RAD_TO_DEG );
234 static inline double get_pitch( void ) {
235 return( cur_fdm_state->get_Theta() );
238 double fgAPget_heading( void ) {
239 return( cur_fdm_state->get_Psi() * RAD_TO_DEG );
242 static inline double fgAPget_altitude( void ) {
243 return( cur_fdm_state->get_Altitude() * FEET_TO_METER );
246 static inline double fgAPget_climb( void ) {
247 // return in meters per minute
248 return( cur_fdm_state->get_Climb_Rate() * FEET_TO_METER * 60 );
251 static inline double get_sideslip( void ) {
252 return( cur_fdm_state->get_Beta() );
255 static inline double fgAPget_agl( void ) {
258 agl = cur_fdm_state->get_Altitude() * FEET_TO_METER
264 // End of copied section. ( thanks for the wheel :-)
265 double fgAPget_TargetLatitude( void ) {
266 fgAPDataPtr APData = APDataGlobal;
267 return APData->TargetLatitude;
270 double fgAPget_TargetLongitude( void ) {
271 fgAPDataPtr APData = APDataGlobal;
272 return APData->TargetLongitude;
275 double fgAPget_TargetHeading( void ) {
276 fgAPDataPtr APData = APDataGlobal;
277 return APData->TargetHeading;
280 double fgAPget_TargetDistance( void ) {
281 fgAPDataPtr APData = APDataGlobal;
282 return APData->TargetDistance;
285 double fgAPget_TargetAltitude( void ) {
286 fgAPDataPtr APData = APDataGlobal;
287 return APData->TargetAltitude;
290 char *fgAPget_TargetLatitudeStr( void ) {
291 fgAPDataPtr APData = APDataGlobal;
292 return APData->TargetLatitudeStr;
295 char *fgAPget_TargetLongitudeStr( void ) {
296 fgAPDataPtr APData = APDataGlobal;
297 return APData->TargetLongitudeStr;
300 char *fgAPget_TargetDistanceStr( void ) {
301 fgAPDataPtr APData = APDataGlobal;
302 return APData->TargetDistanceStr;
305 char *fgAPget_TargetHeadingStr( void ) {
306 fgAPDataPtr APData = APDataGlobal;
307 return APData->TargetHeadingStr;
310 char *fgAPget_TargetAltitudeStr( void ) {
311 fgAPDataPtr APData = APDataGlobal;
312 return APData->TargetAltitudeStr;
315 char *fgAPget_TargetLatLonStr( void ) {
316 fgAPDataPtr APData = APDataGlobal;
317 return APData->TargetLatLonStr;
320 bool fgAPWayPointEnabled( void )
324 APData = APDataGlobal;
326 // heading hold enabled?
327 return APData->waypoint_hold;
331 bool fgAPHeadingEnabled( void )
335 APData = APDataGlobal;
337 // heading hold enabled?
338 return APData->heading_hold;
341 bool fgAPAltitudeEnabled( void )
345 APData = APDataGlobal;
347 // altitude hold or terrain follow enabled?
348 return APData->altitude_hold;
351 bool fgAPTerrainFollowEnabled( void )
355 APData = APDataGlobal;
357 // altitude hold or terrain follow enabled?
358 return APData->terrain_follow ;
361 bool fgAPAutoThrottleEnabled( void )
365 APData = APDataGlobal;
367 // autothrottle enabled?
368 return APData->auto_throttle;
371 void fgAPAltitudeAdjust( double inc )
373 // Remove at a later date
374 fgAPDataPtr APData = APDataGlobal;
377 double target_alt, target_agl;
379 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
380 target_alt = APData->TargetAltitude * METER_TO_FEET;
381 target_agl = APData->TargetAGL * METER_TO_FEET;
383 target_alt = APData->TargetAltitude;
384 target_agl = APData->TargetAGL;
387 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
388 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
390 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
391 target_alt *= FEET_TO_METER;
392 target_agl *= FEET_TO_METER;
395 APData->TargetAltitude = target_alt;
396 APData->TargetAGL = target_agl;
398 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
399 target_alt *= METER_TO_FEET;
400 ApAltitudeDialogInput->setValue((float)target_alt);
401 MakeTargetAltitudeStr( APData, target_alt);
403 get_control_values();
406 void fgAPAltitudeSet( double new_altitude ) {
407 // Remove at a later date
408 fgAPDataPtr APData = APDataGlobal;
410 double target_alt = new_altitude;
412 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
413 target_alt = new_altitude * FEET_TO_METER;
415 if( target_alt < scenery.cur_elev )
416 target_alt = scenery.cur_elev;
418 APData->TargetAltitude = target_alt;
420 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
421 target_alt *= METER_TO_FEET;
422 ApAltitudeDialogInput->setValue((float)target_alt);
423 MakeTargetAltitudeStr( APData, target_alt);
425 get_control_values();
428 void fgAPHeadingAdjust( double inc ) {
429 fgAPDataPtr APData = APDataGlobal;
431 if ( APData->waypoint_hold )
432 APData->waypoint_hold = false;
434 double target = ( int ) ( APData->TargetHeading / inc ) * inc + inc;
436 APData->TargetHeading = NormalizeDegrees( target );
437 // following cast needed ambiguous plib
438 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
439 MakeTargetHeadingStr( APData, APData->TargetHeading );
440 get_control_values();
443 void fgAPHeadingSet( double new_heading ) {
444 fgAPDataPtr APData = APDataGlobal;
446 if ( APData->waypoint_hold )
447 APData->waypoint_hold = false;
449 new_heading = NormalizeDegrees( new_heading );
450 APData->TargetHeading = new_heading;
451 // following cast needed ambiguous plib
452 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
453 MakeTargetHeadingStr( APData, APData->TargetHeading );
454 get_control_values();
457 void fgAPAutoThrottleAdjust( double inc ) {
458 fgAPDataPtr APData = APDataGlobal;
460 double target = ( int ) ( APData->TargetSpeed / inc ) * inc + inc;
462 APData->TargetSpeed = target;
465 // THIS NEEDS IMPROVEMENT !!!!!!!!!!!!!
466 static int scan_number(char *s, double *new_value)
471 char *WordBufPtr = WordBuf;
476 *WordBufPtr++ = *cptr++;
478 while (isdigit(*cptr) ) {
479 *WordBufPtr++ = *cptr++;
483 *WordBufPtr++ = *cptr++; // put the '.' into the string
484 while (isdigit(*cptr)) {
485 *WordBufPtr++ = *cptr++;
490 sscanf(WordBuf, "%lf", new_value);
497 void ApHeadingDialog_Cancel(puObject *)
499 ApHeadingDialogInput->rejectInput();
500 FG_POP_PUI_DIALOG( ApHeadingDialog );
503 void ApHeadingDialog_OK (puObject *me)
508 ApHeadingDialogInput -> getValue( &c );
512 if( scan_number( c, &NewHeading ) )
514 if(!fgAPHeadingEnabled())
516 fgAPHeadingSet( NewHeading );
520 s += " is not a valid number.";
523 ApHeadingDialog_Cancel(me);
524 if( error ) mkDialog(s.c_str());
527 void NewHeading(puObject *cb)
529 // string ApHeadingLabel( "Enter New Heading" );
530 // ApHeadingDialogMessage -> setLabel(ApHeadingLabel.c_str());
531 ApHeadingDialogInput -> acceptInput();
532 FG_PUSH_PUI_DIALOG( ApHeadingDialog );
535 void NewHeadingInit(void)
537 // printf("NewHeadingInit\n");
538 char NewHeadingLabel[] = "Enter New Heading";
541 float heading = fgAPget_heading();
543 (puGetStringWidth( puGetDefaultLabelFont(), NewHeadingLabel ) /2 );
545 ApHeadingDialog = new puDialogBox (150, 50);
547 ApHeadingDialogFrame = new puFrame (0, 0, 260, 150);
549 ApHeadingDialogMessage = new puText (len, 110);
550 ApHeadingDialogMessage -> setDefaultValue (NewHeadingLabel);
551 ApHeadingDialogMessage -> getDefaultValue (&s);
552 ApHeadingDialogMessage -> setLabel (s);
554 ApHeadingDialogInput = new puInput ( 50, 70, 210, 100 );
555 ApHeadingDialogInput -> setValue ( heading );
557 ApHeadingDialogOkButton = new puOneShot (50, 10, 110, 50);
558 ApHeadingDialogOkButton -> setLegend (gui_msg_OK);
559 ApHeadingDialogOkButton -> makeReturnDefault (TRUE);
560 ApHeadingDialogOkButton -> setCallback (ApHeadingDialog_OK);
562 ApHeadingDialogCancelButton = new puOneShot (140, 10, 210, 50);
563 ApHeadingDialogCancelButton -> setLegend (gui_msg_CANCEL);
564 ApHeadingDialogCancelButton -> setCallback (ApHeadingDialog_Cancel);
567 FG_FINALIZE_PUI_DIALOG( ApHeadingDialog );
570 void ApAltitudeDialog_Cancel(puObject *)
572 ApAltitudeDialogInput -> rejectInput();
573 FG_POP_PUI_DIALOG( ApAltitudeDialog );
576 void ApAltitudeDialog_OK (puObject *me)
581 ApAltitudeDialogInput->getValue( &c );
585 if( scan_number( c, &NewAltitude) )
587 if(!(fgAPAltitudeEnabled()))
588 fgAPToggleAltitude();
589 fgAPAltitudeSet( NewAltitude );
593 s += " is not a valid number.";
596 ApAltitudeDialog_Cancel(me);
597 if( error ) mkDialog(s.c_str());
598 get_control_values();
601 void NewAltitude(puObject *cb)
603 ApAltitudeDialogInput -> acceptInput();
604 FG_PUSH_PUI_DIALOG( ApAltitudeDialog );
607 void NewAltitudeInit(void)
609 // printf("NewAltitudeInit\n");
610 char NewAltitudeLabel[] = "Enter New Altitude";
613 float alt = cur_fdm_state->get_Altitude();
615 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_METERS) {
616 alt *= FEET_TO_METER;
620 (puGetStringWidth( puGetDefaultLabelFont(), NewAltitudeLabel )/2);
622 // ApAltitudeDialog = new puDialogBox (150, 50);
623 ApAltitudeDialog = new puDialogBox (150, 200);
625 ApAltitudeDialogFrame = new puFrame (0, 0, 260, 150);
626 ApAltitudeDialogMessage = new puText (len, 110);
627 ApAltitudeDialogMessage -> setDefaultValue (NewAltitudeLabel);
628 ApAltitudeDialogMessage -> getDefaultValue (&s);
629 ApAltitudeDialogMessage -> setLabel (s);
631 ApAltitudeDialogInput = new puInput ( 50, 70, 210, 100 );
632 ApAltitudeDialogInput -> setValue ( alt );
633 // Uncomment the next line to have input active on startup
634 // ApAltitudeDialogInput -> acceptInput ( );
635 // cursor at begining or end of line ?
638 // ApAltitudeDialogInput -> setCursor ( len );
639 // ApAltitudeDialogInput -> setSelectRegion ( 5, 9 );
641 ApAltitudeDialogOkButton = new puOneShot (50, 10, 110, 50);
642 ApAltitudeDialogOkButton -> setLegend (gui_msg_OK);
643 ApAltitudeDialogOkButton -> makeReturnDefault (TRUE);
644 ApAltitudeDialogOkButton -> setCallback (ApAltitudeDialog_OK);
646 ApAltitudeDialogCancelButton = new puOneShot (140, 10, 210, 50);
647 ApAltitudeDialogCancelButton -> setLegend (gui_msg_CANCEL);
648 ApAltitudeDialogCancelButton -> setCallback (ApAltitudeDialog_Cancel);
651 FG_FINALIZE_PUI_DIALOG( ApAltitudeDialog );
654 /////// simple AutoPilot GAIN / LIMITS ADJUSTER
656 #define fgAP_CLAMP(val,min,max) \
657 ( (val) = (val) > (max) ? (max) : (val) < (min) ? (min) : (val) )
659 static void maxroll_adj( puObject *hs ) {
661 fgAPDataPtr APData = APDataGlobal;
663 hs-> getValue ( &val ) ;
664 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
665 // printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
666 APData-> MaxRoll = MaxRollAdjust * val;
667 sprintf( SliderText[ 0 ], "%05.2f", APData->MaxRoll );
668 APAdjustMaxRollText -> setLabel ( SliderText[ 0 ] ) ;
671 static void rollout_adj( puObject *hs ) {
673 fgAPDataPtr APData = APDataGlobal;
675 hs-> getValue ( &val ) ;
676 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
677 // printf ( "rollout_adj( %p ) %f %f\n", hs, val, RollOutAdjust * val ) ;
678 APData-> RollOut = RollOutAdjust * val;
679 sprintf( SliderText[ 1 ], "%05.2f", APData->RollOut );
680 APAdjustRollOutText -> setLabel ( SliderText[ 1 ] );
683 static void maxaileron_adj( puObject *hs ) {
686 APData = APDataGlobal;
688 hs-> getValue ( &val ) ;
689 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
690 // printf ( "maxaileron_adj( %p ) %f %f\n", hs, val, MaxAileronAdjust * val ) ;
691 APData-> MaxAileron = MaxAileronAdjust * val;
692 sprintf( SliderText[ 3 ], "%05.2f", APData->MaxAileron );
693 APAdjustMaxAileronText -> setLabel ( SliderText[ 3 ] );
696 static void rolloutsmooth_adj( puObject *hs ) {
698 fgAPDataPtr APData = APDataGlobal;
700 hs -> getValue ( &val ) ;
701 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
702 // printf ( "rolloutsmooth_adj( %p ) %f %f\n", hs, val, RollOutSmoothAdjust * val ) ;
703 APData->RollOutSmooth = RollOutSmoothAdjust * val;
704 sprintf( SliderText[ 2 ], "%5.2f", APData-> RollOutSmooth );
705 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
709 static void goAwayAPAdjust (puObject *)
711 FG_POP_PUI_DIALOG( APAdjustDialog );
714 void cancelAPAdjust( puObject *self ) {
715 fgAPDataPtr APData = APDataGlobal;
717 APData-> MaxRoll = TmpMaxRollValue;
718 APData-> RollOut = TmpRollOutValue;
719 APData-> MaxAileron = TmpMaxAileronValue;
720 APData-> RollOutSmooth = TmpRollOutSmoothValue;
722 goAwayAPAdjust(self);
725 void resetAPAdjust( puObject *self ) {
726 fgAPDataPtr APData = APDataGlobal;
728 APData-> MaxRoll = MaxRollAdjust / 2;
729 APData-> RollOut = RollOutAdjust / 2;
730 APData-> MaxAileron = MaxAileronAdjust / 2;
731 APData-> RollOutSmooth = RollOutSmoothAdjust / 2;
733 FG_POP_PUI_DIALOG( APAdjustDialog );
738 void fgAPAdjust( puObject * ) {
739 fgAPDataPtr APData = APDataGlobal;
741 TmpMaxRollValue = APData-> MaxRoll;
742 TmpRollOutValue = APData-> RollOut;
743 TmpMaxAileronValue = APData-> MaxAileron;
744 TmpRollOutSmoothValue = APData-> RollOutSmooth;
746 MaxRollValue = APData-> MaxRoll / MaxRollAdjust;
747 RollOutValue = APData-> RollOut / RollOutAdjust;
748 MaxAileronValue = APData-> MaxAileron / MaxAileronAdjust;
749 RollOutSmoothValue = APData-> RollOutSmooth / RollOutSmoothAdjust;
751 APAdjustHS0-> setValue ( MaxRollValue ) ;
752 APAdjustHS1-> setValue ( RollOutValue ) ;
753 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
754 APAdjustHS3-> setValue ( MaxAileronValue ) ;
756 FG_PUSH_PUI_DIALOG( APAdjustDialog );
759 // Done once at system initialization
760 void fgAPAdjustInit( void ) {
762 // printf("fgAPAdjustInit\n");
763 #define HORIZONTAL FALSE
767 int DialogWidth = 230;
769 char Label[] = "AutoPilot Adjust";
772 fgAPDataPtr APData = APDataGlobal;
774 int labelX = (DialogWidth / 2) -
775 (puGetStringWidth( puGetDefaultLabelFont(), Label ) / 2);
776 labelX -= 30; // KLUDGEY
781 int slider_width = 210;
782 int slider_title_x = 15;
783 int slider_value_x = 160;
784 float slider_delta = 0.1f;
786 TmpMaxRollValue = APData-> MaxRoll;
787 TmpRollOutValue = APData-> RollOut;
788 TmpMaxAileronValue = APData-> MaxAileron;
789 TmpRollOutSmoothValue = APData-> RollOutSmooth;
791 MaxRollValue = APData-> MaxRoll / MaxRollAdjust;
792 RollOutValue = APData-> RollOut / RollOutAdjust;
793 MaxAileronValue = APData-> MaxAileron / MaxAileronAdjust;
794 RollOutSmoothValue = APData-> RollOutSmooth / RollOutSmoothAdjust;
796 puGetDefaultFonts ( &APAdjustLegendFont, &APAdjustLabelFont );
797 APAdjustDialog = new puDialogBox ( DialogX, DialogY ); {
798 int horiz_slider_height = puGetStringHeight (APAdjustLabelFont) +
799 puGetStringDescender (APAdjustLabelFont) +
800 PUSTR_TGAP + PUSTR_BGAP + 5;
802 APAdjustFrame = new puFrame ( 0, 0,
803 DialogWidth, 85 + nSliders * horiz_slider_height );
805 APAdjustDialogMessage = new puText ( labelX, 52 + nSliders * horiz_slider_height );
806 APAdjustDialogMessage -> setDefaultValue ( Label );
807 APAdjustDialogMessage -> getDefaultValue ( &s );
808 APAdjustDialogMessage -> setLabel ( s );
810 APAdjustHS0 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
811 APAdjustHS0-> setDelta ( slider_delta ) ;
812 APAdjustHS0-> setValue ( MaxRollValue ) ;
813 APAdjustHS0-> setCBMode ( PUSLIDER_DELTA ) ;
814 APAdjustHS0-> setCallback ( maxroll_adj ) ;
816 sprintf( SliderText[ 0 ], "%05.2f", APData->MaxRoll );
817 APAdjustMaxRollTitle = new puText ( slider_title_x, slider_y ) ;
818 APAdjustMaxRollTitle-> setDefaultValue ( "MaxRoll" ) ;
819 APAdjustMaxRollTitle-> getDefaultValue ( &s ) ;
820 APAdjustMaxRollTitle-> setLabel ( s ) ;
821 APAdjustMaxRollText = new puText ( slider_value_x, slider_y ) ;
822 APAdjustMaxRollText-> setLabel ( SliderText[ 0 ] ) ;
824 slider_y += horiz_slider_height;
826 APAdjustHS1 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
827 APAdjustHS1-> setDelta ( slider_delta ) ;
828 APAdjustHS1-> setValue ( RollOutValue ) ;
829 APAdjustHS1-> setCBMode ( PUSLIDER_DELTA ) ;
830 APAdjustHS1-> setCallback ( rollout_adj ) ;
832 sprintf( SliderText[ 1 ], "%05.2f", APData->RollOut );
833 APAdjustRollOutTitle = new puText ( slider_title_x, slider_y ) ;
834 APAdjustRollOutTitle-> setDefaultValue ( "AdjustRollOut" ) ;
835 APAdjustRollOutTitle-> getDefaultValue ( &s ) ;
836 APAdjustRollOutTitle-> setLabel ( s ) ;
837 APAdjustRollOutText = new puText ( slider_value_x, slider_y ) ;
838 APAdjustRollOutText-> setLabel ( SliderText[ 1 ] );
840 slider_y += horiz_slider_height;
842 APAdjustHS2 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
843 APAdjustHS2-> setDelta ( slider_delta ) ;
844 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
845 APAdjustHS2-> setCBMode ( PUSLIDER_DELTA ) ;
846 APAdjustHS2-> setCallback ( rolloutsmooth_adj ) ;
848 sprintf( SliderText[ 2 ], "%5.2f", APData->RollOutSmooth );
849 APAdjustRollOutSmoothTitle = new puText ( slider_title_x, slider_y ) ;
850 APAdjustRollOutSmoothTitle-> setDefaultValue ( "RollOutSmooth" ) ;
851 APAdjustRollOutSmoothTitle-> getDefaultValue ( &s ) ;
852 APAdjustRollOutSmoothTitle-> setLabel ( s ) ;
853 APAdjustRollOutSmoothText = new puText ( slider_value_x, slider_y ) ;
854 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
856 slider_y += horiz_slider_height;
858 APAdjustHS3 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
859 APAdjustHS3-> setDelta ( slider_delta ) ;
860 APAdjustHS3-> setValue ( MaxAileronValue ) ;
861 APAdjustHS3-> setCBMode ( PUSLIDER_DELTA ) ;
862 APAdjustHS3-> setCallback ( maxaileron_adj ) ;
864 sprintf( SliderText[ 3 ], "%05.2f", APData->MaxAileron );
865 APAdjustMaxAileronTitle = new puText ( slider_title_x, slider_y ) ;
866 APAdjustMaxAileronTitle-> setDefaultValue ( "MaxAileron" ) ;
867 APAdjustMaxAileronTitle-> getDefaultValue ( &s ) ;
868 APAdjustMaxAileronTitle-> setLabel ( s ) ;
869 APAdjustMaxAileronText = new puText ( slider_value_x, slider_y ) ;
870 APAdjustMaxAileronText-> setLabel ( SliderText[ 3 ] );
872 APAdjustOkButton = new puOneShot ( 10, 10, 60, 50 );
873 APAdjustOkButton-> setLegend ( gui_msg_OK );
874 APAdjustOkButton-> makeReturnDefault ( TRUE );
875 APAdjustOkButton-> setCallback ( goAwayAPAdjust );
877 APAdjustCancelButton = new puOneShot ( 70, 10, 150, 50 );
878 APAdjustCancelButton-> setLegend ( gui_msg_CANCEL );
879 APAdjustCancelButton-> setCallback ( cancelAPAdjust );
881 APAdjustResetButton = new puOneShot ( 160, 10, 220, 50 );
882 APAdjustResetButton-> setLegend ( gui_msg_RESET );
883 APAdjustResetButton-> setCallback ( resetAPAdjust );
885 FG_FINALIZE_PUI_DIALOG( APAdjustDialog );
890 // Simple Dialog to input Target Airport
891 void TgtAptDialog_Cancel(puObject *)
893 FG_POP_PUI_DIALOG( TgtAptDialog );
896 void TgtAptDialog_OK (puObject *)
899 APData = APDataGlobal;
902 // FGTime *t = FGTime::cur_time_params;
903 // int PauseMode = t->getPause();
905 // t->togglePauseMode();
908 TgtAptDialogInput->getValue(&s);
911 TgtAptDialog_Cancel( NULL );
913 if ( TgtAptId.length() ) {
914 // set initial position from TgtAirport id
919 FG_LOG( FG_GENERAL, FG_INFO,
920 "Attempting to set starting position from airport code "
923 airports.load("apt_simple");
924 if ( airports.search( TgtAptId, &a ) )
926 double course, reverse, distance;
927 // fgAPset_tgt_airport_id( TgtAptId.c_str() );
928 current_options.set_airport_id( TgtAptId.c_str() );
929 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
931 APData->TargetLatitude = a.latitude; // * DEG_TO_RAD;
932 APData->TargetLongitude = a.longitude; // * DEG_TO_RAD;
933 MakeTargetLatLonStr( APData,
934 APData->TargetLatitude,
935 APData->TargetLongitude);
937 APData->old_lat = fgAPget_latitude();
938 APData->old_lon = fgAPget_longitude();
940 // need to test for iter
941 if( ! geo_inverse_wgs_84( fgAPget_altitude(),
944 APData->TargetLatitude,
945 APData->TargetLongitude,
949 APData->TargetHeading = course;
950 MakeTargetHeadingStr( APData, APData->TargetHeading );
951 APData->TargetDistance = distance;
952 MakeTargetDistanceStr( APData, distance );
953 // This changes the AutoPilot Heading
954 // following cast needed
955 ApHeadingDialogInput->
956 setValue((float)APData->TargetHeading);
958 APData->waypoint_hold = true ;
959 APData->heading_hold = true;
962 TgtAptId += " not in database.";
963 mkDialog(TgtAptId.c_str());
966 get_control_values();
967 // if( PauseMode != t->getPause() )
968 // t->togglePauseMode();
971 void TgtAptDialog_Reset(puObject *)
973 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
974 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
975 TgtAptDialogInput->setValue ( NewTgtAirportId );
976 TgtAptDialogInput->setCursor( 0 ) ;
979 void NewTgtAirport(puObject *cb)
981 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
982 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
983 TgtAptDialogInput->setValue( NewTgtAirportId );
985 FG_PUSH_PUI_DIALOG( TgtAptDialog );
988 void NewTgtAirportInit(void)
990 FG_LOG( FG_AUTOPILOT, FG_INFO, " enter NewTgtAirportInit()" );
991 // fgAPset_tgt_airport_id( current_options.get_airport_id() );
992 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
993 FG_LOG( FG_AUTOPILOT, FG_INFO, " NewTgtAirportId " << NewTgtAirportId );
994 // printf(" NewTgtAirportId %s\n", NewTgtAirportId);
995 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
996 NewTgtAirportLabel ) / 2;
998 TgtAptDialog = new puDialogBox (150, 50);
1000 TgtAptDialogFrame = new puFrame (0,0,350, 150);
1001 TgtAptDialogMessage = new puText (len, 110);
1002 TgtAptDialogMessage -> setLabel (NewTgtAirportLabel);
1004 TgtAptDialogInput = new puInput (50, 70, 300, 100);
1005 TgtAptDialogInput -> setValue (NewTgtAirportId);
1006 TgtAptDialogInput -> acceptInput();
1008 TgtAptDialogOkButton = new puOneShot (50, 10, 110, 50);
1009 TgtAptDialogOkButton -> setLegend (gui_msg_OK);
1010 TgtAptDialogOkButton -> setCallback (TgtAptDialog_OK);
1011 TgtAptDialogOkButton -> makeReturnDefault(TRUE);
1013 TgtAptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1014 TgtAptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1015 TgtAptDialogCancelButton -> setCallback (TgtAptDialog_Cancel);
1017 TgtAptDialogResetButton = new puOneShot (240, 10, 300, 50);
1018 TgtAptDialogResetButton -> setLegend (gui_msg_RESET);
1019 TgtAptDialogResetButton -> setCallback (TgtAptDialog_Reset);
1021 FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
1022 printf("leave NewTgtAirportInit()");
1026 // Finally actual guts of AutoPilot
1027 void fgAPInit( fgAIRCRAFT *current_aircraft ) {
1030 FG_LOG( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem" );
1032 APData = ( fgAPDataPtr ) calloc( sizeof( fgAPData ), 1 );
1034 if ( APData == NULL ) {
1035 // I couldn't get the mem. Dying
1036 FG_LOG( FG_AUTOPILOT, FG_ALERT, "No ram for Autopilot. Dying." );
1040 FG_LOG( FG_AUTOPILOT, FG_INFO, " Autopilot allocated " );
1042 APData->waypoint_hold = false ; // turn the target hold off
1043 APData->heading_hold = false ; // turn the heading hold off
1044 APData->altitude_hold = false ; // turn the altitude hold off
1046 // Initialize target location to startup location
1047 // FG_LOG( FG_AUTOPILOT, FG_INFO, " Autopilot setting startup location" );
1049 APData->TargetLatitude = fgAPget_latitude();
1051 APData->TargetLongitude = fgAPget_longitude();
1053 // FG_LOG( FG_AUTOPILOT, FG_INFO, " Autopilot setting TargetLatitudeStr" );
1054 MakeTargetLatLonStr( APData, APData->TargetLatitude, APData->TargetLongitude);
1056 APData->TargetHeading = 0.0; // default direction, due north
1057 APData->TargetAltitude = 3000; // default altitude in meters
1058 APData->alt_error_accum = 0.0;
1060 MakeTargetAltitudeStr( APData, 3000.0);
1061 MakeTargetHeadingStr( APData, 0.0 );
1063 // These eventually need to be read from current_aircaft somehow.
1067 // the maximum roll, in Deg
1068 APData->MaxRoll = 7;
1069 // the deg from heading to start rolling out at, in Deg
1070 APData->RollOut = 30;
1071 // how far can I move the aleron from center.
1072 APData->MaxAileron = .1;
1073 // Smoothing distance for alerion control
1074 APData->RollOutSmooth = 10;
1077 // the maximum roll, in Deg
1078 APData->MaxRoll = 20;
1080 // the deg from heading to start rolling out at, in Deg
1081 APData->RollOut = 20;
1083 // how far can I move the aleron from center.
1084 APData->MaxAileron = .2;
1086 // Smoothing distance for alerion control
1087 APData->RollOutSmooth = 10;
1089 //Remove at a later date
1090 APDataGlobal = APData;
1092 // Hardwired for now should be in options
1093 // 25% max control variablilty 0.5 / 2.0
1094 APData->disengage_threshold = 1.0;
1096 #if !defined( USING_SLIDER_CLASS )
1097 MaxRollAdjust = 2 * APData->MaxRoll;
1098 RollOutAdjust = 2 * APData->RollOut;
1099 MaxAileronAdjust = 2 * APData->MaxAileron;
1100 RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
1101 #endif // !defined( USING_SLIDER_CLASS )
1103 get_control_values();
1105 // FG_LOG( FG_AUTOPILOT, FG_INFO, " calling NewTgtAirportInit" );
1106 NewTgtAirportInit();
1113 void fgAPReset( void ) {
1114 fgAPDataPtr APData = APDataGlobal;
1116 if ( fgAPTerrainFollowEnabled() )
1117 fgAPToggleTerrainFollow( );
1119 if ( fgAPAltitudeEnabled() )
1120 fgAPToggleAltitude();
1122 if ( fgAPHeadingEnabled() )
1123 fgAPToggleHeading();
1125 if ( fgAPAutoThrottleEnabled() )
1126 fgAPToggleAutoThrottle();
1128 APData->TargetHeading = 0.0; // default direction, due north
1129 MakeTargetHeadingStr( APData, APData->TargetHeading );
1131 APData->TargetAltitude = 3000; // default altitude in meters
1132 MakeTargetAltitudeStr( APData, 3000);
1134 APData->alt_error_accum = 0.0;
1137 get_control_values();
1139 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
1141 APData->TargetLatitude = fgAPget_latitude();
1142 APData->TargetLongitude = fgAPget_longitude();
1143 MakeTargetLatLonStr( APData,
1144 APData->TargetLatitude,
1145 APData->TargetLongitude);
1149 int fgAPRun( void ) {
1150 // Remove the following lines when the calling funcitons start
1151 // passing in the data pointer
1155 APData = APDataGlobal;
1158 // get control settings
1159 double aileron = controls.get_aileron();
1160 double elevator = controls.get_elevator();
1161 double elevator_trim = controls.get_elevator_trim();
1162 double rudder = controls.get_rudder();
1164 double lat = fgAPget_latitude();
1165 double lon = fgAPget_longitude();
1167 #ifdef FG_FORCE_AUTO_DISENGAGE
1168 // see if somebody else has changed them
1169 if( fabs(aileron - APData->old_aileron) > APData->disengage_threshold ||
1170 fabs(elevator - APData->old_elevator) > APData->disengage_threshold ||
1171 fabs(elevator_trim - APData->old_elevator_trim) >
1172 APData->disengage_threshold ||
1173 fabs(rudder - APData->old_rudder) > APData->disengage_threshold )
1175 // if controls changed externally turn autopilot off
1176 APData->waypoint_hold = false ; // turn the target hold off
1177 APData->heading_hold = false ; // turn the heading hold off
1178 APData->altitude_hold = false ; // turn the altitude hold off
1179 APData->terrain_follow = false; // turn the terrain_follow hold off
1180 // APData->auto_throttle = false; // turn the auto_throttle off
1182 // stash this runs control settings
1183 APData->old_aileron = aileron;
1184 APData->old_elevator = elevator;
1185 APData->old_elevator_trim = elevator_trim;
1186 APData->old_rudder = rudder;
1192 // waypoint hold enabled?
1193 if ( APData->waypoint_hold == true )
1195 double wp_course, wp_reverse, wp_distance;
1197 #ifdef DO_fgAP_CORRECTED_COURSE
1198 // compute course made good
1199 // this needs lots of special casing before use
1200 double course, reverse, distance, corrected_course;
1201 // need to test for iter
1202 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
1210 #endif // DO_fgAP_CORRECTED_COURSE
1212 // compute course to way_point
1213 // need to test for iter
1214 if( ! geo_inverse_wgs_84( 0, //fgAPget_altitude(),
1217 APData->TargetLatitude,
1218 APData->TargetLongitude,
1223 #ifdef DO_fgAP_CORRECTED_COURSE
1224 corrected_course = course - wp_course;
1225 if( fabs(corrected_course) > 0.1 )
1226 printf("fgAP: course %f wp_course %f %f %f\n",
1227 course, wp_course, fabs(corrected_course), distance );
1228 #endif // DO_fgAP_CORRECTED_COURSE
1230 if ( wp_distance > 100 ) {
1231 // corrected_course = course - wp_course;
1232 APData->TargetHeading = NormalizeDegrees(wp_course);
1234 printf("APData->distance(%f) to close\n", wp_distance);
1235 // Real Close -- set heading hold to current heading
1236 // and Ring the arival bell !!
1237 NewTgtAirport(NULL);
1238 APData->waypoint_hold = false;
1240 APData->TargetHeading = fgAPget_heading();
1242 MakeTargetHeadingStr( APData, APData->TargetHeading );
1243 // Force this just in case
1244 APData->TargetDistance = wp_distance;
1245 MakeTargetDistanceStr( APData, wp_distance );
1246 // This changes the AutoPilot Heading Read Out
1247 // following cast needed
1248 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1250 APData->heading_hold = true;
1253 // heading hold enabled?
1254 if ( APData->heading_hold == true ) {
1261 NormalizeDegrees( APData->TargetHeading - fgAPget_heading() );
1262 // figure out how far off we are from desired heading
1264 // Now it is time to deterime how far we should be rolled.
1265 FG_LOG( FG_AUTOPILOT, FG_DEBUG, "RelHeading: " << RelHeading );
1268 // Check if we are further from heading than the roll out point
1269 if ( fabs( RelHeading ) > APData->RollOut ) {
1270 // set Target Roll to Max in desired direction
1271 if ( RelHeading < 0 ) {
1272 TargetRoll = 0 - APData->MaxRoll;
1274 TargetRoll = APData->MaxRoll;
1277 // We have to calculate the Target roll
1279 // This calculation engine thinks that the Target roll
1280 // should be a line from (RollOut,MaxRoll) to (-RollOut,
1281 // -MaxRoll) I hope this works well. If I get ambitious
1282 // some day this might become a fancier curve or
1285 TargetRoll = LinearExtrapolate( RelHeading, -APData->RollOut,
1286 -APData->MaxRoll, APData->RollOut,
1290 // Target Roll has now been Found.
1292 // Compare Target roll to Current Roll, Generate Rel Roll
1294 FG_LOG( FG_COCKPIT, FG_BULK, "TargetRoll: " << TargetRoll );
1296 RelRoll = NormalizeDegrees( TargetRoll - fgAPget_roll() );
1298 // Check if we are further from heading than the roll out smooth point
1299 if ( fabs( RelRoll ) > APData->RollOutSmooth ) {
1300 // set Target Roll to Max in desired direction
1301 if ( RelRoll < 0 ) {
1302 AileronSet = 0 - APData->MaxAileron;
1304 AileronSet = APData->MaxAileron;
1307 AileronSet = LinearExtrapolate( RelRoll, -APData->RollOutSmooth,
1308 -APData->MaxAileron,
1309 APData->RollOutSmooth,
1310 APData->MaxAileron );
1313 controls.set_aileron( AileronSet );
1314 controls.set_rudder( AileronSet / 2.0 );
1315 // controls.set_rudder( 0.0 );
1318 // altitude hold or terrain follow enabled?
1319 if ( APData->altitude_hold || APData->terrain_follow ) {
1320 double speed, max_climb, error;
1321 double prop_error, int_error;
1322 double prop_adj, int_adj, total_adj;
1324 if ( APData->altitude_hold ) {
1325 // normal altitude hold
1326 APData->TargetClimbRate =
1327 ( APData->TargetAltitude - fgAPget_altitude() ) * 8.0;
1328 } else if ( APData->terrain_follow ) {
1329 // brain dead ground hugging with no look ahead
1330 APData->TargetClimbRate =
1331 ( APData->TargetAGL - fgAPget_agl() ) * 16.0;
1332 // cout << "target agl = " << APData->TargetAGL
1333 // << " current agl = " << fgAPget_agl()
1334 // << " target climb rate = " << APData->TargetClimbRate
1337 // just try to zero out rate of climb ...
1338 APData->TargetClimbRate = 0.0;
1341 speed = get_speed();
1343 if ( speed < 90.0 ) {
1345 } else if ( speed < 100.0 ) {
1346 max_climb = ( speed - 90.0 ) * 20;
1347 // } else if ( speed < 150.0 ) {
1349 max_climb = ( speed - 100.0 ) * 4.0 + 200.0;
1350 } //else { // this is NHV hack
1351 // max_climb = ( speed - 150.0 ) * 6.0 + 300.0;
1354 if ( APData->TargetClimbRate > max_climb ) {
1355 APData->TargetClimbRate = max_climb;
1358 else if ( APData->TargetClimbRate < -400.0 ) {
1359 APData->TargetClimbRate = -400.0;
1362 error = fgAPget_climb() - APData->TargetClimbRate;
1363 // cout << "climb rate = " << fgAPget_climb()
1364 // << " error = " << error << endl;
1366 // accumulate the error under the curve ... this really should
1368 APData->alt_error_accum += error;
1370 // calculate integral error, and adjustment amount
1371 int_error = APData->alt_error_accum;
1372 // printf("error = %.2f int_error = %.2f\n", error, int_error);
1373 int_adj = int_error / 8000.0;
1375 // caclulate proportional error
1377 prop_adj = prop_error / 2000.0;
1379 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
1380 if ( total_adj > 0.6 ) {
1383 else if ( total_adj < -0.2 ) {
1387 controls.set_elevator( total_adj );
1390 // auto throttle enabled?
1391 if ( APData->auto_throttle ) {
1393 double prop_error, int_error;
1394 double prop_adj, int_adj, total_adj;
1396 error = APData->TargetSpeed - get_speed();
1398 // accumulate the error under the curve ... this really should
1400 APData->speed_error_accum += error;
1401 if ( APData->speed_error_accum > 2000.0 ) {
1402 APData->speed_error_accum = 2000.0;
1404 else if ( APData->speed_error_accum < -2000.0 ) {
1405 APData->speed_error_accum = -2000.0;
1408 // calculate integral error, and adjustment amount
1409 int_error = APData->speed_error_accum;
1411 // printf("error = %.2f int_error = %.2f\n", error, int_error);
1412 int_adj = int_error / 200.0;
1414 // caclulate proportional error
1416 prop_adj = 0.5 + prop_error / 50.0;
1418 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
1419 if ( total_adj > 1.0 ) {
1422 else if ( total_adj < 0.0 ) {
1426 controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
1429 #ifdef THIS_CODE_IS_NOT_USED
1430 if (APData->Mode == 2) // Glide slope hold
1435 // First, calculate Relative slope and normalize it
1436 RelSlope = NormalizeDegrees( APData->TargetSlope - get_pitch());
1438 // Now calculate the elevator offset from current angle
1439 if ( abs(RelSlope) > APData->SlopeSmooth )
1441 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
1442 RelElevator = -APData->MaxElevator;
1444 RelElevator = APData->MaxElevator;
1448 RelElevator = LinearExtrapolate(RelSlope,-APData->SlopeSmooth,-APData->MaxElevator,APData->SlopeSmooth,APData->MaxElevator);
1451 fgElevMove(RelElevator);
1454 #endif // THIS_CODE_IS_NOT_USED
1456 // stash this runs control settings
1457 // get_control_values();
1458 APData->old_aileron = controls.get_aileron();
1459 APData->old_elevator = controls.get_elevator();
1460 APData->old_elevator_trim = controls.get_elevator_trim();
1461 APData->old_rudder = controls.get_rudder();
1463 // for cross track error
1464 APData->old_lat = lat;
1465 APData->old_lon = lon;
1472 void fgAPSetMode( int mode)
1474 //Remove the following line when the calling funcitons start passing in the data pointer
1477 APData = APDataGlobal;
1480 fgPrintf( FG_COCKPIT, FG_INFO, "APSetMode : %d\n", mode );
1482 APData->Mode = mode; // set the new mode
1486 void fgAPset_tgt_airport_id( const string id ) {
1487 FG_LOG( FG_AUTOPILOT, FG_INFO, "entering fgAPset_tgt_airport_id " << id );
1489 APData = APDataGlobal;
1490 APData->tgt_airport_id = id;
1491 FG_LOG( FG_AUTOPILOT, FG_INFO, "leaving fgAPset_tgt_airport_id "
1492 << APData->tgt_airport_id );
1495 string fgAPget_tgt_airport_id( void ) {
1496 fgAPDataPtr APData = APDataGlobal;
1497 return APData->tgt_airport_id;
1501 void fgAPToggleHeading( void ) {
1502 // Remove at a later date
1505 APData = APDataGlobal;
1508 if ( APData->heading_hold || APData->waypoint_hold ) {
1509 // turn off heading hold
1510 APData->heading_hold = false;
1511 APData->waypoint_hold = false;
1513 // turn on heading hold, lock at current heading
1514 APData->heading_hold = true;
1515 APData->TargetHeading = fgAPget_heading();
1516 MakeTargetHeadingStr( APData, APData->TargetHeading );
1517 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1520 get_control_values();
1521 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetHeading: ("
1522 << APData->heading_hold << ") " << APData->TargetHeading );
1525 void fgAPToggleWayPoint( void ) {
1526 // Remove at a later date
1529 APData = APDataGlobal;
1532 if ( APData->waypoint_hold ) {
1533 // turn off location hold
1534 APData->waypoint_hold = false;
1535 // set heading hold to current heading
1536 // APData->heading_hold = true;
1537 APData->TargetHeading = fgAPget_heading();
1539 double course, reverse, distance;
1540 // turn on location hold
1541 // turn on heading hold
1542 APData->old_lat = fgAPget_latitude();
1543 APData->old_lon = fgAPget_longitude();
1545 // need to test for iter
1546 if(!geo_inverse_wgs_84( fgAPget_altitude(),
1548 fgAPget_longitude(),
1549 APData->TargetLatitude,
1550 APData->TargetLongitude,
1554 APData->TargetHeading = course;
1555 APData->TargetDistance = distance;
1556 MakeTargetDistanceStr( APData, distance );
1559 APData->waypoint_hold = true;
1560 APData->heading_hold = true;
1563 // This changes the AutoPilot Heading
1564 // following cast needed
1565 ApHeadingDialogInput->setValue ((float)APData->TargetHeading );
1566 MakeTargetHeadingStr( APData, APData->TargetHeading );
1568 get_control_values();
1570 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetLocation: ( "
1571 << APData->waypoint_hold << " "
1572 << APData->TargetLatitude << " "
1573 << APData->TargetLongitude << " ) "
1578 void fgAPToggleAltitude( void ) {
1579 // Remove at a later date
1582 APData = APDataGlobal;
1585 if ( APData->altitude_hold ) {
1586 // turn off altitude hold
1587 APData->altitude_hold = false;
1589 // turn on altitude hold, lock at current altitude
1590 APData->altitude_hold = true;
1591 APData->terrain_follow = false;
1592 APData->TargetAltitude = fgAPget_altitude();
1593 APData->alt_error_accum = 0.0;
1594 // alt_error_queue.erase( alt_error_queue.begin(),
1595 // alt_error_queue.end() );
1596 float target_alt = APData->TargetAltitude;
1597 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
1598 target_alt *= METER_TO_FEET;
1600 ApAltitudeDialogInput->setValue(target_alt);
1601 MakeTargetAltitudeStr( APData, target_alt);
1604 get_control_values();
1605 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAltitude: ("
1606 << APData->altitude_hold << ") " << APData->TargetAltitude );
1610 void fgAPToggleAutoThrottle ( void ) {
1611 // Remove at a later date
1614 APData = APDataGlobal;
1617 if ( APData->auto_throttle ) {
1618 // turn off altitude hold
1619 APData->auto_throttle = false;
1621 // turn on terrain follow, lock at current agl
1622 APData->auto_throttle = true;
1623 APData->TargetSpeed = get_speed();
1624 APData->speed_error_accum = 0.0;
1627 get_control_values();
1628 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: ("
1629 << APData->auto_throttle << ") " << APData->TargetSpeed );
1632 void fgAPToggleTerrainFollow( void ) {
1633 // Remove at a later date
1636 APData = APDataGlobal;
1639 if ( APData->terrain_follow ) {
1640 // turn off altitude hold
1641 APData->terrain_follow = false;
1643 // turn on terrain follow, lock at current agl
1644 APData->terrain_follow = true;
1645 APData->altitude_hold = false;
1646 APData->TargetAGL = fgAPget_agl();
1647 APData->alt_error_accum = 0.0;
1649 get_control_values();
1651 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetTerrainFollow: ("
1652 << APData->terrain_follow << ") " << APData->TargetAGL );
1655 static double NormalizeDegrees( double Input ) {
1656 // normalize the input to the range (-180,180]
1657 // Input should not be greater than -360 to 360.
1658 // Current rules send the output to an undefined state.
1662 else if ( Input <= -180 )
1663 while ( Input <= -180 )
1668 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
1669 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
1670 //assert(x1 != x2); // Divide by zero error. Cold abort for now
1673 // static double y = 0.0;
1674 // double dx = x2 -x1;
1675 // if( (dx < -FG_EPSILON ) || ( dx > FG_EPSILON ) )
1678 double m, b, y; // the constants to find in y=mx+b
1681 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
1683 b = y1 - m * x1; // calculate the b
1685 y = m * x + b; // the final calculation