Control Systems Lab STM32F4

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 30
At a glance
Powered by AI
The article discusses setting up hardware and software to design and implement embedded control systems using MATLAB/SIMULINK and Waijung.

The original workshop uses a STM32F4DISCOVERY board along with other Aimagin hardware accessories. The author uses just a STM32F4DISCOVERY board, USB-serial module, and components from their workshop.

The author modifies the analog input pin from C0 to C1 and uses a different op-amp due to differences with their hardware. They also solder components to a prototype board and connect it to the STM32F4DISCOVERY.

CONTROL SYSTEMS LAB

This is the first article in our Hello Waijung series.


Part I: Hardware Setup
Waijung (means “so fast” in Thai), developed by Aimagin, is a software product to be used
withMATLAB/SIMULINK and some supported processor board such
as STM32F4DISCOVERY from STMicroelectronics. This software, available for free
download from Aimagin website, is particularly useful for control system design and
simulation. It supports modern design approaches using MATLABpowerful computing
engine and user-friendliness of SIMULINK. Rapid prototyping and hardware-in-the-loop
simulation are among the techniques already familiar to experienced MATLAB users.
Nevertheless, the necessary hardware and software setups are not so trivial to a novice. The
purpose of this article series is to help introduce the audience to this development tool.

Even though one of my research areas is in control engineering, I cannot call myself
aMATLAB/SIMULINK expert, and this line of Aimagin products is new to me as well.
Actually, I am among those who heard about Waijung, bought
the STM32F4DISCOVERY board for some time and left it on the shelf, simply because I
did not know how to get started. Recently, Techsource andAimagin organized a workshop
that included embedded software development session with a nice introduction to this
product combination. Figure 1 captured the atmosphere of this workshop on April 3, 2014,
at King Mongkut’s University of Technology, North Bangkok.

Figure 1 MATLAB & SIMULINK Developer Day 2014 at KMUT-NB

In this event, the instructor used a basic experiment setup in Figure 2 for introductory
purpose. The plant was simply a   resistor and   capacitor connected as a
low-pass filter. An op-amp onaMG SIGCON-A signal conditioning board was formed as
voltage follower to prevent voltage drop from impedance loading. The communication
between the host PC and STM32F4DISCOVERY target was carried out via the aMG USB
converter N2 board. The aMG F4 connect 2 board was used to provide a convenient
connection between the processor and USB converter board.
Figure 2 experiment setup using Aimagin products

Now, I want to mimic this setup at home. I don’t have all the products in Figure 2, but only
the STM32F4DISCOVERY, a standard USB to serial module, and electronic components
on my shelf. In this article I will show you how it can be done.
First we need to draw a circuit diagram of the plant as shown in Figure 3. I search my
drawers and find a   capacitor and   resistor (different value from the one
used in workshop so I need to recomputed the plant transfer function. That’s the topic of
Part II).

Figure 3 circuit diagram of RC plant

The electrolytic capacitor is quite aged (like myself) but for demonstration purpose this
should work fine. The voltage follower circuit is not mandatory, but a good idea to protect
the DAC output of STM32F4DISCOVERY board. So I use 1/4 of MCP604 from
Microchip for such purpose. This op-amp works with low-voltage, single-rail power supply
(2.7 – 6V, according to Microchip datasheet). Here I connect to +5 V from the board.
Another modification form the original setup used in Aimagin workshop is the A/D input
pin. There they used pin C0 (AN10). First I followed that pin assignment and found the
voltage was off from expected reading. It took me a while to figure out from the board
schematics that the pin C0 was connected some other circuit on-board with a pull-up
resistor, hence the voltage reading error. So I guess the STM32F4DISCOVERY board used
in the workshop, if not modified, is somehow different from the one I have. Anyway, for
our project we change the analog input to pin C1 (AN11). Some blocks in
the SIMULINK files then have to be adjusted accordingly.
Figure 4 and 5 show the top and bottom of the prototype board
with STM32F4DISCOVERY mounted. The RC plant is soldered on the board and wired to
the connector pins, which is labeled by handwriting. The surface-mounted MCP 604 is also
soldered and glued to one rim of the board.

Figure 4 top of the prototype board with STM32F4DISCOVERY mounted


Figure 5 bottom of prototype board

*From the above figures, the used prototype board has a couple of old components
unrelated to this experiment. Sorry if this amateur illustration irritates you. I’m really out
of budget. LOL.
That’s all for the plant. Now we shift our attention to the USB connection between the host
and target. Not knowing the schematics of the aMG USB converter N2 board, I can only
check the SIMULINK diagram to see that the Txd and Rxd of USB-to-serial are connected
to pin D9 and D8 ofSTM32F4DISCOVERY, respectively; i.e., USART3 is used. So
regardless of what USB-to-serial hardware you are using, that’s all required to make it
work. Of course, you can use other USART such as wiring Txd to B7 and Rxd to B6 (use
USART1), but then you need to setup the SIMULINK blocks accordingly. For simplicity, I
follow the original setup from the workshop by connecting Txd to D9 and Rxd to D8 of
the STM32F4DISCOVERY board. Don’t forget to wire the ground between the USB
converter and prototype board. Signals for hardware flow control (CTS and RTS) are not
needed.
In my case, I find on my shelf an UM232R module from FDTI. I set up the module for
3.3V logic level and it works just fine. Another convenient choice is the USB-to-serial
cable made by FDTI or some other vendors. Make sure you select a 3.3V signal model.
From my experience, the driver from some cheap product does not work with Windows 8. I
don’t know whether that problem is fixed. Nevertheless, I recommend FDTI products.
Never have problem with them.
Figure 6 shows the UM232R connected to the prototype. Install the VCP driver and check
which COM port it consumes. On my computer it’s COM5. We need to setup
the SIMULINK blocks to match.

Figure 6 connecting a USB-to-serial module to prototype board

This is quite enough material for the hardware setup part of our Hello Waijung. Next time
we will continue the discussion by performing some open-loop experiment and see whether
our hardware works.
Part II: Open-Loop Test

Before you read on, make sure you at least glimpse on our previous artile in the Hello
Waijung series.
 Part I: Hardware Setup
From part I, with the required hardware set up properly, we are now ready to run our first
experiment. To make sure that all the connections and communication work, we will start
with a simple model consisting of only the plant; i.e., our RC network.

If you have not done so, install the ST-Link utility and WaiJung. Consult the developer’s
website for details. On my computer, I experienced during WaiJung installation that the
setup file complained it couldn’t find ST-Link, but after I continued by ignoring that
warning, the tool worked fine.

The SIMULINK setup for this experiment (as well as later ones) comes in pair: the target
and host models are separated to two files. Figure 1 and 2
show OpenLoop_Target.mdl andOpenLoop_Host.mdl, the target and host model files for
our open loop test, respectively.
Figure 1 SIMULINK file for target OpenLoop_Target.mdl
Figure 2 SIMULINK file for host OpenLoop_Host.mdl
The configuration and execution process for these models is listed as follows

 Signal routing
 (target) configure the ADC and DAC modules
 Communication
 (target) select the USART channel and configure
 (host) select the right COM port and configure its parameters to match the
target setup
 (host) compute the plant transfer function, discretize, and put data into model
 (target) update, build and load
 (host) run the simulation to observe real-time signal plots
Now we provide more details for each step.

Target ADC Module Configuration


From the hardware setup in part I, we decide to use AN11 for capacitor voltage reading.
Figure 3 shows how to configure the ADC block in OpenLoop_Target.mdl. Click on the
block to open the parameter dialog window and select Read AN11 (Pin: C1) dialog box.
Figure 3 ADC module configuration in OpenLoop_Target.mdl
Target DAC Module Configuration
To use DAC2, in OpenLoop_Target.mdl, click on the DAC block and select DAC2 (A5)
check box as shown in Figure 4. Leave other parameters intact since they are the values
used on theSTM32F4DISCOVERY DAC module.
Figure 4 DAC module configuration in OpenLoop_Target.mdl
Target USART Module Configuration
Figure 5 illustrates USART configuration in OpenLoop_Target.mdl. USART 3 is selected
with pin D8 and D9 as Tx and Rx, respectively. We recommend that you leave other
parameters as shown to use typical protocol 115200, 8, N, 1 with no hardware flow control.
If some parameter is changed, say, the baud rate, you need to make sure that parameter in
the host model file is adjusted to match. In any case, do not select hardware flow control
since the protocol needs additional signals CTS and RTS, which are not wired in hardware
setup from part I.
There are a couple of related modules in OpenLoop_Target.mdl that need to be configured
to use USART 3; i.e., UART Rx and UART Tx. Click on the modules and select UART
Module 3 from the parameter pull-down menu.
Figure 5 USART module configuration in OpenLoop_Target.mdl
Host COM Port Configuration
Before setting the host communication, you need to know which COM port your USB-to-
serial hardware is using. An easy way to check for Windows OS is to open Control Panel
-> Device Manager, anc click on Ports (COM & LPT). On my computer the UM232R
module consumes COM5, so I set up all the communication blocks
in OpenLoop_Host.mdl to use COM5, as demonstrated in Figure 6. Other parameters must
match those of the target. Leave them as is to use protocol 115200, 8, N, 1 with no
hardware flow control.
Figure 6 COM port configuration in OpenLoop_Host.mdl
Setup the Plant Transfer Function
From basic knowledge of signal & system course, we recall that a transfer function of RC
circuit connected as a LPF (see Figure 3 of Part I ) can be written as

(1) 
substituting   and 

(2) 
Create this transfer function in MATLAB to verify

>> R = 3300; C = 470e-6;


>> G = tf(1,[R*C 1])
 
G =
1
-----------
1.551 s + 1
 
Continuous-time transfer function.
To perform the real-time simulation simultaneously with the real RC circuit, we need a
discrete transfer function representation. The conversion can be done easily in MATLAB
with c2d command. Assume a sampling time of 0.01 second with default ZOH method

>> Gd = c2d(G,0.01)
 
Gd =
0.006427
----------
z - 0.9936
 
Sample time: 0.01 seconds
Discrete-time transfer function.

So, in OpenLoop_Host.mdl , we put Gd into the discrete transfer function block as in


Figure 7. That’s all for the open-loop test, since we don’t need any controller at this
moment.

Figure 7 specifying the discrete transfer function of RC circuit


Update, Build, and Load the Target
Assuming all the setup process is done correctly, the next step is to compile and build the
target code, then load it into the processor on STM32F4DISCOVERY board. You must
have the prototype connected to the PC via ST-Link USB (the mini-B connector on the
board), which is used to download the program. Actually, connect all the hardware from
Part I to the PC at this moment. The USB-to-serial is used for data communication when
simulation is running.
Since we have modified the target model, press Ctrl-D to update diagram. After you see all
the blocks turn red, press Crtl-B or click on the Build Model button. The build process
window appears as in Figure 8. If everything is ok, all the steps turn green and the code is
loaded to the board and run. A red flag on any step indicates error. For
instance, WaiJung may not be installed correctly, or target board is not connected.

Figure 8 build process window


Run the Simulation
I assume you have all green pass like in Figure 8 before clicking the run button in
theOpenLoop_Host.mdl file. If everything works properly as it should, you’d see the plot as
shown in Figure 9 after clicking on any small scope icon along signal path. The plot
continues until it reaches the specified final time, which is set for 30 seconds.
Figure 9 real-time simulation of the open-loop RC circuit
I also create another indicator in hardware to check if the communication works. A green-
orange LED is wired to TxLED and RxLED pins of FT232R USB-to-serial chip. As shown
in Figure 10, the dual color LED blinks as data is transmitted and received between host
and target.
Figure 10 dual color LED blinks during data communication
After the simulation ends, we have the plot shown in Figure 11. To elaborate, the input
voltage fed to the plant input (pink) is generated by the DAC module. With 5 seconds
duration, the voltage switches between 1 V and 2 V. The plant output is specified as voltage
across the capacitor (refer to the circuit diagram in Figure 3 from part I). The theoretical
output from discrete-time transfer function (red) is compared with the real voltage
measured via the ADC (blue). We can see that they match quite well, though voltage error
is noticeable around the end of voltage rise in each cycle. Also, the real voltage is slightly
contaminated by measurement noise.

Figure 11 the simulation result from open-loop test


As simple as it appears, this open-loop test verifies that both the signal passing through the
RC plant and the host-target communication work properly. Now we are confident to craft
a controller and study feedback system responses with the simulated and real plant, which
are the topics for the rest of this Waijung series.

From analysis, design, to implementation

PID Links →
Part III : Feedback Control

Previously in the Hello Waijung series.


 Part I: Hardware Setup
 Part II: Open-Loop Test
At the end of part II, we have a working platform to continue our experiment. From the
open-loop response, as the capacitor is charging/discharging, the output voltage gradually
changes towards the input, though we can see it could not reach Vin level in 5 seconds. If
we need to steer the output to the desired command level within some shorter interval, a
controller is needed. Output voltage feedback is also required for precise regulation. In this
part we investigate two schemes of real-time feedback control simulation
using MATLAB/SIMULINK, Waijung, and STM32F4DISCOVERY board that could help
reducing development time and effort. In the first structure, a controller is constructed as
aSIMULINK block and interacts with the real plant via USB-to-serial communication*.
The second structure is a standalone embedded application where a controller is
implemented on the target processor connected to the real plant. In both cases, a theoretical
output response is simulated for comparison.

*In the workshop they called this “hardware-in-the-loop” and included HIL abbreviation
here and there. I choose to honor the developer by retaining such name, though I make a
caution that it might not quite be consistent with HIL definition in a general sense.
For this part, we need to design a controller and put it in a feedback loop.. It is quite
customary to do the design in continuous-time domain and discretize the controller
later. MATLAB as a powerful control design tool for single-input-single-output system
called sisotool. Figure 1 shows the feedback configuration used by sisotool. In this case we
need to design and implement only the feedback controller C (the red block). The
feedforward controller F and sensor H are set to 1.
Figure 1 general feedback configuration
From part II, our plant transfer function in the s-domain is

(1) 
Figure 2 shows the sisotool graphical display during control design process. If you’re good
at root locus method, use the upper-left subplot. I’m more comfortable with loopshaping
method at right.

Figure 2 control design process with sisotool in MATLAB


So basically what I do is first adding an integrator to eliminate steady state error. Then I
add a zero close to the pole of the plant so that the loop transfer function has gain slope of
-20 dB per decade around my chosen crossover frequency. To achieve better measurement
noise attenuation, I add another pole to cause 2-pole roll-off in the high-frequency region.
Last, I adjust the gain to achieve a desired bandwidth. While doing this, I check the step
response y(t) and controller output u(t) as shown in Figure 3, which is a convenient feature
in sisotool. Higher bandwidth may result in too high controller output exceeding the DAC
voltage.

Figure 3 step response and controller output plots from sisotool


For demonstration purpose, I perform this adhoc design without a solid specification. You
may try formal control design procedure with specified rise-time, percent overshoot, etc.

So, after playing around with sisotool for some time, I eventually settle with this controller

(2) 
It is left as an exercise to the reader that, using ZOH method and sampling time 0.01 sec,
we can derive a discrete-time transfer function of this controller as

(3) 
Instead of doing the conversion by hand, we export the controller
from sisotool to MATLABworkspace, and discretize it with c2d function, with sampling
time 0.01 sec as its argument
>> Cd = c2d(C,0.01)
 
Cd =
 
0.09352 z - 0.09292
---------------------
z^2 - 1.97 z + 0.9705
 
Sample time: 0.01 seconds
Discrete-time transfer function.

You can hard-code this into SIMULINK diagram or make data available in workspace.


SinceSIMULINK discrete transfer function block accepts data in the form of numerator and
denominator, which you can use tfdata function to extract
>> [numz,denz] = tfdata(Cd,'v')
numz =
 
0 0.0935 -0.0929
denz =
 
1.0000 -1.9705 0.9705

then put the variable names into the block. I choose to hard-code the controller into the
models so that I could run it without doing this computation each time.

Once again, the SIMULINK models used for this experiment come in


pair HIL_AinH_Target.mdl, andHIL_AinH_Host.mdl, shown in Figure 4 and 5.
Figure 4 SIMULINK model for target HIL_AinH_Target.mdl
Figure 5 SIMULINK model for host HIL_AinH_Host.mdl
Notice that the controller blocks exist only in the host model file. There are two blocks, one
for theoretical model simulation, and another is put in the loop with the real RC circuit
plant. When real-time simulation starts, the controller, implemented in the host,
communicates with the real plant via the ADC and DAC modules
of STM32F4DISCOVERY.
The build and run process is essentially the same as in part II. The only difference is the
two controller blocks in host model, where the numerator and denominator data of the
controller must be put into.

Update, build, and load the target. Then run the host simulation. Clicking on any scope icon
on signal route yields the plot shown in Figure 6. It turns out that the discrepancy between
the theoretical and actual response is significant. Can you explain what happens?
Figure 6 real-time simulation result from HIL_AinH_Host.mdl
For those who have some experience with PID control, you’d see similar effect in a
situation known as “integrator windup.” The undesirable large overshoot happens when an
integrator is present and there is some saturation at the plant input. This is also the case for
our feedback system. Notice from the controller output   in Figure 3 that it has
maximum variation of nearly 2.5 volts, while the DAC output is limited to 0 – 3 volts, and
we generate input command signal that switches between 1 and 2 volts. So during
transition, the computed controller output could exceed the DAC range, causing worse
response than it should behave in a pure linear system.

To verify whether our explanation above is correct, we lower the control bandwidth a bit by
reducing the controller gain to 55% of its original value. This can be done easily by
multiplying the data in numerator field of the two blocks by 0.55. Figure 7 shows the step
and controller output response after the gain reduction. The controller output variation now
decreases to 1.6.

Figure 7 step and controller output responses after gain reduction of 0.55
Running the new simulation results in the closed-loop response in Figure 8. Now we see
that the comparison matches much better.
Figure 8 simulation result from HIL_AinH_Host.mdl after gain reduction

Standalone Implementation
Suppose that, after a series of simulation and redesign, we are satisfied with a controller
and decide to implement it on the target processor. This can be done easily
with Waijung by moving the discrete controller transfer function block from the host to the
target model file as shown in Figure 9 – 10. Now the host model contains only theoretical
simulation for response comparison.
Figure 9 target standalone model Standalone_Target.mdl
Figure 10 host standalone model Standalone_Host.mdl
First we experiment with the original controller without gain reduction to see the effect
from saturation. The responses are shown in Figure 11. The detrimental effect from DAC
output limit is less than the case when the controller is in SIMULINK, but is still
noticeable.

Figure 11 standalone response from high-gain controller


Now we reduce the controller gain by a factor of 0.55, resulting in the responses in Figure
12. The standalone response now matches that of the theoretical feedback model quite well.
Figure 12 standalone response after controller gain reduction
Conclusion
In these 3 parts of the Hello Waijung series, we examine the power and usefulness
ofMATLAB/SIMULINK and Waijung tool in embedded control system development. The
original workshop requires a set of hardware accessories, which is convenient for those
who have no time to solder and wire electronics by their own. Here, for a budget limited
guy like me, I show how the hardware can be kept minimal and concentrate on using the
software tool. Once you understand the basics, it is straightforward to extend your
knowledge to a more advanced plant than the RC network. Therefore, your first endeavour
is perhaps to replace the RC plant by a more complicated system and use the same
procedure to design, build, simulate, and implement feedback control for such plant.

You might also like