FlexCase

Simulink Hardware-in-loop

  • Connect FlexCase blocks to Simulink running on host PC
  • Configure a connection to the FlexCase
  • Run FlexCase as a hardware-in-loop device

Select Method

There are multiple methods to do hardware-in-loop on the FlexCase. Please follow the applicable guide after selecting your method.

UDP Method

Prerequisites

Overview

In this example, we will have the MCU incrementing a counter every 50ms, which is sent over SPI to the MPU. The MPU will simply pass this value along to a host PC over UDP. The host PC will send a scaling factor to the MCU using the same communication methods.

Power up and connect to the FlexCase using S32DS and VirtualHere in the same manner as the quick-start guide.

Program the FlexCase Processors

Step 1 – Programming MCU

Program the MCU by opening HIL_MCU.slx, building, and flashing the .elf file to the FlexCase MCU. Now the MCU will be sending incrementing counter values to the MPU.

Step 2 – Configure and Program MPU

Configure the MPU model by opening HIL_MPU.slx. Configure the Simulink model as explained in Simulink on MPU. Next, open the UDP Send block and input your PC name or IP address to ensure your PC receives the messages sent by the FlexCase. Your PC name can be found by typing the following code into the MATLAB Command Window.

Copy to Clipboard

Alternatively, you can use the following command on the MPU terminal to get the computer name and IP address.

Copy to Clipboard

Program the MPU by opening and running HIL_MPU_Script.m, This script will automatically open, build, and run the software for the MPU. This software reads the incrementing counter from the MCU and sends it to the PC over UDP.

Step 3 – Running HIL on PC

Open and run HIL_PC.slx and run the model. You can open the scope to observe the incrementing values being sent from the MCU. Now you can change the gain from one to two and observe the value scaling the increment rate.

Build Your Own HIL Applications

Now that the demo is working, you can build on your own applications! Below are some recommendations for running your own programs:

SPI Configuration

You will have to disable the default Audesse SPI communication to achieve reliable data transfer. This can be done

Via SSH

Copy to Clipboard

Via MATLAB Command Window

Copy to Clipboard

UART Method

Prerequisites

Power Up and Connect

Connect to the FlexCase using S32DS and VirtualHere in the same manner as the quick-start guide.

Check the FlexCase COM port

Determine which COM port the FlexCase is on. This can be done most easily with the device manager.

  • Right-click the Windows start button (lower left corner)
  • Left-click Device Manger
  • Expand Ports (COM & LPT)
  • Identify the COM port of the OpenSDA connection. In this example we are using COM4

MCU Simulink Model

Open the UART_MCU.slx model in Simulink. The only additional code beyond the default functions are the following:

The code uses the on-board UART connection between the MCU and MPU, which in turn uses VirtualHere to pass data to the host PC.

Build and flash the model to the FlexCase MCU.

Host PC Simulink Model

  • Open the UART_HostPC.slx model in Simulink, which should look like the image below.
  • Update the three COM port blocks to the port that you noted in the device manager.

Test the Real-Time Control

In this example both the FlexCase and PC can increment the unit8 being sent over UART. Here we will show modifying the PC increment live.

  • Press the Run button to start the simulation.
  • Open the scope to observe the example variable being incremented. The scope should look like the image below. The variable resets to 0 because the uint8 in this example can store a maximum value of 255.

By modifying the constant in the model the slope of the graph will change accordingly.

This basic template can be expanded to send multiple variables and states between the FlexCase and your host PC simulation environment. HIL simulation is particularly useful for testing and rapidly iterating your FlexCase algorithms without needing full physical testing for each iteration.