Re: PID in a PIC
Posted by
Dave Kowalczyk
on 2000-08-14 06:21:52 UTC
Alan,
Here's the "meat" of an algorithm I've used for servo modeling in
Basic. Time intervals for sampling have to be equal for this to work.
'Find applied voltage based on proportion
Vapp = (ThetaGoal - Theta) * P
Applied voltage is proportional to error between position and goal
by a factor "P".
'Find applied voltage based on derivative
Vapp = Vapp + ((ThetaGoal - Theta) - (ThetaGoal - Thetaold)) * D
Thetaold is the prior sample of theta (one interval removed). The
new error minus the old error in one time inteval is the rate of
change in the error. If the time intevals are always equal, then
it's not necessary to divide by time. Modify the applied voltage by
the rate of change multiplied by factor "D".
'Find applied voltage based on integral
Ing = Ing + (ThetaGoal - Theta) - Integral(Ip)
Integral(Ip) = (ThetaGoal - Theta)
Ip = Ip + 1
If Ip = 51 Then Ip = 1
Vapp = Vapp + (I * Ing)
This is a little trickier. The integral in this algorithm is the
sum of the 50 previous error samplings. Again, the time intervals
are equal, so no need to multiply by dt. Ing is a rolling sum found
by adding the current error and subtracting off the error 51 samples
ago, which is stored in the array Integral(). Then the rolling sum
(Ing) is modified by factor "I" and added to the applied
voltage. "I" is usually a small number to avoid oscillation.
Hope this helps! Good luck with your project...
Dave Kowalczyk
Ames IA
Here's the "meat" of an algorithm I've used for servo modeling in
Basic. Time intervals for sampling have to be equal for this to work.
'Find applied voltage based on proportion
Vapp = (ThetaGoal - Theta) * P
Applied voltage is proportional to error between position and goal
by a factor "P".
'Find applied voltage based on derivative
Vapp = Vapp + ((ThetaGoal - Theta) - (ThetaGoal - Thetaold)) * D
Thetaold is the prior sample of theta (one interval removed). The
new error minus the old error in one time inteval is the rate of
change in the error. If the time intevals are always equal, then
it's not necessary to divide by time. Modify the applied voltage by
the rate of change multiplied by factor "D".
'Find applied voltage based on integral
Ing = Ing + (ThetaGoal - Theta) - Integral(Ip)
Integral(Ip) = (ThetaGoal - Theta)
Ip = Ip + 1
If Ip = 51 Then Ip = 1
Vapp = Vapp + (I * Ing)
This is a little trickier. The integral in this algorithm is the
sum of the 50 previous error samplings. Again, the time intervals
are equal, so no need to multiply by dt. Ing is a rolling sum found
by adding the current error and subtracting off the error 51 samples
ago, which is stored in the array Integral(). Then the rolling sum
(Ing) is modified by factor "I" and added to the applied
voltage. "I" is usually a small number to avoid oscillation.
Hope this helps! Good luck with your project...
Dave Kowalczyk
Ames IA
--- In CAD_CAM_EDM_DRO@egroups.com, Tony Jeffree <tony@j...> wrote:
> It's worth taking a look on the http://www.microchip.com/ website -
> microchip publish a wide variety of application notes for the PIC
series,
> including stuff related to implementing 3-term control algorithms.
>
> Regards,
> Tony
>
> At 14:37 10/08/00 -0700, you wrote:
> >Hi to the List,
> >
> >I have been researching PID algorithms, and would like to
implement a
> >PID formula in a simple project(?) for my son. The Project is
a "line
> >following robot", and what we want to build is a PIC micro
controller
> >running a PID calculation. The input "error" is calculated from
levels
> >from an array of IR sensors. There are to be three pots digitized
for
> >the Kp, Ki & Kd gains. The output of the calculation will be used
to
> >control an R/C servo via PWM. The R/C servo steers the robot
(car).
> >
> >The sensors, pots, and PWM stuff is all fine, but in trying to
implement
> >the PID calculations, I find that I don't know much about it.
> >
> >A PID in a PIC has other obvious uses in CNC.
> >
{snip}
Discussion Thread
Alan Marconett KM6VV
2000-08-10 14:38:32 UTC
PID in a PIC
Spehro Pefhany
2000-08-10 16:24:18 UTC
Re: [CAD_CAM_EDM_DRO] PID in a PIC
Tony Jeffree
2000-08-13 13:34:53 UTC
Re: [CAD_CAM_EDM_DRO] PID in a PIC
Dave Kowalczyk
2000-08-14 06:21:52 UTC
Re: PID in a PIC
Alan Marconett KM6VV
2000-08-14 11:28:23 UTC
Re: PID in a PIC
Dave Kowalczyk
2000-08-29 20:06:40 UTC
Re: PID in a PIC