# SDK QuestionAltitude Autopilot

#### JMW

Hi all.
To code a simple altitude autopilot - similar to the one for DG in Scripts, but for C++ so can be incorporated in a .dll
Trying to convert that script, I have this (don't laugh please)
shgt is target height in feet.

Code:
//-- Set up autopilot structures
{
double altid = 0;
double tgtalt = 0;
double maxasc = 30;
double maxdsc = -20;

VECTOR3 as;
alt = shgt*0.30480;//convert to meters
double dt = oapiGetSimStep ();

tgtalt = alt;
double dslope0 = 0;
double dslope_rate;
if (tgtalt !=0) {

alt = GetAltitude()
double tgt_slope = RAD*1e-2 * (tgtalt-alt);

GetAirspeedVector (FRAME_HORIZON,as);
double xz = sqrt (as.x + as.z);
double sl = atan2 (as.y, xz);

double dslope = tgt_slope - sl ;
if (dslope0 == 0) dslope_rate = 0;
else
{
dslope_rate = (dslope-dslope0)/dt;
}
dslope0 = dslope;
double delev = (dslope*0.1 + dslope_rate)*(dt*10.0);
double elev = GetControlSurfaceLevel (AIRCTRL_ELEVATOR);
elev = elev+delev;
if (elev > 1) elev = 1 ;
if (elev < -1) elev = -1;

SetControlSurfaceLevel (AIRCTRL_ELEVATOR,elev);
}
}

I've exhausted all my connotations/variations :beathead:

Last edited:

#### JMW

I think the problem must be in this:
Code:
GetAirspeedVector (FRAME_HORIZON,as);
double xz = sqrt (as.x + as.z);
double sl = atan2 (as.y, xz);

How are square roots dealt with in C++ ?
sqrt (double x) ?
Original script was
sqrt (as.x^2 + as.z^2); but C++ doesn't like that.
is this where it's going wrong ?

I've used sqrt before as here : double var_c = 2*atan2(sqrt(var_a),sqrt(1-var_a));
and works perfectly........

Replies
2
Views
249
Replies
3
Views
2K
Replies
1
Views
1K
Replies
6
Views
3K
Replies
5
Views
3K