SDK Question Altitude Autopilot

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
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.

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:

JMW

Aspiring Addon Developer
Joined
Aug 5, 2008
Messages
611
Reaction score
52
Points
43
Location
Happy Wherever
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........
 
Top