Intro to Control Theory and PID Loops

From FRC11 - Mentor Wiki
Jump to navigation Jump to search

Control Examples

  • Car cruise control
  • Thermostat for Heat or Air Conditioning
  • Navigational control for maintaining a ships heading
  • Autopilot for an aircraft
  • Keeping a Tesla in its own lane

Robotic Control Examples

Open Loop - No Feedback

Simple

Disadvantages?
Advantages?

Trapezoidal (Note - trapezoidal can be either open loop or closed loop depending on how it's implemented.)

  • Ramp the motor speed up to the set voltage
  • Stop a motor from running by ramping the voltage down to zero.
Disadvantages?
Advantages?

Closed Loop - Uses Feedback

  • Keep a wheel spinning at a constant velocity, despite varying resistance slowing it down
  • Maintain a constant speed up a ramp of varying inclination
  • Rotate toward a target at a variable rate. The rate varies such that it rotates faster when it has more to turn.

See image: Stepped output and ramped output

Control Theory Intro

  • Wikipedia Control Theory See Intro, Open-loop and closed-loop (feedback) control, and Other examples, where much of the following is cribbed from.

Control theory deals with the controlling dynamic systems. The objective is to develop a control model for controlling such systems using a control action in an optimum manner without delay or overshoot and ensuring stability.

To do this, a controller with the requisite corrective behavior is required. This controller monitors the controlled process variable (PV), and compares it with the reference or set point (SP). The difference between actual (PV) and desired value (SP) of the process variable, called the error signal, or SP-PV error, is applied as feedback to generate a control action to bring the controlled process variable to the same value as the set point. Other aspects which are also studied are controllability and observability. This is the basis for the advanced type of automation that revolutionized industries. This is feedback control, which involves taking measurements using a sensor and making calculated adjustments to keep the measured variable within a set range by means of a "final control element", such as a control valve.

Open Loop and Closed Loop

Fundamentally, there are two types of control loops: open loop control and closed loop (feedback) control.

In open loop control, the control action from the controller is independent of the "process output" (or "controlled process variable" - PV). A good example of this is a central heating boiler controlled only by a timer, so that heat is applied for a constant time, regardless of the temperature of the building. The control action is the timed switching on/off of the boiler, the process variable is the building temperature, but neither is linked.

In closed loop control, the control action from the controller is dependent on feedback from the process in the form of the value of the process variable (PV). In the case of the boiler analogy, a closed loop would include a thermostat to compare the building temperature (PV) with the temperature set on the thermostat (the set point - SP). This generates a controller output to maintain the building at the desired temperature by switching the boiler on and off. A closed loop controller, therefore, has a feedback loop which ensures the controller exerts a control action to manipulate the process variable to be the same as the "Reference input" or "set point". For this reason, closed loop controllers are also called feedback controllers.

The definition of a closed loop control system according to the British Standard Institution is "a control system possessing monitoring feedback, the deviation signal formed as a result of this feedback being used to control the action of a final control element in such a way as to tend to reduce the deviation to zero."

Likewise; "A Feedback Control System is a system which tends to maintain a prescribed relationship of one system variable to another by comparing functions of these variables and using the difference as a means of control."

Other Examples (straight from wikipedia)

An example of a control system is a car's cruise control, which is a device designed to maintain vehicle speed at a constant desired or reference speed provided by the driver. The controller is the cruise control, the plant is the car, and the system is the car and the cruise control. The system output is the car's speed, and the control itself is the engine's throttle position which determines how much power the engine delivers.

A primitive way to implement cruise control is simply to lock the throttle position when the driver engages cruise control. However, if the cruise control is engaged on a stretch of non-flat road, then the car will travel slower going uphill and faster when going downhill. This type of controller is called an open-loop controller because there is no feedback; no measurement of the system output (the car's speed) is used to alter the control (the throttle position.) As a result, the controller cannot compensate for changes acting on the car, like a change in the slope of the road.

In a closed-loop control system, data from a sensor monitoring the car's speed (the system output) enters a controller which continuously compares the quantity representing the speed with the reference quantity representing the desired speed. The difference, called the error, determines the throttle position (the control). The result is to match the car's speed to the reference speed (maintain the desired system output). Now, when the car goes uphill, the difference between the input (the sensed speed) and the reference continuously determines the throttle position. As the sensed speed drops below the reference, the difference increases, the throttle opens, and engine power increases, speeding up the vehicle. In this way, the controller dynamically counteracts changes to the car's speed. The central idea of these control systems is the feedback loop, the controller affects the system output, which in turn is measured and fed back to the controller.

Using Multiple (Different) Controls for Different Portions of a Non Linear Process

Suppose you have to control a single process variable, but you need it to behave differently near different setpoints or when approaching from above or below.

  • Non-linear Processes and Design Level Of Operation (DLO) To tune a non-linear process that responds differently to changes at different levels of output, you need to determine a representative operating point (DLO). Maybe you need more than one because there is too much variation, in which case - you need more than one controller, each based on its own range of operation.

So the answer is to use different controllers for different portions of the response curve. You can choose from open or closed loop controllers, PID and non PID, or all PID but different parameters.

Simple Closed Loop Controllers

  • Bang-Bang Control. This control is simple like a typical thermostat - either FULL ON - if below the setpoint, or FULL OFF - if above the setpoint. There is a typically a banding (or window) for the control points above and below the set points, to avoid constant switching on and off or to account for hysteresis (lagging of output). There are different ways to use the setpoints, but if hysteresis (lag) is minimal, and there is a desire to avoid constant switching, the setpoints could be implement as follows: For example, if a thermostat is set at 70° F, a Bang-Bang controller may be set to full output below 69° F, and keep running until the system reaches 71° F, then no output above 71° F, and until it falls below 69° F, whereupon the cycle repeats. A system will use full positive control to increase the process variable, and nothing (or full negative control) to decrease it. The process value usually oscillates about the setpoint in a sawtooth shaped pattern.
    • Bang-Bang with Bias. This adds a bias or maintenance element when in the window after the upper threshold has been reached, and before falling back to the lower threshold. The bias is used instead of full off (or full negative output) - it is a partial output that is intended to increase the time spent between the lower and upper control points. This is only applicable for systems with variable output - NOT applicable for control systems that can only be FULL ON or FULL OFF.
  • Trapezoidal or Ramping, when based on error (distance from target). Trapezoidal or Ramping control can be achieved with by a formula for controller output based on distance from the target setpoint. The correction is proportional to the error (maximum output when error exceeds thresholds), and is very similar to just the P portion of PID control.
Sample equations. Note, this is for systems with both positive and negative outputs.
NOTES - in the below equations:
THRESHOLD_LOWER and THRESHOLD_UPPER are both positive numbers. If the problem is symmetrical, then they are equal (otherwise not necessarily)
MAX_POSITIVE_OUTPUT is chosen for the problem at hand, it is always <= The Maximum System Output
MAX_NEGATIVE_OUTPUT is chosen for the problem at hand, it is always >= The Maximum System Negative Output (in some systems this may be zero)
Error = SetPoint - ProcessVariable (actual) so positive means actual is below setpoint, negative means actual is above setpoint
if (Error > THRESHOLD_LOWER)
{ Controller Output = MAX_POSITIVE_OUTPUT }
if (Error < (-1 * THRESHOLD_UPPER))
{ Controller Output = MAX_NEGATIVE_OUTPUT }
if ((Error > 0) and (Error <= THRESHOLD_LOWER))
{ Controller Output = MAX_POSITIVE_OUTPUT * Error / THRESHOLD_LOWER }
if ((Error < 0) and (Error >= -1 * THRESHOLD_LOWER))
{ Controller Output = -1 * MAX_NEGATIVE_OUTPUT * Error / THRESHOLD_UPPER }
(note - make sure you re-evaluate the signs for the last case, and test for proper sign usage based on your design)
NOTE - you may wish to have a deadzone near the setpoint where it's close enough and no output is specified
  • PID (Not so simple. See below.)

PID Loops

Description and Formula

PID Control is a closed loop control architecture that uses monitoring and feedback to improve control of a process variable - that is something you wish to control. It is more robust than open loop controllers that have a set it and forget it type of mentality, which do not monitor what is happening.

PID Controllers have up to 3 components:

  • P, the Proportional component
  • I, the Integral component
  • D, the Derivative component

But if you want them to work, they also have to have the feed-forward or steady state component, also known as the controller bias or null value.

The basic form for the controller output is:

CO = CObias + P + I + D,

or more specifically:

CO = CObias + Kp∙e(t) + Ki∙Sum(e(t0..tt)dt) + Kd∙Slope(e(t))

whereby:

CO is the controller output
CObias is the null value for the controller
Kp is the tuning constant multiplier for P, the Proportional element
e(t) is the error SP-PV at the current time (t)
Ki is the tuning constant multiplier for I, the Integral element
Sum(e(t0..tt) is the integral or summation of the errors from time 0 to time t
dt is the delta time from the first time integral element to the last time integral element
Kd is the tuning constant multiplier for D, the Derivative element
Slope(e(t)) is the slope of the error curve at time t


Fixing the Error Note that all of the components (aside from the CObias) are to deal with the Error, or difference between the setpoint and the process variable.

  • P deals directly with Error: e(t) is the error SP-PV at the current time (t)
  • I deals with the summation of the Error over time: Sum(e(t0..tt) is the integral or summation of the errors from time 0 to time t
  • D deals with the slope of the Error over time: change in Error / change in time, computed as (et - et-1) / (timet - timet-1)


More Details

  • CObias is critical to achieve an output that attempts to remain under control when at the setpoint. With no bias, when the system is at the setpoint, control would turn off, allowing the system to drift with no support. Much better to drift with appropriate support, which is what the bias adds. Moreover, without a bias, proportional control may be insufficient to fully reach the setpoint value, especially when close to the setpoint, as controller output drops towards zero.
    • Zero Bias? You can get away without a bias if your process variable is neutral - i.e. there is no inherent drift regardless of setpoint, and the necessary control output to reach the setpoint is independent on the setpoint value, only on the error (distance from setpoint).
For example, if you need to orient a robot's attitude (direction) on flat ground. It does not matter that it is pointing due north or due south, the effort to correct an orientation is based solely on the error (angular difference from the setpoint).
  • Setting CObias Typically, bias is determined experimentally. Once determined, there are several ways to set the bias, including:
    • Statically - with a fixed value that is the same always
    • Manual input - keep it at a user entered position (value)
    • Functionally - set it with an equation
    • Lookup table - look up values in a table, and use interpolation when in-between
  • KP, KI, and KD Constants These constants are multipliers of each of their respective components, and affect how much or how little they participate in the controller output. Typically, the P component is largest. Each of these constants needs to be determined, usually via initial estimates and some testing.
  • P Component The P portion is the Proportional component, and is the most important element of a PID controller. In fact, many successful controllers use only the P component, or the P component with an appropriate bias. A P component by itself works a lot like a Trapezoidal controller. The proportional element is based on the magnitude and direction of the current error, and compensates based on the size - the greater the error, the greater the correction, the smaller the error, the smaller the correction, in a linear (proportional) manner. For example - a 10% error (from 0 to max error) would result in a 10% of max correction, a 25% error would be a 25% correction, and so forth.
  • I Component The I portion is the Integral component - it integrates (or sums) the error terms over time. This is particularly useful if the standard proportional error correction cannot sufficiently accommodate an external force on the process variable. The Integral element is based on the magnitude and direction of the accumulated error over time. The more accumulated error, the greater the correction.
For example, if a ship's autopilot is trying to maintain a heading (direction) and a side wind is pushing it off course, proportional control will attempt to bring it back, but if the wind is strong enough, it will never quite make it. Over time, the accumulated error can be used to as a component to affect the control output so the correction is stronger, thereby overcoming the wind.
The downside, is that the accumulated error doesn't disappear once the correction is strong enough, and can keep the correction going even past the setpoint. This can be the beginning of oscillation. Advanced techniques, such as windowing the accumulation of error, can help to mitigate some of this.
  • D Component The D portion is the Derivative component, and is often not even used. It looks at how the error changes over time - its slope. The idea is that if the error is changing too rapidly, (especially close to the setpoint) the system will likely cross the setpoint rapidly and begin oscillating. The goal is to have a rapidly changing system damped so it does not swing too wildly - therefore some of the controller output correction is reduced if the process variable (and consequently error) is changing too quickly. The Derivative element is based on the magnitude and direction of the slope of the error over time. The greater the slope, the greater the cancellation of correction. (Be mindful of the signs of the slope).
  • D Component Issues and Solutions
Sampling Noise The D component is calculated as a change in error divided by a change in time. If the time slice is small, these are very small changes. Small errors in sampling the process variable (or noise) can contribute to significant errors in the calculated value of the slope, causes errors not just in magnitude, but even in direction. Because of this, simplistic approaches using only the last time slice to calculate the slope, can result in poor performance when incorporating D. Many systems simply do not use D.
A more sophisticated solution involves averaging recent slopes or calculating the slope over a longer period of time. This is usually a suitable approach - the slope is often not expected to change rapidly, and any lag introduced in the slope computation because it is now over a longer time period, is usually of little significance.
Slope versus Error A steep slope for error over time is a greater issue when the process variable is close to the actual setpoint (error near 0). Rapid changes far from the setpoint are actually desirable, but not when the process variable approaches the setpoint. The D component, in its form currently described, does not account for that. A more sophisticated approach might be to increase the effect of the D component as the process variable gets closer to the setpoint, i.e. as the error tends towards zero.


More on PID Loops

See additional page for PID Control. Contains more links and guides, including for the Talon SRX, plus practical concepts.

Also, these sources are excellent

More Info on Process Control