
Thread Tools 
This thread is privately moderated by shoutchen, who may elect to delete unwanted replies. 



Discussion
Pid processing algorithm
The drawing below is intended to be a representation of what
the computer thinks is the state of the servo actuator at any point in time. The computer updates all the information on a fixed interval and runs the PID computations. As you can see, a lot of the information is based solely on mechanical aspects of how the servo actuator was built, and other measurements you can take on the physical system. Other parts of the information is spec'ed out, like the dead band tolerance. The rest of the information is ascertained prior to the PID computation, like the angle and the set point. Remember, the drawing represents a somewhat ideal example , so a real world version may need to be adjusted to incorporate "as built" values for some of the information. The computational aspect of the PID routine generally follows this algorithm (At least this is what I have come up with, based on my research and experimentation... ), using the state information of the servo actuator: Determine the PID Gains. These are fixed once the system is "tuned". More on tuning PID gains in a later episode... Then, on a "timer basis" do the following steps: 1. If the angle is outside the soft stops, halt PID processing because something is broken. Skip all the following steps and don't drive the output arm. Don't allow PID processing the next time around. 2. Retrieve the set point and angle of the output arm 3. Determine if the angle is outside the dead band 4. If not skip all the steps and don't drive the output arm at all, but allow PID processing the next time around 5. If so, compute the error between the set point and angle and remember the sign of the error 6. Compute the P term using the error and Pgain 7, Limit the Pterm to the max controller output  "offset". Recall that the "offset" is the amount of "shove" to apply to get the output arm to move at all...to overcome the fact it is sitting still. 8. Compute the difference between the last angle reading and the current one. Doing this instead of using the difference in the error from last time to this time avoids using the set point in this computation and thus avoids the "derivative kick". (I believe the Atmel app notes mentions this, but the set point in the Microchip app notes is fixed... Google this to learn more) 9. Limit this to a physically possible range... ( remember the speed spec.. deg/sec) 10. Compute the Dterm using this and the Dgain ( remember the Dterm acts in the opposite manner as the Pterm). 11. Limit the Dterm to the max controller output  "offset" (keep sign straight) 12. Avoid "integral windup" (there are several ways to do this) (See the Atmel and Microchip app notes, and Google this to learn more) 13. If the Iterm is needed, add the error to the running total of error. 14. Compute the I term using the running error total and the Igain 15. Limit the Iterm to the max controller output  "offset" 16. Add the P, I, and D terms 17. Limit the sum of the P, I, and D terms to the max controller output  "offset" 18. Use the controller output and the "offset" to determine what to do to get the servo actuator to move the desired direction and magnitude. This normally translates to setting the duty cycle of a PWM waveform going to the servo actuator coil and selecting the direction of the current through it depending on the sign of the controller output) 19. Repeat all the steps starting at #1 after a fixed time Like I said... a simple math problem SteveH 

Last edited by shoutchen; Oct 31, 2008 at 07:37 PM.


