Quantcast

Simulink Coder

Rapid Control Prototyping and Hardware-in-the-Loop Using xPC Target

This example shows how you can use xPC Target for real-time rapid control prototyping and hardware-in-the-loop simulation. As an example, we will use an automatic transmission system that includes an event-driven controller. We first look at a non-real-time Simulink model of the closed-loop system and the results of running a simulation of this model in Simulink.

We then look at a version of this model that has been modified to run in real time, on a dedicated PC using xPC Target. The model will combine the concepts of rapid control prototyping and hardware-in-the-loop simulation in a single real-time environment. This will enable more realistic testing of the controller.

Contents

Initial Non-Real-Time Simulink Model

The Control System Architecture

In this simple schematic of the control system architecture, the input is represented by the blue block, which drives the controller, represented in green. The controller is connected to its plant, shown in yellow. Output values, represented in red, can be read from the plant. Finally, this system includes a feedback loop, which feeds the plant output values back to the controller to adjust the controller output, making it a closed-loop control system.

Figure 1: Control system architecture.

Vehicle Simulation of an Automatic Transmission System

The Simulink model below represents our non–real-time system. You can see the two input profiles (throttle and brake) and the resulting outputs of a simulation. The blocks use the same color coding as the control system architecture diagram above.

  • Yellow blocks represent the plant, an automotive powertrain system.
  • Blue blocks represent the throttle and brake inputs to the powertrain system.
  • Green blocks represent the controller portion of the system and include an event-driven controller and algorithmic calculations.
  • Red blocks represent the output oscilloscope displays.

Figure 2: Non-real-time Simulink model.

Simulation Input Profiles

Using the Signal Builder, shown below, you can design test cases in Simulink to verify that your requirements are met. Each tab represents a different test case.

Figure 3: Simulink input profiles.

Simulation Results

In the plot below, we can see what happened during the simulation. The driver increases the throttle to start the car moving, then gradually releases the throttle to slowly reach a cruising speed of about 50 mph. The driver then increases the throttle quickly to make a lane change. Corresponding to these throttle changes, you can see the speed increase, level off, and then increase more rapidly.

Figure 4: Throttle (red) and speed (yellow) vs. time.

In the plot below we can see the changes in the engine RPM as the driver moves through this passing maneuver. From this, we can infer where the various gear changes occurred.

Figure 5: Engine RPM vs. time.

Real-Time Simulation Using xPC Target

We can modify the Simulink model to interface with prototype hardware or actual plant hardware such as an engine or motor, or the controller hardware such as a prototype PC or embedded control unit. This approach allows us to test the controller and/or plant in real-time. The I/O device interface blocks used in the Simulink model are provided as part of xPC Target. The real-time operating system and the tools to monitor the operation of the application in real time are also provided with xPC Target. Real-Time Workshop is used to generate code directly from the Simulink model. This generated code includes the needed driver interface code, derived from the xPC Target driver blocks used in the Simulink model. The complete application, including the I/O interfaces, is compiled, linked, and downloaded to xPC Target for real-time testing.

We can also modify the system so that the plant and controller are separate components that communicate with each other using a CAN (controller area network) bus. This mimics the real system where a controller would be implemented on an embedded control unit which would communicate through a CAN bus with the actual hardware. This is done using CAN I/O blocks that are provided with xPC Target. Real-Time Workshop is used to generate the C code that is automatically downloaded and run on a dedicated target PC running the xPC Target real-time kernel.

The Control System Architecture with CAN Communication

Figure 6: Control system architecture with CAN communication.

The Simulink diagram in Figure 7 represents the modified system including the CAN blocks, shown in orange. The inputs are provided by physical brake and throttle pedals connected to the target PC using an A/D (analog to digital) input board. The simulation scopes are replaced with xPC Target Scope blocks, shown in red. These blocks allow you to view signals in real time on a monitor connected to the target PC. You can see how this Simulink model is very similar to the original control architecture outlined above.

Figure 7: Real-time Simulink model with CAN communication.

Figure 8: Simulation input pedals.

Hardware Setup and Real-Time Simulation Steps

We are now ready to run a simulation and view the results.

To run the real-time simulation:

1. Connect a second target PC configured with the needed I/O cards to your main host PC using a standard Ethernet cable or RS-232 cable.

2. Connect your pedals to the target PC through the A/D board input connection.

3. Generate code from the real-time model and download it to the target PC using Real-Time Workshop.

4. Run the simulation and view the target screen outputs as you change the throttle pedal input, all in real time.

5. Plot data logged from the simulation for additional analyses.

The hardware setup is shown below. The blue box is an industrial PC, which is being used as the target PC. The laptop serves as the host PC.

Figure 9: Host-target hardware setup.

Viewing the Data on the Target Scope and Tuning Parameters

The target PC can be connected directly to a monitor to view signals in real time. Figure 10 shows a single capture from the target screen. You can see the six xPC Target scopes that correspond to the six red scope blocks shown in Figure 7 in the CAN-enabled Simulink model. When you run the real-time simulation, the output scopes will show a number of output values.

The top right and middle right scopes show the values from the input throttle pedal. The top left and bottom left scopes show the resulting speed of the vehicle as the throttle is pushed and the vehicle accelerates. The bottom right scope shows the gears used as the vehicle accelerates. The middle left scope shows the analog values that correspond to the brake and throttle inputs.

The target screen also gives you additional information such as sample time, elapsed simulation time, and memory allocation. In addition, you can connect a keyboard to the target machine and tune parameters in real time directly on the target while you view your changes on the target monitor.

Figure 10: xPC Target scope output.

Analyzing the Data in MATLAB

In addition to viewing the data in real time on the target scopes, you can log data from a simulation to the MATLAB workspace. This allows you to plot and analyze the data more closely to help with the parameter tuning.

Additional Information

The key products used in this example include:

To learn more:

  • Visit the xPC Target product page for product details, supported I/O hardware, and access to trial software.
  • Read user stories of how engineers are using xPC Target to add rapid prototyping and hardware-in-the-loop testing capabilities to their development processes.