#### JMW

##### Aspiring Addon Developer

Hi all.

Seems like a simple task, but I've gotta admit defeat.

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.

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

please help...........

Seems like a simple task, but I've gotta admit defeat.

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);
if (tgt_slope > maxasc*RAD) tgt_slope = maxasc*RAD;
if (tgt_slope < maxdsc*RAD) tgt_slope = maxdsc*RAD;
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:

please help...........

Last edited: