Knowledge Base

Welcome to OPAL-RT’s Knowledge Base

OPAL-RT offers a repository of support information for optimal use of its technology.

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
HYPERSIM + UDP + Python

HYPERSIM + UDP + Python

Or: How to Run a Python Script on a Windows Computer that Communicates with an HYPERSIM Simulation. 


Context:

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.




 Note: If you don't know the IP address of the windows computer, simply open a command prompt (press 'Windows Key' + r, then type 'cmd') and type the command 'ipconfig'.

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:

  • 2 constants to enable the transmission / reception streams.
  • 2 dummy constants as values to send
  • 1 sine wave as value to send
  • 1 Point-On-Wave to have the time from the beginning of the simulation.
  • 4 gains to receive the values from the Python script (with gain = 1).
  • 4 monitoring blocks to see the values directly on the schematic.


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.


IO Interface

Open the IO Interface and add a new "TCP-UDP" driver.







In the general options, select the option "Enable verbose mode"



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.



 NOTE: The Python script is expecting double. If the type is changed, the script must be updated.




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.



 NOTE: Running the script through a command prompt does not work.

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.


python_scripts_rpi.zip1.6 KbDownload File
UDP.zip27.8 KbDownload File