# Improve altitude control on an outer loop PD controller for a UAV

I'm playing around with using PID control as an outer loop control for the position of a DJI drone in a Hardware in the loop simulator.

I have three separate controller instances for each axis x, y, z but I'm having issues with undershoot in the Z axis.

I've added an image of the output below. X and Y ramp up nicely and reach the setpoint but Z kind of flatlines for a bit before before rising again.

I've mostly applied P and D gains to my controllers and even with a massive Proportional gain on the Z controller, the behaviour is still the same.

A quick pseudocode for my step function is:

pid_x.PID_init(0.6, 0.0, 0.02, maxSpeed, minSpeed);
pid_y.PID_init(0.6, 0.0, 0.02, maxSpeed, minSpeed);
pid_z.PID_init(2.9, 0, 0.0, maxSpeed, minSpeed);
float xCmd, yCmd, zCmd;

void step()
{

if (std::abs(desired_pos.x - current_local_pos.x) < 0.3)
{
finished_x = true;
xCmd = 0;
}
else
{
finished_x = false;
xCmd = pid_x.PID_update(current_local_pos.x, desired_pos.x, dt);
}

if (std::abs(desired_pos.y - current_local_pos.y) < 0.3)
{
finished_y = true;
yCmd = 0;
}
else
{
finished_y = false;
yCmd = pid_y.PID_update(current_local_pos.y, desired_pos.y, dt);
}
if (std::abs(desired_pos.z - current_local_pos.z) < 0.3)
{
finished_z = true;
zCmd = 0;
}
else
{
finished_z = false;
zCmd = pid_z.PID_update(current_local_pos.z, desired_pos.z, dt);
}

//apply xCmd, yCmd, zCmd to drone.
}


My question is how do I improve this, or is the model too simple to achieve my objective?

• Can you add motor output to these graphs? My suspicion is that something is clipping at the lower level PIDs and zCmd is just pegged at maxSpeed for most of the time. – Mark Booth May 14 at 18:16