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>
37 #include <Scenery/scenery.hxx>
39 // #ifdef NEEDNAMESPACESTD
40 // using namespace std;
43 #include "autopilot.hxx"
45 #include <Include/fg_constants.h>
46 #include <Debug/fg_debug.h>
49 // static list < double > alt_error_queue;
52 // The below routines were copied right from hud.c ( I hate reinventing
53 // the wheel more than necessary)
55 // The following routines obtain information concerntin the aircraft's
56 // current state and return it to calling instrument display routines.
57 // They should eventually be member functions of the aircraft.
61 static double get_speed( void )
65 f = current_aircraft.flight;
66 return( FG_V_equiv_kts ); // Make an explicit function call.
69 static double get_aoa( void )
73 f = current_aircraft.flight;
74 return( FG_Gamma_vert_rad * RAD_TO_DEG );
77 static double fgAPget_roll( void )
81 f = current_aircraft.flight;
82 return( FG_Phi * RAD_TO_DEG );
85 static double get_pitch( void )
89 f = current_aircraft.flight;
93 double fgAPget_heading( void )
97 f = current_aircraft.flight;
98 return( FG_Psi * RAD_TO_DEG );
101 static double fgAPget_altitude( void )
105 f = current_aircraft.flight;
107 return( FG_Altitude * FEET_TO_METER /* -rough_elev */ );
110 static double fgAPget_climb( void )
114 f = current_aircraft.flight;
116 // return in meters per minute
117 return( FG_Climb_Rate * FEET_TO_METER * 60 );
120 static double get_sideslip( void )
124 f = current_aircraft.flight;
129 static double fgAPget_agl( void )
134 f = current_aircraft.flight;
135 agl = FG_Altitude * FEET_TO_METER - scenery.cur_elev;
140 // End of copied section. ( thanks for the wheel :-)
142 // Local Prototype section
144 double LinearExtrapolate( double x,double x1, double y1, double x2, double y2);
145 double NormalizeDegrees( double Input);
147 // End Local ProtoTypes
149 fgAPDataPtr APDataGlobal; // global variable holding the AP info
150 // I want this gone. Data should be in aircraft structure
154 void fgAPInit( fgAIRCRAFT *current_aircraft )
158 fgPrintf( FG_AUTOPILOT, FG_INFO, "Init AutoPilot Subsystem\n" );
160 APData = (fgAPDataPtr)calloc(sizeof(fgAPData),1);
162 if (APData == NULL) // I couldn't get the mem. Dying
163 fgPrintf( FG_AUTOPILOT, FG_EXIT,"No ram for Autopilot. Dying.\n");
165 APData->heading_hold = 0 ; // turn the heading hold off
166 APData->altitude_hold = 0 ; // turn the altitude hold off
168 APData->TargetHeading = 0.0; // default direction, due north
169 APData->TargetAltitude = 3000; // default altitude in meters
170 APData->alt_error_accum = 0.0;
172 // These eventually need to be read from current_aircaft somehow.
174 APData->MaxRoll = 7; // the maximum roll, in Deg
175 APData->RollOut = 30; // the deg from heading to start rolling out at, in Deg
176 APData->MaxAileron= .1; // how far can I move the aleron from center.
177 APData->RollOutSmooth = 10; // Smoothing distance for alerion control
179 //Remove at a later date
180 APDataGlobal = APData;
186 // Remove the following lines when the calling funcitons start
187 // passing in the data pointer
191 APData = APDataGlobal;
194 // heading hold enabled?
195 if ( APData->heading_hold == 1 ) {
202 NormalizeDegrees( APData->TargetHeading - fgAPget_heading());
203 // figure out how far off we are from desired heading
205 // Now it is time to deterime how far we should be rolled.
206 fgPrintf( FG_AUTOPILOT, FG_DEBUG, "RelHeading: %f\n", RelHeading);
209 // Check if we are further from heading than the roll out point
210 if ( fabs(RelHeading) > APData->RollOut ) {
211 // set Target Roll to Max in desired direction
212 if (RelHeading < 0 ) {
213 TargetRoll = 0-APData->MaxRoll;
215 TargetRoll = APData->MaxRoll;
218 // We have to calculate the Target roll
220 // This calculation engine thinks that the Target roll
221 // should be a line from (RollOut,MaxRoll) to (-RollOut,
222 // -MaxRoll) I hope this works well. If I get ambitious
223 // some day this might become a fancier curve or
226 TargetRoll = LinearExtrapolate( RelHeading, -APData->RollOut,
227 -APData->MaxRoll, APData->RollOut,
231 // Target Roll has now been Found.
233 // Compare Target roll to Current Roll, Generate Rel Roll
235 fgPrintf( FG_COCKPIT, FG_BULK, "TargetRoll: %f\n", TargetRoll);
237 RelRoll = NormalizeDegrees(TargetRoll - fgAPget_roll());
239 // Check if we are further from heading than the roll out smooth point
240 if ( fabs(RelRoll) > APData->RollOutSmooth ) {
241 // set Target Roll to Max in desired direction
243 AileronSet = 0-APData->MaxAileron;
245 AileronSet = APData->MaxAileron;
248 AileronSet = LinearExtrapolate( RelRoll, -APData->RollOutSmooth,
250 APData->RollOutSmooth,
251 APData->MaxAileron );
254 controls.set_aileron( AileronSet );
255 controls.set_rudder( 0.0 );
258 // altitude hold or terrain follow enabled?
259 if ( (APData->altitude_hold == 1) || (APData->terrain_follow == 1) ) {
260 double speed, max_climb, error;
261 double prop_error, int_error;
262 double prop_adj, int_adj, total_adj;
264 if (APData->altitude_hold == 1) {
265 // normal altitude hold
266 APData->TargetClimbRate =
267 (APData->TargetAltitude - fgAPget_altitude()) * 8.0;
268 } else if (APData->terrain_follow == 1) {
269 // brain dead ground hugging with no look ahead
270 APData->TargetClimbRate =
271 ( APData->TargetAGL - fgAPget_agl() ) * 16.0;
273 // just try to zero out rate of climb ...
274 APData->TargetClimbRate = 0.0;
279 if ( speed < 90.0 ) {
281 } else if ( speed < 100.0 ) {
282 max_climb = (speed - 90.0) * 20;
284 max_climb = ( speed - 100.0 ) * 4.0 + 200.0;
287 if ( APData->TargetClimbRate > max_climb ) {
288 APData->TargetClimbRate = max_climb;
291 if ( APData->TargetClimbRate < -400.0 ) {
292 APData->TargetClimbRate = -400.0;
295 error = fgAPget_climb() - APData->TargetClimbRate;
297 // accumulate the error under the curve ... this really should
299 APData->alt_error_accum += error;
301 // calculate integral error, and adjustment amount
302 int_error = APData->alt_error_accum;
303 // printf("error = %.2f int_error = %.2f\n", error, int_error);
304 int_adj = int_error / 8000.0;
306 // caclulate proportional error
308 prop_adj = prop_error / 2000.0;
310 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
311 if ( total_adj > 0.6 ) { total_adj = 0.6; }
312 if ( total_adj < -0.2 ) { total_adj = -0.2; }
314 controls.set_elevator( total_adj );
317 // auto throttle enabled?
318 if ( APData->auto_throttle == 1 ) {
320 double prop_error, int_error;
321 double prop_adj, int_adj, total_adj;
323 error = APData->TargetSpeed - get_speed();
325 // accumulate the error under the curve ... this really should
327 APData->speed_error_accum += error;
328 if ( APData->speed_error_accum > 2000.0 ) {
329 APData->speed_error_accum = 2000.0;
331 if ( APData->speed_error_accum < -2000.0 ) {
332 APData->speed_error_accum = -2000.0;
335 // calculate integral error, and adjustment amount
336 int_error = APData->speed_error_accum;
338 // printf("error = %.2f int_error = %.2f\n", error, int_error);
339 int_adj = int_error / 200.0;
341 // caclulate proportional error
343 prop_adj = 0.5 + prop_error / 50.0;
345 total_adj = 0.9 * prop_adj + 0.1 * int_adj;
346 if ( total_adj > 1.0 ) { total_adj = 1.0; }
347 if ( total_adj < 0.0 ) { total_adj = 0.0; }
349 controls.set_throttle( fgCONTROLS::FG_ALL_ENGINES, total_adj );
353 if (APData->Mode == 2) // Glide slope hold
358 // First, calculate Relative slope and normalize it
359 RelSlope = NormalizeDegrees( APData->TargetSlope - get_pitch());
361 // Now calculate the elevator offset from current angle
362 if ( abs(RelSlope) > APData->SlopeSmooth )
364 if ( RelSlope < 0 ) // set RelElevator to max in the correct direction
365 RelElevator = -APData->MaxElevator;
367 RelElevator = APData->MaxElevator;
371 RelElevator = LinearExtrapolate(RelSlope,-APData->SlopeSmooth,-APData->MaxElevator,APData->SlopeSmooth,APData->MaxElevator);
374 fgElevMove(RelElevator);
385 void fgAPSetMode( int mode)
387 //Remove the following line when the calling funcitons start passing in the data pointer
390 APData = APDataGlobal;
393 fgPrintf( FG_COCKPIT, FG_INFO, "APSetMode : %d\n", mode );
395 APData->Mode = mode; // set the new mode
399 void fgAPToggleHeading( void )
401 // Remove at a later date
404 APData = APDataGlobal;
407 if ( APData->heading_hold ) {
408 // turn off heading hold
409 APData->heading_hold = 0;
411 // turn on heading hold, lock at current heading
412 APData->heading_hold = 1;
413 APData->TargetHeading = fgAPget_heading();
416 fgPrintf( FG_COCKPIT, FG_INFO, " fgAPSetHeading: (%d) %.2f\n",
417 APData->heading_hold,
418 APData->TargetHeading);
422 void fgAPToggleAltitude( void )
424 // Remove at a later date
427 APData = APDataGlobal;
430 if ( APData->altitude_hold ) {
431 // turn off altitude hold
432 APData->altitude_hold = 0;
434 // turn on altitude hold, lock at current altitude
435 APData->altitude_hold = 1;
436 APData->terrain_follow = 0;
437 APData->TargetAltitude = fgAPget_altitude();
438 APData->alt_error_accum = 0.0;
439 // alt_error_queue.erase( alt_error_queue.begin(),
440 // alt_error_queue.end() );
443 fgPrintf( FG_COCKPIT, FG_INFO, " fgAPSetAltitude: (%d) %.2f\n",
444 APData->altitude_hold,
445 APData->TargetAltitude);
449 void fgAPToggleAutoThrottle ( void )
451 // Remove at a later date
454 APData = APDataGlobal;
457 if ( APData->auto_throttle ) {
458 // turn off altitude hold
459 APData->auto_throttle = 0;
461 // turn on terrain follow, lock at current agl
462 APData->auto_throttle = 1;
463 APData->TargetSpeed = get_speed();
464 APData->speed_error_accum = 0.0;
467 fgPrintf( FG_COCKPIT, FG_INFO, " fgAPSetAutoThrottle: (%d) %.2f\n",
468 APData->auto_throttle,
469 APData->TargetSpeed);
472 void fgAPToggleTerrainFollow( void )
474 // Remove at a later date
477 APData = APDataGlobal;
480 if ( APData->terrain_follow ) {
481 // turn off altitude hold
482 APData->terrain_follow = 0;
484 // turn on terrain follow, lock at current agl
485 APData->terrain_follow = 1;
486 APData->altitude_hold = 0;
487 APData->TargetAGL = fgAPget_agl();
488 APData->alt_error_accum = 0.0;
491 fgPrintf( FG_COCKPIT, FG_INFO, " fgAPSetTerrainFollow: (%d) %.2f\n",
492 APData->terrain_follow,
496 double LinearExtrapolate( double x,double x1,double y1,double x2,double y2)
498 // This procedure extrapolates the y value for the x posistion on a line defined by x1,y1; x2,y2
499 //assert(x1 != x2); // Divide by zero error. Cold abort for now
501 double m, b, y; // the constants to find in y=mx+b
503 m=(y2-y1)/(x2-x1); // calculate the m
505 b= y1- m * x1; // calculate the b
507 y = m * x + b; // the final calculation
513 double NormalizeDegrees(double Input)
515 // normalize the input to the range (-180,180]
516 // Input should not be greater than -360 to 360. Current rules send the output to an undefined state.