## Predictive Control and

Intelligent Sensors

An amazing project-based course taught by Dr. R. Dubay, in which we were introduced to a few different approaches for controlling industrial processes including Model Predictive Control (MPC), Dahlin and Smith Predictor compared to the conventional PID control scheme. We've used MATLAB for simulation/verification, LabWindows/CVI to interface with a Honeywell DC motor plant through a National Instruments DAC and to design a GUI. The following PowerPoint slideshow was prepared for one of our projects that demonstrates our approach in design and implementation of a PI, Dahlin and MPC controllers compared to one another; product of a teamwork with Salim, my former classmate. The plots inside the GUI are captured in real-time.

Everything up to and including slide 8 is preparation and simulation done in MATLAB. From slide 9 and after, real-time data is captured in the GUI for performance demonstration. As seen, PI proves its ability to easily handle simple first order systems. In fact, PI has produced a comparable outcome with MPC. Once the analysis is done, and proper values for Kc and Ki are obtained it takes some minimal coding to implement this controller. MPC provided similar output while adding some noticeable complexity to the design. As a result, for this particular case PI is sufficient to control this plant. Dahlin on the other hand, provided a much desirable response. It converged rapidly and perfectly to the setpoints. Dahlin seems an appropriate controller in this case with regards to the amount of complexity, performance and loading compensation.

One should also note that the system under investigation in this project was fairly simple. Thus, one may decide to choose PI or Dahlin over MPC since the amount of computation and complexity of MPC outweighs the first two controllers. Lastly, between PI and Dahlin, the latter appears faster to react to disturbances and is more robust.

All in all, what is clear regarding MPC controllers is their ability to control an unknown plant with minimal information. The only information that was provided was a unit step test. Thus, the quality of this controller is highly correlated to the quality of the model used. In the case of this project, an empirical model (step test) was used. This kind of model is easy to obtain. If one were forced to use an empirical model, it would be a good idea to formulate various models over different regions of operation of the plant in order to examine the reliability of their model.

## Real Time Systems - **Calculating **Pi

This course was instructed by Dr. R. Tervo where we had to deal with a completely new operating system QNX, which is a Real-Time Unix based OS. I have used the POSIX library, to perform message passing (task division) in this particular case. One of our class projects was to design a server machine that would divide chunks of calculation for the Pi number between two distinct computers over LAN in order to speed up the convergence using the Gregory series. I was required to calculate the Pi number to as many decimal places as desired and prove that using more than one computer/processor would decrease computation time.

In the following slideshow you would find my MATLAB's proof of concept that uses two identical functions to calculate the first 500 iterations of the Gregory series. The last image depicts the result only to 1 decimal place. In the video however, you can see the QNX realization of this concept using a machine equipped with 2 CPUs (2 cores each). In order to keep the length of the video short, only 11 decimals were obtained.

In the following slideshow you would find my MATLAB's proof of concept that uses two identical functions to calculate the first 500 iterations of the Gregory series. The last image depicts the result only to 1 decimal place. In the video however, you can see the QNX realization of this concept using a machine equipped with 2 CPUs (2 cores each). In order to keep the length of the video short, only 11 decimals were obtained.

I would suggest to watch this on YouTube in full-screen HD quality so you can follow much easier.

Jump to YouTube

Jump to YouTube

## Embedded Systems - SD-VRAOT

Prototype of a device we, Mahya Dehbozorgi and I, called it

The device operates at a relatively high speed of approximately 3 radians per second, under the condition that the target is within ± 57 degrees (one radian) in front of the sensors.

The following components were used:

**S**ingle**D**imensional -**V**isual**R**ange**A**ctive**O**bject Tracker that is capable of tracking any light emitting/moving object along the horizontal axis in close proximity (visual range). The light emitting source could be any incandescent light, thermal source, laser beam, etc. The programming was done in the C language and a bang-bang controller that would take all possibilities into account was employed.The device operates at a relatively high speed of approximately 3 radians per second, under the condition that the target is within ± 57 degrees (one radian) in front of the sensors.

The following components were used:

- HCS12DT256 microcontroller
- Freescale Semiconductor board (PBMCUSLK Project Board)
- DC motor
- Phototransistors (BPW76)
- Digital to Analog Converter (0832LCN)
- Operational amplifier (TL082)
- Dual Power Operational Amplifier (KA334 )
- Dual-rail Power supply ±15V

The test plan, sensors' operating region, circuit, system block, architectural and the Algorithmic State Machine (ASM) diagrams are shown in the following photo gallery. And of course the dude in the last photo is one of the proud designers :D

I've recorded this video to demonstrate SD-VRAOT in operation.

## A Magnetic Levitation System

MLS

The goal in this project was to design and build a Magnetic Levitation System that would pick up and suspend a ferromagnetic object. I was a member of a group of 4 senior electrical engineering students and my responsibility was to generate a mathematical model of the electromagnet (shown on the left), design a controller, convert to a discrete model, simulate and verify the functionality. I will later explain my part, but I'd like to introduce a few other important components first. The discrete controller that I've designed requires data at a high rate with minimal noise, acquired from sensors via a Microchip PIC18F4431 or LabJack (interfaced with MATLAB) as an alternative solution. Hall effect sensors (SS49E) were the choice in this project as for position sensing units. As seen in the following photo gallery, two sensors were implemented at the top and bottom of the electromagnet.

The Bottom sensor was calibrated as a function of the Top sensor which yields the following equation:

Estimation of Bottom sensor = 1.7×〖10〗^(-7) Top sensor + 1.01 Top sensor - 606.77

This equation uses the output of the Top sensor to estimate the output of Bottom sensor because the sensors are located in spots with relatively the same electromagnetic field strength. Now that we have an idea about the bottom region's actual value (in the absence of levitated object), we can use the equation below to extract the field strength induced by the steel object near the Bottom sensor only (as it is far enough from the Top sensor and the effects can be neglected):

Final value from Hall sensors = Estimation of Bottom sensor - Bottom sensor

One more thing to consider is the that the steel object becomes magnetized in the presence of a stronger field and induces its own magnetic field (refer to the picture above). The strength of this field depends on the current flow in the coil. A high precision resistor and a differential amplifier were used to remove the current effect from the output of the sensor. Finally the distance z, is obtained as outlined:

O/P voltage of the amplifier = (ADC value)/((2^16-1) ) * Vref, (Vref = 5V)

I/P voltage of the amplifier = (O/P voltage) / Gain, (Gain = 4V/V)

Current = (I/P voltage) / R, (R = 0.05±1% Ω)

Final value from Hall sensors → x

O/P voltage of the amplifier (representative of current) → y

Distance → z

Where z is a forth-order equation, calibrated through experiment and represents the best surface fit for 18000 data points:

Estimation of Bottom sensor = 1.7×〖10〗^(-7) Top sensor + 1.01 Top sensor - 606.77

This equation uses the output of the Top sensor to estimate the output of Bottom sensor because the sensors are located in spots with relatively the same electromagnetic field strength. Now that we have an idea about the bottom region's actual value (in the absence of levitated object), we can use the equation below to extract the field strength induced by the steel object near the Bottom sensor only (as it is far enough from the Top sensor and the effects can be neglected):

Final value from Hall sensors = Estimation of Bottom sensor - Bottom sensor

One more thing to consider is the that the steel object becomes magnetized in the presence of a stronger field and induces its own magnetic field (refer to the picture above). The strength of this field depends on the current flow in the coil. A high precision resistor and a differential amplifier were used to remove the current effect from the output of the sensor. Finally the distance z, is obtained as outlined:

O/P voltage of the amplifier = (ADC value)/((2^16-1) ) * Vref, (Vref = 5V)

I/P voltage of the amplifier = (O/P voltage) / Gain, (Gain = 4V/V)

Current = (I/P voltage) / R, (R = 0.05±1% Ω)

Final value from Hall sensors → x

O/P voltage of the amplifier (representative of current) → y

Distance → z

Where z is a forth-order equation, calibrated through experiment and represents the best surface fit for 18000 data points:

The z axis gives the distance from center of the object to the bottom of the electromagnet in units of millimeter. Higher and lower order functions were found but the forth-order fits the measured data better. In addition to finding the best surface fit, another method called Spline interpolation was tried but the result didn't seem to give the distance as accurately.

Of course we still need to filter sensors' output so as to remove higher/lower frequency noise that fall outside of our desired sampling frequency. Anyhow, we now have a good idea about how the distance could be obtained, so let's move on to the modeling and controller design.

Linearized Continuous Model

The following force and inductor models were used to describe the physical characteristics of the electromagnet:

Free-body diagram:

Linearized Continuous Model

The following force and inductor models were used to describe the physical characteristics of the electromagnet:

Free-body diagram:

Where I is current, X is distance (i.e. z), X-double-dot is the second derivative of position, Alpha and Beta are coefficients obtained through experimentation, m is the mass of the object and g is the gravitational constant. Wherever in this article a "Gain" block is seen, it should be regarded as the mass taken to the other side of the equation as 1/m.

The coil in frequency domain:

The coil in frequency domain:

Where Iout is the output current (from the coil), Vin is the voltage applied, s is the complex argument, L is inductance and R is winding's internal resistance.

The force model introduced was linearized to see if a PID controller would work within a narrow region. This linearized force model was obtained based on the first order Taylor series expansion (higher orders were neglected).

This model was placed into a subsystem which itself is part of the closed loop system equipped with a tuned PID controller. The controller feeds into the electromagnet's transfer function and then into the linearized model. The systems are displayed below in Simulink/MATLAB environment.

Based on a desired response time, PID coefficients (Kp = -1155, Ki = -1374, Kd = - 214) were obtained that would produce a promising result. The voltage and current values were what I've expected (within system's capacity) according to previous measurements. This applies to the total displacement prior to steady state as well.

Original Non-linear Continuous Model

Now that we know the PID can handle the linearized model, it's time to replace that model with the original and examine system's behavior again. The non-linear model shown below (on the right) goes together with the integrators (which convert acceleration to position) and they resemble the original plant (i.e. with the Alpha and Beta coefficients obtained). The same PID gains are used again.

Now that we know the PID can handle the linearized model, it's time to replace that model with the original and examine system's behavior again. The non-linear model shown below (on the right) goes together with the integrators (which convert acceleration to position) and they resemble the original plant (i.e. with the Alpha and Beta coefficients obtained). The same PID gains are used again.

As seen below, system's behavior is almost identical. At a different set point this time, all output values are again as expected. However, a very important/obvious observation can be made. The region within which the PID had reliable performance is now decreased, resulting in a very intolerant system with regards to object's initial position. This may result in the need for a more precise positioning system!

Non-linear Discrete System

This is were things get very interesting! Simulink makes it easy to simulate but it doesn't really help us with what we need to write in microcontrollers. A new idea was put to practice so as to overcome this challenge. MATLAB Embedded Function is the key, which will serve the purpose of writing discrete code similar to C. One can imagine it to be the microcontroller that will execute a desired algorithm in cycles. Therefore, any code that is written in an embedded function can simply be translated to C or any other languages for that matter. Using the exact same non-linear system described earlier, I removed the continuous PID block and replaced it with an embedded function as displayed here.

This is were things get very interesting! Simulink makes it easy to simulate but it doesn't really help us with what we need to write in microcontrollers. A new idea was put to practice so as to overcome this challenge. MATLAB Embedded Function is the key, which will serve the purpose of writing discrete code similar to C. One can imagine it to be the microcontroller that will execute a desired algorithm in cycles. Therefore, any code that is written in an embedded function can simply be translated to C or any other languages for that matter. Using the exact same non-linear system described earlier, I removed the continuous PID block and replaced it with an embedded function as displayed here.

I found it much easier to employ global variables within the embedded function to serve the controller's purpose. I would also save you a lot of time by telling you that this feature is only available in Simulink V7.5 and above; so don't waste your time looking for it in older versions! The controller was converted into a discrete PID controller as seen below and ready to be verified.

I found that a sampling rate of 1KHz in this case is suitable. The monitored values in the next picture display an exceptionally desirable behavior in comparison to previous controllers/models. The same restrictions with respect to initial position however, still apply. The farther away the object is from the initial set point, the faster sampling time is required in order for a reliable performance. In the initial simulation model, the controller was able to cover a relatively wide range (about approx. 4cm) where a continuous PID controller was used. The discrete PID has lowered this range significantly. This unfortunately, leaves not much room for any noise or error in sensory information. A higher sampling rate on the other hand, would provide more flexibility. There's still room for improvement in this code such as adding limits to the output voltage, converting voltage to PWM and removing negative output values which don't make sense.

Finally, I thought it would be a good idea to design a GUI and use LabJack to monitor data in real-time.

## DC Power Supply

Our team wanted to design and construct a 120VAC to 9VDC power supply, and analyze the associated characteristics of the final product.

This unit is still being used by my former classmate as a power supply for his guitar amplifier!