Published on *LEARN.PARALLAX.COM* (http://learn.parallax.com)

Most of us tend to enjoy the benefits of control systems without ever giving them a second thought. Consider the heaters, coolers, and thermostats that maintain the comfortable temperatures in our homes and workplaces. We just set the temperature on the thermostat, and the control system takes care of the rest. In this case, the control system's job is to regulate temperature, and when it does it's job we hardly think about it at all. Now, if it stops working during a heat wave or cold snap, well, that's an entirely different story.

More examples of control systems can be found inside our cars, where they are responsible for the smooth ride, anti-lock braking, fuel delivery, and ignition, just to name a few. Factories make use of control systems to maintain fluid levels in tanks, temperatures in curing ovens, and automated placement of parts in assembly lines. In the robotics world, control systems are used to help them maintain balance, distance from other robots or walls, and to refine navigation. With so many projects and products using control systems, the design techniques for control systems can be a useful addition to just about any inventor's toolbox.

Closed loop control is the kind of control where information from sensors is used to determine what the actuators in the system do. It is so named because the diagrams that describe the interaction between sensors and actuators draws a closed loop. (See Figure 1.) Some examples of sensors and actuators in closed loop systems include distance sensors and motors, temperature sensors and heaters/coolers, and fluid/gas level sensors and valve controls.

The block diagram for closed loop control in Figure 1 describes a process that is repeated over and over again. A sensor measurement is compared to the desired measurement (called the set point), resulting in an error calculation. Some math is performed on this error value (control calculations) to determine the output that goes to the actuator. The actuator has some effect on the system. The sensor is again checked to find out the result of the actuators influence, and the whole loop is repeated. Monitoring the influence of an actuator on a system and adjusting its influence on that system based on the sensor measurement is called feedback. The closed loop in Figure 1 is often referred to as a feedback loop.

There are lots of different control calculations that can be made to determine the actuator output. One of the most common and effective methods is called proportional-integral-derivative control, which is abbreviated PID. In fact, many of the factory, automotive, and robotic control system examples mentioned earlier involve PID.

This tutorial series introduces PID control, explains how it works, and demonstrates how you can do PID calculations with the BASIC Stamp 2. In this tutorial's activities, you will provide the BASIC Stamp with sensor feedback by sending it messages from the Debug Terminal's transmit window-pane. You will also monitor the PID controller’s output responses to get more familiar with the role of each of the three types of control.

The P in PID stands for proportional. This activity introduces closed loop control with just the proportional calculation along with a PBASIC program that performs this calculation. This activity demonstrates how a proportional calculation can be used to have an effect on the system that is "proportional" do the difference between the measured level and the desired level.

Proportional control is sufficient for some systems, and examples of proportional control can be found in *Robotics with the Boe-Bot*. As circuit schematics are used to describe circuits, block diagrams are used to describe control systems. A block diagram for proportional control is shown in Figure 2. The circle on the left is called a summing junction, and it shows how the measured sensor value is subtracted from the desired sensor value (the set point) to calculate the error. This is the error that needs to be corrected by the control system. Proportional control attempts to correct this error by multiplying it by some constant value (Kp). The resulting actuator output exerts a correcting influence on the system. By definition, this influence is proportional to the measured error. Since the actuator output has some effect on the system, the sensor value is checked again, and the whole process is repeated, over and over again to maintain the level(s) in the system.

Let's say a robot is trying to maintain a distance between itself and a target, but something keeps moving the target. Proportional control will detect the change in distance and apply drive to the robots wheels to make it back away from or catch up to the target. This drive is proportional to the error in the distance measurement. Keep in mind that "error" doesn't mean the distance measurement is wrong, it just means that the measurement is different from the desired value. Figure 3 shows a graph of the change in some error measurements over time. Notice how the error is 3 in the sample at time = 2. Since Kp is 10, the output is 30. In the second sample at time = 7, the error is -2, so the output is -20.

**Example Program - ProportionalAlgorithm.bs2**

Let's look more closely at how proportional control works with ProportionalAlgorithm.bs2. This program takes a sensor input that you enter into the Debug Terminal's transmit windowpane, performs the error and proportional calculations, and sends the drive value to the Debug Terminal's receive windowpane. (See Figure 4.)

- Enter and run ProportionalAlgorthm.bs2.
- For each number you enter, the output value should be ten times the calculated error.
- Try this sequence of numbers, making sure to press the space bar once between each number to make the DEBUGIN command accept it. 1 2 3 2 1 0 -1 -2 -3.

' ProportionalAlgorithm.bs2 ' Demonstrates how proportional control influences error correction ' in a feedback loop. ' {$STAMP BS2} ' {$PBASIC 2.5} SetPoint CON 0 ' Set point Kp CON 10 ' Proportionality constant Current CON 0 ' Current error sensorInput VAR Word ' Input error VAR Word(1) ' One element error array p VAR Word ' Proportional term drive VAR Word ' Output DO DEBUG "Enter sensor input value: " DEBUGIN SDEC sensorInput ' Calculate error. error(Current) = SetPoint - sensorInput ' Calculate proportional term. p = Kp * error(current) ' Calculate output. drive = p ' Display values. DEBUG CR, CR, "ERROR", CR, SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR, "PROPORTIONAL", CR, SDEC ? Kp, SDEC ? error(Current), SDEC ? p, CR, "OUTPUT", CR, SDEC ? drive, CR, CR LOOP

The DO...LOOP repeats the operations in the proportional control block over and over again. Figure 5 shows how each element in the block diagram is performed by a PBASIC statement. You are the System block and the sensor input. The program gets the sensorInput variable from you with the command DEBUGIN SDEC sensorInput. It then performs the error calculation with this statement error(Current) = SetPoint - sensorInput. The proportional block is executed with this statement p = Kp * error(current). The actuator output, which is also an input to the system is determined by this command: drive = p.

Let's say that you expect sensor values from -10 to 10, but you need to use these values to control a servo. Let's also say that the useful signal input range for the servo is from 1.3 ms to 1.7 ms. That translates to PULSOUT Duration arguments that range from 650 to 850 for the BASIC Stamp 2. You will need to add an offset to the output values as shown in Figure 5. Since the input is from -10 to 10, and Kp is 100, it means the result of the proportional block (Kp) will be -100 to 100. So, your offset value will have to be 750 to give you PULSOUT Duration arguments that range from 650 to 850. To make the program accommodate the extra summing junction, you will have to modify this statement:

' Calculate output. drive = p

One simple way to do it would be like this: drive = p + 750. However, it's a better coding practice to declare a constant at the beginning of the program and then use it in the drive statement, so try this:

- Add this constant declaration to the beginning of the program: Offset CON 750.
- Change the drive calculation to drive = p + Offset.
- Run the modified version of the program and verify that inputs from 10 to -10 result in outputs from 650 to 850.

Proportional is just one way to react to an error in the system. The problem with proportional control is that it can't detect trends and adjust to them. This is the job of integral control.

There is another example graph of the error in a system over time on the left of Figure 6. Again, it might be the distance of a robot from an object, or it could be fluid level in a tank, or the temperature in a factory oven. Perhaps the target the robot is following keeps on going away from the robot at a speed that the robot isn't catching up with. Maybe the oven door seal is worn; maybe the fluid draw from the tank is unusually large. Regardless of the cause, since proportional is not designed to react to trends it can't detect and correct the problem. That's where integral control comes into the picture.

Integral measures the area between the error values and the time axis. If the error doesn't return to zero, the area of the error gets larger and larger. The right side of Figure 6 shows how the integral output can react to this kind of trend. As the area between the error curve and the time axis increases, the output increases proportional to this area. As a result, the output drives the actuator harder and harder to correct the error.

So what happens when the error isn't a straight line, like the curve shown in Figure 8? That's what the calculus operation of integration determines, the area between a curve and an axis. In the case of integral control, as more time passes with an error, the area under the curve grows, and so does the value that the integral calculation will use to drive against the system error. If the error curve drops below the time axis, the buildup of negative area subtracts from the buildup of positive area. When tuned correctly, integral control can help the system hone in on an error of zero.

The BASIC Stamp can approximate the error under the curve with numerical integration. Figure 9 shows how you can approximate the error under a curve by adding up the area of a bunch of little rectangles between the error curve and the time axis. The area of each box is the error multiplied by the time between measurements. By adding up all the box areas, you get an approximation of the area under the curve.

So long as your measurements are evenly spaced, you can call the width of each box a value of 1. This makes the math much simpler than trying to account for 20 ms between samples, 5 minutes between samples, or whatever your sampling rate turns out to be. Instead of multiplying error by the time increment between samples and then adding to the next error multiplied by time, you can just multiply each error sample by a time of 1. The result is that you can just keep a running total of error measurements for your integral calculation. Here is an example of how to do this with PBASIC:

' Calculate integral term. error(Accumulator) = error(Accumulator) + error(Current) i = Ki * error(Accumulator)

The next example program performs numerical integration on the error signal and adjusts the output accordingly. As with proportional control, there is a constant that scales the integration output to the desired value. For simplicity's sake, we'll use 10 again for Ki. Figure 10 shows a block diagram of the control loop. The term Kp ∫ edt refers to Kp multiplied by the integral of the error over time. In other words, Kp multiplied by the accumulated area between the error curve and the time axis.

**Example Program - IntegralAlgorithm.bs2**

- Enter, save, and run IntegralAlgorithm.bs2.
- Enter this sequence of values into the Debug Terminal's transmit windowpane: 3 3 3 3 3 3 3 3 3. Notice how the integral output gets larger every time the 3 is repeated. That's integral's job, to detect trends and increase the drive to correct it as needed.
- Now try this sequence: 1 2 3 4 5 4 3 2 1 0 -1 -2 -3 -4 -3 -2 -1 0. The area under this curve is 0 since the negative area is the same as the positive area. That is also what the integral calculation will arrive at when you have finished entering the sequence.

' IntegralAlgorithm.bs2 ' Demonstrates how integral control influences error correction ' in a feedback loop. ' {$STAMP BS2} ' {$PBASIC 2.5} SetPoint CON 0 ' Set point Ki CON 10 ' Integral constant Current CON 0 ' Array index for current error Accumulator CON 1 ' Array index for accumulated error sensorInput VAR Word ' Input error VAR Word(2) ' Two element error array i VAR Word ' Integral term drive VAR Word ' Output DO DEBUG "Enter sensor input value: " DEBUGIN SDEC sensorInput ' Calculate error. error(Current) = SetPoint - sensorInput ' Calculate integral term. error(Accumulator) = error(Accumulator) + error(Current) i = Ki * error(Accumulator) ' Calculate output. drive = i ' Display values. DEBUG CR, CR, "ERROR", CR, SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR, "INTEGRAL", CR, SDEC ? Ki, SDEC ? error(accumulator), SDEC ? i, CR, "OUTPUT", CR, SDEC ? i, SDEC ? drive, CR, CR LOOP

This program is a ProportionalAlgorithm.bs2 modified to perform the integral calculation instead. There are now two different types of errors to keep track of, so there the error variable array was expanded to two elements. The **error(0)** variable tracks the current error, and **error(1)** tracks the accumulated error. The constants **Current CON 0 **and **Accumulator CON 1** make the bookkeeping a little more sensible with **error(current)** and **error(accumulator)**.

The other change that was made is that the error is accumulated and the integral output is calculated with these two statements:

' Calculate integral term. error(Accumulator) = error(Accumulator) + error(Current) i = Ki * error(Accumulator)

As you may have gathered from repeatedly entering 3 into the Debug Terminal, the integral drive output can really start to run away. You can use the **MIN** and **MAX** operators to keep integral control from going overboard with the output. Let's repeat the servo offset activity and limit the output from 650 to 850.

- Change the
**drive = i**statement to**drive = i + Offset MIN 650 MAX 850**. - Run the program and verify that the integral output is limited to values between 650 and 850. Try entering 2 repeatedly, then -2 repeatedly.

Here's a situation that neither proportional nor integral control really deal with. It's rapid changes to the system that come from an external source. Keeping the system steady when outside influences are making it change abruptly is the job of derivative control. In calculus, derivative is an operation that measures the rate of change of a curve, such as the error curve shown in Figure 11. By taking action based on the rate the error is changing, the output drive can respond rapidly to disturbances to the system.

Like the samples in Figure 11, calculus can be used to examine any point on the curve and give you an exact slope. Circuits that involve operational amplifiers do these continuous calculations for continuous time control systems. When a signal is digitally sampled periodically, the derivative can be an algebraic slope calculation. The BASIC Stamp can then take the difference between two sampled points on a curve, and divide by the distance (time) between those two points.

In the case of the error vs. time graph, derivative would be the change in error divided by the change in time. That's ∆e/∆t. As with integration, numerical differentiation (calculating the derivative) can be simplified for control purposes by always considering the time between samples to be a value of 1. Then, numerical differentiation can be accomplished by subtracting the value of the previous error measurement from the current error measurement. In other words, derivative control can be accomplished with simple subtraction:

' Calculate derivative term. error(Delta) = error(Current) - error(Previous) d = Kd * error(delta)

Although ∆t might be a small or a large value, so long as it's always the same interval, the derivative constant, Kd can be scaled to compensate for the fact that we made ∆t equal to 1. Keep in mind that the rate that the errors are sampled also has to be faster than the error signal's tendency to change.

The job of derivative control is to react to changes in the system. For example, a constant error of -3 will be dealt with by proportional and integral control, but derivative won't touch it. Why? Because derivative only reacts to changes. With a series of errors of the same value, there's no change. However, any kind of change, such as the error going from -3 to 4, will cause derivative control to resist. Let's try an example program and observe how this works.

Figure 13 shows a block diagram for the derivative control loop the next example program will emulate. As with the other examples, Kd will be 10. This program will measure the rate of change of the error measurement (de/dt) and multiply by Kp = 10. This result will go to your Debug Terminal (actuator output).

**Example Program - DerivativeAlgorithm.bs2**

Try entering a series of sensor inputs that don't change, like 3 3 3 3 3 3 3 -4. Derivative will resist the first 3 because that's a change from 0 to 3, but all the rest of the threes will result in no correction from the derivative calculation. Then, with the big change from 3 to -4, the derivative will drive hard to correct this abrupt change.

- Enter, save, and run DerivativeAlgorithm.bs2.
- Try 3 3 3 3 3 3 3 -4. Verify that derivative resisted the first 3 but made no attempt to correct the others.
- Also verify that the the big change from 3 to -4 resulted in a large output to correct the change in error.

' DerivativeAlgorithm.bs2 ' Demonstrates how derivative influences error correction ' in a feedback loop. ' {$STAMP BS2} ' {$PBASIC 2.5} SetPoint CON 0 ' Set point Kd CON 10 ' Derivative constant Current CON 0 ' Current error Previous CON 2 ' Previous error Delta CON 3 ' Accumulated error sensorInput VAR Word ' Input error VAR Word(4) ' Different types of errors d VAR Word ' Derivative term drive VAR Word ' Output DO DEBUG "Enter sensor input value: " DEBUGIN SDEC sensorInput ' Calculate error. error(Current) = SetPoint - sensorInput ' Calculate derivative term. error(Delta) = error(Current) - error(Previous) d = Kd * error(delta) ' Calculate output. drive = d ' Display values. DEBUG CR, CR, "ERROR", CR, SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR, "DERIVATIVE", CR, SDEC ? Kd, SDEC ? error(Delta), SDEC ? d, CR, "OUTPUT", CR, SDEC ? d, SDEC ? drive, CR, CR ' Save current error to previous error before next iteration. error(Previous) = error(Current) LOOP

The error array variable now has four elements. Element 0 is still the current error, element 1 is the accumulator, which is unused at present since there are not integral calculations in this program. Element 2 is used to store the previous error measurement, and element 3 is used to store the difference between the previous and current measurements. The constant for element 2 is **Previous**, and the constant for element 3 is **Delta**. That makes the derivative calculation much easier to read.

' Calculate derivative term. error(Delta) = error(Current) - error(Previous) d = Kd * error(delta)

At the end of each pass through the loop, the current measurement has to be saved as the previous measurement. There will be a new **error(current)** the next time through the loop, and a new **error(delta)** will be made with the updated **error(Previous)** value.

' Save current error to previous error before next iteration. error(Previous) = error(Current)

- Try changing the derivative constant (Kd) from 10 to 20.· It should react twice as strongly as before to abrupt changes in the error.

A PID control loop involves some contributions from each of the three kinds of control: proportional, integral, and derivative. The amount of contribution that each of the controls makes can be adjusted by changing their proportionality constants, Kp, Ki, and Kd. By making these constants larger or smaller, you can make the contribution of one of the controls more dominant or subtle in the system. One system might need only light integral control, some proportional and strong derivative, while another system might need strong integral and proportional controls, but not much derivative, while still another system might need roughly equal measures of each.

Here are several important things to keep in mind when working with PID control loops:

- Error is the difference between the level you want and the level that is measured, and control loops work to correct error.
- Proportional control resists error by applying an opposing influence that is proportional to the error.
- Integral control detects and corrects trends in error over time.
- Derivative control detects and resists abrupt changes in the system.

The next example program will perform this PID control loop. To make comparisons easier, Kp, Ki, and Kd are all set to 10.

**Example Program - PidAlgorithm.bs2**

Keep in mind that proportional always does some work when there's some error. However, integral and derivative are ready to do extra work to correct the error, integral to correct trends, and derivative to correct abrupt changes.

- Enter, save, and run PidAlgorithm.bs2.
- Here is a series of sensor inputs that the integral control will detect as a trend and resist much more strongly than either proportional or derivative: 1 2 3 4 5 5 5 5 5 5 5 5 5
- Here is a series of sensor measurements that derivative will work hard to correct while proportional and integral don't do nearly as much: 3 -3 4 -4 5 -5 6 -6 5 -5 4 -4

' PidAlgorithm.bs2 ' Demonstrates how a combination of proportional, integral, and ' derivative control influence error correction in a feedback loop. ' {$STAMP BS2} ' {$PBASIC 2.5} SetPoint CON 0 ' Set point Kp CON 10 ' Proportionality constant Ki CON 10 ' Integral constant Kd CON 10 ' Derivative constant Current CON 0 ' Array index - current error Accumulator CON 1 ' Array index - accumulated error Previous CON 2 ' Array index - previous error Delta CON 3 ' Array index - change in error sensorInput VAR Word ' Sensor input variable error VAR Word(4) ' Four different types of errors p VAR Word ' Proportional term i VAR Word ' Integral term d VAR Word ' Derivative term drive VAR Word ' Output DO DEBUG "Enter sensor input value: " DEBUGIN SDEC sensorInput ' Calculate error. error(Current) = SetPoint - sensorInput ' Calculate proportional term. p = Kp * error(current) ' Calculate integral term. error(Accumulator) = error(Accumulator) + error(Current) i = Ki * error(Accumulator) ' Calculate derivative term. error(Delta) = error(Current) - error(Previous) d = Kd * error(delta) ' Calculate output. drive = p + i + d ' Display values. DEBUG CR, CR, "ERROR", CR, SDEC ? SetPoint, SDEC ? sensorInput, SDEC ? error(Current), CR, "PROPORTIONAL", CR, SDEC ? Kp, SDEC ? error(Current), SDEC ? p, CR, "INTEGRAL", CR, SDEC ? Ki, SDEC ? error(accumulator), SDEC ? i, CR, "DERIVATIVE", CR, SDEC ? Kd, SDEC ? error(Delta), SDEC ? d, CR, "OUTPUT", CR, SDEC ? p, SDEC ? i, SDEC ? d, SDEC ? drive, CR, CR ' Save current error to previous error before next iteration. error(Previous) = error(Current) LOOP

Assume that your sensor inputs will range from -10 to 10. Adjust your constants (MIN and MAX operators.

Kp, Ki, and Kd ) so that the maximum contribution any of the controls can make to the output ranges fr om 650 to 850. To get the integral control to adhere to this requirement, you will also have to use the