Dear Community User! We have started the migration process.
This community is now in READ ONLY mode.
Read more: Important information on the platform change.

MauroRiboniMX_0-1693383228378.png

Python SDK: run Python examples directly inside the IDE app plus SDK explanation

MauroRiboniMX
Contributor
Disclaimer

The goal is to be able to run the examples for python in the SDK and start to program in minutes. This solution requires no particular app installation and no build environment on PC.

Rexroth IDERexroth IDE

Requirements:
  • ctrlX CORE - x3 or x7 or a ctrlX OS installation
  • IDE app
  • Python runtime app
STEP 1: download the sdk, unzip it and upload the examples

Navigate to ctrlX AUTOMATION SDK repo and download the full repo as a zip. LinkToRepo 

Download SDKDownload SDK

Open the "IDE - TextualCoding" app.

Open the textual coding appOpen the textual coding app

Extract the folder, drag&drop the folder samples-python in the "active-configuration" folder.

Import ExamplesImport Examples

STEP 2: start the examples, brief explanation of the most important examples

Lets start to run the most easy Example: "datalayer.client.light". For any example we have a main.py and we can start the file by clicking the "play" button. Once we're done we can stop the file stopping the terminal clicking on the X. Easy!

Code ExecutionCode Execution

 

Willing to start from 0 playing around with the SDK I, personally suggest to go over following this sequence:

  1. Sart from "datalayer.client.light", is the most easy example with this we can start to interact with the datalayer, read data and subscribe to Datalayer nodes.
  2. Understand how to create nodes with "datalayer.provider".
  3. Continue with "datalayer.client" to understand How to Use Variant type to write nodes.
  4. Fill the competences over Flatbuffers and provider features 🙂
STEP 3: let's start this Journey together
The datalayer.client.light example

Note: The code has been a little shrinked from comments to better fit in page. Starting with this example is very simple, the code starts importing the needed libraries and modules:

Modules importModules import

The main starts with the initialization of the Datalayer system, the first two lines of code are mandatory. The third line of code reacts differently if we're inside a snap or outside. In a snap we're using the ipc communication (so ip and ssl_port are ignored) and the system works out of the box. (the password and user box are omitted in this example but can be specified).
If we pass these lines of code the system is good and we can then start.

Client initClient init

The example continues with the subscription part: the subscription has to be set with the various parameters and then created and linked to a subscription function that is then called anytime there is a notify event from Datalayer. In this example we're subscribing with a 2s interval to the cpu usage of the core.

Subscription SetupSubscription Setup

The function prints the result of the notification with the timestamp.

Subscription functionSubscription function

FInally we see the synchronous read function which is "read_sync".

Read_synch usageRead_synch usage

Running the code, this is the code output:

Code outputCode output

The datalayer.provider.all-data example
NOTE:

At the time that I write this guide the code has 2 simple bugs, i am attaching the "nodeManagerAllData.py" file that fixes the bugs. Please substitute this in the example.

The structure is the following:

  • the alldataprovider folder contains the code in nodeManagerAllData and the providerNodeAllData class that is used.
  • The main.py is triggers all the functions.

Software structureSoftware structure

The code creates some dynamic (editable) and static (non editable) nodes in the datalayer:

Code OutputCode Output

 

Mixing Up the examples

Now lets think that we want to create a datalayer node which is called "Powermeter" and contains two nodes which are "PowerDevice1" and "PowerDevice2" which contain the powers measured reading two analog ethercat inputs and multiplying them for a factor. We'll keep the things simple. We're going to modify the two examples and run them separately.

Starting from the provider part: we need to modify in main.py, example datalayer.provider.all-data the address_root in two positions: line 16 and line 51. Plus we comment out the "create_static" part. Dont' forget to save both files!

Root Main 1Root Main 1

Here we have the second part to modify in main.py

Root Main 2Root Main 2

Then we move to nodeManagerAllData.py and we remove the "dynamic" subnode since we don't need it:

Create nodesCreate nodes

Then we modify accordingly the create_nodes function:

Nodes CreationNodes Creation

The Result of this code from datalayer point of view is the following:

Nodes ResultNodes Result

We continue then with datalayer.client.light but before we stop the previous script and we start it in a separate terminal, in this way it will continue to run independently

Terminal ProviderTerminal Provider

 Modifying the main.py we remove the subscription and flatbuffer parts obtaining a super easy example.

Main.py shrinkedMain.py shrinked

 

In this way any 2 seconds we read the memused-percent and we print the result. Since we have to read a uint16 and we need to write it multiplied by a value as a float32 we modify the example introducing the writing part:

Final ClientFinal Client

In the end the programs reads the ethercat analog input and writes a calculation on the PowerDavice2 node.

Final ResultFinal Result

MauroRiboniMX
MauroRiboniMX
Hello, I am Mauro a ctrlX DEVELOPR at night and a̶n̶ A̶p̶p̶l̶i̶c̶a̶t̶i̶o̶n̶ E̶n̶g̶i̶n̶e̶e̶r̶ Product Manager during the day 😎 . Ask me anything about ctrlX AUTOMATION but my best topics are IoT, AI, SDK and Communication!
Must Read
Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist