Need help with PID for motor control on a F877
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 pseudo-code for a PID loop is pretty basic. Say you want
to control motor rpm:
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
tuning the gains to get your system to respond the way you want. The
term gets you to a steady state operating condition; the proportional term
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
non-zero). In a PI system, if the proportional gain is too large, the
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
- 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
(I've heard this called "winding up"); you'll probably want to clamp it
some value that represents the maximum control signal allowable.
the integral gain is 1, and the proportional gain is large - which means
need to make sure that the proportional gain calculation is "legal",
- 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
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
Also, you might have a condition where you save the integral sum, then
it later. For example, to stop quickly, you set the pwm output to zero;
to get going again, if you know you want to be back at the same speed,
restore the old integral sum, rather than waiting for the loop to build
back up again. Another example of integral sum manipulation: if the
rpm changes by a large amount, you could add or subtract some chunk from
integral sum to give the pid loop a head start on getting to the new
- if you need to really get fancy, obviously the gains don't have to be
They can be proportional to the error, or different for different ranges
error, or different for positive and negative errors, etc. The limit on
integral sum also doesn't need to be constant. Just start simple, as
can get really complex - especially when your system is "crossing
- and on and on...
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email mitvma.mit.edu with SET PICList DIGEST in the body listserv
See also: www.piclist.com/techref/microchip/ios.htm?key=motor
You must be a member of the
piclist mailing list
(not only a www.piclist.com member) to post to the