A quad rotor which of course has four propellers (named as Tim, Tod, Tom and Ted when counting in clockwise sense and we are working on it and its called Albatross).

[[Tim and Tom are entangled with each other and so are Ted and Tod. If Tim is made to rotate CW then Tom should rotate CCW and if Ted is made to rotate CW then Tod should rotate CCW, so as to balance the reaction from newtons 3rd Law. And we’re using MPU6050 for obtaining YAW PITCH AND ROLL using the DMP code from the library of course..will may be use complementary filter later on just for learnings sake.]]

EDIT: part in double brackets above is what I wrote about 2 months before. So as I was saying above if Tim rotates CW then Tom has to rotate CCW same goes for Ted and Tod. If one carries on with the above config, you wont be able to balance the yaw axis(which is very important). so to balance yaw axis too we need Tim and Tom both CW and Ted and Tod both CCW.

Back to PID. PID stands for pelvic inflammatory disease..if someone was wondering.

Proportional–

Suppose there are two poles named as Robert and Page with 500 meters distance b/w them and you are standing initially near R and your final destination is P.

Current Error Equals= 500;

so you start moving with initial speed ZERO and high acceleration as error is quite high. But as the error reduces you also reduce your acceleration accordingly with equation-

Acceleration = Kp * Current Error;

so at the time when you reach P your acceleration is zero but velocity isn’t.

Integral–

Forget I even said integral.

In the image above you might be able to read Tim and Tom. To keep the quad levelled, we implemented proportional control according to the following equation-

Errror =angle by which the mean position is disturbed like in case of a pendulum.

When, TIM is below horizontal level Error is positive and if TOM is below horizontal level Error is negative

if (error>0) //Tilted towards TIM. increase TIM speed and decrease TOM speed { Speed_TIM= Kp * current_error; Speed_TOM= Kp * -1 * current_error } if(error<=0) //Tilted towards TOM. increase TOM speed and decrease TIM speed { Speed_TIM=Kp * current_error; Speed_TOM=Kp * -1 * current_error; }

now suppose TIM(propeller) is 20 Kg heavier than TOM in this case TIM would have to rotate much faster than TOM to adjust the tilt due its weight.

Kp can only work under limited range of error..if the error gets too high Kp * Error isn’t of much help(Propeller would create large lift due to large error and as error decreases lift would decrease. So Oscillations would be there Error High Low High Low..).

So we try to do it layer by layer i.e. accumulating previous errors so as speed increases gradually in smooth steps

loop() { t=micros(); //Micros returns time since the program started but not //strictly ofcourse because it takes time to start the timer //and before you start the timer there stuff to do. get_current_error(); Error_sum=Error_sum + Current_error * dt; speed_TIM=Kp * Error_sum; speed_TOM=Kp * -1 * Error_sum; dt=micros()-t; }

so with time error accumulates more and more and tries to increase until tilt is reduced to zero.

But there’s an even better way

loop() { t=micros(); //Micros returns time since the program started but not //strictly ofcourse because it takes time to start the timer //and before you start the timer there stuff to do. get_current_error(); Error_sum=Error_sum + Current_error * dt; speed_TIM=Kp * Error_sum; speed_TOM=Kp * -1 * Error_sum; dt=micros()-t; }

dt isn’t exactly what I’ve written above but it is the time difference between two consecutive tilt readings from sensor aka sampling time.

what we’re doing above is accumulating tilt along with additional information that “how long that particular tilt was maintained”

so we’re not just using tilt for correcting the speed of TIM but also time for which that tilt was maintained.

replace Kp with Ki and there goes your integral.

Derivative-

We know how inductor opposes the change in current..whether current is trying to increase or decrease..same thing goes here..derivative tries oppose the change..in other words..smooths the whole thing.

if- Error is going more and more positive(in case where TIM is going below horizontal level with time) Kd * derr/dt(will increase TIM’s speed).

else if- Error is going more and more negative(in case where TOM is going below horizontal level with time) Kd * -1 * derr/dt(will increase TOM’s speed).

and here’s something-

*working variables*/ unsigned long lastTime; double Input, Output, Setpoint; double errSum, lastErr; double kp, ki, kd; void Compute() { /*How long since we last calculated*/ unsigned long now = millis(); double timeChange = (double)(now - lastTime); /*Compute all the working error variables*/ double error = Setpoint - Input; errSum += (error * timeChange); double dErr = (error - lastErr) / timeChange; /*Compute PID Output*/ Output = kp * error + ki * errSum + kd * dErr; /*Remember some variables for next time*/ lastErr = error; lastTime = now; } void SetTunings(double Kp, double Ki, double Kd) { kp = Kp; ki = Ki; kd = Kd; }