Network Tutorial 2 : UDP Communication with Matlab Simulink

In this tutorial, you will learn how to exchange information with a Simulink model program using the Vortex UDP extensions.

Communication between multiple simulation software can be very useful in order to integrate models coming from different platforms. In Vortex® Studio, this is made possible by using the UDP communication protocol to exchange information between the Vortex simulation and other software running on a local or remote network.

The example used in this tutorial is a simplified cruise control of a sports car. When activated, the cruise control automatically regulates the throttle of the engine in order to keep the speed of the car constant to the current value. The cruise control program is a Matlab Simulink model and will be ran on the same machine as Vortex®

For more information about the UDP extensions, consult the Network Extensions documentation.

In this section, we will add "Receive" and "Send" UDP extensions to the car mechanism allowing information to be exchanged with the Simulink program.

  1. Open the sports car vehicle workshop mechanism found in your Vortex Demo Scenes folder: (../Equipment/Vehicle_Sports_Car/Dynamic/Sports Car_Workshop.vxmechanism).

Note This car has all of the parts, constraints, collision geometry, car model and, driving controls already defined. Only the cruise control program needs to be added.

  1. In the Network section of the Toolbox, insert a UDP Receive object in the Vechicles folder (by drag-and-drop) in the Explorer. Repeat this step with a UDP Send object.
  2. In the Network section of the Toolbox, insert a UDP Receive in the Vechicles folder (by drag-and-drop) in the Explorer.
  3. From the Explorer, right-click on the UDP Receive object and select Edit from the drop down menu.
  4. Add an output and change its name to Throttle. Make sure it type is set to Double and Physical Dimension to None, then click on "Ok".

  1. In the Parameters section of the UDP Receive Properties, change the Local port value to 20103.

Note The port number 20103 was used for simplicity, since the default remote port used by Vortex is 20102. Any known unused port can be used. A list of known TCP/UDP ports can be found here

  1. From the Explorer, right-click on the newly added UDP Send object and select Edit from the drop down menu.
  2. Add three inputs with the following properties :
NameTypePhysical Dimension
Current SpeedDoubleNone
Current ThrottleDoubleNone
Cruise Control ActiveDoubleNone

Warning Following the order is important because it will have to match the order in which the data is packed in the cruise control program.

Note The UDP Receive block in Simulink doesn't support boolean data, nor different data types on the same channel. The "Cruise Control Active" will be converted into a boolean in the Simulink model.

  1. Save the mechanism in your working directory.

In this section, we connect the inputs and output from the UDP communication to variables from the components in the mechanism.

  1. From the Basicssection in the Toolbox, insert a Connection Container. Rename it Cruise Control Connections.
  2. Open the Connections Editor by double-clicking on it.
  3. Locate the following objects in the Explorer and insert the listed input or output from their Properties in the connection container:
Path > ObjectInput/Output (Section)
Vehicles > UDP ReceiveThrottle (Outputs)
Vehicles > UDP SendCurrent Speed; Current Throttle; Cruise Control Active (Inputs)
Roles > JoystickToggleButton4 (Outputs)
Vehicles > Car FWD > Dynamic ComponentInput Throttle (Inputs > Throttle Logic); Speed (Outputs > Chassis)
Vehicles > Vehicle ControllerThrottle (Outputs)
  1. Link the inputs and outputs together as shown in the picture:

  1. Save the mechanism.

In this section, we configure the UDP communication in the Simulink cruise control model.

Note This tutorial uses blocks only available in the Instrument Control Toolbox. This toolbox can be found in the Add-On Explorer.

  1. In Matlab Simulink, open the Cruise Control Simulink model found in the Cruise Control folder of the sports car (../Equipment/Vehicle_Sports_Car/Vehicle_Sports_Car/Cruise Control/CruiseControl_Workshop.slx).

Note This model was made using Matlab 2018b. It's using a discrete PID to control the speed of the vehicle.

  1. Insert a UDP Receive block from the Instrument Control Toolbox in the Simulink Library. Double-click on it to edit it properties.
  2. Set the following properties to these values, then click OK.
PropertyValue
Local address127.0.0.1
Local port20102
Data size3
Source Data typedouble
Byte orderLittleEndian
  1. Connect the output of the UDP Receive block to the Demux block.
  2. Insert a UDP Send block from the Instrument Control Toolbox in the Simulink Library. Double-click on it to edit its properties.
  3. Set the following properties to these values, then click OK.
PropertyValue
Remote address127.0.0.1
Remote port20103
Byte orderLittleEndian
  1. Connect the block to the output of the Activate Cruise Control function.

  1. Save the model in your work directory.

In this section, we add the sports car mechanism to an empty scene and test the cruise control program.

Note An Xbox (or any Xinput) controller is needed for this section. If you are using a different input device, refer to the "Establishing Connections" section of this tutorial and replace the input for "Cruise Control Active" with the proper output of your device. This variable receives a "ToggleButton" type input, meaning that it changes state (True/False) when the button is released.

  1. Open the Sports Car Workshop scene found in your Vortex Demo Scenes folder: (../Scenario/SportsCar Scene/Sports Car_Workshop.vxscene).
  2. From the Basics section of the Toolbox, add a Mechanisms From Files
  3. If your Sports Car Mechanism is open on another tab, choose it from the list of Opened Resources. If not, click on Browse and locate it.
  4. Move the mechanism on the road or in a location where it is going to be easy to maneuver.
  5. In Simulink, Run (Ctrl + T) the CruiseControl Simulink model.
  6. Test (F7) the scene in Vortex.
  7. Drive the car in Vortex. If using an Xbox controller, pressing the Y button will activate or deactivate the cruise control. Notice that when the cruise control is activated, the speed of the car is stable even if using the throttle joystick. Deactivating the cruise control lets you regain control of the car's acceleration.