Simulating with Hardware in the Loop

About Dedicated Hardware Targets for Stewart Platform Simulation

A common step after generating and compiling code from a model is to download the compiled executable to a computer dedicated to running just that application. For a model with a control system, you can download the complete model as a unit or separate the controller and plant into different executables on different computers. You can also execute the controller part as embedded code on a dedicated computer that controls an actual plant. Such application deployments are known as hardware in the loop or rapid prototyping [8].

xPC Target software and Real-Time Workshop allow you to generate and compile code from a SimMechanics model and download it to a computer with an IBM PC-type processor. xPC Target software acts as another target within Real-Time Workshop and requires a fixed-step solver. You can use xPC Target software to implement controller-plant models in many configurations [9].

This case study outlines some model conversion-downloading applications based on the Stewart platform modeled with SimMechanics blocks.

For More Information About xPC Target™ Software

Consult the xPC Target documentation for full instructions on downloading and running executable code in different configurations.

Files Needed for This Study

This study requires mech_stewart_xpc, as well as the initialization M-files.

Adjusting Hardware for Computational Demands

Simulation with a fixed simulation time is subject to the basic tradeoff between accuracy and speed. (See Improving Performance in the Running Mechanical Models chapter.) You can make a simulation more accurate by reducing its step size, but at the expense of creating more time steps and slowing down the real clock time. You can speed up the simulation by increasing the time step size, but you risk losing enough accuracy that the simulation fails to converge.

Real-Time Simulation Tradeoff

A typical requirement for code running on dedicated processors is that the simulation run in real time. That is, the compiled code should run with

These requirements are particularly critical for controller code.

With SimMechanics models, the accuracy-speed tradeoff is acute. SimMechanics simulation is computationally intensive and become even more so the more closed loops and constraints you add.

Simple SimMechanics models require central processor speeds in the mid-hundreds of megahertz (MHz) range. More complex models such as the Stewart platform (with 36 degrees of freedom, as well as 5 independent closed loops and 40 constraints arising from cutting those loops) demand more processor speed, starting in the low gigahertz (GHz) range.

Mitigating the Real-Time Simulation Tradeoff

You have two ways to alleviate the conflict between accuracy and speed in real-time simulation.

Both approaches are complicated by additional factors, such as memory caching and bus speed. Real-time simulation distinguishes between the sample time in signal buses and the solver step size.

Downloading a Complete Model to the Target

As a trial of running the Stewart platform simulation on dedicated hardware, here you convert a model to code, then download it and run it on an external PC-type computer. The model requires a processor of speed approximately 2 GHz or faster, and a separate target computer monitor.

Consult the xPC Target documentation for details on preparing the target computer, establishing the host-target connection, and interacting with the target from the host.

Setting Up the Target Computer and Host-Target Connection

The results here were obtained with host and target PC-type computers, each with a 3 GHz Pentium 4 processor and 1 gigabyte of RAM, communicating with each other by an RS-232 connection.

To set up the connection and start the target, you need an RS-232 cable and a blank, formatted floppy disk. The target requires a floppy disk drive. You can observe target simulation on a target monitor, your host monitor, or both.

  1. Connect the host and target computer to one another with their respective RS-232 ports and a cable.

  2. From MATLAB, prepare an xPC Target boot floppy disk.

  3. Insert the prepared xPC Target boot disk into the target PC floppy drive. Start the target computer.

  4. After the target has finished booting, confirm the host-target connection.

Examining and Running the xPC Model — Data Type Conversion

For this example, you use a variant of the code generation model presented in the preceding study, Generating and Simulating with Code.

  1. Open this model, mech_stewart_xpc. Update the diagram (Ctrl+D). The vector signals now appear as wide lines and display their data types.

    Stewart Platform with Control Reference Trajectory for xPC Target™ Simulation

  2. Open the Force Conversion and Length Conversion subsystems. Each subsystem converts a vector signal from floating doubles to 16-bit integers (typical of hardware buses) and back to doubles. These subsystems mimic the effect of hardware buses communicating between controller and plant.

    Before the data are converted to integer format, they must be converted from floating to fixed point, truncating the floating double signals. The Data Type Conversion blocks that change doubles to fixed points apply scaling to ensure that information lost to truncation is "small," as defined by the force and leg length numbers typical of this simulation. These scalings are set in the Data Type Conversion block dialogs.

  3. Close the Conversion subsystems. Open the Scope.

  4. Run the model and observe the motion. Afterward, close the Scope.

    The difference between this Stewart platform simulation and earlier ones is clear in the Leg Forces scope trace, which exhibits a small level of "noise" after the initial transient has passed. This "noise" is due to data truncation when the floating doubles are converted to fixed point.

Generating and Downloading Code from the xPC Model

In the next steps, you convert the model to code and download it to the target.

  1. Confirm the solver step size (dt1) and sample time (dt2) by entering

    dt1, dt2

    at the command line. The values are 5.0 milliseconds (ms).

  2. Check the code generation target selection in Configuration Parameters, under the Real-Time Workshop node, Target selection > System target file. The target selection is xpctarget.tlc.

    Under the Real-Time Workshop node, check the xPC Target options entry. Leave these default settings.

  3. On the Real-Time Workshop panel, click Build to start code generation.

    Follow the progress on the command window, as Real-Time Workshop generates and compiles the model, then downloads it to your target computer. When the download is complete, you see the four empty xPC Target scope windows on the target monitor.

Running the xPC Stewart Platform Model on the Target

The xPC Target interface creates an object called tg that allows you to control the application on the target machine.

  1. Using the xPC Target interface, start the target application.

    The target computer monitor displays the execution. In the Command Window, the xPC Target interface summarizes the execution results.

  2. Stop the target application. The Command Window displays the execution summary. The target scopes display the simulation results.

Viewing the Target Simulation with xPC Scopes

xPC Target software allows you to observe simulation in various ways. The xPC Target documentation explains the details.

Adjusting the Step and Sample Times — Testing for CPU Overload

You can make your simulation more accurate by reducing the solver step size. But by requiring more steps, you also make the simulation more intensive. If the solver step size drops below the task execution time (TET), the target processor cannot keep up with the simulation and suffers CPU overload.

The xPC Target summary in the Command Window indicates if CPU overload has occurred when you start or stop target object (tg) execution.

Test for CPU overload by reducing dt1 and dt2.

  1. Enter

    dt1 = 0.0025; dt2 = 0.0025;
  2. Build and download the generated code again.

  3. Start the target application.

You can understand how close to, or how far into, CPU overload your model is by comparing the TET with the solver/sample time.

You can keep reducing the solver/sample time until you cause CPU overload. This point is the limit of your target processor with this model. You can work around CPU overload by

See Adjusting Hardware for Computational Demands preceding.

Configuring for Realistic Hardware

Typical goals of downloading compiled code to a dedicated computer are

Separating Controller and Plant — Bus Communication — Discretization

Controller and plant communicate through a hardware bus configured with a specific data protocol. The xPC Target block library contains communication blocks based on a variety of data protocols matching common hardware buses. In realistic applications, the controller is often already discretized (simulated with discrete states) and requires no conversion from floating point.

The plant simulation remains continuous (not discrete) to better imitate the actual physical system.

Hardware Configuration Possibilities

Choose a model and hardware configuration depending on your needs.

Mitigating Real-Time Trade-offs

Real-time simulations are restricted by the tradeoff between accuracy and speed and limited by target execution time and maintaining convergence. You need to ensure that your memory caching and bus, not just your processor(s), are fast enough to cope with SimMechanics computational demands. See Adjusting Hardware for Computational Demands preceding.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS