I am building an Arduino based flight controller for a quadcopter and have got it flying but it is not performing how I want it to. The drone is flying but when I release the sticks the drone does not stay perfectly still. There is some random and small oscillations that occur when it is just hovering.
Right now I am using a single PID controller for each axis though functions like this.
dt=0.005;
error = *in - *set;
P = (error * kp);
I = I + (error * ki)*dt;
D_M = ((*in - in1)*kd)/dt;
D_E = ((error - error1)*kd)/dt;
D=D_M*(1-jerk) + D_E*(jerk);
I = constrain(I, LOWER_BOUND*0.75, UPPER_BOUND*0.75);
in1 = *in;
error1 = error;
pid = P + I + D;
pid = constrain(pid, LOWER_BOUND, UPPER_BOUND);
*out = pid;
The error is degree angles and the setpoint is the angle I want it to go to.
I then output the PID controller values to the motors using the Arudino Servo library.
motor1 = throttle;
motor2 = throttle;
motor3 = throttle;
motor4 = throttle;
motor1 += rollOUT;
motor2 += rollOUT;
motor3 -= rollOUT;
motor4 -= rollOUT;
motor1 -= pitchOUT;
motor2 += pitchOUT;
motor3 -= pitchOUT;
motor4 += pitchOUT;
motor1 += yawOUT;
motor2 -= yawOUT;
motor3 -= yawOUT;
motor4 += yawOUT;
Is there something I am missing about quadcopter PID controllers? Thank you for the help!
What output range are you using? 0-90 degrees or 1000-2000 microseconds?
Oscillation could be due to bad tuning of the PID constants. Read up on PID tuning.
1000-2000us range. I have been tuning for the past few weeks and have tried a lot of different methods. The results are all the same. Drone holds find for a few seconds, then I get these small (5 degree) oscillations that dampen out after a second and then the process repeats.
That sort of oscillation is not at all typical of a PID loop, or of any second order system.
There must be errors elsewhere in the code that are building up to start the oscillation, but then somehow cancel out periodically.
Is there something I am missing about quadcopter PID controllers?
Probably, but you did not post much of the code, and for what little you did post, you did not bother to explain the choice of terms to include or selection of constants, so it would be anyone's uninformed guess.
For example, why do you expect that control of your quadcopter would require the following elaboration of the derivative term?
D_M = ((*in - in1)*kd)/dt;
D_E = ((error - error1)*kd)/dt;
D=D_M*(1-jerk) + D_E*(jerk);
What processor and clock speed are you using? Is there enough time to do all these floating point calculations in multiple PID loops, and maintain accurate, constant loop timing?
Did you consider that every change in motor speed will result in an additional acceleration, that should be anticipated and subtracted from the measured accelerations, before entering into the PIDs.