Please note that OPAL-RT knowledge base is not fully optimized for mobile platforms.
For optimal experience, use a desktop computer.
Reference Number: AA-02046 // Views: 20 // Created: 2021-10-22 16:04:49 // Last Updated: 2021-10-25 08:48:49
HYPERSIM + UDP + Python
HYPERSIM + UDP + Python
Or: How to Run a Python Script on a Windows Computer that Communicates with an HYPERSIM Simulation.
This article presents step-by-step instruction to setup a Python script that communicates with an RT-LAB simulation in real-time using UDP.
Prerequisites for the Windows 10 Computer
Let's suppose we are starting with a new Windows 10 computer. Here is the required software to install:
Procedure for Windows and Python
Download the two Python scripts (main.py and ServerRP.py) attached to this article and unzip them anywhere on the computer.
Open the file "main.py" using IDLE (it is installed automatically with Python).
Inside the Python script, update the IP address to match the one of the windows computer where the script is running. In this example it is 10.168.11.42. The 50000 is the port number being used.
Save the script and run it in IDLE. It will show 'Waiting for connection from simulator'. The HYPERSIM model is not yet running, therefore it is the expected behaviour that the Python script does not connect to it.
Procedure for HYPERSIM
Let's create the model from scratch. Open HYPERSIM and click on "New Default Document". Save the new model anywhere.
Reproduce the model presented below:
The model contains:
Inside the monitoring block, the expression is "name_of_block_to_monitor"."signal_to_monitor". In this case, the signal to monitor is "y", the output of the block.
Select all the sensors of all the blocks (except the monitoring block).
Make sure the model runs on the assigned target. Stop the simulation.
Open the IO Interface and add a new "TCP-UDP" driver.
Then, add a new stream.
Configure the stream as presented below:
The "UDP remote port" and the "Remote IP address" must match the value written on in the Python script.
The last step in the IO interface is to configure the streams to transmit and receive. In this example, we send and receive 4 values of type double.
Back to the model, open the sensor form and do the following mapping (the unused signals were removed for visibility):
The model is now ready to be used!
Procedure to Run the Example
Start the Python script through IDE.
At this point, the script should display "Waiting for connection from simulator...".
Start the HYPERSIM Simulation. In the .simout file, make sure the IP address is the correct one.
The connection should be made, and data should be displayed in IDE interface:
To validate the results in HYPERSIM, whether use ScopeView or use the monitoring blocks. To start the monitoring blocks, press "Start Monitoring".
The expected results are: 12, 24, 45, [Increasing number]. The Python script is sending data roughly every two seconds.
You're done. Good job! The script and the HYPERSIM model are attached to this KB for reference.