In this guide we describe the basics of how to set up a ctrlX CORE to work with a Python script by using the PLC app. We will show you a PLC sample project that illustrates how to read/write data in the datalayer and how to let axes move in the Motion app with a Python script.
You can download the example project below and try starting it yourself.
Prerequisites
What you need:
- ctrlX CORE or COREvirtual
- ctrlX WORKS
- PLC App
- Motion App
- Python Runtime App
Step-by-Step Guide
So, let's start by adding the CXA_Python library which lets us execute, delete, reset and monitor Python scripts on script instances. Go to the Library Manager, click Add Library and search for CXA_Python.
The next step is to create a global variables list.
- The first block represents the nodes we create in the datalayer. You can find them in ‘plc/app/Application/sym/[name of GVL file]/‘ later.
- The second block represents the script instance.
- The third block represents the script file that executes the python commands later. Here you can give the instance a name and specify the path to the python script we create in a few steps.
The variables for the script instance and the script file will be linked to the visualization elements later.
The next step is to create the script instance and the script executable. We do that by creating a new POU program called PRG_Script with Structured Text as implementation language. Here we create some variables for the script instance and file. We can use some structures from the CXA_Python library for that, that will be shown in the visualization later.
In the next part we can create the script instance and start the script file. The instance lets you execute the python script, but an instance only lets you execute one script at the same time. In this case the script instance can only be started on the startup or when restarting after the script finished running.
Use the declared variables to create and start the instance and file.
Another important step is to use the global variables that we want to become nodes in the datalayer (see line 1). This is important because you might not see variables in the next step if you aren’t using them.
So, what we do next is export the variables we want to see in the datalayer. We do that by going into the Symbol Configuration and checking the variables, but first you need to build the project for all variables to appear.
Then we simply call this new program from PLC_PRG.
Now we can create a visualization and link the buttons and display elements to the according variables. That’s how we control the instance and the file.
After the visualization is done, we can start with the python script which is quite simple. We can use it just like a normal python script from here on. Here you can test reading from the datalayer.
You can also manipulate the data and write it back into the datalayer.
And finally, here are some examples to show that it’s possible to move axes with the Motions app by using a python script.
All we need to do now is to upload the python script onto the ctrlX CORE. To do that click “Manages app data” on the top right corner of the overlay of he ctrlX CORE.
Then open the File View by clicking on the three dots.
Now we can upload the python script to the path we picked earlier as a global variable in the GVL_Script file. In this case it’s just the home folder 'activeConfiguration/MyPythonScript1.py'. You can also edit the script in this view.
How to start the example
We uploaded this project so you can download it below and start it, if ctrlX WORKS is installed.
To test this python script, we need to add a linear axis called “Axis_X” in the motions app.
We start this program by going into the PLC project, connecting it to the ctrlX CORE, logging into the ctrlX via the PLC project and starting it.
Now we can start and stop the python script and instance with the visualization we created earlier.
The script instance needs to be enabled first and then you can execute the file with the buttons. Below the buttons you can see the current state of the script instance and the script file to check if it’s running correctly or if any errors occurred. You can also reset or abort the python script.
Everything should be green in the visualization. You should be able to see the axis move, and you should be able to see the changes in the datalayer if the program is running correctly.
Known Issue
As of June 2024 something prevents a restart of the python script, if the "OPC UA Pub/Sub" app is installed on the same ctrlX CORE.