Why do we add all the errors in PID line following code? i am not able to understand the answer to it properly. FORMULA: total_error = int(kp* proportional + ki* integral + kd * derivative)
The proportional term gives a value that is proportional to the current error value. The integral error adds an error amount that is proportional to both the magnitude and the duration of the error. And the derivative error is the slope of the error over time at a particular time. They all interact to provide the output value for the PID algorithm.
The reason these are all added together is so that when the various gain (K) values in the terms are optimized, you get the best-tuned, most stable response in a given system, if stability is possible. But as any source will tell you, PID tuning can be a difficult problem because these errors, occurring out of phase and dynamically with time and system change, all interact in time. They are designed to be added together to create a closed-form equation that can be optimized with different gain values.
There is a wealth of information about how these all interact. You can get a good introduction at Wikipedia. This pseudocode and the accompanying description from Wikipedia demonstrate where in the algorithm the next output is derived, from adding the errors:
previous_error = 0
integral = 0
loop:
error = setpoint - measured_value
integral = integral + error*dt
derivative = (error - previous_error)/dt
output = Kp*error + Ki*integral + Kd*derivative
previous_error = error
wait(dt)
goto loop