How to calculate Kp, Kd and Ki?
To tune a PID controller you need to take into account the entire system where the motor is installed.

There are tons of rules of thumb that you can use, some more adequate than others depending on your application. But none of them really tailored for your particular application.

My generic suggestion for tuning a PID controller of a system that you can play with (you don't always have this luxury, but I understand you do) is:


Raise Kp until the system's response (even with a little overshoot) is satisfyingly fast to track (quasi-) step changes in your setpoint. This proportional component of a PID defines the 'stiffness' of your control system's response.
Raise Kd until the system's response is adequately damped. You don't need this if you don't have an overshoot. This derivative component defines an artificial damping for your system.
Raise Ki until the steady-state error (which you will have) with respect to the setpoint is corrected fast enough, without affecting the initial dynamics too much.
Fine tune all gains.


Change all values slowly, as too large values may cause instabilities in the system.