Open Journal of Applied Sciences, 2013, 3, 61-64
doi:10.4236/ojapps.2013.32B012 Published Online June 2013 (
Copyright © 2013 SciRes. OJAppS
Real-Time Debugging and Testing a Control System
Using Matlab
Zenghui Wang
Department of Electrical and Mining Engineering, University of South Africa, Florida, South Africa
Email: wangzengh@g
Received 2013
In this paper two methods for real-time debugging and testing of a control system are proposed. The basic instruments
used are personal computers, a Visual C++ compiler and MATLAB including the GUI Design Environment, Simulink,
real-time workshop, xPC target, and some relevant hardware. For the first method, MATLAB functions are used to
build a control system debugging and testing environment. This method is flexible and only one RS-232 serial cable is
used. Limited programming is used for the second method and ready-made blocks in MATLAB/Simulink are used to
build the simulation environment and communication channel. In both methods, the parameters of the emulation system
can be modified online, important graphs can be drawn in real time and relevant data can be easily saved for the later
analysis. As can be seen from the presented examples, both techniques are easily realized.
Keywords: Control System; Matlab; Simulink; GUI; Visual C++
1. Introduction
It is a practical necessity to debug and test a controller
before deploying it in a physical environment. A physical
simulation system can be constructed [1], or the control-
ler can be directly put into the field to test its perfor-
mance. However, these methods have excessive time and
cost implications and debugging and testing are also con-
straint by the set-up and physical environment. It is
moreover difficult to gather data on controller perfor-
mance, especially when the amount of data is large and
changes quickly. However, if the simulation environment
is software-defined, the process becomes much easier.
There is software available to control a PC’s I/O inter-
face. MATLAB, which is widely used, also has this ca-
pability. In Refs [2]-[4], MATLAB, supported by a C++
compiler and relevant MATLAB toolboxes, is used to
realize a control algorithm and communication channel
and to debug and test the controller.
In this paper, two methods using MATLAB and rele-
vant hardware are proposed to debug and test a controller.
MATLAB is used to set up a flexible software environ-
ment. A communication channel is established to facili-
tate the exchange of information between the controller
and the PC for debugging and performance assessment.
2. First Real-time Debugging and Testing
In order to debug and test a physical controller, the
hardware must first be established. The first method li-
mited hardware and a single computer. The hardware is
employed to sample, convert and transfer the date be-
tween the controller and the computer. Sometimes, the
data is not used to compute the control signals but for
later analysis. The conversion changes the data format
according to the transmission protocol. A PC with
MATLAB is used to establish the debugging and testing
environment. As most plants can be mathematically
modeled, MATLAB can easily be used to describe the
plant characteristics. The general system architecture for
real-time debugging and testing system is shown in Fig-
ure 1.
In Figure 1, the middle block is the hardware men-
tioned. It is the bridge between the controller and the
model. Control commands and the output of the control-
ler are transferred via the bridge between the control
hardware and the simulation software. Figure 2 shows
the system architecture from a data flow viewpoint
Since all data is exchanged through the bridge, the
bridge plays an important role. Considering the flexibili-
ty, generality and cost any Microprogrammable Control
Sample Data
Figure 1. System architecture (1).
Copyright © 2013 SciRes. OJAppS
Controler Model
Input Output
Figure 2. Block diagram.
Unit (MCU) which has a USART module to communi-
cate with the computer and a multi-channel Analo g -to-
Digital converter to sample and enough digital I/O port,
can act as a bridge
2) In this method, the information channel is a serial
port (RS-232, RS-422 or RS-485). Among these interface
standards, the most widely used interface to connect
computers with peripheral devices is RS-232. Many
MCUs have USART modules which can support RS-232,
making it a popular choice. As MATLAB provides func-
tionality to directly access peripheral devices, it is easy to
exchange information between the controller and the
Although this method is simple and flexible, it has a
delay-time limitation. The main reasons for this limita-
tion are A/D conversion, data transformation and the
transmission of the data between the controller and the
computer. The use of this set-up is therefore limited.
Nevertheless, the following method can overcome this
limitation: Among the factor contributing to the delay,
the RS-232 transmission time is the largest. If the MCU
is fast enough, the sampling time will not be more than
50μs and the data processing time to prepare for trans-
mission will be minimal. Although a normal desktop
computer serial port can support very high baud rates, the
error rate and cable length will influence data quality.
The sample time and data processing time can be ignored
when compared with the RS-232 transfer time. The delay
time of the simulation can almost be deduced from the
data transfer time which will determine whether this me-
thod suits a particular controller. To use this method, the
following condition
max (8 2)
AD sampleperror
>+++ + (1)
must therefore be met. Here
is the maximum delay
time the system can beartolerate; b
N is the number of
bytes to transmit between the controller and computer; v
is the RS-232 transmission baud rate; (8+2) indicates 8
bits of data and a start bit and a stop bit;
is the
number of analog-to-digital conversion channels;
is the time for analog-to-di g ital conversion;
is the
data processing time; error
T is the error checking time.
Note that all time durations refer to a single cycle.
As mentioned
The condition can be simplified to
(8 2).
> ++
For example, if two single floating point numbers are
exchanged between the controller and computer, baud
is set to 19200 and there is no error checking, then the
minimum delay time which the system can handle should
not be less than 4.167 ms.
This method depends heavily on MATLAB program-
ming and using the MATLAB GUI environment aids
debugging and testing the controller. The following sec-
tion illustrates the use of this method.
First, confirm that you have the correct template for
your paper size. This template has been tailored for out-
put on the custom paper size (21 cm * 28.5 cm).
3. Experiment One
In this experiment, a multiple variable predictive con-
troller is debugged and tested. The core of the control
system is a PIC18F458 which is a 40-Pin high perfor-
mance, enhanced FLASH microcontrollers with CAN.
The control arithmetic is realized using this MCU. As
this is a digital controller, it is not robust enough for real
time applications and the first method can be used to
debug and test this controller. As the PIC18F458 has a
USART module, the hardware of middle module in Fig-
ure 1 is not needed and its function can be realized by
the MCU. In this application, the only additional hard-
ware are a serial cable and a desktop or notebook PC.
MATLAB 6.5 was used along with the instrument con-
trol toolbox, the real-time workshop and version6 GUI.
Several models were used to debug and test the con-
troller in MATLAB. To test the robustness of the con-
troller, some noise was added to the models. In order to
analyze the control system performance, the expected
trajectory is transmitted to the controller by MATLAB.
The tracing trajectories are drawn in real time to make
the results more clear, and the control signals and the
model outputs are saved for the later analysis.
Since data is the core of the whole system, the flow of
the data must be carefully arranged according to the plant
configuration. To control the plant, the control system
must first be given the output and the set point values
followed by the control commands. Figure 3(a) shows
the control flow chart in the MCU.
After initialization, the start of control system is trig-
gered by the MATLAB through a protocol/trigger chosen
by the designer. The time after the data is exported de-
Copyright © 2013 SciRes. OJAppS
pends on the plant. The exported data includes not only
the control information but also some important parame-
ters used to analyze the control performance. When re-
ceiving the data, MATLAB uses the control variables to
control the model and plot the output curve. Considering
the whole system, the flow is shown in Figure 3( b).
Since the plant model is running on the PC, it’s output
is almost instantly available. Generally, PC should wait
for the control information. During this time, the impor-
tant figures can be drawn to observe the performance of
the controller.
To make the operation simple, the GUI Development
Environment can be used to build a graphic interface as
shown in Figure 4. From the graphic interface, the con-
troller is easily controlled and its performance is directly
Choose model
Set parameters
Control the
controller to run
Run the model
Export the output
Save the output
and plot
data come?
Save the
received data
Begin? N
Run control
Export the data
It’s time
to export?
(a) (b)
Figure 3. (a) Flow chart of the controller; (b) Flow chart in
the simulating environment.
Figure 4. Simulating graphic interface.
observed. Our simulation had 3 models. Only the result
of second model (model 2) is shown in Figure 4. In
Figure 4, the horizontal and vertical axes indicated the
time and plant outputs/expected trajectory, respectively.
4. Second Real-time Debugging and Testing
For this method, the entire architecture, shown in F igure
5, is different from the first one method.Yet the flow
diagram is almost the same as Figure 2. In MATLAB,
Simulink and other blocks are used to construct the si-
mulation environment. To model the plant, the designer
can use the Simulink models. It is easily used, and little
programming is needed. xPC Target is used to set up the
conne ctions between the computer and the controller.
xPC Target provides many hardware board driver blocks
which support analog, digital, CAN, GPIB, RS232, UDP,
counters, timers, and signal conditioning. There are three
types of embedded target applications. For simplicity, the
BootFloppy was chosen as the xPC Target Embedded
Option. Both a host PC and a target PC are therefore
needed. The host PC can be a desktop or notebook PC on
which MATLAB, Simulink, Real-time workshop, xPC
Target, xPC Target Embedded Option and visual C++
compiler must be installed. Here a desktop computer is
acted as the target PC which must has the resource of
CPU, RAM, and serial port or network adapter. The
network TCP/IP was chosen as the Host-Target connec-
tion, since it has more advantages than serial RS232.
This method is superior to trhe first method. Because a
variety of data acquisition cards can be used to transfer
data, the delay introduced can be minimised. As the
whole environment is constructed using Simulink
andblocksets, the connection of the whole system is more
like the real system. There are many ready-made models
that can be used to construct the system. Moreover, re-
mote testing and debugging can be realize if network
TCP/IP is chosen as the Host-Target connection.
The following section illustrates the use of this me-
5. Experiment Two
A single variable general predictive controller was tested
using the second method. The communication channel
between the controller and the target PC was also serial
RS232. After the hardware was set up, the following
steps were used to set up Simulink and xPC Target to
Controller I/O
Target PCHost PC
Figure 5. System Architecture (2).
Copyright © 2013 SciRes. OJAppS
Figure 6. Simulink Diagram based on xPC Target.
Figure 7. Output Figure.
debug and test the controller.
(1) Build the simulink model based on the xPC Target
as shown in Figure 6.
(2) Setup the xPC working environment and create a
target boot disk with the Graphical User Interface.
(3) Enter the Real-Time Workshop Parameters and
Boot the Target PC.
(4) Use the Real-Time Workshop and the Visual C++
compiler to create the target application.
(5) Run the Target Application
There are three ways to control the target PC to change
the target model parameters, add xPC Target scopes and
to select and log signals in the target PC. The controller
performance is shown in Figu re 7.
6. Conclusions
The two methods have their own advantages and disad-
vantages. Although the first method is fairly flexible and
can debug and test a controller using a single serial
RS232 channel, it has some delay limitations. The
second method is more flexible, but numerous data ac-
quisition cards might be needed for multiple loop con-
trollers. We have used a desktop computer to simulate
the plant and for debugging and testing a controller.
When the controller is put into the field, it runs very well
after several control parameters were small adjusted. The
met hods proposed in this paper are easily realized, can
reduce the cost and improve the efficiency of controller
deve lopmen t.
7. Acknowledgements
This work was supported by China/South Africa Re-
search Cooperation Programme (No. 78673 & CS06-
L02), South African National Research Foundation In-
centive Grant (No. 81705), SDUST Research Fund (No.
2010KYTD101) and Key scientific support program of
Qingdao City (No. 11-2-3-51-nsh) .
[1] P. Xu, C. N. Huang and Y. T. Wang, “Studying A Physi-
cal Simulation System for Char acteristics in Targets and
Background of the Remote Sensor on Spaceto r ne,” Jour-
nal of System Simulation, Vol. 15, No. 12, 2003, pp.
[2] A. Davari and D. Y. Shen, “On-line Control of A Real
System with Matlab/Simulink,Proceedings of 30th
Southeastern Symposium, 1998, pp. 7-9.
[3] F. C. TENG, “Real-time Control Using Matlab Simu-
link,” IEEE International Conference, 2000, pp.
[4] P. S. Shiakolas and D. Piyabongkarn, “On the Develop-
ment of A Real-Time Digital Control System Using
Xpc-Target and A Magnetic Levitation Device,” Pro-
ceedings of the 40th IEEE Conference on Decision and
Control, 2001, pp. 1348-1351.
[5] L. Fu and H. H. Jin, “Research and Design of An Emu-
lating Environment Based on Real-time and Testing for
Embedded Software,” Computer Engineering and Appli-
cations, Vol. 39, No. 3, 2003, pp. 117-119.