# PID Control

I’m new to PID controlling, and am looking to learn. Anyone have a suggestion as to where to start?

As far as I understand, it seems as if a PID is to verify that a variable is actually where it’s supposed to be by checking, such as rotational velocity with a motor and an encoder or angle with a drivetrain and a gyro, etc. Is that right?

Not PID but very useful for controls for autonomous mode.

http://www.wildstang.org/main/stangps.php

Unless I missed something, I don’t think I saw anything about PID in that presentation. Very informative though.

Wikipedia has a rather helpful article on the subject. Even has some pseudo-code to look over:

``````
previous_error = 0
integral = 0
start:
error = setpoint - actual_position
integral = integral + (error*dt)
derivative = (error - previous_error)/dt
output = (Kp*error) + (Ki*integral) + (Kd*derivative)
previous_error = error
wait(dt)
goto start

``````

PID stands for Proportional-Integral-Derivative
Each is a “term” and the result of each is added to get the motor motion or change.
Proportional is, well, proportional to the difference between where you are and where you want to be. Basically, you say error = want - at, and then multiply that by a constant that testing reveals to be correct (the highest number without too much overshoot). Integral is the integration (sum) of the already corrected error, and gives the offset that should have been previously corrected.Derivative adjusts the rate of change by determining the slope of the error, the rate of change. This helps to even out noise.
Each term is multiplied by a gain that is statically set and manually tuned - this determines how much affect each term will have on the final output. It is also possible to switch from several gains based on set conditions, for example if you have a multi-jointed arm you might change the gain of the shoulder based on how far out the end-effector is from the shoulder, to prevent tippage when far out but allow it to move fast when tucked in.
PID is used in closed-loop control, basically you set a setpoint and it actively works to achieve it. This can implement itself in various ways, such as checking the angle of a potentiometer and adjusting it with a motor (used for angle control), checking the speed of a motor using an encoder and making it correct (useful for shooter wheels or such that must run at a certain speed), or even during autonomous to get the accumulated angle of a gyro and turn the chassis to acheve the desired angle, or setting the distance to travel and actively trying to get there without overshoot.
The standard FRC LabVIEW installation includes a PID toolkit - that can help. All you need to do is set the gain of each term.
If you want to learn how to do I would recommend implementing the simple Proportional control, tuning it, and seeing how it works. If you have problems, add either I or D depending on the problem. If you still have problems, add the other one.
One very very important thing to remember when working with potentiometers - if you turn them past their limit, they will break. Make sure that a positive motor direction = positive change in voltage. If this is not so, multiply the output by -1 or swap the wires on the OUTPUT side of the speed controller or the motor input. You could also swap the RED and BLACK wires on the pot.

As for where to start…
If you have large articulating structures on your robot, the most useful would be to use a potentiometer and a motor to control the angle. This would allow you to baically have a giant servo.
If you want to do autonomous stuff, then try to control the rotation (X on an arcade drive) using a gyro and a desired angle. When you get close to the desired angle, it will automatically slow down as the gain decreases and you will have a perfect turn.
And one last tip:
Always limit the output to -1 to 1. This is the range of the output and it is always good practice to do so. Do not rely on the WPIlib to do it for you.

And wikipedia has an article on the subject.

Matt Krass’ White Paper on PID Control Theory

I’m no software guy, but I understood it once I finished that paper.

I made a spreadsheet to model an ideal PID system. Its useful for understanding some of the basics, and how changing the parameters affects the output of the system.

I agree the Wikipedia is very helpful on the subject, and if you want even easier to read guides all I did was google “simple PID”

Also often I find that a PD or just a P loop will work for many simple actions. So starting with those to determine if you need the added complexity of a full PID is a good place to start.

awesome, I’ll take a look at all of those. I haven’t looked myself, but does the WPILibJ (that is, Java) include code for PIDs?

Well, I know there’s a PID toolkit provided with the FRC LabVIEW software.
However, there’s a bit about it I don’t understand.
I’m used to dealing with Proportional gain, Integral gain, and Derivative gain.
With the NI functions, the inputs are Proportional gain, Integral time, and Derivative time.
What are Integral time and Derivative time, and why would those be used as constants instead of the gain being used as constants?

Control Toolkit User Manual?

As Joe said, they are convertible. Kp equals Kc, Ki equals Kc/Ti, and Kd equals KcTd.

If you understood that without the manual, you are amazing.

This is how I learned PID

as well. Just note that the source code in it is a bit out of date. It’s from back in the days when we wrote in C, and PWM
outputs were unsigned char values instead of wonderful floats.

I have not used it, but WPILib (and presumably J and LV too) has a few Pid classes, so you shouln’t have to completely write a PID loop

Last year I was on 2343 (back in the USA) and was the lead on Autonomous. We used a PID loop, and in 6 weeks I went from knowing nothing about them, to programming one. I used the WPIlib documentation, as well as FIRST wiki, and the experience of my mentors. Between those three sources, I got a good understanding how the PID class works.

Dustin,

Your spreadsheet looks cool, but have you stopped to think what is this “position” you’re calculating? Is the spreadsheet a) simply a way of showing how the P, I and D gains of the controller change the step response of the closed loop system with a particular plant (in your case, an integrator), or b) a simulator to predict how the PID

controller will affect “a system” whatever it may be, from a steam locomotive speed or airplane roll to a FIRST
robot’s arm?

The answer it a), of course, and it is important to know the difference and clearly state it. If a student downloads your file and then uses it to tune his team’s robot arm/speed/position control not understanding what exactly this spreadsheet does - obviously it is not magic as to work for every plant in the world -, he could come up with an unstable or severely oscillatory system, damaging equipment or worse, getting injured; at the very best, he’ll be frustrated the robot doesn’t behave the way it is expected to. With that disclaimer, I believe your spreadsheet can be useful in teaching students the basics of PID

control.

PID control takes some experience to tune.

Start with P, than I. Usually you don’t need D. Sometimes you can get away with just P

Here are some good articles

http://www.controleng.com/article/268148-Loop_Tuning_Fundamentals.php

http://zone.ni.com/devzone/cda/tut/p/id/6368

http://zone.ni.com/devzone/cda/tut/p/id/3782

Great book