RT-CON package

advertisement
RT-CON package
Student’s Reference Guide
(brief)
Version 1.2
February 2000
MATLAB, Simulink and RTW are registered trademarks of The MathWorks, Inc.
Windows 95, Windows NT are registered trademarks of Microsoft Corporation
WATCOM is a trademark of WATCOM Intrenational Corp.
RT-CON Student’s version
2
Contents
1. INTRODUCTION ............................................................................................................................................. 3
2. DEVICE DRIVERS........................................................................................................................................... 7
2.1 GENERAL PURPOSE DRIVERS........................................................................................................................... 7
2.2 DRIVERS FOR FEEDBACK EQUIPMENT ........................................................................................................... 11
2.2.1 Digital Servo Workshop................................................................................................... ..................... 11
2.2.2 Twin Rotor MIMO System ................................................................................................... ................. 12
3. BUILDING THE PROGRAM........................................................................................................ ................ 14
4. RUNNING THE PROGRAM......................................................................................................... ................ 17
4.1 RTW INTERFACE WINDOW ........................................................................................................................... 18
4.2 EXTERNAL SIMULATION MODE ..................................................................................................................... 21
5. APPLICATION EXAMPLE: DIGITAL SERVO CONTROL ................................................................... 23
5.1 PID CONTROLLER ......................................................................................................................................... 23
5.1.1 Building the executable ........................................................................................................................ 24
5.1.2 Real-time experiments .......................................................................................................................... 25
2
RT-CON Student’s version
3
1. Introduction
This documentation describes RT-CON Professional: an open-architecture software related to
Real Time Workshop (RTW) applications in the Windows 95/NT environment. The RT-CON
Professional extends the RTW for seamless and automatic real-time implementation of Simulink
models. Unique for this solution is that a single low-costs computer can be employed as a
development and target platform. This approach reduces the cost and time of development of a new
application. The documentation can be considered as a supplement or replacement of chapter 9 of the
Simulink. Real-Time Workshop.User's Guide [1], and describes how to create and execute a real-time
program using Simulink models, device drivers and specialized user interfaces.
The next sections include:
• description of the RT-CON architecture and data flow between its modules,
• description of the device drivers and examples how to use them,
• instruction how to build the program,
• description of user interfaces allowing on-line access to process data and on-line tuning of
the parameters of the real-time application,
It should be emphasis that RT-CON was designed for real-time control and monitoring in the
Windows 95/NT environment. One of the main features of this software is a proper timing ensuring
that data read from an external process or data sent to the external hardware will never be lost.
Notice also, that distinction should be made between the real-time operation of the executable file
(referred to as RTW DLL executable) and on-line data flow between RTK (Real-Time Kernel) and
MATLAB/Simulink. Only the first mechanism guarantees a proper real-time information exchange.
It is assumed that the user of this software is familiar with MATLAB, Simulink and RTW
Toolbox from MathWorks. The following abbreviations will be used in the next sections:
RTW
- Real-Time Workshop
RT-CON
- Real-Time Connection
RT-DAC
- Real-Time Data Acquisition Card
DLL
- Dynamically Linked Library
GUI
- Graphical User Interface
IPC
- Inter-Process Communication
3
RT-CON Student’s version
4
Model
of the process
Project
Model
of the controller
Model
of the controller
Input
Driver
Output
Driver
Adding I/O
drivers
Building the
program
Process
*HQHUDO 6LPXOLQN PRQLWRU IRU 57:
Select Simulation/Start
to start monitoring
6KRZ SDUDPHWHUV
5HI3RV?9DOXH
6KRZ RXWSXWV
6FRSH
,QYHUWHG 3HQGXOXP0HPRU\?;
&RQYHUW WR 3KLVLFDO 8QLWV6XP?3HQG3RV
$OID 1RUPDOL]DWLRQ$OID1RUP?3HQG3RV2XW
'HPX[
,QYHUWHG 3HQGXOXP*DLQ?&DUW9HO
6FRSH81&211(&7('
5HDO 7LPH 7DVN
6FRSH81&211(&7('
Real-time
controller
UWZBKLVW
6FRSH81&211(&7('
7R :RUNVSDFH
&ORFN
6FRSH81&211(&7('
6FRSH81&211(&7('
Simulink
Fig.1.1 General concept of RTW/RT-CON application for rapid prototyping of controllers
Figure 1.1 shows the general concept that comes within the scope of RTW and RT-CON. We
wish to use MATLAB and Simulink to design, test and analyse a model of a control or data system.
Once the project is complete we can add an appropriate input/output device drivers and to generate an
executable code directly from the Simulink block diagram. When the real-time program is running we
can communicate with MATLAB and Simulink to capture and analyze data or to change parameters
of the currently running real-time program.
RT-CON Professional is suitable for control of variety of systems if these systems meet the
specified hardware and software requirements (see section 2). Figure 1.2 shows an example
configuration used for rapid prototyping of the control system for the inverted pendulum [2]. The
purpose of the inverted pendulum control algorithm is to apply a sequence of forces of constrained
magnitude to the cart such that the pendulum starts to swing with an increasing amplitude and the
cart does not override the ends of the rail. The pendulum is swung up to achieve a vicinity of its
4
RT-CON Student’s version
5
upright position. Once this has been achieved, the controller is maintaining the pendulum in vertical
position and is bringing the cart back to the centre of the rail. The pendulum-cart control system
consists of:
• PC computer equipped with I/O board,
• pendulum and cart,
• encoder interface and DC motor driver,
• real-time control software automatically generated from Simulink model in the Windows 95/NT
environment.
Notice, that all software tools and final application are installed on a single PC. Hence, an
integrated, low-cost platform for development of real-time control systems is created.
cart
& angle sensor
DC motor
& position sensor
measurement
control
power interface
I/O board
Running Windows 95/NT,
MATLAB, Simulink,
RTW and RT-CON,
C-compiler
Rys.1.2 The example of a typical system controlled by the RT-CON software package.
Figure 1.3 shows the hardware and software configuration used for running a real-time program.
The executable code of the project is embedded in the RTW DLL executable file. The data required
by the control algorithm or data acquisition algorithm are provided from external peripherals by the
data acquisition board (I/O Board). At each timer interrupt the RTW DLL library performs all the
real-time procedures associated with a single sampling period. Typically, in this step it reads inputs
from I/O board, calculates the control signals and writes them to the outputs of I/O board. An
important procedure included is rtw_call. It converts the format of data which are exchanged between
the RTW DLL library and the MATLAB environment. The Inter-Process Communication Protocol
(IPC) is used to manage transfer of data between these two processes.
5
RT-CON Student’s version
6
PROCESS
I/O Board
COMPUTER
Win NT Dev.Driv.
Device Driver(s)
Timer Interrupts
Ÿ
RTW DLL
Executable
Simulink
external mode
Interface
RTW DLL Interface
( rtw_call.dll )
RT-CON Toolbox
MATLAB 5 / SIMULINK
Windows 95 / NT
- Inter-Process Communication Protocol
Fig.1.3 General structure of the control system for MATLAB 5 and Windows NT.
The operation of the RTW DLL executable can be monitored from MATLAB using RT-CON
Toolbox. The toolbox contains a collection of M-files and MDL-files which extend the MATLAB
environment in order to tune parameters of the RTW DLL library and to collect data from real-time
program. Another possibility of monitoring of the operation of the RTW DLL is an external mode
operation of the Simulink. This data transfer channel is pointed out in Fig.1.3.
The block Win NT Dev. Driv. is marked as optional in the Fig.1.3. This special driver is required
only by Windows NT operating system. The reason is that Windows NT operating system by default
protectes the microprocessor I/O space. The Win NT Dev. Driv. block enables access to the RTW
DLL executable.
6
RT-CON Student’s version
7
2. Device drivers
The generated RTW DLL library communicates with the I/O boards using C-code files referred as
device drivers. Driver software is the part of software that directly operates on the registers of I/O
boards, managing its operation and its integration with the computer resources. Several device drivers
were developed to support different I/O hardware configurations. They are grouped into two libraries:
• general-purpose device drivers,
• device drivers dedicated to the Feedback Ltd laboratory models (e.g. inverted pendulum, twin
rotor MIMO system, helicopter and digital servo systems).
Any driver block from the library can be added to the Simulink model. The program building
procedure automatically includes the blocks into the generated RTW DLL library.
The library of drivers is called from the MATLAB Command Window. To display this library type:
» rtcon at the MALAB prompt.
The library of drivers opens (Fig.6.1). There are three types of general purpose drivers. The first
group is dedicated to the RT-DAC board. The second group is dedicated to the PCL 812PG
Advantech board. Additionally, the IBM speaker driver is available for adding of a sound output to
the models, and the RS-232 driver for serial transmission of data.
DRIVERS
'ULYHUV IRU )HHGEDFN (TXLSPHQW
General Purpose
57'$&
,QYH UWHG 3H QGXOXP
'6:
'ULYH UV
'ULYH UV
'ULYH UV
,%0 3& 6SHDN HU
'ULYH U
3&/3*
'ULYH UV
7506
(6
'ULYH UV
'ULYH UV
Executables
Examples
'6:
,QYH UWHG 3H QGXOXP
7506
57&21
&RQWUROOHUV
&RQWUROOHUV
&RQWUROOHUV
'// OLEUDULH V
(6
,%0 3&
&RQWUROOHUV
6SHDN HU
Fig.6.1 Drivers library
The dedicated drivers are available for a variety of Feedback laboratory models. They are design
for use with a specified I/O configuration appropriate for the external hardware. For each model an
example of simple controller is presented. These examples demonstrate how to use the dedicated
drivers for a different real-time controlled peripheries.
2.1 General purpose drivers
If you click on RT-DAC drivers block the window containing drivers for RT-DAC board opens
(see Fig.6.2). The library consists of:
• analog input and output drivers,
• digital input and output drivers,
7
RT-CON Student’s version
8
• incremental encoder driver.
57'$&
57'$&
$QDORJ 2XWSXWV
57'$&
$QDORJ ,QSXWV
,QFUHPHQWDO (QFRGHUV
57'$&
57'$&
57'$&
'LJLWDO 2XWSXWV
'LJLWDO ,QSXWV
5HVHW (QFRGHUV
Fig.6.2 RT-DAC drivers
Each driver block has a dialogue box that can be used to configure its particular options. Double
clicking on the icon displays the dialog box. If you click on RT-DAC Analog Outputs icon (Fig. 6.3),
you have access to the following fields:
• Base Address- it is the beginning of the I/O address space assigned to the board. The value
specified in this field must match to the value fixed on the board. Refer to the manual of the
board for details.
• Sample Time- it determines how frequent the driver code is executed. Remember that the
Sample Time determined for any I/O blocks and the Step Size parameter determined in
the Simulation Parameters dialog box (see section 7) must have a common divider.
Fig.6.3 RT-DAC Analog Outputs mask
Figure 6.4 shows the interior of RT-DAC Analog Outputs block. Notice, that the block has only
one input. As the RT-DAC board has two analog output channels the output of your Simulink model
must be vectorised with a Mux block (see example in Fig. 6.19).
UWGDFBGD
LQB
'$&
Fig.6.4 RT-DAC Analog Outputs interior
8
RT-CON Student’s version
9
The dialog box for RT-DAC Analog Inputs block contains of four fields (Fig.6.5):
• Base Address and Sample Time fields have the same meaning as described for
RT-DAC Analog Outputs block,
• Select channel - selects channels to be scanned during a single sampling. The channel
number can be an integer ranging from 0 to 15. If the number of channel is duplicated it
means that for this channel the A/D conversion is performed more than once during
a single sampling period,
• Select gain - specifies the gain applied to the analog inputs. To each channel corresponds
the input signal gain. Refer the RT-DAC board documentation for the available gains.
Fig.6.5 RT-DAC Analog Inputs mask
Figure 6.6 shows the interior of RT-DAC Analog Inputs block. Notice, that the block has only
one output. As the RT-DAC board has sixteen analog input channels, so the output from the driver is
a vector. The width of the vector relates to the number of channels selected in the Select channel field.
UWGDFBDG
2XWSRUW
$QDORJ ,QSXWV
Fig.6.6 RT-DAC Analog Inputs interior
The dialog box for RT-DAC Digital Outputs block includes similar parameters as the RT-DAC
Analog Outputs block. The input to the block is a scalar value, which is converted to 16-bit integer
number. This integer value is use set or reset the digital outputs.
Similar rule is applied for the RT-DAC Digital Inputs block. The output from the block is 16-bit
integer number corresponding to the 16 digital inputs of the board.
The incremental encoder blocks can be used for communication with the encoder-type position
and angle sensors. The Incremental Encoders block returns a vector of two numbers, which are
obtained from the pair of HCT-2016 incremental encoders. The parameters of the block are the same
as described for RT-DAC Digital Outputs block. The Reset Encoders block is used to set an initial
9
RT-CON Student’s version
10
position of the HCT-2016 incremental encoders. The encoders are reset when the input to the block is
equal to one.
The library of drivers for PCL-812PG board is shown in Fig. 6.7. The dialog box for each block
can be displayed by double clicking the appropriate icon.
3&/3*
$QDORJ 2XWSXWV
3&/3*
3&/3*
$QDORJ ,QSXWV
,QFUHPHQWDO (QFRGHUV
3&/3*
3&/3*
3&/3*
'LJLWDO 2XWSXWV
'LJLWDO ,QSXWV
5HVHW (QFRGHUV
Fig.6.7 PCL-812PG Drivers
There are not significant differences between the PCL-812PG and RT-DAC drivers. The
meaning of the fields in configuration dialog boxes is similar, but the Base Address must be
different if two or more boards are installed on a single PC.
Double clicking on the IBM-PC Speaker Driver icon displays the dialog box (Fig.6.8). The driver
controls the PC speaker to generate a sound of frequency corresponding to the value of the input
signal. You have access to four fields:
• Sound type - selects the type of sound. In the case of the impulse mode the driver does not
require any input signal,
• Continuous - specifies four parameters of the continuous sound. They are: minimum and
maximum value of the driver input signal and upper and lower limits of its frequency,
• Impulse - specifies how often the sound of the given frequency is generated. For example,
the parameters in Fig.6.8 specify that the sound of frequency 1000Hz is generated every 50
sampling periods and lasts only one sampling period.
Fig.6.8 IBM PC Speaker Driver mask
10
RT-CON Student’s version
11
The C-source codes of all drivers from Fig.6.2 and 6.7 are available in the following files.
Device drivers for RT-DAC multi I/O board:
RTDAC_AD.C analog input
RTDAC_DA.C analog output
RTDAC_DI.C
digital input
RTDAC_DO.C digital output
RTDAC_IE.C
data from incremental encoders,
RTDAC_RE.C reset incremental encoders.
Device drivers for PCL-812 PG multi I/O board:
PCL_AD.C
analog input
PCL_DA.C
analog output
PCL_DI.C
digital input
PCL_DO.C
digital output
PCL_IE.C
data from incremental encoders
PCL_RE.C
reset incremental encoders.
Device drivers for MIC 926 board (see interior of the blocks from Fig.6.14):
DSW_INP.C
input driver,
DSW_OUT.C
output driver.
2.2 Drivers for Feedback equipment
If you use the PCL-812PG Advantech’s board or RT-DAC board in your data acquisition or control
applications then you can combine and use the drivers according to your needs. In this section we
describe how to build control applications for Feedback computer controlled equipment. The drivers
developed for Feedback models consist of the basic drivers taken from the driver’s library and typical
Simulink blocks, finally creating the models ready to handle the inputs and outputs from the
external hardware in real time.
2.2.1 Digital Servo Workshop
The driver for the digital servomechanism is given in Fig. 6.14. The interiors of the blocks are
shown in Fig. 6.15 and 6.16. There are four signals generated by the Sensors driver: two positions:
analog measured by the potentiometer and digital measured by the Gray code disk, one analog
velocity measured by the tachogenerator and one desired position measured by the potentiometer.
There is one control signal for DC motor generated by the Actuator driver. The I/O board for the
DSW is equipped with 8-bit A/D and D/A converters. The bias equal to 127 sets the middle of D/A
converter range.
3RVLWLRQ $' 3RVLWLRQ *UD\ GLVN &RQWURO
'6:
'6:
9HORF LW\ $' 0 ,&
$FWXDWRU
0 ,&
'HVLUHG Y DOXH $' 6HQVRUV
Fig. 6.14 Digital Servo Workshop drivers
11
RT-CON Student’s version
12
Fig. 6.15 Digital Servo Workshop Sensors driver
interior
Fig. 6.16 Digital Servo Workshop Actuator
driver interior
2.2.2 Twin Rotor MIMO System
The TRMS drivers are shown in Fig. 6.17. The interiors of the blocks are shown in Fig. 6.18 and
6.19. There are six measured signals (four position measurements – two digital and two analog and
two „observed” velocities).
5(6(7
(1&2'(56
3&/
& RQW UROB9
7 50 6
7 50 6
& RQW UROB+
3&/
$FWXDWRUV
3&/
5RWB9BYHO
5RWB+BYHO
9HUWBYHO
9HUWBSRV
+RUL]BYHO
+RUL]BSRV
6HQVRUV
5(6(7
(1&2'(56
57'$&
&RQWUROB9
7 50 6
7 50 6
&RQWUROB+
57 '$&
B$FWXDWRUVB
57 '$&
5RWB9BYHO
5RWB+BYHO
9HUWBYHO
9HUWBSRV
+RUL]BYHO
+RUL]BSRV
B6HQVRUVB
Fig. 6.17 Twin Rotor MIMO System drivers
The driver converts the signals to physical units expressed in [rad] and [rad/s]. It is designed
in similar way as in the case of the inverted pendulum driver. The same remark is valid for the
Actuators driver. The only difference is that the second control signal passed to the driver is not equal
to zero. The Reset Encoders driver is added to reset the encoders.
12
RT-CON Student’s version
13
Fig. 6.18 Interior of the Sensors block for Twin Rotor MIMO System driver
Fig. 6.19 Interior of the Actuators block for Twin MIMO Rotor System driver
13
RT-CON Student’s version
14
3. Building the program
Once a system has been designed with Simulink the code for real-time control can be generated,
compiled, linked and downloaded into the processor.
By use of the Target Language Compiler the TLC code is generated (see description of the
Simulink Target Language [5]). The makefile is used to build and to download object files to the
target hardware automatically.
First, you must specify the simulation parameters of your Simulink model in the Simulation
parameters dialog box. The RTW page appears when you select the RTW tab (Fig.7.1). The RTW
page allows you to set the real-time build options and then to start the RTW DLL executable file
building process.
Fig.7.1 RTW page of the Simulation parameters dialog box
The rtw_dll.tlc is the system target file. It manages the code generation process. The rtw_watc.tmf
is the template makefile prepared for C code generation using the WATCOM compiler.
In the window there are three options which must be properly marked, as presented in Fig.7.1:
• Inline parameters - not to be used when building a real-time program,
• Retain .rtw file - if marked, an auxiliary information is stored in the file (with .rtw
extension),
• Generate code only - if marked, a code is generated but compilation is not performed.
The Solver page appears when you select the Solver tab. (Fig.7.2). The Solver page allows you to
set the simulation parameters. Several parameters and options are available in the window.
14
RT-CON Student’s version
15
Fig.7.2 Solver page of the Simulation parameters dialog box
The Fixed-step solver is obligatory for real-time applications. The Fixed-step size is a
sampling period (in the example is set to 0.01 [s]). If you use any block from the discrete Simulink
library or a block from the driver library remember that different sampling periods must have
a common divider.
The Start time must be set to 0. One can select: ode5, ode4, ode3, ode2, ode1 or discrete
integration method. In the example ode4, the fourth-order Runge-Kutta formula, was chosen.
The following window opens when you select the RTW External tab (Fig.7.3).
Fig.7.3 RTW External page of the Simulation parameters dialog box
On this page the ext_rtw mex-file name for external interface is defined. This file manages
communication between the Simulink model and the running RTW DLL program in the External
simulation mode. If MEX-file arguments are specified as ‘-debug’ then any change of parameter
values is acknowledged in the MATLAB Command Window. These changes can be introduced by
use of MATLAB, which communicates with the DLL library.
15
RT-CON Student’s version
16
If all parameters are set properly you can start the building of the DLL executable process. For
this purpose press the Build push button on the RTW page (Fig.7.1).
A successful compilation and linking process should be finished with the following
announcements:
### Created executable: MODEL.RTC
### Successful completion of RTW build procedure for model: MODEL
If not, error massages will be displayed in the MATLAB Command Window.
16
RT-CON Student’s version
17
4. Running the Program
To load the generated real-time program the following command must be executed in MATLAB
Command Window:
rtw_load( 'model_name' );
where the model_name is the name of your Simulink model. After that command the following
window appears (Fig.8.1).
Fig.8.1 Loading a real-time program (PC-speaker example)
When you press the OK push button the real-time application starts.
To terminate the running real-time program execute the following command:
rtw_unload;
which terminates the experiment and displays the message box presented in Fig.8.2.
Fig.8.2 Terminating an experiment
The data flow during the program execution is shown in Fig.8.3. The RT-CON Toolbox
contains ready-to-use Graphical User Interface (GUI) which allows the following on-line operations:
•
rtw.m -the interface file: using this file you can define the outputs and parameters to be displayed
or tunned,
•
rtw_plot.m and rtw_tune.m files: GUI for plotting data and tuning the parameters,
•
rtw_gm - Simulink general-purpose monitor.
Additionally, all parameters of the generated real-time program can be changed using the
external simulation mode. This mode must be marked in the Simulink model window which was
17
RT-CON Student’s version
18
used to generate your real-time program. In this case, the external simulation mode available in the
Simulation menu option allows to exchange parameter data between the Simulink model and the realtime application.
4.1 RTW Interface window
In the RTW Interface window the user must select outputs and parameters to be visualised or
available for tuning during the real-time session. The three push buttons are available in this window:
Plot – it starts the rtw_plot application which displays time diagrams of the outputs and
parameters selected in the RTW Interface window (see Fig.8.5),
Tune – displays edit fields and sliders which can be used for modifications of the parameters
selected in the RTW Interface window (see Fig.8.6 for an example),
Simulink monitor – displays the Simulink model which must be executed in the normal
simulation mode and is able to display the selected outputs and parameters using a typical
Simulink display devices (see Fig.8.7).
Fig.8.4 RTW Interface window.
18
RT-CON Student’s version
19
Fig.8.5 Example of the rtw_plot window.
Fig.8.6 Example of the rtw_tune window.
19
RT-CON Student’s version
20
Fig.8.7 Simulink monitor.
The menu option Plot parameters displays the window presented in Fig.8.8 which selects the mode of
data acquisition procedures.
Fig.8.8 Plot parameters window
The fields from Fig.8.8 have the following meaning:
20
RT-CON Student’s version
21
Plot length - defines the length of the time axis in the rtw_plot window, scaled in seconds.
This parameter is used only in the Continuous mode,
Buffer length - defines the number of samples in the data acquisition buffer. It is used in the
Single and Triggered data acquisition modes,
Trigger - defines the parameters of the software trigger. The trigger signal is selected from the
list-box located on the top of the Trigger frame and is used to start data acquisition
process. The trigger parameters are as follows:
Mode - selects the data acquisition mode. In the Continuous mode the trigger signal is
neglected and the data acquisition process must be started and terminated by the
user. In the Single mode the Start option from the rtw_plot window is used to
perform a single data acquisition operation. In the Triggered mode the single data
acquisition is started when the triggering condition described bellow are fulfilled,
Trigger event - determines the relations between the current value of the trigger signal
and the value defined in the Signal level field which generates the trigger event. The
occurrence of the trigger event starts data acquisition. For example, if the Signal
level is equal to 0 and the Rising box is marked the trigger event will be generated
only if the trigger signal crosses the zero level from negative to positive values,
Signal level - determines the value of the trigger signal which generates the trigger
event,
Trigger offset - defines how many samples will be collected in the data acquisition
buffer before the trigger event will be generated. For example, let us assume that the
length of the data acquisition buffer is 1000 samples. In such a case if the Trigger
offset field is 200 it will result in storing only of 800 samples after the occurrence of
the trigger event. The proceeding 200 samples will be stored after the data
acquisition is started but before the trigger event is detected.
4.2 External simulation mode
The Simulink model which was used to generate the real-time executable can be simulated in the
external simulation mode. This mode enables to change all parameters directly from the Simulink
model. In this case the tune window (e.g. Fig.8.6) is not necessary. Before starting the simulation in
the external mode the RTW External tab in the Tools/RTW options must be set, as presented in Fig.8.9.
Fig.8.9 Tools/RTW Options /RTW External tab
Next, the Simulation/External option must be marked in the Simulation menu option for the
Simulink model. To start the external mode simulation execute the Simulation/Start menu option.
21
RT-CON Student’s version
22
In the external simulation mode the parameters may be changed immediately from the Simulink
blocks. Any change of parameters in any of the Simulink block of your model is transferred
immediately to the running real-time program.
22
RT-CON Student’s version
23
5. Application example: Digital servo control
The example contains the Simulink controllers developed for Feedback Ltd digital servo model and
uses input-output device drivers specialised for this equipment. The application can be opened by
double clicking on the corresponding block in the rtcon window (see Fig.6.1).
5.1 PID controller
In Fig.10.1 the Simulink model of the PID control system for the digital servomechanism 38-100
is presented. Input and output drivers for the MIC926 data acquisition board are used as connections
with the DSW hardware. Notice that this model does not differ from typical Simulink models.
Device drivers are applied in the same way as other blocks from the Simulink library. The only
difference is that the model is used by Real Time Workshop/RT-CON Professional to create the RTW
DLL executable library.
The experiment deals with the problem of tracking of a desired trajectory, which can be given by:
• RefPos and Bias blocks if IntGen is equal to '1' and ExtGen is equal to '0' or,
• external generator if ExtGen is equal to '1' and IntGen is equal to '0'.
5HI3RV
,QW*HQ
([W*HQ
%L DV
5HI6XP
%L DV6XP
3RVLW LRQ $' 3RVLW LRQ *UD\ GLVN 3,'
& RQWURO
'6:
'6:
9HORFLW\ $' VY
0 ,&
0 ,&
' HVLUHG Y DOXH $' 3,'
$FWXDWRU
6HQVRUV
SRV
7KH IROORZ LQJ SDUDPHWHUV PXVW EH GHILQHG
7 VDPSOH SHULRG
%DVH$GGU EDVH DGGUHVV RI WKH0,& FDUG
67$57
6723
Fig.10.1. Simulink model of the PID control system (rtw_dsw.mdl file)
The external generator is available on the 38-100 board. If the external generator is selected a
suitable wiring on the board is required.
The output of the Sensors device driver is a vector which contains information about position of the
DC motor (measurements from the A/D converter and the Gray code disk), angular velocity of the DC
motor and reference position from the external generator (Fig.10.1).
The PID controller contains integrator with upper and lower saturation limits and output saturation
block (see Fig.10.2 ).
23
RT-CON Student’s version
24
Fig.10.2 PID controller details
Once the model of the closed-loop control system has been designed and proper input/output drivers
were connected, the code for real-time controller can be build.
5.1.1 Building the executable
To use the RTW tools you must perform the following steps:
In the MATLAB Command Window define the base address of the MIC-926 board e.g.:
BaseAddr = 768
These parameters are necessary to compile RTW model.
Next, specify the simulation parameters in the Tools/RTW Options/Solver dialog box (Fig.10.3):
• Type:
- Fixed-step Ode4(Runge-Kutta),
• Fixed step size: - 0.05,
24
RT-CON Student’s version
25
Fig.10.3 Solver settings
Set the IntGen gain block to 1 and the ExtGen gain block to 0 (Fig.10.1). It means that the source of
the reference position is the RefPos generator block from the Simulink library. Open the RefPos block
and set the parameters as given in Fig.10.4.
Fig.10.4 Settings in the RefPos block
Then, start the Tools/RTW Build option in the Simulink model window. Successful compilation and
linking process should be finished with the following messages:
### Created executable: rtw_dsw_pid.RTC
### Successful completion of RTW build procedure for model: rtw_dsw_pid
In the case of errors the appropriate massages will be displayed in the MATLAB Command Window.
5.1.2 Real-time experiments
To start the experiment double click the START block (Fig.10.1). You will see the following
information window (Fig.10.5):
25
RT-CON Student’s version
26
Fig.10.5 Starting the RTW executable information window
Click the OK button to start the experiment. In this moment DC motor begins to move. To monitor the
system outputs type the rtw command at the MATLAB prompt. The window presented in Fig.10.6
opens.
Fig.10.6 RTW Interface window
Find the signals, which you wish to observe and mark the appropriate check boxes. In the example the
OUTPUT/DSW Input Drive/MIC 926\(3), RefSum and Pos signals were selected. Next, to plot the
marked signals click the Plot button. The window presented in Fig.10.7 opens. Click the Start button
and after a short time the Autoscale button. The results are shown in Fig.10.7.
26
RT-CON Student’s version
27
Fig.10.7 Plot window
Next, return to RTW Interface window and double click the Simulink monitor button. The window
presented in Fig.10.8 opens. Notice, that three scopes are distinguished by the colours and the block
titles. These scopes displays the signals selected in the RTW Interface window.
Fig.10.8 Simulink monitor window
Open the Real Time Task block (Fig.10.9) and set the Decimation ratio to 1. Now each sample will be
transferred to the output of the Real Time Task block. If the computer is not fast enough the
Decimation ratio must be increased to plot data on-line.
27
RT-CON Student’s version
28
Fig.10.9 Decimation ratio setting
Connect the floating Scope to the RefSum and Pos signals and execute the Simulation/Start menu
option. The results of the experiment are presented in Fig.10.10.
Fig.10.10 Results of experiment in the Simulink monitor window
You can use the rtw_hist workspace variable (Fig.10.8) to see details of the experiment. For
example to display the position, desired position and the velocity of the servo type at the MATLAB
prompt:
a=1; b=560;
% set lower and upper data limits
position=rtw_hist(a:b,1);
t=rtw_hist(a:b,11);
despos=rtw_hist(a:b,2);
speed=rtw_hist(a:b,3);
plot(t,position,t,speed,t,despos);
AXIS([2 20 80 200]);grid;
title('Position, desired position and speed');
xlabel('time [s]');
To zoom the plot type:
28
RT-CON Student’s version
29
position=position(140:280);
t=t(140:280);
despos=despos(140:280);
speed=speed(140:280);
plot(t,position,t,speed,t,despos); axis([12 18 0 255]);grid;
title('Position, desired position and speed;); xlabel('time [s]');
The single step response is presented in Fig.10.12.
Fig.10.11 Results of the experiment using data from the rtw_hist workspace variable
Fig.10.12 Single step response
To terminate the real-time program execute the following command:
rtw_unload
29
Download