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-01968// Views: AA-01968// Created: 2020-11-25 16:12:16// Last Updated: 2020-11-25 16:55:17 General Article RT-LAB + Raspberry PI + Python + UDP (Using IO Interface) Or: How to Run a Python Script on a Raspberry PI that Communicates with an RT-LAB Simulation using Ethernet UDP and the IO Interface NOTE: There are two similar demos using the Raspberry PI and UDP to connect to the Opal simulator. 1- Using the AsyncIP C code (the python script is for Python 3.5): RT-LAB + Raspberry PI + Python + UDP (Using AsyncIP C code) 2- Using the IO Interface (the python script is for Python 3.7): This KB. Context1) Python is an interpreted and high-level programming language with many available open-source libraries that can be used to develop various control algorithms (link). In the electrical engineering domain, examples of such control algorithms are:
2) Raspberry PI are inexpensive and easy-to-use devices that can run Python scripts (link). 3) RT-LAB provides the flexibility to achieve complex real-time simulations that can feed data to a control algorithm (link). Therefore, applying their respective strengths by mixing them in one configuration offers a powerful trio for a broad range of studies. This article presents step-by-step instructions on how to make a Python script that runs on a Raspberry PI and that communicates with an RT-LAB simulation in real-time using UDP, as shown in Figure 1. Note: The same script can also be run directly on Windows, simply change the IP address for the one from the computer instead of the Raspberry PI. List of Materials/Software
*For installing the OS and enabling the SSH only. Prerequisites for the Windows 10 ComputerLet’s suppose we are starting with a new Windows 10 computer. Here is the required software to install:
*Python is optional on the Windows computers because the script can be edited directly on the Raspberry PI. However, it is usually more convenient to use an IDE such as VS Code, Anaconda or other. In general, any Python (2.X or 3.X) can be used. However, ensure you are using the same version on both the Raspberry PI and on Windows. In this example, Python 3.7.3 is used. **NOTE: The Python script in this KB works only with Python 3.7 versions! Finally, make sure you can ping the target using the command prompt. For instance, if the IP address of the target is 192.168.2.204, then: Prerequisites for the Raspberry PILet’s suppose we are starting with a new Raspberry PI. We will need to install the OS on a micro-SD card, enable SSH and install Python 3.7 on the PI. Here are the procedures:
Finally, make sure you can ping the Raspberry PI using the command prompt. Procedure for the Raspberry PIOpen MobaXterm and connect to the Raspberry PI using an SSH session. (In this example, the IP address of the Raspberry PI is 192.168.22.19). The username is always pi.
The default password for the PI is ‘raspberry’ (unless it was changed during the installation of the OS). Once connected, it should resemble the figure below. Notice the sftp tab on the left: In the console type ‘python’ or ‘python3’ to ensure that Python is properly installed. In this example, Python 3.7 is used, therefore the keyword ‘python3’ is used. You can exit by typing ‘exit()’ Change the IP address in the program ‘main.py’ to the one for your Raspberry PI (the script is attached to the KB entry). Drag and drop the two Python scripts, main.py and ServerRP.py. (Both are attached to this KB entry.). The To start the Python script, type ‘python3 main.py’ or ‘python main.py’ (depending on which Python version you are using). It should be the same keyword used previously to ensure that Python was properly installed. MobaXterm now displays ‘Waiting for connection from simulator’. The RT-LAB model is not running yet, therefore, it is expected behaviour that the Raspberry PI does not connect to it. To stop the python script, press CTRL + C Procedure for RT-LABImport
the project attached to this KB. It is a modified version of the
example model TCP_UDP Loopback (the model was modified using MATLAB 2016). See "version.txt" document in the zip attached for all the versions use Open the IO interface of the project and update the UDP remote port and the IP address. They must match the ones from the Python script. Open the model, run it, save it, compile, load and execute the model. When loading the model, you should see the IP address of the Raspberry PI in the console. Start
the Python script on the Raspberry PI; type ‘python3 main.py’ or
‘python main.py’, depending on which Python version you are using (it
should be the same keyword previously used to ensure that Python was
properly installed). Double-click on the manual switch in the Simulink Console to enable the transmission and reception.
Final comments Notice that it will show ‘connected’ in the Raspberry PI console. Every second, the script will receive 4 values from the simulator (1 sine wave, 2 contants and one clock). Every other second, the Python script will send four values (three constants [12, 24, 45] and one value from a counter). Note
also in the RT-LAB console the values received from the Raspberry PI.
You can also change the values in the constants block and see that the
Raspberry PI receives the new values. Also, note that the Simulator sends way more often than the Raspberry PI. The rate that the Raspberry PI is limited by the sleep time in the script 'main.py': Whereas the rate of the OPAL is defined in the IO interface: In this example, the time step is 100us and the decimation factor is 10. Therefore the UDP packets are transmitted every 1 millisecond. Done! You can use this KB as a starting point for your next project using OPAL-RT simulators and Raspberry PI!
|