Introduction to xPC Target in the Caterpillar Mechatronics Laboratory

Introduction to xPC Target in the Caterpillar Mechatronics Laboratory
xPC Target is a powerful and user friendly tool for rapidly implementing real-time control
systems on a digital computer. The software works through MATLAB Simulink, allowing a
control system to be designed in block diagram form in Simulink and then realized in the
physical world with no need for any low-level programming or circuit assembly. The block
diagram is simply compiled to an executable and then loaded onto a dedicated computer (in this
case, the xPC TargetBox) for real-time execution.
There are three major components to any xPC Target control system: The host PC, the xPC
TargetBox, and the plant. The host PC can be any PC running MATLAB, Simulink, and the
Real-Time Workshop and xPC Target toolboxes. A block diagram representing the controller is
created on the host PC in Simulink. Special Simulink blocks can be inserted into the diagram
that represent different I/O hardware in the xPC TargetBox. When the user is ready to run the
controller, all that they must do is instruct MATLAB/Simulink to compile the diagram to an
executable and upload it to the TargetBox (this is all done with a single click of the mouse).
The xPC TargetBox contains the computer hardware that executes the controller. The TargetBox
is a small PC-compatible computer that is designed specifically to run the xPC Target real-time
operating system. This means that the entire machine is dedicated to doing nothing but running
the controller program; the controller need not compete with other concurrently running
programs for system resources which could cause it to stop responding for small (but critical)
amounts of time.
Through its I/O hardware, the TargetBox will then interact with the plant. It will read sensor
signals through its inputs and then output control signals based on the control law designed by
the user. Additionally, the TargetBox can optionally send recorded data back to the host PC
either in near real-time or after the controller execution has terminated. However, once the
executable program has been uploaded from the host PC to the TargetBox, the TargetBox is
essentially autonomous and need not even be connected to the host computer to run the
controller. However, data can optionally be sent back to the host PC in near real-time and some
block parameters can be changed while the controller is running, if the TargetBox remains
connected to the Host PC.
E-H Bench
Host PC
Host PC
CAN Data
Figure 1a and 1b: Typical xPC Target Hardware Configuration for (a) Direct Control of a
Plant and (b) Control of the E-H Workbench via the ECM
Figure 1 shows the two configurations that will be used in the Mechatronics Laboratory. In
Figure 1a, a plant is controlled directly by the xPC TargetBox through its internal DAC
hardware. The TargetBox is equipped with hardware for outputting and receiving both analog
and digital voltages, and for receiving signals from incremental encoders. Figure 1b shows the
configuration that will be used for interfacing with the electrohydraulic workbenches. Instead of
directly connecting the TargetBox to the actuators and sensors on the benches, it connects only to
the Caterpillar ECM (Electronic Control Module) via a CAN (Control Area Network)
connection. The ECM then is connected directly to the sensors and actuators on the bench; it
relays sensor data and receives control instructions from the TargetBox through the CAN
Using xPC Target to Implement a Controller
For any user that is already familiar with creating simulations in Simulink, building a controller
with xPC Target is straightforward: in place of a simulated plant (i.e. a plant transfer function),
special xPC Target blocks are used which send or read physical control or sensor signals from
the I/O hardware. A simple example follows.
xPC TargetBox
Figure 2: Example system
The system shown in Figure 2 consists of a motor with a tachometer being driven by a power
amplifier and resisted by an unknown load. The amplifier is connected to an analog output
channel in the TargetBox, and the tachometer output is connected to an analog input channel.
For simplicity, only a simple proportional controller, like the one shown in Figure 3, will be used
in this example.
+ _
Input (V)
Figure 3: Controller block diagram for example motor-tachometer system
The first step in creating a control system in xPC Target is to open a new Simulink model and
configure it to be used as a real-time control program. To do this, select “Simulation” and then
“Configuration Parameters” from the pull-down menus at the top of the new Simulink diagram.
Under the “Solver” heading (Figure 4), change the solver type from “Variable Step” to “Fixed
Step.” If this program will be interfacing with the Caterpillar ECM, set the stop time to “inf” and
the Fixed step size to 0.001s; otherwise, these parameters can be set to any values, subject to
computational limitations. More complicated diagrams require more computational time so they
must be run with larger step sizes; simpler diagrams may be run faster. However, nearly any
program that will be used in the Mechatronics Lab will run at a step size of 0.001s.
Figure 4: Solver options in the Configuration Parameters Dialog
Next, click on the Real-Time Workshop heading. Here, MATLAB will be told to compile the
Simulink diagram for use with xPC Target by choosing a Real-Time Workshop system target
file. Under the “Target Selection” heading, click the “Browse” button and then select
“xpctarget.tlc.” The dialog box should now look like Figure 5. Click “OK” when done to close
the configuration parameters dialog.
Figure 5: Real-Time Workshop Options in the Configuration Parameters Dialog
Finally, the diagram must be set to run in “External” mode (the diagram will interface with I/O
hardware). At the top of the new, blank diagram, there is a drop-down box that currently reads
“Normal;” change it to “External.” The menu bar at the top of the blank diagram should now
look like Figure 6. Now, the diagram is ready to begin building a control system.
Figure 6: Menu Bar of xPC Target Simulink Diagram
In order to turn virtual control signals from within Simulink into voltages in the real world and
actual sensor readings back into virtual signals, several special driver blocks are used which
correspond to specific I/O hardware in the xPC TargetBox. A summary of the hardware
contained in the xPC TargetBox that is used in the Caterpillar Mechatronics Lab is shown in
Table 1, along with the locations of the driver blocks. For additional details on the operation of
each driver block, consult MATLAB help by right-clicking on the block, and selecting “Help.”
Table 1: Summary of I/O Hardware in xPC TargetBox
Driver Block Location
Analog Voltage Output
Analog Voltage Input
xPC Target => D/A => Diamond => MM-32
xPC Target => A/D => Diamond => MM-32
Digital Input
xPC Target => Digital Input => Diamond =>
Digital Output
xPC Target => Digital Output => Diamond =>
Incremental Encoder
xPC Target => Incremental Encoder =>
Real-Time Devices => DM6814
Digital Input
xPC Target => Digital Input =>
Real-Time Devices => DM6814
Digital Output
xPC Target => Digital Output =>
Real-Time Devices => DM6814
For this example, the analog voltage input and output functions on the Diamond multifunction
board will be used to control the motor and read the tachometer voltage, respectively. The driver
blocks for the analog input and analog output can be added to the Simulink block diagram by
simply locating them in the library browser window and dragging them into the blank diagram.
The diagram should now look like Figure 7.
Figure 7: Example Block Diagram Containing an Analog Input and Output Driver Block
Parameters for each of the driver blocks can be adjusted by double-clicking on the block. First,
open the parameters for the analog output block. The dialog should look like Figure 8. In
general, the default values for most parameters will work for the hardware configurations used in
the Mechatronics Lab. The MATLAB help information provides detailed explanations of each
parameter. However, the “Sample Time” parameter must be set to match the fundamental step
size parameter from the Configuration Parameters dialog. Additionally, the “Base Address”
must be set to match the address listed in Table 1, or the program will fail to collect any data
from the hardware. The Base address for each board in the xPC TargetBox is set in hardware, so
it cannot be changed by the user.
Figure 8: Block Parameters for Analog Output Block
The parameters for the analog input block (Figure 9) are similar to the output block. The only
difference here is that the user must select the “Channel Configuration.” The analog I/O board in
the xPC TargetBox has 32 individual analog input lines. Selecting “1-32SI” will make allow all
32 lines to be used, but they must all share a common ground. Selecting “1-16DI” will instruct
the board to measure the voltage between two individual lines. For this case, the “1-16DI”
option will be used.
Figure 9: Block Parameters for Analog Input Block
Now, the remainder of the proportional control system can be filled in with standard Simulink
blocks, as shown in Figure 10.
Figure 10: Proportional Motor/Tachometer Control System Using xPC Target
Nearly all of the logical and mathematical blocks in Simulink will continue to function properly
when used in an xPC Target diagram, including both continuous and discrete functions.
However, most of the data collection and storage blocks will not work, including scopes, x-y
graphs, to-file, to-workspace, and others. There are two options for observing and recording
numerical data when running a real-time control system with xPC Target: The xPC Scope and
the “out” blocks.
The xPC Scope block can be found in the Simulink library browser under “xPC Target => Misc.
=> Scope (xPC).” The xPC Scope can be wired into a Simulink diagram just like the standard
scope block. Double-clicking on it will bring up the parameters dialog box, as shown in Figure
11. As before, most of the default parameters will work. However, in order to be able to view
the scope on the Host PC, the “Scope Type” must be changed to “Host.” Also, the “Number of
Samples” parameter indicates the number of samples that will be displayed at a time on the
scope. If the fundamental step size is set to 0.001 seconds, then the default 250 samples would
mean that the scope shows 0.25 seconds of data at a time. This parameter can be adjusted to best
suit the current application. In order to view the scope, type in the command xpcscope on the
MATLAB command line. If the scope is opened before execution of the controller is started,
then it is necessary to click the “Start” button at the bottom of the scope window to begin
collecting data.
Figure 11: xPC Scope Block Parameters
Numerical data can also be recorded by using an “out” block, located in the library browser at
“Simulink => Ports and Subsystems => out.” Any signals going into this block will be stored in
memory on the TargetBox and can be retrieved after execution has been stopped. Also, a time
vector is automatically recorded, so it is not necessary to explicitly output time. Multiple “out”
blocks can be used in a single diagram to record multiple signals. However the total number of
data points that can be stored in memory on the TargetBox is limited to about 33000, so saving
more signals will decrease the length of time that the signals can be recorded for. If this limit is
exceeded, then earlier data is lost in favor of the more recent data.
Data sent to out blocks can be retrieved after execution has stopped by making use of the “tg”
object. This object (similar to a class in the C/C++ programming language) contains member
functions “tg.time” (for retrieving the time vector) and “tg.out” (for retrieving data sent
through out blocks). Therefore, the following MATLAB commands will store the data in
workspace variables “time” and “data”:
time = tg.time;
data = tg.out;
Note that “tg.time” and “tg.out” are functions that retrieve data from the TargetBox, they are
not variables themselves; until the above commands are run, the data does not exist in the
workspace on the host PC. If the controller program is started again or another program is
compiled without first running the above commands, the recorded data will be lost! Figure 12
shows the example block diagram with the input and output signals being sent to both an xPC
Scope and to an out block.
If multiple out blocks are used in a single diagram, or multiple signals are combined (using a
mux block) and sent to a single out block, then the output of is a matrix with as many
columns as output signals and the same number of rows as the vector output by tg.time. The
first column will represent the first signal input into the first out block, and the second column
either the second input into the first out block or, if only one signal is sent to the first out block,
the first signal into the second out block.
Figure 12: Controller Block Diagram with xPC Scope and Out Block
The block diagram is now finished, and is ready to control the system and collect data. To run
the diagram, it must first be compiled and uploaded to the xPC TargetBox. To do this, simply
click on the “Incremental Build” button at the top of the diagram window. Depending on the
complexity of the diagram, it may take a few minutes to compile. Watch the main MATLAB
window to view the progress; when the compilation process has completed, the message
“Successful completion of xPC Target build procedure for model” will be displayed. If any
errors occur, an error message dialog will appear that will provide information about the errors
that have occurred.
When the diagram has been successfully compiled, it will be automatically uploaded to the
TargetBox and is then ready to run. First click on the “Connect to Target” button at the top of
the diagram window, and then the “Start” button to begin execution. Once the “Start” button has
been pressed, it will turn into a “Stop” button which will stop execution of the program. The
program will also stop after the amount of time specified earlier in the Configuration Parameters
dialog (unless it was set to “inf”).
Using xPC Target with the Caterpillar ECM
When using the Caterpillar Electrohydraulic Workbenches, all control signals and sensor
readings will go through the Caterpillar ECM (Electronic Control Module). The ECM is
designed specifically to interface with the control hardware and sensors used by Caterpillar on
their products as well as on the E-H workbenches. It contains its own processor, power
amplifiers, I/O hardware, and everything else needed to interact with the hardware on the
workbenches. When using the E-H Workbenches, the ECM essentially takes the place of the
TargetBox I/O boards from the previous example. However, all controller logic is still executed
on the TargetBox and control signal and sensor values are relayed back and forth in real time
through the CAN connection between the ECM and TargetBox.
In order to send and receive signals through the ECM, special Simulink blocks developed by
Caterpillar are used. On the lab computers, all of the Caterpillar blocks are contained in the
“RPAC” directory in the Simulink library browser. The “RPAC_Config” block provides some
general configuration options for the ECM. In the Mechatronics Lab, none of the parameters
contained in this block will need to be changed from their default values, however, it is always
necessary to have an “RPAC_Config” block in every block diagram that will be used with the
ECM. The compilation of the diagram will fail without it.
The “RPAC_ReadWrite_IO” block is the block that actually routes signals into and out of the
ECM. The block is highly configurable, allowing for the number and type of inputs to be
specified in a flexible manner in any diagram using only the one block. A block diagram
containing an “RPAC_Config” and “RPAC_ReadWrite_IO” block is shown in Figure 13.
Figure 13: Block Diagram with “RPAC_Config” and “RPAC_ReadWrite_IO” Blocks
There are several input and output signal types supported by the ECM. For the Caterpillar
Electrohydraulic Benches, all outputs from the ECM that will be used will be of the type
“Proportional current driver” and all inputs to the ECM will be either “Active PWM” or “Analog
voltage.” The proportional current driver essentially produces a specified electrical current on
the output channel. Active PWM inputs receive a PWM (Pulse-Width Modulation) signal and
measure the duty cycle percentage (from 0% to 100%). Analog voltage inputs read an analog
voltage between 0 and 5V.
To configure the “RPAC_ReadWrite_IO” block (often just referred to as the “ECM Block”),
double-click on it. This will produce the dialog box shown in Figure 14. The “CAN Board,”
“CAN Port,” and “ECM Module ID” parameters should all be left at the default value of 1. The
“ECM Type” must be changed to “ABL2CS V3:” Do this first, as changing this value will reset
all other parameters. The “ECM Loop Time” specifies how often (in milliseconds) the ECM
will update sensor readings and control signals. This value can generally be set as low as 5ms,
and sometimes even lower depending on the complexity of the ECM configuration. Setting it
too low may cause the ECM to not work.
Figure 14: ECM I/O Configuration Dialog
In order to add an output channel to the ECM block, click on the “<-- New” button on the left
side of the box. This will cause a channel labeled “New Channel” to be created under the
“Outputs from ECM” heading. Use the fields at the center of the dialog box to give the channel
a name (the name can be anything and does not affect any functionality; it will only be used to
label the port on the ECM block), to specify the output type (all outputs will be “Proportional
current drivers”), and to specify the channel number. After clicking on the “OK” or “Apply”
button, an input port will appear on the ECM block. Note that the “Outputs from ECM” heading
refers to physical outputs from the ECM, so therefore these channels will correspond to inputs
ports on the ECM block in the Simulink diagram.
Additional inputs and outputs can be added in a similar fashion. Table 2 lists all of the devices
that are connected to the ECM inputs and outputs, in addition to their signal types and channel
numbers. This information should allow the user to create an ECM block that allows access to
all actuators and sensors on the E-H workbench. The ECM Block should look like the one
shown in Figure 15 when this configuration has been done.
Table 2: ECM Input and Output Channels
Outputs from ECM
Inputs to ECM
Pilot Valve (forward)
Pilot Valve (reverse)
Motor Bypass Valve
Pump Restrictor
Current Driver
Current Driver
Current Driver
Current Driver
Temperature Sensor
PWM Input
Pressure Sensor #1
PWM Input
Pressure Sensor #2
PWM Input
Joystick Roller #1
PWM Input
Joystick Roller #2
Cylinder Position
Joystick X-Axis
Joystick Y-Axis
Motor Speed Sensor
PWM Input
PWM Input
PWM Input
PWM Input
Analog Input
Figure 15: ECM Block Containing Ports for All E-H Workbench Sensors and Actuators
All of the ports shown on the ECM block in Figure 15 correspond to either a sensor or an
actuator on the E-H Bench. Sending a specified current to one of the four solenoid valves is as
trivial as connecting the corresponding port on the ECM block to the output of any other block in
the diagram. Similarly, raw data from each of the nine sensors can be read through the output
ports of the ECM block. Note, however, that this data is simply a raw voltage or duty cycle
percentage from the sensor; all of these sensor readings must be calibrated in order to get useful