piclist 2001\07\19\140551a
>
Thread:
Need help with PID for motor control on a F877
www.piclist.com/techref/microchip/ios.htm?key=motor
BY
:
Mike Mansheim email (remove spam text)
> I have NO clue about a PID loop, so if someone could give me some code to
> look at and get an understanding, I'd greatly appreciate it!
[WARNING]: this ended up being a long post
Actually, the pseudocode for a PID loop is pretty basic. Say you want
to control motor rpm:
pid_calc:
last_error = error
error = target_rpm  measured_rpm
sum_error = sum_error + error
delta_error = error  last_error
proportional_term = error * proportional_gain
integral_term = sum_error * integral_gain
derivative_term = delta_error * derivative_gain
control_signal = proportional_term + integral_term + derivative_term
and the pwm for the motor is derived from control_signal. The real trick
is
tuning the gains to get your system to respond the way you want. The
integral
term gets you to a steady state operating condition; the proportional term
reacts
to sudden changes in speed (therefore, load), and the derivative term helps
react to a rapidly changing error. The derivative term is often not needed

I would certainly start without it. Using just the integral will work, but
the system will react relatively slowly to changes. You can also use just
the proportional term, but the speed will always be at some offset from the
target speed (some error would always be required to have the control
signal
nonzero). In a PI system, if the proportional gain is too large, the
system
will oscillate around the target (hunting).
Some other considerations/thoughts for turning this into actual code:
 pid_calc should be called at a consistent time interval, which should be
based on how fast the systems responds to a change in the control signal.
Do NOT adjust faster than the system can respond, or the feedback is
meaningless!
 be aware of the effect that filtering the feedback signal can have on the
response of the system. For example, if the rpm is being measured via
a frequency to voltage converter fed into an a/d input on the pic  that
is likely being done more frequently than pid_calc, with the result
filtered before being used in the pid calculations.
 error has a sign  this has to be dealt with in all the math. You want
the integral sum (sum_error) to be able to move up & down  so you want
a signed error  but that can be accomplished without actually using
signed variables, if that is your preference. On the other hand, a
negative control signal doesn't make sense in many systems. For
example, controlling the speed of a motor that can turn in both
directions: the motor direction is probably set elsewhere and is not
tied to this control signal. So you would trap a negative error and
set it to zero for the proportional term calculation.
 bounds checking in general: you don't want the integral term to roll
over
(I've heard this called "winding up"); you'll probably want to clamp it
at
some value that represents the maximum control signal allowable.
Typically,
the integral gain is 1, and the proportional gain is large  which means
you
need to make sure that the proportional gain calculation is "legal",
etc...
 one approach is to have control_signal 16 bits, error 8 bits, and the pwm
output the high byte of control_signal. With the integral gain = 1, this
is
a good place to start for a controlled ramping up to a target value.
 you'll have to decide when to reset the integral sum (sum_error) to
zero.
Also, you might have a condition where you save the integral sum, then
restore
it later. For example, to stop quickly, you set the pwm output to zero;
then
to get going again, if you know you want to be back at the same speed,
you just
restore the old integral sum, rather than waiting for the loop to build
it
back up again. Another example of integral sum manipulation: if the
target
rpm changes by a large amount, you could add or subtract some chunk from
the
integral sum to give the pid loop a head start on getting to the new
speed.
 if you need to really get fancy, obviously the gains don't have to be
constant.
They can be proportional to the error, or different for different ranges
of
error, or different for positive and negative errors, etc. The limit on
the
integral sum also doesn't need to be constant. Just start simple, as
things
can get really complex  especially when your system is "crossing
boundaries".
 and on and on...

http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spam_OUTlistservspam_OUT.....mitvma.mit.edu with SET PICList DIGEST in the body
<OFAFA6DC62.01FB6857ON86256A8E.0053EEC4@graco.com>
See also: www.piclist.com/techref/microchip/ios.htm?key=motor
Reply
You must be a member of the
piclist mailing list
(not only a www.piclist.com member) to post to the
piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
month overview.
new search...