# Is it my PID that is unstable or my physical system? (Quadcopter)

I've been working on this Arduino-MPU6050 quadcopter for a while now, and it looks like it's close to being finished. I have programmed it in rate mode, so the PID's control the rotational velocity. Once I get those perfected I will write an outer set of positional PID's to control the rate ones.

But anyway, I'm still having issues getting the drone perfectly stable, and it drifts around more than it should. Below you can see a screenshot of a program I wrote, which shows the angular velocity in blue and the angular position in pink:

As you can see, the quadcopter is wobbling around pretty much randomly, and since there aren't any steady oscillations I'm guessing my PID is okay and that the instability is from something physical like vibration. Is this a reasonable assumption?

I am looking for any suggestions/possible explanations for this instability, and guidance on what I should do next. Invest in ant-vibration foam? Revise my PID?

I should also mention that I have not flown the quadcopter. I have it suspended using ropes attached to the legs of an upside down chair so that I can test one axis at a time.

General List of thing's I've Tried:

• Modified the Arduino Servo.h to update the ESC's more frequently.
• Changed the precision of my gyro from default 1/16.4 deg to 1/65.5 deg.
• Balanced the props with bits of electrical tape.
• PID sample rate set to 3ms (333hz).

EDIT Here is a snippet of my PID:

if (millis() - updateTimerPID >= sampleMillisPID) {
if (thrust <= 1200) { // Don't turn on PID until sufficient throttle is reached.
NWPower = thrust; //
NEPower = thrust; // If PID not activated, set motors to base-throttle.
SWPower = thrust; //
SEPower = thrust; //

inAutoRoll = false;  // Roll-Axis PID not activated.
inAutoPitch = false; // Pitch-Axis PID not activated.
inAutoYaw = false;   // Yaw-Axis PID not activated.

I_rollRate = 0;   // Reset roll rate integral term.
I_pitchRate = 0;  // Reset pitch rate integral term.
I_yawRate = 0;    // Reset yaw rate integral term.

} else { //PID is active - adjust Roll/Pitch/Yaw.

}
updateTimerPID = millis(); // Reset PID timer.
}

float offset = requestedRollRate - rollRate; //How far off from the wanted roll angular velocity.

I_rollRate += KrI * offset; // Adjust roll rate integral term.

if(!inAutoRoll) { // Did the PID just turn on?
lastRollRateOffset = offset; // The previous offset is set to the current one (D-term is now zero for this instance).
inAutoRoll = true; // The PID is now on.
}

float adjust = (KrP * offset) + I_rollRate + (KrD * (offset -lastRollRateOffset)); // Motor power adjust value.

if (NWPower > maxOutPID)NWPower = maxOutPID;
else if (NWPower < minOutPID)NWPower = minOutPID;

if (NEPower > maxOutPID)NEPower = maxOutPID;
else if (NEPower < minOutPID)NEPower = minOutPID;

if (SWPower > maxOutPID)SWPower = maxOutPID;
else if (SWPower < minOutPID)SWPower = minOutPID;

if (SEPower > maxOutPID)SEPower = maxOutPID;
else if (SEPower < minOutPID)SEPower = minOutPID;

lastRollRateOffset = offset;    // Remember the offset for next time.
}

• You said you've written rate controllers, but haven't yet implemented position controllers. With no position control, I would expect drift. You're seeing oscillations in angular speed, but are those real or are they a product of sensor noise? What does your output look like if the aircraft is stationary with the motors off? Does it still wobble? What doe it physically look like when it flies? Can you actually see a wobble? – Chuck Apr 10 '17 at 14:19
• I would be wary about trying to get a super high resolution out of your gyro, too. Read the data sheet for the sensor, but typically higher resolutions come at the cost of longer intervals between samples. Are you actually getting new readings each sample interval? How did you balance the props? What did you use to measure the imbalances? How do you know there was a problem, and that you fixed it and didn't exacerbate or create one? Are you including the PID sample rate in the integral and derivative error calculations? – Chuck Apr 10 '17 at 14:21
• The Oscillations in angular speed are real, and they go away when the motors are turned off. The wobbling is real; I can see the quadcopter wobbling. To balance the props I scaled up the sensitivity on my graphing program and added electrical tape to the props until the noise was the lowest. Sample rate is not included in error calculations because the sample rate is constant, so it is factored in as part of the PID gains. – Ember Apr 11 '17 at 23:39
• How are you tuning the PID controller? What is your hardware setup? Did you use strings or rods to isolate and tune each axis? Before tuning the PID controllers, did you properly set the base throttle such that the quadcopter is able to lift itself and is not sitting on the string/rod? If this is not set properly, no matter how much you tune, you won't get anywhere. How are you computing your I and D controllers in your code? Can you post the code snippet of the latter so we can evaluate it? What is the range of the angle error? The numerical values aren't displayed in your plot. – user123456098 Apr 12 '17 at 8:25