1 /**************************************************************************
2 * autopilot.cxx -- autopilot subsystem
4 * Written by Jeff Goeke-Smith, started April 1998.
6 * Copyright (C) 1998 Jeff Goeke-Smith, jgoeke@voyager.net
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 **************************************************************************/
35 #include <Include/fg_stl_config.h>
36 #include <Scenery/scenery.hxx>
38 #ifdef NEEDNAMESPACESTD
42 #include "autopilot.hxx"
44 #include <Include/fg_constants.h>
45 #include <Debug/fg_debug.h>
48 static list < double > alt_error_queue;
51 // The below routines were copied right from hud.c ( I hate reinventing
52 // the wheel more than necessary)
54 // The following routines obtain information concerntin the aircraft's
55 // current state and return it to calling instrument display routines.
56 // They should eventually be member functions of the aircraft.
59 static double get_throttleval( void )
61 fgCONTROLS *pcontrols;
63 pcontrols = current_aircraft.controls;
64 return pcontrols->throttle[0]; // Hack limiting to one engine
67 static double get_aileronval( void )
69 fgCONTROLS *pcontrols;
71 pcontrols = current_aircraft.controls;
72 return pcontrols->aileron;
75 static double get_elevatorval( void )
77 fgCONTROLS *pcontrols;
79 pcontrols = current_aircraft.controls;
80 return pcontrols->elevator;
83 static double get_elev_trimval( void )
85 fgCONTROLS *pcontrols;
87 pcontrols = current_aircraft.controls;
88 return pcontrols->elevator_trim;
91 static double get_rudderval( void )
93 fgCONTROLS *pcontrols;
95 pcontrols = current_aircraft.controls;
96 return pcontrols->rudder;
99 static double get_speed( void )
103 f = current_aircraft.flight;
104 return( FG_V_equiv_kts ); // Make an explicit function call.
107 static double get_aoa( void )
111 f = current_aircraft.flight;
112 return( FG_Gamma_vert_rad * RAD_TO_DEG );
115 static double fgAPget_roll( void )
119 f = current_aircraft.flight;
120 return( FG_Phi * RAD_TO_DEG );
123 static double get_pitch( void )
127 f = current_aircraft.flight;
131 double fgAPget_heading( void )
135 f = current_aircraft.flight;
136 return( FG_Psi * RAD_TO_DEG );
139 static double fgAPget_altitude( void )
143 f = current_aircraft.flight;
145 return( FG_Altitude * FEET_TO_METER /* -rough_elev */ );
148 static double fgAPget_climb( void )
152 f = current_aircraft.flight;
154 // return in meters per minute
155 return( FG_Climb_Rate * FEET_TO_METER * 60 );
158 static double get_sideslip( void )
162 f = current_aircraft.flight;
167 static double fgAPget_agl( void )
172 f = current_aircraft.flight;
173 agl = FG_Altitude * FEET_TO_METER - scenery.cur_elev;
178 // End of copied section. ( thanks for the wheel :-)
180 // Local Prototype section
182 double LinearExtrapolate( double x,double x1, double y1, double x2, double y2);
183 double NormalizeDegrees( double Input);
185 // End Local ProtoTypes
187 fgAPDataPtr APDataGlobal; // global variable holding the AP info
188 // I want this gone. Data should be in aircraft structure
192 void fgAPInit( fgAIRCRAFT *current_aircraft )
196 fgPrintf( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem\n" );
198 APData = (fgAPDataPtr)calloc(sizeof(fgAPData),1);
200 if (APData == NULL) // I couldn't get the mem. Dying
201 fgPrintf( FG_AUTOPILOT, FG_EXIT,"No ram for Autopilot. Dying.\n");
203 APData->heading_hold = 0 ; // turn the heading hold off
204 APData->altitude_hold = 0 ; // turn the altitude hold off
206 APData->TargetHeading = 0.0; // default direction, due north
207 APData->TargetAltitude = 3000; // default altitude in meters
208 APData->alt_error_accum = 0.0;
210 // These eventually need to be read from current_aircaft somehow.
212 APData->MaxRoll = 7; // the maximum roll, in Deg
213 APData->RollOut = 30; // the deg from heading to start rolling out at, in Deg
214 APData->MaxAileron= .1; // how far can I move the aleron from center.
215 APData->RollOutSmooth = 10; // Smoothing distance for alerion control
217 //Remove at a later date
218 APDataGlobal = APData;
224 // Remove the following lines when the calling funcitons start
225 // passing in the data pointer
229 APData = APDataGlobal;
232 // heading hold enabled?
233 if ( APData->heading_hold == 1 ) {
240 NormalizeDegrees( APData->TargetHeading - fgAPget_heading());
241 // figure out how far off we are from desired heading
243 // Now it is time to deterime how far we should be rolled.
244 fgPrintf( FG_AUTOPILOT, FG_DEBUG, "RelHeading: %f\n", RelHeading);
247 // Check if we are further from heading than the roll out point
248 if ( fabs(RelHeading) > APData->RollOut ) {
249 // set Target Roll to Max in desired direction
250 if (RelHeading < 0 ) {
251 TargetRoll = 0-APData->MaxRoll;
253 TargetRoll = APData->MaxRoll;
256 // We have to calculate the Target roll
258 // This calculation engine thinks that the Target roll
259 // should be a line from (RollOut,MaxRoll) to (-RollOut,
260 // -MaxRoll) I hope this works well. If I get ambitious
261 // some day this might become a fancier curve or
264 TargetRoll = LinearExtrapolate( RelHeading, -APData->RollOut,
265 -APData->MaxRoll, APData->RollOut,
269 // Target Roll has now been Found.
271 // Compare Target roll to Current Roll, Generate Rel Roll
273 fgPrintf( FG_COCKPIT, FG_BULK, "TargetRoll: %f\n", TargetRoll);
275 RelRoll = NormalizeDegrees(TargetRoll - fgAPget_roll());
277 // Check if we are further from heading than the roll out smooth point
278 if ( fabs(RelRoll) > APData->RollOutSmooth ) {
279 // set Target Roll to Max in desired direction
281 AileronSet = 0-APData->MaxAileron;
283 AileronSet = APData->MaxAileron;
286 AileronSet = LinearExtrapolate( RelRoll, -APData->RollOutSmooth,
288 APData->RollOutSmooth,
289 APData->MaxAileron );
292 fgAileronSet(AileronSet);
296 // altitude hold enabled?
297 if ( APData->altitude_hold == 1 ) {
299 double prop_error, int_error;
300 double prop_adj, int_adj, total_adj;
302 // normal altitude hold
303 // APData->TargetClimbRate =
304 // (APData->TargetAltitude - fgAPget_altitude()) * 8.0;
306 // brain dead ground hugging with no look ahead
307 APData->TargetClimbRate =
308 ( 500 - fgAPget_agl() ) * 8.0;
310 if ( APData->TargetClimbRate > 200.0 ) {
311 APData->TargetClimbRate = 200.0;
313 if ( APData->TargetClimbRate < -200.0 ) {
314 APData->TargetClimbRate = -200.0;
317 error = fgAPget_climb() - APData->TargetClimbRate;
319 // push current error onto queue and add into accumulator
320 alt_error_queue.push_back(error);
321 APData->alt_error_accum += error;
323 // if queue size larger than 60 ... pop front and subtract
325 size = alt_error_queue.size();
327 APData->alt_error_accum -= alt_error_queue.front();
328 alt_error_queue.pop_front();
332 // calculate integral error, and adjustment amount
333 int_error = APData->alt_error_accum / 10;
334 printf("error = %.2f int_error = %.2f\n", error, int_error);
335 int_adj = int_error / 2000.0;
337 // caclulate proportional error
339 prop_adj = prop_error / 1000.0;
341 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
342 if ( total_adj > 0.5 ) { total_adj = 0.5; }
343 if ( total_adj < -0.5 ) { total_adj = -0.5; }
345 fgElevSet( total_adj );
349 if (APData->Mode == 2) // Glide slope hold
354 // First, calculate Relative slope and normalize it
355 RelSlope = NormalizeDegrees( APData->TargetSlope - get_pitch());
357 // Now calculate the elevator offset from current angle
358 if ( abs(RelSlope) > APData->SlopeSmooth )
360 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
361 RelElevator = -APData->MaxElevator;
363 RelElevator = APData->MaxElevator;
367 RelElevator = LinearExtrapolate(RelSlope,-APData->SlopeSmooth,-APData->MaxElevator,APData->SlopeSmooth,APData->MaxElevator);
370 fgElevMove(RelElevator);
381 void fgAPSetMode( int mode)
383 //Remove the following line when the calling funcitons start passing in the data pointer
386 APData = APDataGlobal;
389 fgPrintf( FG_COCKPIT, FG_INFO, "APSetMode : %d\n", mode );
391 APData->Mode = mode; // set the new mode
395 void fgAPToggleHeading( void )
397 // Remove at a later date
400 APData = APDataGlobal;
403 if ( APData->heading_hold ) {
404 // turn off heading hold
405 APData->heading_hold = 0;
407 // turn on heading hold, lock at current heading
408 APData->heading_hold = 1;
409 APData->TargetHeading = fgAPget_heading();
412 fgPrintf( FG_COCKPIT, FG_INFO, " fgAPSetHeading: (%d) %.2f\n",
413 APData->heading_hold,
414 APData->TargetHeading);
418 void fgAPToggleAltitude( void )
420 // Remove at a later date
423 APData = APDataGlobal;
426 if ( APData->altitude_hold ) {
427 // turn off altitude hold
428 APData->altitude_hold = 0;
430 // turn on altitude hold, lock at current altitude
431 APData->altitude_hold = 1;
432 APData->TargetAltitude = fgAPget_altitude();
433 APData->alt_error_accum = 0.0;
434 alt_error_queue.erase( alt_error_queue.begin(), alt_error_queue.end() );
437 fgPrintf( FG_COCKPIT, FG_INFO, " fgAPSetAltitude: (%d) %.2f\n",
438 APData->altitude_hold,
439 APData->TargetAltitude);
443 double LinearExtrapolate( double x,double x1,double y1,double x2,double y2)
445 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
446 //assert(x1 != x2); // Divide by zero error. Cold abort for now
448 double m, b, y; // the constants to find in y=mx+b
450 m=(y2-y1)/(x2-x1); // calculate the m
452 b= y1- m * x1; // calculate the b
454 y = m * x + b; // the final calculation
460 double NormalizeDegrees(double Input)
462 // normalize the input to the range (-180,180]
463 // Input should not be greater than -360 to 360. Current rules send the output to an undefined state.