This webinar provides a brief introduction on embedded control systems along with a practical example. Viewers will learn about the differences between open and closed loop systems, a few prerequisites for embedded control systems, controller math, and best practices to follow when designing and working with control systems.

### Related Articles

- How to Secure Consumable Components of Medical Devices and Other Embedded Systems
- Introduction to Closed-Loop Control and PID

### Related Courses

- Embedded Software Boot Camp
- Hardware Interfacing with C
- Embedded Linux Customization and Driver Development

### Transcript

#### Slide 1: Title

Robert Van Rooyen: Welcome everyone and thank you for joining us for this webinar on Embedded Control Systems in Practice.

#### Slide 2: Robert van Rooyen

My name is Robert Van Rooyen and I will be your guide as we learn a few basics about control systems and how they apply to a real world use case. I have had the privilege of working as a multi-discipline engineer for over 30 years and today’s topic stems from my research and understanding and reproducing human motion in the context of precaution instruments. I have found this area of robotics research to be quite interesting given the incredible speed, diversity and dynamics of virtuoso performances. And I hope it will serve as an interesting example of applying closed loop control theory.

#### Slide 3: Overview

The goal of this webinar is to provide a brief introduction on embedded control systems along with a practical example. Some of the related mathematics will be covered but only to the extent that it helps us understand the high-level components of closed loop control. The key takeaways include understanding some of the differences between open and closed loop systems, a few prerequisites, controller math, and finally a list of best practices to follow when designing and working with control systems.

#### Slide 4: Open Loop Example #1

We won’t spend a lot of time on open loop control systems, but it is worth a couple of slides for contrast with your closed loop counterparts. A simple open loop system in the form of a toaster is shown here. Given our collective experiences with toasters, we can see how the basic attribute list can be applied to this example.

Manual calibration consists of setting the timer and running a few cycles until you find a number that yields the best piece of toast. The system uses a static starting position which in this case is ambient temperature and the deterministic control as a timer. Other than cancelling the cycle manually or removing power, the toaster is unable to deal with any other disturbances on its own. The feed-forward control is current flowing through the heating element as determined by the resistance of the element and the timer setting. Note that if the timer is defective, we may need to quickly intervene with a fire extinguisher.

#### Slide 5: Open Loop Example #2

Another example of an open loop system is a typical clothes dryer. The control panel allows us to specify a cycle and perhaps a few other features before hitting the start button. Based on our selections, the dryer will run for a predetermined duration as defined by the manufacturer under similar working conditions.

What happens if we open the dryer door to pick up the quarter we forgot in our pocket? Will the dryer continue where it left off or will it start from the beginning again? Will the dryer tell us when the cycle has completed with an annoying buzzer or will we need to check it now and then? And what happens if the blanket was just a bit too big and waterlogged for the cycle we selected? With an open loop system of this type, it is up to you to determine their correct cycle and you may need to intervene to get the job done.

#### Slide 6: Closed Loop

So, what happens when we close the loop in the system? This type of control system is used heavily in industry and it is also used in your home and even your car. Controlling the temperature of a room or system, setting the position of a machine, and controlling the velocity and/or acceleration of a motor are all good examples of closed loop systems.

As the schematic diagram suggests, there is a feedback loop where measurements from the system being controlled are used to make dynamic adjustments. This active control design gives us the ability to deal with disturbances while guaranteeing reliable performance. Further, we can employ self-calibrating features that will allow the system to adapt to changing conditions, such as the environment and component ware.

#### Slide 7: Closed Prerequisites

Let’s take a look at some of the key ingredients of a closed loop system. The first is the ability to define a set point. For example, if we had a system that was controlling the temperature, we might use a set point of 68 degrees Fahrenheit.

Next, the system needs to compute the difference between the set point temperature and the actual temperature, which is defined as the error. The difference between the desired and the actual temperature is coupled to a forward path that is shown here as a PID controller. We will dig a bit deeper into the PID controller shortly. However, in this diagram, it is generating a value that is based on the error and will serve to modify the operating point of the driver, which is usually referred to you as the plant or process. Finally, the output of the plant is measured and fed back to where we started at the error computation, and thus we have a closed loop system.

#### Slide 8: Measured Feedback

In many control systems, the measured feedback is a digital sample of an analog sensor. Each discreet sample X as shown in the illustration is separated by a period of time or delta that has been optimized for the settling time of the system. If the period between samples is too long, we run the risk of missing important changes. Whereas if the sample period is too short, it will induce instability in the control loop.

Predictive control is an estimate on the rate of change and it is very sensitive to noise due to the weight it is given within the control loop. By reducing the noise component of discrete samples, the predictive control can rapidly respond to changes without being overwhelmed by noise that is essentially unrelated to the plant or process. An inconsistent sample period will also reduce the effectiveness of predictive control. Therefore, ensuring a consistent sample period with low noise and jitter will encourage a stable and responsive closed loop system.

#### Slide 9: PID Controller

Okay, I said we would talk about the PID controller and here it is. It turns out that PID controllers and their subsets are the most often used controllers. So, we will take some time to get familiar with this algorithm. At first glance, this figure looks a bit intimidating, but if we break it down to its components and see an actual implementation in firmware, it will begin to make sense.

To get started, we will identify a few key pieces of the equation that are also nicely illustrated graphically in the figure. The first is the set point that we are already familiar with from the previous slide. The set point is represented mathematically as r(t), where t is time. So, r(t) simply means a discrete sample of the set point.

Next, we have the error sample shown here as e(t). As you might have guessed, u(t) is a sample of the control value driving the plant. And y(t) is the actual measured or process value that is fed back to the beginning of the loop. This figure also shows a variable named beta that is added to the equation as a fourth term. In the following slides, we will dissect this equation. But at this point, we should also define what PID means.

PID is simply an acronym for the proportional, integral, and derivative terms in the equation. Together, they form the basis of a generalized controller that can be tuned to operate effectively in a wide variety of applications. With regard to subsets, a P, PI, or PD controller can be configured by simply setting the gain of the unused term to zero.

#### Slide 10: Error Sample

We have already discussed the error sample, but what does it really mean? Simply put, it is a measure of the distance from here to there. If we set the temperature to 68 degrees and the actual temperature was 85 degrees, then the error is 68 minus 85 or negative 17 degrees Fahrenheit. This tells us that for this discreet sample, the room temperature must be cooled by 17 degrees to reach our desired temperature.

Mathematically, we are just calculating the magnitude and direction of the error, which is the difference between the set point variable r(t) and the process variable y(t). As we will see in later slides, the goal of a closed loop control system is to drive the error sample magnitude to zero.

#### Slide 11: Proportional Term

Now we are getting to the core of the PID algorithm. The first term known as the proportional or P term is the value that answers the question. What is happening right now? The objective of this term is to write a component value to counteract the error sample. Note that the error value is akin to a disturbance that is either internally or externally applied.

The magnitude of this component is determined by a constant gain, K-sub-p that will be use to tune the system response to a disturbance. As the gain increases, so does the proportional response to the disturbance. If the gain is too low, the control loop will not be able to adequately compensate for the disturbance in terms of position or speed. In contrast, if the gain is too high, the control loop will overcompensate and put the system into a dampening or sustained oscillation.

#### Slide 12: Integral Term

Next is the integral or I term which answers the question what has been happening? This is achieved by summing or more formally integrating all of the error samples since the beginning of system time. As was the case with the p term, a constant gain is used to determine the magnitude of this term in the PID equation. Again, we will be using the gain to tune the system response to a disturbance.

An important consideration for the I term is its ability to quickly overwhelm the PID equation if the current measurement is far from the desired set points. This effect is known as windup and we will actually be placing a limit on the I term value to constraint its contribution in the implementation.

Ultimately, the goal of this term is to compensate for sustained or long term0errors and/or offsets. As the error begins to approach and oscillate around zero after a disturbance, the integral term will help drive the system towards zero and improve accuracy. With regard to gain, too little or too much can result in a lack of accuracy and dramatic instability respectively.

#### Slide 13: Derivative Term

Finally, we have the D or derivative term which answers the question what is about to happen. This value represents a prediction of what will be needed to counteract the disturbance based on the prior sample. As was the case with the previous PI terms, this term uses a constant gain that is multiplied by the rate of change of the error sample. As before, we will adjust the gain to get an optimal disturbance response from the system.

The derivative has a large effect on the response time of the system, which also makes it quite sensitive to noise in the measured feedback signal. For this term, too much gain will result in high frequency oscillations. For some control system applications, it is entirely reasonable and in fact necessary to use a gain of zero, which simply means a derivative term is not used since the nature of such control systems are not conducive to rapid changes.

#### Slide 14: Bias Term

Remember the fourth beta term? This is the bias and it can be used to nudge the system or compensate for things like static loads such as gravity. Aside from nudging the control loop, we can also use this to inject the disturbance. This could be intentional as part of the application, but it is also quite useful for tuning as we will see later.

#### Slide 15: Closed Loop Example

Now that we waded a bit through the PID equation, it is time to see a close loop system example. In this application, we are using a voice coil actuator to approximate human motion in the context of striking a drum. The system uses an industrial voice coil actuator with very high acceleration and peak force. The linear motion is being translated to a rotary motion in order to move the striking implement about an axis. This translation offers a magnification of the peak force while simultaneously providing a means to monitor its position with an optical quadrature encoder.

The last ingredient is the use of a pulse width modulated signal and an H-bridge that allows us to control the magnitude and direction of the current through the voice coil actuator as directed by the PID controller. In the next few slides, we will briefly review the major components in a bit more detail, but first let’s see how these components are interconnected at a high level.

#### Slide 16: Block Diagram

Starting at the top of the closed loop and moving clockwise, the first component is the quadrature encoder, which provides the rotation, speed, and direction information to the quadrature decoder. The decoder translates the speed and direction information to the absolute location of the optical disc that is fixed to the axis of rotation.

The PID controller takes the location at set point values and generates the direction and pulse width modulation signals for the H-bridge driver. Given the input signals from the controller, the driver adjusts the polarity and duty cycle of the current flowing through the voice coil actuator. The voice coil actuator in turn moves proportionally to the magnitude and direction of the current from the H-bridge driver.

Finally, the two degrees of freedom coupler translates the linear motion to rotary motion at the axis of rotation which closes the control loop. In this application, a striking implement is attached to the axis of rotation and therefore it can be precisely positioned as directed by the external set point. Additionally, the bias value can be used to induce a temporary disturbance or sustained offset of any magnitude and direction up to the maximum limit.

#### Slide 17: Voice Coil Actuator

A voice coil actuator is a continuous analog device that uses magnetic coupling with no backlash given the absence of gears. Since all of the variables in the force equation are constant by design, the force is directly proportional to the current magnitude and direction. Voice coil actuators are capable of resolutions on the order of microns depending on the type and quality of the control system and they are used extensively in industry for component positioning as well as controlling the read/write head in disk drives.

You might have also guessed that this is the same technology that you will find in a loud speaker. But instead of moving a paper cone to modulate air pressure and produce sound, this voice coil moves a shaft through a linear bearing. Although the coil will stop when it bottoms out, there is nothing inherently in place to keep the coil from becoming airborne in the other direction. As a result, some type of mechanical stop must be in place to prevent extreme positions, unless your intent is to produce an expensive one time use projectile.

#### Slide 18: Linear to Rotary Motion

Translating the linear actuator motion of the voice coil to a rotary motion provides an opportunity to magnify the peak force by increasing the torque. Two degrees of freedom are used to couple the voice coil through the rotating axis that is held in a fixed bearing. The primary shaft as a rotary assembly can be use to mount a standard optical quadrature encoder as well as a striking implement.

#### Slide 19: Quadrature Encoder

The quadrature encoder is a standard method to measure the speed and direction of motion, which in this case is a rotary configuration using an optical disc and encoder. The encoder generates two signals whose phase relationship and the frequency indicates speed and direction. There is also an optional index signal that is active on each full rotation of the disk.

The encoder signals are connected to a decoder that provides an absolute position count that will be used as our location variable, y(t). The encoder operates from a single viable power supply but can be notoriously sensitive to both mechanical and electrical noise. Filtered power, good shielding, stable mounting, and precision alignment are all essential ingredients for getting good signals.

#### Slide 20: PWM and H-Bridge Driver

A pulse with modulation signal is used to set the average current through the coil and the H-bridge circuit provides a means to change the direction of the current. In addition, the H-bridge includes safeguards to prevent overcurrent conditions and back EMF damage to the MOSFET drivers.

As depicted in the schematic diagram, enabling the pair of power MOSFET Q1 and Q4 while simultaneously disabling Q2 and Q3 will allow the current to flow in one direction through the motor, or in this case a voice coil. Conversely, by disabling Q1 and Q4 and enabling Q2 and Q3, the current is able to flow in the opposite direction. For this application, the PWM frequency is 20 kilohertz and the duty cycle resolution is 8 bits or zero to 255 decibel.

#### Slide 21: Firmware Implementation

Now that we have a bit of background on the application and electromechanical design, let’s take a look at the firmware over the next few slides. In this simplified bare metal example, the function main continually reads the location, updates the controller by calling the PID function, adjust the H-bridge driver by setting the H-bridge direction and PWM duty cycle and sleeps for a sample period before repeating the same sequence again indefinitely.

In this trivial example, we will take for granted that the PID gains, K-sub-P, K-sub-I and K-sub-D are set to pre-tuned default constants and that the desired position is being specified and updated by a separate thread of execution. The readLocation, pwm and usleep functions are rather self-explanatory. But let’s have a look at the all important PID function.

#### Slide 22: Firmware Implementation

As we have seen in the previous slide, the PID implementation is a single function that is called on a periodic basis that is equal to the sample rate of the location. This value is passed into the function as the LOC variable, which is the current location of the system. This function includes a reset feature that allows the calling code to clear the previous location sample and the integral value on demand.

As we can see, the error is calculated by simply subtracting the current location from the desired position, which is the equivalent of e(t) equals R(t) minus y(t). Recall that the goal of the PID controller is to eliminate the error and restore the system to the set point following a disturbance. Note that both the previous and integral variables are declared static in order to maintain their values between calls.

#### Slide 23: Firmware Implementation

Continuing with the PID function, we can see the integration of the error value along with windup or protection. The derivative is calculated by subtracting the current location from the previous location sample which represents the rate of change. All of the terms including the bias are assigned to the sum or u(t). Note that each of the terms are multiplied by their respective gain constants that were passed in to the PID function, which gives us the ability to adjust the gains at runtime. Finally, the direction is determined based on the sign of the sum, which will be used to control the direction of current flow through the H-bridge.

#### Slide 24: Firmware Implementation

In the final fragment of the pid function, the sum is clamp to limit the PWM duty cycle value and the sign is removed to establish the magnitude of the current through the coil. Last but not the least, we save the current location since it will be needed to compute the derivative on the next call.

#### Slide 25: Hardware Acceleration

Although the firmware fragment we reviewed can be run directly on a microcontroller, it was written for portability with C2RDL translation applications. This enables hardware acceleration, sample stability, and offloads the microcontroller to constraint on other critical tasks.

Depending on the performance and resource requirements of the design, the code can also use fixed point instead of floating point math, which dramatically reduces a number of resources needed in the FPGA fabric while simultaneously increasing the performance.

#### Slide 26: PID Tuning

PID tuning can be done manually or by using an auto-tuning algorithm. In either case, the method is dependent on the class of device and many other static and dynamic variables in the system. In our striking implement example, we can clearly see three cases when a disturbance is injected into the system.

The first plot in blue shows an oscillation that slowly dampens over a very long period of time, which is due to a high gain on the proportional term. By reducing the proportional term and applying a bit of gain to the derivative term, we can eliminate the oscillation as shown in the red plot, but there is significant overshoot in the signal. Finally, by bringing up the integral gain we can reduce the amplitude of the overshoot and reduce the settling time to approximately 250 milliseconds. Ideally, we would want to eliminate the overshoot entirely. However, in this case, that would require more tuning at the risk of introducing instability and potentially increasing the settling time.

For reference, the actual proportional, integral, and derivative term gains used for this plot were 0.35, 0.005, and 10.0 respectively. Generalized PID tuning methods are beyond the scope of this webinar, however you are encouraged to review the existing body of literature for methods that are applicable to your specific application.

#### Slide 27: Simulation

A number of tools and development environments are available to simulate a closed loop system. Assuming that the behavioral characteristics of the system are properly modeled, one can derive a first order approximation for PID gains that produce an optimal disturbance plot. Further, experiments with sample rates, data acquisition resolutions, and fixed versus single or double precision floating point can be conducted to optimize the design in terms of precision, performance, and cost.

#### Slide 28: Best Practices

There are a few best practices when it comes to designing closed loop systems. In general, the sample rate should fall within 1/10th to 1/100th of the expected settling time. In our example, we had a settling time of approximately 250 milliseconds, which implies a sample rate range of 40 hertz to 400 hertz. Setting the sample rate is part of the tuning process and if the plant is difficult to control, requires high derivative gain, or must be very precise then you should probably raise the sample rate.

Conversely, if the plant of easy to control and precision is not a major concern, then a reduction in the sample rate would be acceptable. Note that if the sample rate is too high, you can easily overflow the integral term and create noise issues with the derivative term. The integral term is effectively averaging the error value over time, which provides some immunity to temporary stability. Having said that, the sample rate should not be allowed to vary more than 20% within the 10 sample interval and ideally the sample rate jitter should fall within 1% to 5%.

From an ARTOS Perspective, this will imply the use of our hardware timer and interrupt service routine to acquire the sample that can be processed by a task with sufficient priority to complete the calculations before the next sample arrives. The derivative term is very sensitive to noise so the data acquisition system should be at good quality with appropriate low-pass filtering.

It can also be an advantage to over sample and average location value in an effort to further reduce noise in the sample that is used by the PID controller. As you have seen in the PID tuning slide, it is very important to be able to plot the position and measurement traces, which can also be rendered on an oscilloscope in real time and for digital to analog conversion facility is available in the design.

#### Slide 29: Best Practices Continued

In many cases, it will be important, if not critical to monitor system faults. As an example, monitoring the magnitude of the error value versus time can indicate events that fall outside of the expected settling time and that require immediate action to protect personnel and/or equipment. Other examples include data acquisition signal validation to ensure proper operating bounds are met.

With respect to tuning methods, there are many approaches that have been published in the literature and have been proven in the field. However, each closed loop system is unique and not all tuning methods will work all of the time. In fact, the vast majority of systems are only marginally tuned and continue to work quite well in practice.

#### Slide 30: Application Modifications

Once we have a stable system through proper design and tuning, we can start to do some interesting things with our example beyond maintaining a position. Remember the beta term? We can use that feature to intentionally inject a disturbance in order to find the limits of motion and calibrate our system to the real world, not to mention striking the drum surface with extreme vigor. Further, we can use it to train our system through dynamic conditions that will affect its performance while executing a large set of rudiments.

#### Slide 31: Recap

Congratulations, you have received a crash course in open and closed loop systems along with a practical example. As you might have guessed by now, this presentation just scratched the surface on the deep topic of control theory, but I hope it has given you a sense of what to consider when looking at open and closed loop systems. Further, you should have a new appreciation for PID controllers and some of the best practices that yield good design and stable systems.

There are many well-written books, articles, academic papers, and tutorials on closed loop systems and I would encourage you to review these sources in order to gain deeper insight into this very interesting topic. As is the case with mostly engineering learning opportunities, there is no substitute for hands on experience.

If you are not working on a closed loop system already or have not done so in the past, I would recommend experimenting with some form of closed loop control using components you already have or are relatively inexpensive to purchase. Use your imagination and discover new application that can benefit from embedded control systems in practice.