Microcontroller Software Library For Process Control: January 2015

Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/283857117

Microcontroller software library for process control

Article · January 2015

CITATIONS READS

0 409

3 authors:

Jan Dolinay Petr Dostálek


Tomas Bata University in Zlín Tomas Bata University in Zlín
43 PUBLICATIONS 127 CITATIONS 55 PUBLICATIONS 200 CITATIONS

SEE PROFILE SEE PROFILE

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.

The user has requested enhancement of the downloaded file.


WSEAS TRANSACTIONS on SYSTEMS and CONTROL Jan Dolinay, Petr Dostálek, Vladimír Vašek

Microcontroller Software Library for Process Control


JAN DOLINAY, PETR DOSTÁLEK, VLADIMÍR VAŠEK
Department of Automation and Control Engineering
Tomas Bata University in Zlin
nám. T. G. Masaryka 5555, 76001 Zlín
CZECH REPUBLIC
[email protected]

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.

Key-Words: - discrete controller, hcs08, kinetis, microcontroller, program library, pwm

1 Introduction applications, but it may be in part also caused by the


Nowadays, microcontrollers (MCU) can be lack of effort to write portable code. This can hardly
encountered in all areas of our life. MCU be surprising given the tight deadlines and pressure
applications range from simple devices, such as for high performance from the employers and the
toys, to complex embedded systems found in human nature of choosing the easier way to achieve
modern cars or aircrafts. Many of the MCU the goal. For the programmer it is easier to write
applications require implementing some control hardware-specific code for single MCU than write
algorithm in the program. These applications could more generic code which is ready for future porting
benefit from a library, which would contain such to another MCU.
control algorithms, and possibly also some related Whatever the reasons are, the result is that the cost
useful code, in a form ready-to-use, without the of embedded software is high and time-to-market is
need to write the algorithm from scratch and then long. Or, in some cases, the quality of the software
debug it. is poor.
Generally speaking, one of biggest challenges in One possible solution is in the usage of program
software development is the reuse of existing code. libraries, which provide code usable in different
Probably everyone will agree that it can save applications. In the area of embedded programming,
considerable time and money, but in reality the however, the hardware may be very diverse in
situation is far from ideal and huge amount of code different applications - which use different MCUs.
is rewritten over and over again. The higher-level Due to great variety in the hardware, it is in general
programming languages used for PC programming not easy to create program libraries, which would be
encourage code reuse in some ways, but most of the usable on several types of MCUs. Nevertheless,
MCU applications are written in C language and the there are areas of problems which are virtually
code reuse is more in the hands of the programmer. hardware-independent. For such areas it is possible
Writing reusable code requires carefully designed to create libraries which will work on wide range of
and implemented code modules which are not devices. One such area is system control. The
focused just on fulfilling the task on given MCU, control algorithm may be relatively complex, which
but also take into account the possible reuse on a makes it hard and time-consuming to implement and
different MCU. This is naturally harder than writing debug, but on the other hand, once the code is
the code just for the application currently in focus written and debugged in a portable programming
and it probably explains why it seems that in MCU language, such as C, it can be used on many devices
programming big part of the code is developed from without change.
scratch for every application. Certainly, there are In this paper we describe such program library for
some good reasons for this, such as that the control applications. The main part of the library
hardware differs much across the MCU consists of discrete controllers, but it also proposes
the framework for the whole control application

E-ISSN: 2224-2856 105 Volume 10, 2015


WSEAS TRANSACTIONS on SYSTEMS and CONTROL Jan Dolinay, Petr Dostálek, Vladimír Vašek

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.

E-ISSN: 2224-2856 106 Volume 10, 2015


WSEAS TRANSACTIONS on SYSTEMS and CONTROL Jan Dolinay, Petr Dostálek, Vladimír Vašek

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

These functions are described in detail in the


3 Usage of the library following chapter. For explaining the concepts here,
This section describes how the library is used in the their names should be sufficiently self-explanatory.
client program. This description should also provide As shown in the figure 1, the basic user code is
better insight into the overall design of the library. contained within the main.c file. From this file the
The following picture shows the main components ucp_app interface functions are called. These
of the application which uses the library in the ucp_app functions then contain most of the
recommended way. hardware-independent functionality of the
application, such as initializing the controllers and
calling the controller “on_step” function to calculate
new value of the actuating signal in each sampling
main.c User code period.
Example of a simple main.c file can be seen in
ucp_app_init
figure 2.
ucp_app.c ucp_app_on_step

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.

E-ISSN: 2224-2856 107 Volume 10, 2015


WSEAS TRANSACTIONS on SYSTEMS and CONTROL Jan Dolinay, Petr Dostálek, Vladimír Vašek

InitTop from its software driver. Obviously, the user


will most likely not need this particular code, but it
illustrates how the various peripherals used by the
application can be initialized using their own
software drivers. Another option is to perform the
initialization by directly accessing the MCU’s
peripheral registers from the ucphal_init function.

Fig. 3 The ucp_app_init function in the template file

Fig. 5 The ucphall_init function in the HAL template

Figure 6 shows the ucphal_read_input function


which is called in each step of the control process
from the ucp_app_on_sample and should obtain the
current value of the output of the controlled plant. In
the template this is again illustrated by reading the
temperature from the model of the heating plant
using its driver function GetTemp. In general, the
Fig. 4 The ucp_app_on_sample function user will write here the code to access the peripheral
used to measure the signal, for example, analog-to-
For accessing the MCU hardware the library digital converter.
recommends using its hardware-abstraction layer
(HAL) which is represented by the ucp_hal interface
in the library. As can be seen in the listings in
figures 3 and 4, the ucp_app functions do not access
the hardware directly, but use the ucp_hal functions,
for example, ucphal_init or ucphal_read_input.
Besides the calls to the HAL, there is some example
code in the comments which shows how to initialize
the controller and how to obtain the new controller
output in each sampling period.
The following two figures show the functions
contained in the HAL template file. As in case of
the ucp_app functions, also the HAL functions
contain some example code in comments.
Figure 5 shows the ucphal_init function, which is
called when the application starts and should Fig. 6 The ucphall_read_input function in the HAL
perform all the initialization for the hardware used template
by the application. The example code provided in
the template initializes the software PWM It should be noted, that the library supports multiple
generator, which is part of the library. It also controlled variables and controllers in one
initializes the model of the heating plant, which was application. In the code the term channel is used in
used for testing the library, as mentioned later in this the meaning of one pair of input and output signal or
article. The model is initialized by calling function one controlled property (one instance of a

E-ISSN: 2224-2856 108 Volume 10, 2015


WSEAS TRANSACTIONS on SYSTEMS and CONTROL Jan Dolinay, Petr Dostálek, Vladimír Vašek

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

E-ISSN: 2224-2856 109 Volume 10, 2015


WSEAS TRANSACTIONS on SYSTEMS and CONTROL Jan Dolinay, Petr Dostálek, Vladimír Vašek

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

E-ISSN: 2224-2856 110 Volume 10, 2015


WSEAS TRANSACTIONS on SYSTEMS and CONTROL Jan Dolinay, Petr Dostálek, Vladimír Vašek

“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.

Fig. 10 FRDM-KL25Z board used for tests [4]

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

E-ISSN: 2224-2856 111 Volume 10, 2015


WSEAS TRANSACTIONS on SYSTEMS and CONTROL Jan Dolinay, Petr Dostálek, Vladimír Vašek

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.

E-ISSN: 2224-2856 112 Volume 10, 2015

View publication stats

You might also like