Microcontroller Software Library For Process Control: January 2015
Microcontroller Software Library For Process Control: January 2015
Microcontroller Software Library For Process Control: January 2015
net/publication/283857117
CITATIONS READS
0 409
3 authors:
Vladimír Vašek
Tomas Bata University in Zlín
68 PUBLICATIONS 154 CITATIONS
SEE PROFILE
All content following this page was uploaded by Jan Dolinay on 19 November 2015.
Abstract: - This paper deals with library of program modules for process control applications running on
microcontrollers. The aim of the library is to make it easier to create control applications for microcontrollers.
It should allow creating such applications by putting together existing program modules provided in the library
with little new code required. The software is written in C language and works with Freescale HCS08 8-bit
microcontrollers and the Kinetis series 32-bit ARM-based microcontrollers. It should also be easy to port the
code to other platforms.
which allows separating the hardware-dependent 2.2 Template code for user applications
code from the hardware-independent and thus Besides the actual working code - the controller
makes the application easily portable to new MCUs. modules, the library also suggest preferred way of
In the course of writing and testing the library also using these controllers by providing template files
some supporting code (hardware drivers) was for user application. These template files contain
created, which is also included in the library. skeleton code of a general control application. The
The described software was developed for Freescale user is advised to add these template files into
HCS08 8-bit microcontrollers [2] and for Freescale his/her program and use it as a starting point for
Kinetis family, which are 32-bit MCUs with ARM his/her application. The template files should guide
architecture [3], [4]. The idea of such a library the user in implementing the application-specific
originates from our previous work [7], [8], but the and hardware-specific code.
design and code is completely new. However, the user is not forced to use these
application templates or any other particular style of
programming. He/she is free to use any part of the
2 Control library library separately, for example, just the controller
When designing the library, the following module(s), which are C functions and therefore can
requirements were defined: be called from any C program.
• Provide discrete controllers usable in many
common MCU applications 2.3 PWM generator
• The library should have easy-to-use Another part of the library is a multi-channel
programming interface generator of pulse-with-modulated signal. This
• It should be easily portable to new MCUs signal can be used as a simple replacement for
digital-to-analogue converter and therefore is
From the logical point of view the library can be utilized in many control applications for driving the
divided into four main parts: actuators, e.g. for turning a heating element on and
• controller modules off in applications which control temperature.
• template code for user application In a typical microcontroller, there are several
hardware timers capable of generating PWM signal,
• software-PWM generator
but if the application can use PWM signal with
• supporting code
relatively long period (about 1 second or more), the
• documentation and examples software generator included in the library can be
advantageous, because it is easier to use than the
The core of the library is represented by the hardware PWM generator contained in the MCU
controller modules. The word module is used here and the code is hardware-independent.
in the meaning common in C-language
programming; i. e. the module is physically a pair of
2.4 Supporting code
header (.h) and source (.c) file, which contains the
The library also contains supporting code, which
definition and implementation of a single controller.
was created during the development of the library
In the following subsections the main parts of the
for testing its functions on real hardware. An
library are described in more details.
example of such code is the driver for serial
communication interface (UART).
2.1 Controller modules This code can be directly used in applications
As already mentioned, the controller modules can be targeting one of the MCUs supported by the library.
considered the core part of the library. From the For other MCUs it may at least provide starting
logical point of view, the controller module is an point for the user’s own implementation of similar
“object” of one type of a controller, e.g. a discrete code, which will be likely needed when using the
PID controller. The interface of the controller library on any MCU.
modules implemented so far in the library consists
of two C functions:
2.5 Documentation and examples
• initialization function which initializes the
The library also contains documentation and
internal data of the controller
example programs. The documentation consists of a
• and “step” function which computes the user manual, which describes the library and its use
controller output in each step of the control and reference documentation created from the
process (in each sampling period). source code comments using the Doxygen tool.
There are also example programs which For the HAL module (ucp_hal):
demonstrate the recommended use of the library in • ucphal_init
real application. For user convenience, the example • ucphal_read_input
programs are provided with complete projects for • ucphal_read_setpoint
the CodeWarrior IDE. • ucphal_write_output
Library
ucphal_init templates
ucphal_read_input
ucp_hal.c ucphal_read_setpoint
ucphal_write_output
User code
Drivers +
Library code
Microcontroller hardware
Fig. 2 The main function of a control application with the
library
Fig. 1 Overall view of the application using the library
As can be seen in the code listing, all that happens
As can be seen in the figure 1, there are three main in the main function is a call to the ucp_app_init
parts of the software of the application (on top of the function at the start of the program and then
MCU hardware): user code, library template files repeated calls to the ucp_app_on_sample. These
and device drivers. calls should occur with the sampling period of the
Considering just the library code, there are two main system. In real-world application it would probably
parts: use better timing tool than the busy-wait delay used
• Application (ucp_app); in this simple example.
• Hardware abstraction layer (ucp_hal). Both the ucp_app_init and ucp_app_on_sample
functions are implemented in the template file
These main parts are located in two template files ucp_app.c provided by the library. Obviously, the
provided by the library: ucp_app.c and ucp_hal.c. template cannot deliver the functionality for the user
The interface between these files and the user application. It is the user of the library who needs to
application is defined by the library and consists of write the program. However, the library tries to
the following functions: make this task easier by providing as much code as
For the application module (ucp_app): possible; for example, calling the HAL API where
• ucp_app_init needed and providing skeleton of the application
• ucp_app_on_sample together with example code. The default contents of
these two functions can be seen in figures 3 and 4.
controller). As can be seen in the code listings, the HAL initialization function, which initializes the
functions such as ucphal_read_input receive the MCU peripherals used by the application. This code
channel number as their input parameter. needs to be written by the user of the library. It
The recommended scenario for using the library is cannot be provided in the template files as the actual
as follows: peripherals (e.g. I/O pins) are strictly application-
• User will create an instance of a controller specific. However, the user can take advantage of
by defining one variable (a C structure the library drivers for some peripherals or write
which holds the private data of this instance custom code for the required hardware.
of the controller). The ucp_app_on_sample function is called by the
• After creating the variable for controller main program in every step of the control process.
data, user passes this variable into the In general, it performs these steps:
controller initialization function. • Read the input(s)
• Then he/she ensures that controller “step” • Read the setpoint(s)
function is called regularly with the period • Compute the actuator signal(s)
equal to sampling period of the system. This • Output the actuator signal(s)
can be done using simple busy loop in the
main function, or (preferably) by using The HAL API provides standardized functions for
hardware timer. these tasks. However, the internals of these
The user is assumed to add the template files functions need to be written by the user. For
ucp_app.h and .c and ucp_hal.h and .c to his/her example, the reading of the input signal can be
project and implement the application and hardware performed using the Analog-to-Digital Converter
specific code in these files. The application logic (ADC) in the MCU, by processing a signal from a
including the controller variable(s) is contained in sensor, such as value encoded by PWM, or even
ucp_app.c file. through some communication interface such as SPI
Note that the fact that the user provides the memory or UART. This depends on the application and
for storing the controller data is advantageous for cannot be handled in the library templates.
embedded systems with limited amount of RAM, Reading the setpoint is similar, although the setpoint
because only as much memory is occupied as is is typically entered by the user. There are many
needed. Other approaches which provide the ways how entering the desired output of the
memory internally and automatically in the library controlled process can be handled in the application.
may be somewhat easier to use, but they require Therefore the UCP library uses the same ways of
more resources. Either the memory needs to be abstracting access to this property as with input and
statically allocated inside the library, which limits output signals. As a result, it does not matter for the
the number of available controllers and wastes application whether the setpoint is obtained by
memory of the unused controllers, or there needs to reading an analog value set by a potentiometer, read
be dynamic memory management used, which from memory where the value is stored and updated
brings large overhead to the code. when user presses some buttons, etc. This hardware-
specific code is isolated in the ucphal_read_setpoint
function from the rest of the application.
4 API of the library The computation of the actuator signal is one part of
This section describes the application programming the code which is handled from the biggest part by
interface of the library’s Application (ucp_app) and the library itself. This is done by calling the
HAL (ucphal) modules. controller “on step” function, for example
ucp_psd_on_step.
4.1 Application API The realization of the output signal also depends on
Currently, the ucp_app API consists of just two the application greatly. One often used type of
functions: output is a PWM signal. As already mentioned, the
• ucp_app_init library provides built-in support for this kind of
• ucp_app_on_sample output. For other types of output signal, for
example, digital to analog converter or simple
As already mentioned, these functions are discrete output, the user must provide his/her own
code. In any case this hardware-specific code should
implemented in the ucp_app.c file.
be hidden from the rest of the application in the
The ucp_app_init function initializes the application
ucphal_write_output function.
including the hardware. Its main task is to call the
4.2 HAL API used to compute the control signal in each sampling
The ucphal API consists of four functions: period is:
• ucphal_init
• ucphal_read_input u (k ) = u (k − 1) + q 0 e(k ) + q1e(k − 1) + q 2 e(k − 2) (1)
• ucphal_write_output
• ucphal_read_setpoint Where k denotes the step number, for example, e(k)
is the control error in current step and e(k-1) is the
These functions are implemented in the ucp_hal.c error in previous step.
file. The coefficients of the controller q0, q1 and q2 can
The ucphal_init function should perform all the be obtained by methods for controller tuning, such
initialization of the hardware. As with all functions as [1].
in the HAL module, this needs to be written by the From the programmer’s perspective, the interface of
user of the library according to the needs specific this module is represented by data structure
for given application. The default content of this UCP_PSD_REG and functions ucp_psd_init and
function in the template file provided by the library ucp_psd_step. Signatures of these functions can be
can be seen in figure 5 above. seen in figure 7:
The ucphal_read_input function should return the
current value of the controlled-process output for
given channel. The requested channel number is
specified as input parameter to this function. As
already discussed, this function provides uniform
interface to the control application for obtaining the Fig. 7 Interface of the discrete PID controller
input signal without regard to the actual hardware
principle for obtaining this input. The default The “init” function is called by user program once at
implementation from the template file can be seen in the beginning to initialize the controller. The “step”
figure 6 above. function should be called in every sampling period
The ucphal_write_output function can be described to obtain new value of the control signal. Both these
as opposite to the previous function. It takes care of functions receive pointer to the data structure of the
applying the output calculated by the controller to controller as their first parameter. Parameter y is the
the actuator for given channel. Again, this function current output of the controlled plant; setpoint is the
represents uniform interface for the control desired value of the controlled signal; the minval
application for performing this task, hiding the and maxval are the boundary values for the control
actual principle used for controlling the actuator signal (e.g. 0 and 100 percent for PWM signal).
from the application code. These values need to be provided as the input
The ucphal_read_setpoint function is responsible parameters of the controller function, because the
for obtaining the current value of the setpoint for old values of u(k) stored inside the controller must
given channel. As already discussed, the means of correspond to control signal really applied to the
obtaining the setpoint value can vary and the controlled process.
purpose of this function is to allow the control
application to use one standard way of obtaining the 5.2 On-off controller
setpoint value without dealing with these This module implements simple On-Off controller
differences. with optional hysteresis. The data structure for this
controller is called UCP_ONOFF_REG. As in the
case of the discrete PID controller, there are two
5 Library controller modules functions. The signatures are shown in the figure 8.
Currently, two control algorithms are implemented
in the library, i.e. two types of controllers are
available: discrete PID controller (PSD controller)
and On-Off controller with hysteresis.
Fig. 8 Interface of the on off controller
5.1 Discrete PID controller (PSD controller)
This module implements the incremental version of
As can be seen in the figure, the user can specify
the discrete PID algorithm. The recursive equation
two values of the hysteresis; one for the “up”
direction of the controlled signal and one for the
“down” direction. The other parameters have the In figure 9 the evaluation kit can be seen. There can
same meaning as explained in the discrete PID also be seen the model of a heating plant [5]
controller section. attached to this kit, which was used for experimental
verification of the library. This model contains a
resistor which can be heated using digital output of
6 Target hardware the MCU. There is also temperature sensor attached
The library aims to be as much hardware- to this resistor whose output is attached to the MCU.
independent as possible. The hardware-specific code
which is necessary should be easily portable to other 6.2 Kinetis 32-bit microcontrollers
MCUs. However, it was necessary to implement and The second hardware platform for which the library
test the library on some specific hardware. We was implemented is Freescale Kinetis series of 32-
selected Freescale’s 8-bit HCS08 MCUs and 32-bit bit microcontrollers. Compared to the HCS08 core,
ARM-based Kinetis series MCUs as two the 32-bit MCUs offer higher computing power and
representatives of relatively different MCU more memory, which makes them suitable even for
platforms. Important role in the selection played the more demanding control applications.
availability of the device in a development kit. For There is a line of low cost evaluation boards
these reasons we used the development kit from the available for the Kinetis microcontrollers called
MCU-programming lessons at our faculty, which Freedom platform [3]. In our case FRDM-KL25Z
contains HCS08 GB60 MCU for the 8-bit board was used. This board contains
microcontroller implementation. For the 32-bit KL25Z128VLK4 microcontroller with 128 kB of
version a new, very affordable development kit Flash and 16 kB of RAM memory together with
FRDM-KL25Z with the Kinetis MCU was used [4]. programming and debugging interface (openSDA).
The layout of the board is compatible with the
6.1 HCS08 8-bit microcontrollers layout of popular Arduino platform [9] which makes
As mentioned above, for testing the library on 8-bit it possible to connect expansion boards (so called
MCUs we used development kit shields) for Arduino to this board.
M68EVB908GB60. This kit is no longer
manufactured, but still used in our lessons. The
GB60 derivative used in this kit is still available and
besides, it should require little effort to use the code
for the GB family on another (more modern)
member of the wide HCS08 product line, e.g. the
QG or SH families.
7. Experimental verification
To verify the functionality of the library several
control applications were created. The controlled
system for this verification was represented by a
model of heat plant, which we also use in
programming lessons. This model was described in
detail in [5]. The model represents a 2nd order
system with transfer function approximately:
0.8
G ( s) = (2)
(86.5s + 1)(18.2 s + 1)
Fig. 9 HCS08 development kit with heating-plant model
attached
This model was attached to the evaluation board generator. Documentation and example programs
with HCS08 microcontroller by the means of its are provided as well.
connector – the same way it is used in our The library is written in C language and currently
lessons. In case of the Kinetis board, which implemented and tested on two types of
microcontrollers: Freescale’s 8-bit HCS08 and 32-
does not have compatible connector, the model
bit Kinetis (ARM) series. However, porting it to
was connected using wires. other microcontrollers should be relatively easy and
Figure 11 shows the result of control process straightforward.
with the discrete PID controller module. It is This work was supported by the European Regional
very simple control process, but it demonstrates Development Fund under the project CEBIA-Tech
the correct function of the program library. The No. CZ.1.05/2.1.00/03.0089)
parameters of the controller were designed
using method [1]. The control signal is References:
generated using the software PWM module [1] A. Víteček, M. Vítečková, Inverse Dynamics
which is part of the library. In the figure, the set Method Tuning and Basic Quality Indices, 9th
point and output of the plant are depicted in International Scientific Conference CO-MAT-
TECH 2001, Slovenská technická univerzita,
degrees Celsius; the control signal is shown in
2001, pp. 412-417.
per-cents. The set point value was 50 °C. [2] Freescale Semiconductor, CPU08 Central
Processor Unit Reference Manual, rev.4,
Available: http://www.freescale.com.
[3] Freescale Semiconductor, Freescale Freedom
Development Platform. Available:
http://www.freescale.com/webapp/sps/site/over
view.jsp?code=FREDEVPLA.
[4] Freescale Semiconductor, FRDM-KL25Z:
Freescale Freedom Development Platform for
Kinetis KL14/15/24/25 MCUs, Available:
http://www.freescale.com/webapp/sps/site/prod
_summary.jsp?code=FRDM-KL25Z.
[5] J. Dolinay, P. Dostálek, V. Vašek,
Educational models for lessons of
microcontroller programming, in Proc. 11th
Fig. 11 Experimental verification of the PSD controller International Research/Expert conference TMT
2007, Hammamet 2007, pp. 1447-1450.
[6] P. Dostálek, V. Vašek, J. Dolinay, Design
and implementation of portable data
8 Conclusion acquisition unit in process control and
This article described program library we created
supervision applications, in Proc. 13th WSEAS
for control applications. The library makes it easier
International Conference on CIRCUITS,
and quicker to create microcontroller applications
Rhodes 2009, pp. 799-808.
for control systems. This aim is achieved by set of
[7] J. Dolinay, V. Vašek, P. Dostálek,
hardware-independent modules, which are ready-to-
Implementation and Application of a Simple
use and also by providing framework for writing the
Real-time OS for 8-bit Microcontrollers, in
control application. This framework includes also
Proc. 10th WSEAS International Conference on
hardware-dependent code and the interface between
ELECTRONICS, HARDWARE, WIRELESS and
this code and the rest of the application.
OPTICAL COMMUNICATIONS (EHAC '11),
The core of the library is formed by controller
Cambridge 2011, pp. 023-026.
modules which can be directly deployed in user
[8] J. Dolinay, P. Dostálek, V. Vašek, P. Vrba,
application. There are also template files which
Platform for teaching embedded programming,
provide the skeleton of the whole control
International journal of mathematical models
application with strict separation of hardware-
and methods in applied sciences, vol. 5, no. 6,
specific and generic code. Besides those two
pp. 1110-1117, 2011.
components, the library contains also supporting
[9] Arduino, Open-source electronics prototyping
code such as device drivers and software PWM
platform, Available: arduino.cc.