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 // Climb speed constants
50 const double min_climb = 70.0; // kts
51 const double best_climb = 75.0; // kts
52 const double ideal_climb_rate = 500.0; // fpm
54 /// These statics will eventually go into the class
55 /// they are just here while I am experimenting -- NHV :-)
56 // AutoPilot Gain Adjuster members
57 static double MaxRollAdjust; // MaxRollAdjust = 2 * APData->MaxRoll;
58 static double RollOutAdjust; // RollOutAdjust = 2 * APData->RollOut;
59 static double MaxAileronAdjust; // MaxAileronAdjust = 2 * APData->MaxAileron;
60 static double RollOutSmoothAdjust; // RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
62 static float MaxRollValue; // 0.1 -> 1.0
63 static float RollOutValue;
64 static float MaxAileronValue;
65 static float RollOutSmoothValue;
67 static float TmpMaxRollValue; // for cancel operation
68 static float TmpRollOutValue;
69 static float TmpMaxAileronValue;
70 static float TmpRollOutSmoothValue;
72 static puDialogBox *APAdjustDialog;
73 static puFrame *APAdjustFrame;
74 static puText *APAdjustDialogMessage;
75 static puFont APAdjustLegendFont;
76 static puFont APAdjustLabelFont;
78 static puOneShot *APAdjustOkButton;
79 static puOneShot *APAdjustResetButton;
80 static puOneShot *APAdjustCancelButton;
82 //static puButton *APAdjustDragButton;
84 static puText *APAdjustMaxRollTitle;
85 static puText *APAdjustRollOutTitle;
86 static puText *APAdjustMaxAileronTitle;
87 static puText *APAdjustRollOutSmoothTitle;
89 static puText *APAdjustMaxAileronText;
90 static puText *APAdjustMaxRollText;
91 static puText *APAdjustRollOutText;
92 static puText *APAdjustRollOutSmoothText;
94 static mySlider *APAdjustHS0;
95 static mySlider *APAdjustHS1;
96 static mySlider *APAdjustHS2;
97 static mySlider *APAdjustHS3;
99 static char SliderText[ 4 ][ 8 ];
101 ///////// AutoPilot New Heading Dialog
103 static puDialogBox *ApHeadingDialog;
104 static puFrame *ApHeadingDialogFrame;
105 static puText *ApHeadingDialogMessage;
106 static puInput *ApHeadingDialogInput;
107 static puOneShot *ApHeadingDialogOkButton;
108 static puOneShot *ApHeadingDialogCancelButton;
111 ///////// AutoPilot New Altitude Dialog
113 static puDialogBox *ApAltitudeDialog = 0;
114 static puFrame *ApAltitudeDialogFrame = 0;
115 static puText *ApAltitudeDialogMessage = 0;
116 static puInput *ApAltitudeDialogInput = 0;
118 static puOneShot *ApAltitudeDialogOkButton = 0;
119 static puOneShot *ApAltitudeDialogCancelButton = 0;
122 /// The beginnings of Lock AutoPilot to target location :-)
123 // Needs cleaning up but works
124 // These statics should disapear when this is a class
125 static puDialogBox *TgtAptDialog = 0;
126 static puFrame *TgtAptDialogFrame = 0;
127 static puText *TgtAptDialogMessage = 0;
128 static puInput *TgtAptDialogInput = 0;
130 static char NewTgtAirportId[16];
131 static char NewTgtAirportLabel[] = "Enter New TgtAirport ID";
133 static puOneShot *TgtAptDialogOkButton = 0;
134 static puOneShot *TgtAptDialogCancelButton = 0;
135 static puOneShot *TgtAptDialogResetButton = 0;
138 // global variable holding the AP info
139 // I want this gone. Data should be in aircraft structure
140 fgAPDataPtr APDataGlobal;
142 // Local Prototype section
143 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 );
144 static double NormalizeDegrees( double Input );
145 // End Local ProtoTypes
147 extern char *coord_format_lat(float);
148 extern char *coord_format_lon(float);
151 static inline double get_ground_speed( void ) {
152 // starts in ft/s so we convert to kts
153 double ft_s = cur_fdm_state->get_V_ground_speed()
154 * current_options.get_speed_up();;
155 double kts = ft_s * FEET_TO_METER * 3600 * METER_TO_NM;
159 // The below routines were copied right from hud.c ( I hate reinventing
160 // the wheel more than necessary)
162 // The following routines obtain information concerntin the aircraft's
163 // current state and return it to calling instrument display routines.
164 // They should eventually be member functions of the aircraft.
166 static void get_control_values( void ) {
168 APData = APDataGlobal;
169 APData->old_aileron = controls.get_aileron();
170 APData->old_elevator = controls.get_elevator();
171 APData->old_elevator_trim = controls.get_elevator_trim();
172 APData->old_rudder = controls.get_rudder();
175 static void MakeTargetHeadingStr( fgAPDataPtr APData, double bearing ) {
178 else if(bearing > 360. )
180 sprintf(APData->TargetHeadingStr, "APHeading %6.1f", bearing);
183 static inline void MakeTargetDistanceStr( fgAPDataPtr APData, double distance ) {
184 double eta = distance*METER_TO_NM / get_ground_speed();
185 if ( eta >= 100.0 ) { eta = 99.999; }
187 if ( eta < (1.0/6.0) ) {
188 // within 10 minutes, bump up to min/secs
192 minor = (int)((eta - (int)eta) * 60.0);
193 sprintf(APData->TargetDistanceStr, "APDistance %.2f NM ETA %d:%02d",
194 distance*METER_TO_NM, major, minor);
195 // cout << "distance = " << distance*METER_TO_NM
196 // << " gndsp = " << get_ground_speed()
197 // << " time = " << eta
198 // << " major = " << major
199 // << " minor = " << minor
203 static inline void MakeTargetAltitudeStr( fgAPDataPtr APData, double altitude ) {
204 sprintf(APData->TargetAltitudeStr, "APAltitude %6.0f", altitude);
207 static inline void MakeTargetLatLonStr( fgAPDataPtr APData, double lat, double lon ) {
209 tmp = APData->TargetLatitude;
210 sprintf( APData->TargetLatitudeStr , "%s", coord_format_lat(tmp) );
211 tmp = APData->TargetLongitude;
212 sprintf( APData->TargetLongitudeStr, "%s", coord_format_lon(tmp) );
214 sprintf(APData->TargetLatLonStr, "%s %s",
215 APData->TargetLatitudeStr,
216 APData->TargetLongitudeStr );
219 static inline double get_speed( void ) {
220 return( cur_fdm_state->get_V_equiv_kts() );
223 static inline double get_aoa( void ) {
224 return( cur_fdm_state->get_Gamma_vert_rad() * RAD_TO_DEG );
227 static inline double fgAPget_latitude( void ) {
228 return( cur_fdm_state->get_Latitude() * RAD_TO_DEG );
231 static inline double fgAPget_longitude( void ) {
232 return( cur_fdm_state->get_Longitude() * RAD_TO_DEG );
235 static inline double fgAPget_roll( void ) {
236 return( cur_fdm_state->get_Phi() * RAD_TO_DEG );
239 static inline double get_pitch( void ) {
240 return( cur_fdm_state->get_Theta() );
243 double fgAPget_heading( void ) {
244 return( cur_fdm_state->get_Psi() * RAD_TO_DEG );
247 static inline double fgAPget_altitude( void ) {
248 return( cur_fdm_state->get_Altitude() * FEET_TO_METER );
251 static inline double fgAPget_climb( void ) {
252 // return in meters per minute
253 return( cur_fdm_state->get_Climb_Rate() * FEET_TO_METER * 60 );
256 static inline double get_sideslip( void ) {
257 return( cur_fdm_state->get_Beta() );
260 static inline double fgAPget_agl( void ) {
263 agl = cur_fdm_state->get_Altitude() * FEET_TO_METER
269 // End of copied section. ( thanks for the wheel :-)
270 double fgAPget_TargetLatitude( void ) {
271 fgAPDataPtr APData = APDataGlobal;
272 return APData->TargetLatitude;
275 double fgAPget_TargetLongitude( void ) {
276 fgAPDataPtr APData = APDataGlobal;
277 return APData->TargetLongitude;
280 double fgAPget_TargetHeading( void ) {
281 fgAPDataPtr APData = APDataGlobal;
282 return APData->TargetHeading;
285 double fgAPget_TargetDistance( void ) {
286 fgAPDataPtr APData = APDataGlobal;
287 return APData->TargetDistance;
290 double fgAPget_TargetAltitude( void ) {
291 fgAPDataPtr APData = APDataGlobal;
292 return APData->TargetAltitude;
295 char *fgAPget_TargetLatitudeStr( void ) {
296 fgAPDataPtr APData = APDataGlobal;
297 return APData->TargetLatitudeStr;
300 char *fgAPget_TargetLongitudeStr( void ) {
301 fgAPDataPtr APData = APDataGlobal;
302 return APData->TargetLongitudeStr;
305 char *fgAPget_TargetDistanceStr( void ) {
306 fgAPDataPtr APData = APDataGlobal;
307 return APData->TargetDistanceStr;
310 char *fgAPget_TargetHeadingStr( void ) {
311 fgAPDataPtr APData = APDataGlobal;
312 return APData->TargetHeadingStr;
315 char *fgAPget_TargetAltitudeStr( void ) {
316 fgAPDataPtr APData = APDataGlobal;
317 return APData->TargetAltitudeStr;
320 char *fgAPget_TargetLatLonStr( void ) {
321 fgAPDataPtr APData = APDataGlobal;
322 return APData->TargetLatLonStr;
325 bool fgAPWayPointEnabled( void )
329 APData = APDataGlobal;
331 // heading hold enabled?
332 return APData->waypoint_hold;
336 bool fgAPHeadingEnabled( void )
340 APData = APDataGlobal;
342 // heading hold enabled?
343 return APData->heading_hold;
346 bool fgAPAltitudeEnabled( void )
350 APData = APDataGlobal;
352 // altitude hold or terrain follow enabled?
353 return APData->altitude_hold;
356 bool fgAPTerrainFollowEnabled( void )
360 APData = APDataGlobal;
362 // altitude hold or terrain follow enabled?
363 return APData->terrain_follow ;
366 bool fgAPAutoThrottleEnabled( void )
370 APData = APDataGlobal;
372 // autothrottle enabled?
373 return APData->auto_throttle;
376 void fgAPAltitudeAdjust( double inc )
378 // Remove at a later date
379 fgAPDataPtr APData = APDataGlobal;
382 double target_alt, target_agl;
384 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
385 target_alt = APData->TargetAltitude * METER_TO_FEET;
386 target_agl = APData->TargetAGL * METER_TO_FEET;
388 target_alt = APData->TargetAltitude;
389 target_agl = APData->TargetAGL;
392 target_alt = ( int ) ( target_alt / inc ) * inc + inc;
393 target_agl = ( int ) ( target_agl / inc ) * inc + inc;
395 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET ) {
396 target_alt *= FEET_TO_METER;
397 target_agl *= FEET_TO_METER;
400 APData->TargetAltitude = target_alt;
401 APData->TargetAGL = target_agl;
403 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
404 target_alt *= METER_TO_FEET;
405 ApAltitudeDialogInput->setValue((float)target_alt);
406 MakeTargetAltitudeStr( APData, target_alt);
408 get_control_values();
411 void fgAPAltitudeSet( double new_altitude ) {
412 // Remove at a later date
413 fgAPDataPtr APData = APDataGlobal;
415 double target_alt = new_altitude;
417 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
418 target_alt = new_altitude * FEET_TO_METER;
420 if( target_alt < scenery.cur_elev )
421 target_alt = scenery.cur_elev;
423 APData->TargetAltitude = target_alt;
425 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
426 target_alt *= METER_TO_FEET;
427 ApAltitudeDialogInput->setValue((float)target_alt);
428 MakeTargetAltitudeStr( APData, target_alt);
430 get_control_values();
433 void fgAPHeadingAdjust( double inc ) {
434 fgAPDataPtr APData = APDataGlobal;
436 if ( APData->waypoint_hold )
437 APData->waypoint_hold = false;
439 double target = ( int ) ( APData->TargetHeading / inc ) * inc + inc;
441 APData->TargetHeading = NormalizeDegrees( target );
442 // following cast needed ambiguous plib
443 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
444 MakeTargetHeadingStr( APData, APData->TargetHeading );
445 get_control_values();
448 void fgAPHeadingSet( double new_heading ) {
449 fgAPDataPtr APData = APDataGlobal;
451 if ( APData->waypoint_hold )
452 APData->waypoint_hold = false;
454 new_heading = NormalizeDegrees( new_heading );
455 APData->TargetHeading = new_heading;
456 // following cast needed ambiguous plib
457 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
458 MakeTargetHeadingStr( APData, APData->TargetHeading );
459 get_control_values();
462 void fgAPAutoThrottleAdjust( double inc ) {
463 fgAPDataPtr APData = APDataGlobal;
465 double target = ( int ) ( APData->TargetSpeed / inc ) * inc + inc;
467 APData->TargetSpeed = target;
470 // THIS NEEDS IMPROVEMENT !!!!!!!!!!!!!
471 static int scan_number(char *s, double *new_value)
476 char *WordBufPtr = WordBuf;
481 *WordBufPtr++ = *cptr++;
483 while (isdigit(*cptr) ) {
484 *WordBufPtr++ = *cptr++;
488 *WordBufPtr++ = *cptr++; // put the '.' into the string
489 while (isdigit(*cptr)) {
490 *WordBufPtr++ = *cptr++;
495 sscanf(WordBuf, "%lf", new_value);
502 void ApHeadingDialog_Cancel(puObject *)
504 ApHeadingDialogInput->rejectInput();
505 FG_POP_PUI_DIALOG( ApHeadingDialog );
508 void ApHeadingDialog_OK (puObject *me)
513 ApHeadingDialogInput -> getValue( &c );
517 if( scan_number( c, &NewHeading ) )
519 if(!fgAPHeadingEnabled())
521 fgAPHeadingSet( NewHeading );
525 s += " is not a valid number.";
528 ApHeadingDialog_Cancel(me);
529 if( error ) mkDialog(s.c_str());
532 void NewHeading(puObject *cb)
534 // string ApHeadingLabel( "Enter New Heading" );
535 // ApHeadingDialogMessage -> setLabel(ApHeadingLabel.c_str());
536 ApHeadingDialogInput -> acceptInput();
537 FG_PUSH_PUI_DIALOG( ApHeadingDialog );
540 void NewHeadingInit(void)
542 // printf("NewHeadingInit\n");
543 char NewHeadingLabel[] = "Enter New Heading";
546 float heading = fgAPget_heading();
548 (puGetStringWidth( puGetDefaultLabelFont(), NewHeadingLabel ) /2 );
550 ApHeadingDialog = new puDialogBox (150, 50);
552 ApHeadingDialogFrame = new puFrame (0, 0, 260, 150);
554 ApHeadingDialogMessage = new puText (len, 110);
555 ApHeadingDialogMessage -> setDefaultValue (NewHeadingLabel);
556 ApHeadingDialogMessage -> getDefaultValue (&s);
557 ApHeadingDialogMessage -> setLabel (s);
559 ApHeadingDialogInput = new puInput ( 50, 70, 210, 100 );
560 ApHeadingDialogInput -> setValue ( heading );
562 ApHeadingDialogOkButton = new puOneShot (50, 10, 110, 50);
563 ApHeadingDialogOkButton -> setLegend (gui_msg_OK);
564 ApHeadingDialogOkButton -> makeReturnDefault (TRUE);
565 ApHeadingDialogOkButton -> setCallback (ApHeadingDialog_OK);
567 ApHeadingDialogCancelButton = new puOneShot (140, 10, 210, 50);
568 ApHeadingDialogCancelButton -> setLegend (gui_msg_CANCEL);
569 ApHeadingDialogCancelButton -> setCallback (ApHeadingDialog_Cancel);
572 FG_FINALIZE_PUI_DIALOG( ApHeadingDialog );
575 void ApAltitudeDialog_Cancel(puObject *)
577 ApAltitudeDialogInput -> rejectInput();
578 FG_POP_PUI_DIALOG( ApAltitudeDialog );
581 void ApAltitudeDialog_OK (puObject *me)
586 ApAltitudeDialogInput->getValue( &c );
590 if( scan_number( c, &NewAltitude) )
592 if(!(fgAPAltitudeEnabled()))
593 fgAPToggleAltitude();
594 fgAPAltitudeSet( NewAltitude );
598 s += " is not a valid number.";
601 ApAltitudeDialog_Cancel(me);
602 if( error ) mkDialog(s.c_str());
603 get_control_values();
606 void NewAltitude(puObject *cb)
608 ApAltitudeDialogInput -> acceptInput();
609 FG_PUSH_PUI_DIALOG( ApAltitudeDialog );
612 void NewAltitudeInit(void)
614 // printf("NewAltitudeInit\n");
615 char NewAltitudeLabel[] = "Enter New Altitude";
618 float alt = cur_fdm_state->get_Altitude();
620 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_METERS) {
621 alt *= FEET_TO_METER;
625 (puGetStringWidth( puGetDefaultLabelFont(), NewAltitudeLabel )/2);
627 // ApAltitudeDialog = new puDialogBox (150, 50);
628 ApAltitudeDialog = new puDialogBox (150, 200);
630 ApAltitudeDialogFrame = new puFrame (0, 0, 260, 150);
631 ApAltitudeDialogMessage = new puText (len, 110);
632 ApAltitudeDialogMessage -> setDefaultValue (NewAltitudeLabel);
633 ApAltitudeDialogMessage -> getDefaultValue (&s);
634 ApAltitudeDialogMessage -> setLabel (s);
636 ApAltitudeDialogInput = new puInput ( 50, 70, 210, 100 );
637 ApAltitudeDialogInput -> setValue ( alt );
638 // Uncomment the next line to have input active on startup
639 // ApAltitudeDialogInput -> acceptInput ( );
640 // cursor at begining or end of line ?
643 // ApAltitudeDialogInput -> setCursor ( len );
644 // ApAltitudeDialogInput -> setSelectRegion ( 5, 9 );
646 ApAltitudeDialogOkButton = new puOneShot (50, 10, 110, 50);
647 ApAltitudeDialogOkButton -> setLegend (gui_msg_OK);
648 ApAltitudeDialogOkButton -> makeReturnDefault (TRUE);
649 ApAltitudeDialogOkButton -> setCallback (ApAltitudeDialog_OK);
651 ApAltitudeDialogCancelButton = new puOneShot (140, 10, 210, 50);
652 ApAltitudeDialogCancelButton -> setLegend (gui_msg_CANCEL);
653 ApAltitudeDialogCancelButton -> setCallback (ApAltitudeDialog_Cancel);
656 FG_FINALIZE_PUI_DIALOG( ApAltitudeDialog );
659 /////// simple AutoPilot GAIN / LIMITS ADJUSTER
661 #define fgAP_CLAMP(val,min,max) \
662 ( (val) = (val) > (max) ? (max) : (val) < (min) ? (min) : (val) )
664 static void maxroll_adj( puObject *hs ) {
666 fgAPDataPtr APData = APDataGlobal;
668 hs-> getValue ( &val ) ;
669 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
670 // printf ( "maxroll_adj( %p ) %f %f\n", hs, val, MaxRollAdjust * val ) ;
671 APData-> MaxRoll = MaxRollAdjust * val;
672 sprintf( SliderText[ 0 ], "%05.2f", APData->MaxRoll );
673 APAdjustMaxRollText -> setLabel ( SliderText[ 0 ] ) ;
676 static void rollout_adj( puObject *hs ) {
678 fgAPDataPtr APData = APDataGlobal;
680 hs-> getValue ( &val ) ;
681 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
682 // printf ( "rollout_adj( %p ) %f %f\n", hs, val, RollOutAdjust * val ) ;
683 APData-> RollOut = RollOutAdjust * val;
684 sprintf( SliderText[ 1 ], "%05.2f", APData->RollOut );
685 APAdjustRollOutText -> setLabel ( SliderText[ 1 ] );
688 static void maxaileron_adj( puObject *hs ) {
691 APData = APDataGlobal;
693 hs-> getValue ( &val ) ;
694 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
695 // printf ( "maxaileron_adj( %p ) %f %f\n", hs, val, MaxAileronAdjust * val ) ;
696 APData-> MaxAileron = MaxAileronAdjust * val;
697 sprintf( SliderText[ 3 ], "%05.2f", APData->MaxAileron );
698 APAdjustMaxAileronText -> setLabel ( SliderText[ 3 ] );
701 static void rolloutsmooth_adj( puObject *hs ) {
703 fgAPDataPtr APData = APDataGlobal;
705 hs -> getValue ( &val ) ;
706 fgAP_CLAMP ( val, 0.1, 1.0 ) ;
707 // printf ( "rolloutsmooth_adj( %p ) %f %f\n", hs, val, RollOutSmoothAdjust * val ) ;
708 APData->RollOutSmooth = RollOutSmoothAdjust * val;
709 sprintf( SliderText[ 2 ], "%5.2f", APData-> RollOutSmooth );
710 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
714 static void goAwayAPAdjust (puObject *)
716 FG_POP_PUI_DIALOG( APAdjustDialog );
719 void cancelAPAdjust( puObject *self ) {
720 fgAPDataPtr APData = APDataGlobal;
722 APData-> MaxRoll = TmpMaxRollValue;
723 APData-> RollOut = TmpRollOutValue;
724 APData-> MaxAileron = TmpMaxAileronValue;
725 APData-> RollOutSmooth = TmpRollOutSmoothValue;
727 goAwayAPAdjust(self);
730 void resetAPAdjust( puObject *self ) {
731 fgAPDataPtr APData = APDataGlobal;
733 APData-> MaxRoll = MaxRollAdjust / 2;
734 APData-> RollOut = RollOutAdjust / 2;
735 APData-> MaxAileron = MaxAileronAdjust / 2;
736 APData-> RollOutSmooth = RollOutSmoothAdjust / 2;
738 FG_POP_PUI_DIALOG( APAdjustDialog );
743 void fgAPAdjust( puObject * ) {
744 fgAPDataPtr APData = APDataGlobal;
746 TmpMaxRollValue = APData-> MaxRoll;
747 TmpRollOutValue = APData-> RollOut;
748 TmpMaxAileronValue = APData-> MaxAileron;
749 TmpRollOutSmoothValue = APData-> RollOutSmooth;
751 MaxRollValue = APData-> MaxRoll / MaxRollAdjust;
752 RollOutValue = APData-> RollOut / RollOutAdjust;
753 MaxAileronValue = APData-> MaxAileron / MaxAileronAdjust;
754 RollOutSmoothValue = APData-> RollOutSmooth / RollOutSmoothAdjust;
756 APAdjustHS0-> setValue ( MaxRollValue ) ;
757 APAdjustHS1-> setValue ( RollOutValue ) ;
758 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
759 APAdjustHS3-> setValue ( MaxAileronValue ) ;
761 FG_PUSH_PUI_DIALOG( APAdjustDialog );
764 // Done once at system initialization
765 void fgAPAdjustInit( void ) {
767 // printf("fgAPAdjustInit\n");
768 #define HORIZONTAL FALSE
772 int DialogWidth = 230;
774 char Label[] = "AutoPilot Adjust";
777 fgAPDataPtr APData = APDataGlobal;
779 int labelX = (DialogWidth / 2) -
780 (puGetStringWidth( puGetDefaultLabelFont(), Label ) / 2);
781 labelX -= 30; // KLUDGEY
786 int slider_width = 210;
787 int slider_title_x = 15;
788 int slider_value_x = 160;
789 float slider_delta = 0.1f;
791 TmpMaxRollValue = APData-> MaxRoll;
792 TmpRollOutValue = APData-> RollOut;
793 TmpMaxAileronValue = APData-> MaxAileron;
794 TmpRollOutSmoothValue = APData-> RollOutSmooth;
796 MaxRollValue = APData-> MaxRoll / MaxRollAdjust;
797 RollOutValue = APData-> RollOut / RollOutAdjust;
798 MaxAileronValue = APData-> MaxAileron / MaxAileronAdjust;
799 RollOutSmoothValue = APData-> RollOutSmooth / RollOutSmoothAdjust;
801 puGetDefaultFonts ( &APAdjustLegendFont, &APAdjustLabelFont );
802 APAdjustDialog = new puDialogBox ( DialogX, DialogY ); {
803 int horiz_slider_height = puGetStringHeight (APAdjustLabelFont) +
804 puGetStringDescender (APAdjustLabelFont) +
805 PUSTR_TGAP + PUSTR_BGAP + 5;
807 APAdjustFrame = new puFrame ( 0, 0,
808 DialogWidth, 85 + nSliders * horiz_slider_height );
810 APAdjustDialogMessage = new puText ( labelX, 52 + nSliders * horiz_slider_height );
811 APAdjustDialogMessage -> setDefaultValue ( Label );
812 APAdjustDialogMessage -> getDefaultValue ( &s );
813 APAdjustDialogMessage -> setLabel ( s );
815 APAdjustHS0 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
816 APAdjustHS0-> setDelta ( slider_delta ) ;
817 APAdjustHS0-> setValue ( MaxRollValue ) ;
818 APAdjustHS0-> setCBMode ( PUSLIDER_DELTA ) ;
819 APAdjustHS0-> setCallback ( maxroll_adj ) ;
821 sprintf( SliderText[ 0 ], "%05.2f", APData->MaxRoll );
822 APAdjustMaxRollTitle = new puText ( slider_title_x, slider_y ) ;
823 APAdjustMaxRollTitle-> setDefaultValue ( "MaxRoll" ) ;
824 APAdjustMaxRollTitle-> getDefaultValue ( &s ) ;
825 APAdjustMaxRollTitle-> setLabel ( s ) ;
826 APAdjustMaxRollText = new puText ( slider_value_x, slider_y ) ;
827 APAdjustMaxRollText-> setLabel ( SliderText[ 0 ] ) ;
829 slider_y += horiz_slider_height;
831 APAdjustHS1 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
832 APAdjustHS1-> setDelta ( slider_delta ) ;
833 APAdjustHS1-> setValue ( RollOutValue ) ;
834 APAdjustHS1-> setCBMode ( PUSLIDER_DELTA ) ;
835 APAdjustHS1-> setCallback ( rollout_adj ) ;
837 sprintf( SliderText[ 1 ], "%05.2f", APData->RollOut );
838 APAdjustRollOutTitle = new puText ( slider_title_x, slider_y ) ;
839 APAdjustRollOutTitle-> setDefaultValue ( "AdjustRollOut" ) ;
840 APAdjustRollOutTitle-> getDefaultValue ( &s ) ;
841 APAdjustRollOutTitle-> setLabel ( s ) ;
842 APAdjustRollOutText = new puText ( slider_value_x, slider_y ) ;
843 APAdjustRollOutText-> setLabel ( SliderText[ 1 ] );
845 slider_y += horiz_slider_height;
847 APAdjustHS2 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
848 APAdjustHS2-> setDelta ( slider_delta ) ;
849 APAdjustHS2-> setValue ( RollOutSmoothValue ) ;
850 APAdjustHS2-> setCBMode ( PUSLIDER_DELTA ) ;
851 APAdjustHS2-> setCallback ( rolloutsmooth_adj ) ;
853 sprintf( SliderText[ 2 ], "%5.2f", APData->RollOutSmooth );
854 APAdjustRollOutSmoothTitle = new puText ( slider_title_x, slider_y ) ;
855 APAdjustRollOutSmoothTitle-> setDefaultValue ( "RollOutSmooth" ) ;
856 APAdjustRollOutSmoothTitle-> getDefaultValue ( &s ) ;
857 APAdjustRollOutSmoothTitle-> setLabel ( s ) ;
858 APAdjustRollOutSmoothText = new puText ( slider_value_x, slider_y ) ;
859 APAdjustRollOutSmoothText-> setLabel ( SliderText[ 2 ] );
861 slider_y += horiz_slider_height;
863 APAdjustHS3 = new mySlider ( slider_x, slider_y, slider_width, HORIZONTAL ) ;
864 APAdjustHS3-> setDelta ( slider_delta ) ;
865 APAdjustHS3-> setValue ( MaxAileronValue ) ;
866 APAdjustHS3-> setCBMode ( PUSLIDER_DELTA ) ;
867 APAdjustHS3-> setCallback ( maxaileron_adj ) ;
869 sprintf( SliderText[ 3 ], "%05.2f", APData->MaxAileron );
870 APAdjustMaxAileronTitle = new puText ( slider_title_x, slider_y ) ;
871 APAdjustMaxAileronTitle-> setDefaultValue ( "MaxAileron" ) ;
872 APAdjustMaxAileronTitle-> getDefaultValue ( &s ) ;
873 APAdjustMaxAileronTitle-> setLabel ( s ) ;
874 APAdjustMaxAileronText = new puText ( slider_value_x, slider_y ) ;
875 APAdjustMaxAileronText-> setLabel ( SliderText[ 3 ] );
877 APAdjustOkButton = new puOneShot ( 10, 10, 60, 50 );
878 APAdjustOkButton-> setLegend ( gui_msg_OK );
879 APAdjustOkButton-> makeReturnDefault ( TRUE );
880 APAdjustOkButton-> setCallback ( goAwayAPAdjust );
882 APAdjustCancelButton = new puOneShot ( 70, 10, 150, 50 );
883 APAdjustCancelButton-> setLegend ( gui_msg_CANCEL );
884 APAdjustCancelButton-> setCallback ( cancelAPAdjust );
886 APAdjustResetButton = new puOneShot ( 160, 10, 220, 50 );
887 APAdjustResetButton-> setLegend ( gui_msg_RESET );
888 APAdjustResetButton-> setCallback ( resetAPAdjust );
890 FG_FINALIZE_PUI_DIALOG( APAdjustDialog );
895 // Simple Dialog to input Target Airport
896 void TgtAptDialog_Cancel(puObject *)
898 FG_POP_PUI_DIALOG( TgtAptDialog );
901 void TgtAptDialog_OK (puObject *)
904 APData = APDataGlobal;
907 // FGTime *t = FGTime::cur_time_params;
908 // int PauseMode = t->getPause();
910 // t->togglePauseMode();
913 TgtAptDialogInput->getValue(&s);
916 TgtAptDialog_Cancel( NULL );
918 if ( TgtAptId.length() ) {
919 // set initial position from TgtAirport id
924 FG_LOG( FG_GENERAL, FG_INFO,
925 "Attempting to set starting position from airport code "
928 airports.load("apt_simple");
929 if ( airports.search( TgtAptId, &a ) )
931 double course, reverse, distance;
932 // fgAPset_tgt_airport_id( TgtAptId.c_str() );
933 current_options.set_airport_id( TgtAptId.c_str() );
934 sprintf( NewTgtAirportId, "%s", TgtAptId.c_str() );
936 APData->TargetLatitude = a.latitude; // * DEG_TO_RAD;
937 APData->TargetLongitude = a.longitude; // * DEG_TO_RAD;
938 MakeTargetLatLonStr( APData,
939 APData->TargetLatitude,
940 APData->TargetLongitude);
942 APData->old_lat = fgAPget_latitude();
943 APData->old_lon = fgAPget_longitude();
945 // need to test for iter
946 if( ! geo_inverse_wgs_84( fgAPget_altitude(),
949 APData->TargetLatitude,
950 APData->TargetLongitude,
954 APData->TargetHeading = course;
955 MakeTargetHeadingStr( APData, APData->TargetHeading );
956 APData->TargetDistance = distance;
957 MakeTargetDistanceStr( APData, distance );
958 // This changes the AutoPilot Heading
959 // following cast needed
960 ApHeadingDialogInput->
961 setValue((float)APData->TargetHeading);
963 APData->waypoint_hold = true ;
964 APData->heading_hold = true;
967 TgtAptId += " not in database.";
968 mkDialog(TgtAptId.c_str());
971 get_control_values();
972 // if( PauseMode != t->getPause() )
973 // t->togglePauseMode();
976 void TgtAptDialog_Reset(puObject *)
978 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
979 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
980 TgtAptDialogInput->setValue ( NewTgtAirportId );
981 TgtAptDialogInput->setCursor( 0 ) ;
984 void NewTgtAirport(puObject *cb)
986 // strncpy( NewAirportId, current_options.get_airport_id().c_str(), 16 );
987 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
988 TgtAptDialogInput->setValue( NewTgtAirportId );
990 FG_PUSH_PUI_DIALOG( TgtAptDialog );
993 void NewTgtAirportInit(void)
995 FG_LOG( FG_AUTOPILOT, FG_INFO, " enter NewTgtAirportInit()" );
996 // fgAPset_tgt_airport_id( current_options.get_airport_id() );
997 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
998 FG_LOG( FG_AUTOPILOT, FG_INFO, " NewTgtAirportId " << NewTgtAirportId );
999 // printf(" NewTgtAirportId %s\n", NewTgtAirportId);
1000 int len = 150 - puGetStringWidth( puGetDefaultLabelFont(),
1001 NewTgtAirportLabel ) / 2;
1003 TgtAptDialog = new puDialogBox (150, 50);
1005 TgtAptDialogFrame = new puFrame (0,0,350, 150);
1006 TgtAptDialogMessage = new puText (len, 110);
1007 TgtAptDialogMessage -> setLabel (NewTgtAirportLabel);
1009 TgtAptDialogInput = new puInput (50, 70, 300, 100);
1010 TgtAptDialogInput -> setValue (NewTgtAirportId);
1011 TgtAptDialogInput -> acceptInput();
1013 TgtAptDialogOkButton = new puOneShot (50, 10, 110, 50);
1014 TgtAptDialogOkButton -> setLegend (gui_msg_OK);
1015 TgtAptDialogOkButton -> setCallback (TgtAptDialog_OK);
1016 TgtAptDialogOkButton -> makeReturnDefault(TRUE);
1018 TgtAptDialogCancelButton = new puOneShot (140, 10, 210, 50);
1019 TgtAptDialogCancelButton -> setLegend (gui_msg_CANCEL);
1020 TgtAptDialogCancelButton -> setCallback (TgtAptDialog_Cancel);
1022 TgtAptDialogResetButton = new puOneShot (240, 10, 300, 50);
1023 TgtAptDialogResetButton -> setLegend (gui_msg_RESET);
1024 TgtAptDialogResetButton -> setCallback (TgtAptDialog_Reset);
1026 FG_FINALIZE_PUI_DIALOG( TgtAptDialog );
1027 printf("leave NewTgtAirportInit()");
1031 // Finally actual guts of AutoPilot
1032 void fgAPInit( fgAIRCRAFT *current_aircraft ) {
1035 FG_LOG( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem" );
1037 APData = ( fgAPDataPtr ) calloc( sizeof( fgAPData ), 1 );
1039 if ( APData == NULL ) {
1040 // I couldn't get the mem. Dying
1041 FG_LOG( FG_AUTOPILOT, FG_ALERT, "No ram for Autopilot. Dying." );
1045 FG_LOG( FG_AUTOPILOT, FG_INFO, " Autopilot allocated " );
1047 APData->waypoint_hold = false ; // turn the target hold off
1048 APData->heading_hold = false ; // turn the heading hold off
1049 APData->altitude_hold = false ; // turn the altitude hold off
1051 // Initialize target location to startup location
1052 // FG_LOG( FG_AUTOPILOT, FG_INFO, " Autopilot setting startup location" );
1054 APData->TargetLatitude = fgAPget_latitude();
1056 APData->TargetLongitude = fgAPget_longitude();
1058 // FG_LOG( FG_AUTOPILOT, FG_INFO, " Autopilot setting TargetLatitudeStr" );
1059 MakeTargetLatLonStr( APData, APData->TargetLatitude, APData->TargetLongitude);
1061 APData->TargetHeading = 0.0; // default direction, due north
1062 APData->TargetAltitude = 3000; // default altitude in meters
1063 APData->alt_error_accum = 0.0;
1065 MakeTargetAltitudeStr( APData, 3000.0);
1066 MakeTargetHeadingStr( APData, 0.0 );
1068 // These eventually need to be read from current_aircaft somehow.
1072 // the maximum roll, in Deg
1073 APData->MaxRoll = 7;
1074 // the deg from heading to start rolling out at, in Deg
1075 APData->RollOut = 30;
1076 // how far can I move the aleron from center.
1077 APData->MaxAileron = .1;
1078 // Smoothing distance for alerion control
1079 APData->RollOutSmooth = 10;
1082 // the maximum roll, in Deg
1083 APData->MaxRoll = 20;
1085 // the deg from heading to start rolling out at, in Deg
1086 APData->RollOut = 20;
1088 // how far can I move the aleron from center.
1089 APData->MaxAileron = .2;
1091 // Smoothing distance for alerion control
1092 APData->RollOutSmooth = 10;
1094 //Remove at a later date
1095 APDataGlobal = APData;
1097 // Hardwired for now should be in options
1098 // 25% max control variablilty 0.5 / 2.0
1099 APData->disengage_threshold = 1.0;
1101 #if !defined( USING_SLIDER_CLASS )
1102 MaxRollAdjust = 2 * APData->MaxRoll;
1103 RollOutAdjust = 2 * APData->RollOut;
1104 MaxAileronAdjust = 2 * APData->MaxAileron;
1105 RollOutSmoothAdjust = 2 * APData->RollOutSmooth;
1106 #endif // !defined( USING_SLIDER_CLASS )
1108 get_control_values();
1110 // FG_LOG( FG_AUTOPILOT, FG_INFO, " calling NewTgtAirportInit" );
1111 NewTgtAirportInit();
1118 void fgAPReset( void ) {
1119 fgAPDataPtr APData = APDataGlobal;
1121 if ( fgAPTerrainFollowEnabled() )
1122 fgAPToggleTerrainFollow( );
1124 if ( fgAPAltitudeEnabled() )
1125 fgAPToggleAltitude();
1127 if ( fgAPHeadingEnabled() )
1128 fgAPToggleHeading();
1130 if ( fgAPAutoThrottleEnabled() )
1131 fgAPToggleAutoThrottle();
1133 APData->TargetHeading = 0.0; // default direction, due north
1134 MakeTargetHeadingStr( APData, APData->TargetHeading );
1136 APData->TargetAltitude = 3000; // default altitude in meters
1137 MakeTargetAltitudeStr( APData, 3000);
1139 APData->alt_error_accum = 0.0;
1142 get_control_values();
1144 sprintf( NewTgtAirportId, "%s", current_options.get_airport_id().c_str() );
1146 APData->TargetLatitude = fgAPget_latitude();
1147 APData->TargetLongitude = fgAPget_longitude();
1148 MakeTargetLatLonStr( APData,
1149 APData->TargetLatitude,
1150 APData->TargetLongitude);
1154 int fgAPRun( void ) {
1155 // Remove the following lines when the calling funcitons start
1156 // passing in the data pointer
1160 APData = APDataGlobal;
1163 // get control settings
1164 double aileron = controls.get_aileron();
1165 double elevator = controls.get_elevator();
1166 double elevator_trim = controls.get_elevator_trim();
1167 double rudder = controls.get_rudder();
1169 double lat = fgAPget_latitude();
1170 double lon = fgAPget_longitude();
1172 #ifdef FG_FORCE_AUTO_DISENGAGE
1173 // see if somebody else has changed them
1174 if( fabs(aileron - APData->old_aileron) > APData->disengage_threshold ||
1175 fabs(elevator - APData->old_elevator) > APData->disengage_threshold ||
1176 fabs(elevator_trim - APData->old_elevator_trim) >
1177 APData->disengage_threshold ||
1178 fabs(rudder - APData->old_rudder) > APData->disengage_threshold )
1180 // if controls changed externally turn autopilot off
1181 APData->waypoint_hold = false ; // turn the target hold off
1182 APData->heading_hold = false ; // turn the heading hold off
1183 APData->altitude_hold = false ; // turn the altitude hold off
1184 APData->terrain_follow = false; // turn the terrain_follow hold off
1185 // APData->auto_throttle = false; // turn the auto_throttle off
1187 // stash this runs control settings
1188 APData->old_aileron = aileron;
1189 APData->old_elevator = elevator;
1190 APData->old_elevator_trim = elevator_trim;
1191 APData->old_rudder = rudder;
1197 // waypoint hold enabled?
1198 if ( APData->waypoint_hold == true )
1200 double wp_course, wp_reverse, wp_distance;
1202 #ifdef DO_fgAP_CORRECTED_COURSE
1203 // compute course made good
1204 // this needs lots of special casing before use
1205 double course, reverse, distance, corrected_course;
1206 // need to test for iter
1207 geo_inverse_wgs_84( 0, //fgAPget_altitude(),
1215 #endif // DO_fgAP_CORRECTED_COURSE
1217 // compute course to way_point
1218 // need to test for iter
1219 if( ! geo_inverse_wgs_84( 0, //fgAPget_altitude(),
1222 APData->TargetLatitude,
1223 APData->TargetLongitude,
1228 #ifdef DO_fgAP_CORRECTED_COURSE
1229 corrected_course = course - wp_course;
1230 if( fabs(corrected_course) > 0.1 )
1231 printf("fgAP: course %f wp_course %f %f %f\n",
1232 course, wp_course, fabs(corrected_course), distance );
1233 #endif // DO_fgAP_CORRECTED_COURSE
1235 if ( wp_distance > 100 ) {
1236 // corrected_course = course - wp_course;
1237 APData->TargetHeading = NormalizeDegrees(wp_course);
1239 printf("APData->distance(%f) to close\n", wp_distance);
1240 // Real Close -- set heading hold to current heading
1241 // and Ring the arival bell !!
1242 NewTgtAirport(NULL);
1243 APData->waypoint_hold = false;
1245 APData->TargetHeading = fgAPget_heading();
1247 MakeTargetHeadingStr( APData, APData->TargetHeading );
1248 // Force this just in case
1249 APData->TargetDistance = wp_distance;
1250 MakeTargetDistanceStr( APData, wp_distance );
1251 // This changes the AutoPilot Heading Read Out
1252 // following cast needed
1253 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1255 APData->heading_hold = true;
1258 // heading hold enabled?
1259 if ( APData->heading_hold == true ) {
1266 NormalizeDegrees( APData->TargetHeading - fgAPget_heading() );
1267 // figure out how far off we are from desired heading
1269 // Now it is time to deterime how far we should be rolled.
1270 FG_LOG( FG_AUTOPILOT, FG_DEBUG, "RelHeading: " << RelHeading );
1273 // Check if we are further from heading than the roll out point
1274 if ( fabs( RelHeading ) > APData->RollOut ) {
1275 // set Target Roll to Max in desired direction
1276 if ( RelHeading < 0 ) {
1277 TargetRoll = 0 - APData->MaxRoll;
1279 TargetRoll = APData->MaxRoll;
1282 // We have to calculate the Target roll
1284 // This calculation engine thinks that the Target roll
1285 // should be a line from (RollOut,MaxRoll) to (-RollOut,
1286 // -MaxRoll) I hope this works well. If I get ambitious
1287 // some day this might become a fancier curve or
1290 TargetRoll = LinearExtrapolate( RelHeading, -APData->RollOut,
1291 -APData->MaxRoll, APData->RollOut,
1295 // Target Roll has now been Found.
1297 // Compare Target roll to Current Roll, Generate Rel Roll
1299 FG_LOG( FG_COCKPIT, FG_BULK, "TargetRoll: " << TargetRoll );
1301 RelRoll = NormalizeDegrees( TargetRoll - fgAPget_roll() );
1303 // Check if we are further from heading than the roll out smooth point
1304 if ( fabs( RelRoll ) > APData->RollOutSmooth ) {
1305 // set Target Roll to Max in desired direction
1306 if ( RelRoll < 0 ) {
1307 AileronSet = 0 - APData->MaxAileron;
1309 AileronSet = APData->MaxAileron;
1312 AileronSet = LinearExtrapolate( RelRoll, -APData->RollOutSmooth,
1313 -APData->MaxAileron,
1314 APData->RollOutSmooth,
1315 APData->MaxAileron );
1318 controls.set_aileron( AileronSet );
1319 controls.set_rudder( AileronSet / 2.0 );
1320 // controls.set_rudder( 0.0 );
1323 // altitude hold or terrain follow enabled?
1324 if ( APData->altitude_hold || APData->terrain_follow ) {
1325 double speed, max_climb, error;
1326 double prop_error, int_error;
1327 double prop_adj, int_adj, total_adj;
1329 if ( APData->altitude_hold ) {
1330 // normal altitude hold
1331 APData->TargetClimbRate =
1332 ( APData->TargetAltitude - fgAPget_altitude() ) * 8.0;
1333 } else if ( APData->terrain_follow ) {
1334 // brain dead ground hugging with no look ahead
1335 APData->TargetClimbRate =
1336 ( APData->TargetAGL - fgAPget_agl() ) * 16.0;
1337 // cout << "target agl = " << APData->TargetAGL
1338 // << " current agl = " << fgAPget_agl()
1339 // << " target climb rate = " << APData->TargetClimbRate
1342 // just try to zero out rate of climb ...
1343 APData->TargetClimbRate = 0.0;
1346 speed = get_speed();
1348 if ( speed < min_climb ) {
1350 } else if ( speed < best_climb ) {
1351 max_climb = ((best_climb - min_climb) - (best_climb - speed))
1353 / (best_climb - min_climb);
1355 max_climb = ( speed - best_climb ) * 10.0 + ideal_climb_rate;
1358 // this first one could be optional if we wanted to allow
1359 // better climb performance assuming we have the airspeed to
1361 if ( APData->TargetClimbRate > ideal_climb_rate ) {
1362 APData->TargetClimbRate = ideal_climb_rate;
1365 if ( APData->TargetClimbRate > max_climb ) {
1366 APData->TargetClimbRate = max_climb;
1369 if ( APData->TargetClimbRate < -ideal_climb_rate ) {
1370 APData->TargetClimbRate = -ideal_climb_rate;
1373 error = fgAPget_climb() - APData->TargetClimbRate;
1374 // cout << "climb rate = " << fgAPget_climb()
1375 // << " error = " << error << endl;
1377 // accumulate the error under the curve ... this really should
1379 APData->alt_error_accum += error;
1381 // calculate integral error, and adjustment amount
1382 int_error = APData->alt_error_accum;
1383 // printf("error = %.2f int_error = %.2f\n", error, int_error);
1384 int_adj = int_error / 8000.0;
1386 // caclulate proportional error
1388 prop_adj = prop_error / 2000.0;
1390 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
1391 // if ( total_adj > 0.6 ) {
1393 // } else if ( total_adj < -0.2 ) {
1394 // total_adj = -0.2;
1396 if ( total_adj > 1.0 ) {
1398 } else if ( total_adj < -1.0 ) {
1402 controls.set_elevator( total_adj );
1405 // auto throttle enabled?
1406 if ( APData->auto_throttle ) {
1408 double prop_error, int_error;
1409 double prop_adj, int_adj, total_adj;
1411 error = APData->TargetSpeed - get_speed();
1413 // accumulate the error under the curve ... this really should
1415 APData->speed_error_accum += error;
1416 if ( APData->speed_error_accum > 2000.0 ) {
1417 APData->speed_error_accum = 2000.0;
1419 else if ( APData->speed_error_accum < -2000.0 ) {
1420 APData->speed_error_accum = -2000.0;
1423 // calculate integral error, and adjustment amount
1424 int_error = APData->speed_error_accum;
1426 // printf("error = %.2f int_error = %.2f\n", error, int_error);
1427 int_adj = int_error / 200.0;
1429 // caclulate proportional error
1431 prop_adj = 0.5 + prop_error / 50.0;
1433 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
1434 if ( total_adj > 1.0 ) {
1437 else if ( total_adj < 0.0 ) {
1441 controls.set_throttle( FGControls::ALL_ENGINES, total_adj );
1444 #ifdef THIS_CODE_IS_NOT_USED
1445 if (APData->Mode == 2) // Glide slope hold
1450 // First, calculate Relative slope and normalize it
1451 RelSlope = NormalizeDegrees( APData->TargetSlope - get_pitch());
1453 // Now calculate the elevator offset from current angle
1454 if ( abs(RelSlope) > APData->SlopeSmooth )
1456 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
1457 RelElevator = -APData->MaxElevator;
1459 RelElevator = APData->MaxElevator;
1463 RelElevator = LinearExtrapolate(RelSlope,-APData->SlopeSmooth,-APData->MaxElevator,APData->SlopeSmooth,APData->MaxElevator);
1466 fgElevMove(RelElevator);
1469 #endif // THIS_CODE_IS_NOT_USED
1471 // stash this runs control settings
1472 // get_control_values();
1473 APData->old_aileron = controls.get_aileron();
1474 APData->old_elevator = controls.get_elevator();
1475 APData->old_elevator_trim = controls.get_elevator_trim();
1476 APData->old_rudder = controls.get_rudder();
1478 // for cross track error
1479 APData->old_lat = lat;
1480 APData->old_lon = lon;
1487 void fgAPSetMode( int mode)
1489 //Remove the following line when the calling funcitons start passing in the data pointer
1492 APData = APDataGlobal;
1495 fgPrintf( FG_COCKPIT, FG_INFO, "APSetMode : %d\n", mode );
1497 APData->Mode = mode; // set the new mode
1501 void fgAPset_tgt_airport_id( const string id ) {
1502 FG_LOG( FG_AUTOPILOT, FG_INFO, "entering fgAPset_tgt_airport_id " << id );
1504 APData = APDataGlobal;
1505 APData->tgt_airport_id = id;
1506 FG_LOG( FG_AUTOPILOT, FG_INFO, "leaving fgAPset_tgt_airport_id "
1507 << APData->tgt_airport_id );
1510 string fgAPget_tgt_airport_id( void ) {
1511 fgAPDataPtr APData = APDataGlobal;
1512 return APData->tgt_airport_id;
1516 void fgAPToggleHeading( void ) {
1517 // Remove at a later date
1520 APData = APDataGlobal;
1523 if ( APData->heading_hold || APData->waypoint_hold ) {
1524 // turn off heading hold
1525 APData->heading_hold = false;
1526 APData->waypoint_hold = false;
1528 // turn on heading hold, lock at current heading
1529 APData->heading_hold = true;
1530 APData->TargetHeading = fgAPget_heading();
1531 MakeTargetHeadingStr( APData, APData->TargetHeading );
1532 ApHeadingDialogInput -> setValue ((float)APData->TargetHeading );
1535 get_control_values();
1536 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetHeading: ("
1537 << APData->heading_hold << ") " << APData->TargetHeading );
1540 void fgAPToggleWayPoint( void ) {
1541 // Remove at a later date
1544 APData = APDataGlobal;
1547 if ( APData->waypoint_hold ) {
1548 // turn off location hold
1549 APData->waypoint_hold = false;
1550 // set heading hold to current heading
1551 // APData->heading_hold = true;
1552 APData->TargetHeading = fgAPget_heading();
1554 double course, reverse, distance;
1555 // turn on location hold
1556 // turn on heading hold
1557 APData->old_lat = fgAPget_latitude();
1558 APData->old_lon = fgAPget_longitude();
1560 // need to test for iter
1561 if(!geo_inverse_wgs_84( fgAPget_altitude(),
1563 fgAPget_longitude(),
1564 APData->TargetLatitude,
1565 APData->TargetLongitude,
1569 APData->TargetHeading = course;
1570 APData->TargetDistance = distance;
1571 MakeTargetDistanceStr( APData, distance );
1574 APData->waypoint_hold = true;
1575 APData->heading_hold = true;
1578 // This changes the AutoPilot Heading
1579 // following cast needed
1580 ApHeadingDialogInput->setValue ((float)APData->TargetHeading );
1581 MakeTargetHeadingStr( APData, APData->TargetHeading );
1583 get_control_values();
1585 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetLocation: ( "
1586 << APData->waypoint_hold << " "
1587 << APData->TargetLatitude << " "
1588 << APData->TargetLongitude << " ) "
1593 void fgAPToggleAltitude( void ) {
1594 // Remove at a later date
1597 APData = APDataGlobal;
1600 if ( APData->altitude_hold ) {
1601 // turn off altitude hold
1602 APData->altitude_hold = false;
1604 // turn on altitude hold, lock at current altitude
1605 APData->altitude_hold = true;
1606 APData->terrain_follow = false;
1607 APData->TargetAltitude = fgAPget_altitude();
1608 APData->alt_error_accum = 0.0;
1609 // alt_error_queue.erase( alt_error_queue.begin(),
1610 // alt_error_queue.end() );
1611 float target_alt = APData->TargetAltitude;
1612 if ( current_options.get_units() == fgOPTIONS::FG_UNITS_FEET )
1613 target_alt *= METER_TO_FEET;
1615 ApAltitudeDialogInput->setValue(target_alt);
1616 MakeTargetAltitudeStr( APData, target_alt);
1619 get_control_values();
1620 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAltitude: ("
1621 << APData->altitude_hold << ") " << APData->TargetAltitude );
1625 void fgAPToggleAutoThrottle ( void ) {
1626 // Remove at a later date
1629 APData = APDataGlobal;
1632 if ( APData->auto_throttle ) {
1633 // turn off altitude hold
1634 APData->auto_throttle = false;
1636 // turn on terrain follow, lock at current agl
1637 APData->auto_throttle = true;
1638 APData->TargetSpeed = get_speed();
1639 APData->speed_error_accum = 0.0;
1642 get_control_values();
1643 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: ("
1644 << APData->auto_throttle << ") " << APData->TargetSpeed );
1647 void fgAPToggleTerrainFollow( void ) {
1648 // Remove at a later date
1651 APData = APDataGlobal;
1654 if ( APData->terrain_follow ) {
1655 // turn off altitude hold
1656 APData->terrain_follow = false;
1658 // turn on terrain follow, lock at current agl
1659 APData->terrain_follow = true;
1660 APData->altitude_hold = false;
1661 APData->TargetAGL = fgAPget_agl();
1662 APData->alt_error_accum = 0.0;
1664 get_control_values();
1666 FG_LOG( FG_COCKPIT, FG_INFO, " fgAPSetTerrainFollow: ("
1667 << APData->terrain_follow << ") " << APData->TargetAGL );
1670 static double NormalizeDegrees( double Input ) {
1671 // normalize the input to the range (-180,180]
1672 // Input should not be greater than -360 to 360.
1673 // Current rules send the output to an undefined state.
1677 else if ( Input <= -180 )
1678 while ( Input <= -180 )
1683 static double LinearExtrapolate( double x, double x1, double y1, double x2, double y2 ) {
1684 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
1685 //assert(x1 != x2); // Divide by zero error. Cold abort for now
1688 // static double y = 0.0;
1689 // double dx = x2 -x1;
1690 // if( (dx < -FG_EPSILON ) || ( dx > FG_EPSILON ) )
1693 double m, b, y; // the constants to find in y=mx+b
1696 m = ( y2 - y1 ) / ( x2 - x1 ); // calculate the m
1698 b = y1 - m * x1; // calculate the b
1700 y = m * x + b; // the final calculation