(Editor's additional note: if you are not familiar with the principles and analysis of PID control: you should be; It is the most important, studied, understandable, and applied closed-loop control ...
The ABCs of PID control. Considerations for interfacing elements in the electrical circuit to accomplish those parts of the PID function for position control. Firmware code implementation.