BeamNG.tech Support for MATLAB and Simulink allows connecting the Mathworks products with the BeamNG.tech software with three different methods:
- MATLAB
$\leftrightarrow$ BeamNGpy$\leftrightarrow$ BeamNG.tech - Simulink (S-function)
$\leftrightarrow$ BeamNG.tech (co-simulation tool) - Simulink (FMI)
$\leftrightarrow$ BeamNG.tech (vehicle-lua controller)
Both the bridges can be use independently from each other and have different scopes. However, they can be really handy when used together as in this example .
The MATLAB bridge with BeamNG.tech is implementation of BeamNgpy in MATLAB through the MATLAB-python integration.
After installing the compatible python version with MATLAB, make sure to include the path of executable python file (exe) in your in "path" variable of "environment variables" as explained here.
make sure that python engine is connected to your MATLAB engine
pe = pyenv;
pe.Version % This should return your python version linked with your MATLAB
pyenv(ExecutionMode="OutOfProcess") % This should return paths for your python executable, library, etc.
you can see some examples here of MATLAB scripts that uses BeamNGpy to connect with BeamNG.tech.
Tightly-coupled systems involve two processes running simultaneously and interacting with each other in a dependent way. This involves one side sending data to the other process and vice versa, on a regular cycle. BeamNG can be coupled to a third party (such as MathWorks Simulink) in this manner. This page outlines some considerations which the user should understand when attempting co-simulation with BeamNG.tech.
Just follow this link, and install the toolbox from the MathWorks file exchange.
The Co-Simulation Editor allows the BeamNG.tech user to set up a tightly-coupled system between a BeamNG vehicle and third-party software (such as Mathworks Simulink). A GUI-based tool facilitates the creation of .csv files which are used as a contract to describe the parameters of the message-passing between the two sides of the coupling. The user can use the mouse to choose from a multitude of properties across the vehicle (kinematic properties, wheel data, vehicle electrics data, powertrain data, etc) - properties which are to be sent to the third party. Incoming properties (from the third party) are also selected in a similar way.
To establish a tight-coupling communication with Simulink through S-function, follow the following steps:
-
Start the BeamNG.tech simulation in your favorite map.
-
Click
F11
to access theWorld Editor
. -
Click on the Co-Simulation Editor.
Figure 1: The
Co-Simulation Editor
window -
If you have a your coupling-configuration csv file in your user-folder (it is located in
%USERPROFILE%\AppData\Local\BeamNG.tech\0.3x
by default), you have to reload the csv by clicking onStart/stop coupling with 3rd party
button, then anotherFile dialog window
will appear to load your csv.Figure 2: The File dialog window to load your csv.
-
If you want to create your signals file, click on
Open the available signal for this vehicle
button, it will open another window where you can select your signalsFrom
andTo
for sending and receiving signals.Figure 3: The
available signal list
window. -
Save your csv by click on
save the current signals configuration, for this vehicle, to disk
button.Figure 4: The File dialog window to save your configuration as csv file.
-
Click on
Start/stop coupling with 3rd party
button at your Co-Simulation Editor window to start the coupling. -
Open your Simulink model, add your S-function.
-
double-click on your S-function, and type the path of your csv file.
-
Load your csv signals file to your S-function.
Figure 5: Loading csv file window in S-function.
-
Set the Simulation time in Simulink in model settings use
Fixedstep
type and calculate yourFixed-step size
to match the simulation time in BeamNG, using the following formula:ceil(SimulinkDt / physicsDt) * physicsDt
where
SimulinkDt
is the Simulink computation time,physicsDt
is the BeamNG physics step time (fixed at 0.0005 seconds), and ceil is the ceiling operator. Figure below shows where this is set (highlighted in yellow).Figure 6: The Configuration Parameters window of Simulink model.
-
Run your S-function model in Simulink.
-
To stop coupling, stop coupling at BeamNG.tech end by pressing on the same button that started the coupling i.e.,
Start/stop coupling with 3ed party
, or click on CTRL+R to reload the vehicle, then press onStop
Simulink end.
We have some examples here that shows basic controller that have S-function connected with BeamNG.tech.
It's recommended open only one instance of S-function, having more than one instance of S-function might disturb the coupling.
A BeamNG FMI for the Functional Mock-up Interface allows the BeamNG.tech user to set up a tightly-coupled system between a BeamNG vehicle and third-party software (such as Mathworks Simulink). The vehicleSystemsCoupling
lua controller will facilitates the communication and signal selection.
The Lua controller must be loaded in order to start the tight coupling. We can do this with the following three steps:
- Launch the BeamNG.drive simulator and navigate to the main menu.
- Select the "Freeroam" option to enter the free-roaming mode.
- Choose your preferred map from the available options.
- Access the Lua console debug window by pressing the backtick (`) key or the tilde (~) key.
- The vehicle should then be selected at the bottom-left of this screen (usually this will be “BeamNG - Current Vehicle”).
- The following command should be typed into the command bar, to load the controller:
controller.loadControllerExternal('tech/vehicleSystemsCoupling', 'vehicleSystemsCoupling', {})
, or you can use the shortcut for the specific controller i.e., CTRL+V - Open your Simulink and add your FMI block.
- Configure your FMI model by using this format at your Command Window in MATLAB
fmudialog.createBusType('<YOUR_MODEL>/<FMI_BLOCK>')
. This command should list your inputs and outputs. - Run your FMI model.
- To stop coupling, stop coupling at BeamNG.tech end by pressing on CTRL+V to stop the coupling or CTRL+R to reload the vehicle, then Simulink end.
Figure 7 shows the bar at the bottom of the console window in detail. Note the vehicle selection menu on the left, and the command bar on the right, where one can enter commands.
The Simulink process should also be started. If BeamNG is not running, Simulink will block its execution until it receives a message from BeamNG. The reverse is also true; if Simulink is not executing, BeamNG will block execution.
When communication has been established over the UDP send and receive sockets (after both ends of the communication have started executing), the tight coupling process will commence.
Figure 7: The Console Window Command Bar
you can see some examples here with Simulink examples, and Simulink only example. To make your custom FMU model, follow the BeamNG FMU repository.
Running the BeamNG-MATLAB-Simulink generic interface requires three individual software components, here is a list of compatible versions.
BeamNG.tech | BeamNGpy | BeamNG-MATLAB-Simulink-integration | MATLAB | Python |
---|---|---|---|---|
0.32 | 1.29 | 1.3.0 | R2023b & later | 3.9 |
0.31 | 1.28 | 1.2.0 | R2022b & later | 3.9 |
0.30 | 1.27.1 | 1.1.0 | R2022b & later | 3.9 |
0.29 | 1.26.1 | 1.0.0 | R2022b & later | 3.9 |
This section lists common issues with BeamNG-MATLAB-Simulink generic interface in particular. Since this library is closely tied to BeamNG.tech, it is also recommended to consult the support in documentation website on BeamNG.tech
If you can not see the toolbox in your Simulink library Browser, you may have to add the toolbox path to MATLAB search path, please do the following steps:
1- open your MATLAB and navigate to your toolbox folder i.e., %USERPROFILE%\AppData\Roaming\MathWorks\MATLAB Add-Ons\Toolboxes
2- from current folder
window, right-click on the toolbox folder, and click on Remove from path
, select Selected Folders
as shown below
Figure 8: Remove toolbox folder from path
3-Then, right click on the folder and from add path
, select Selected Folders and Subfolders
as shown below
Figure 9: Add toolbox folder and Subfolders to path
3-Lastly you have to save this changes in MATLAB search path
, look for Set Path
option and click Save
Figure 10: Save the changes in Set Path
We always welcome user contributions, be sure to check out our contribution guidelines first, before starting your work.
Copyright © 2024, BeamNG GmbH. All rights reserved. This project is licensed under the BSD 3-Clause License - see the LICENSE file for details.
The BeamNG FMU is a fork of the Reference FMUs by Modelica Association Project, which are a fork of the Test FMUs by Dassault Systèmes, which are a fork of the FMU SDK by QTronic, all of which are released under the 2-Clause BSD License.