Introducing ctrlX CORE IDE App
Bosch rexroth

In this video, you will see how we can run Python scripts in ctrlX CORE and trigger the scripts using Node-RED and PLC Engineering
๐Ÿ“ข Coming soon
โ€‹

Example1.py
1
import functools import sys
2
#flush all prints
3
print = functools.partial(print, flush=True)
4
#For loop
5
for x in range(2):
6
print(x)
7
sys.exit(0)
Example2.py
1
#Reading from ctrlX Data Layer
2
Channel1 = datalayer.read("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/input/data/XI110116/Channel_1.Value")
3
โ€‹
4
#Writing to ctrlX Data Layer
5
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_1.Value", Channel1)
6
โ€‹
7
sys.exit(0)python
Example3.py
1
import time
2
import sys
3
โ€‹
4
while True:
5
#Reading Channel 1 status from ctrlX Data Layer
6
Channel1 = datalayer.read("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/input/data/XI110116/Channel_1.Value")
7
#Reading Channel 3 status from ctrlX Data Layer
8
Channel3 = datalayer.read("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/input/data/XI110116/Channel_3.Value")
9
10
#blinking loop
11
if Channel1 is True:
12
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_1.Value", True)
13
time.sleep(1) #delay of 1s
14
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_2.Value", True)
15
time.sleep(1)
16
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_3.Value", True)
17
time.sleep(1)
18
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_3.Value", False)
19
time.sleep(1)
20
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_2.Value", False)
21
time.sleep(1)
22
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_1.Value", False)
23
time.sleep(1)
24
else:
25
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_1.Value", False)
26
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_2.Value", False)
27
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_3.Value", False)
28
if Channel3 is True:
29
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_1.Value", False)
30
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_2.Value", False)
31
datalayer.write("fieldbuses/ethercat/master/instances/ethercatmaster/realtime_data/output/data/XI211116/Channel_3.Value", False)
32
sys.exit(0)
33
โ€‹
34
โ€‹
35
โ€‹
Example4PLC
1
import sys
2
import time
3
import json
4
โ€‹
5
#Empty list
6
data = []
7
โ€‹
8
#Reading PLC variable from ctrlX Data Layer
9
plcdata = datalayer.read("plc/app/Application/sym/PLC_PRG/ScriptOutData.Value")
10
data.append(plcdata)
11
time.sleep(0.3)
12
โ€‹
13
plcdata = datalayer.read("plc/app/Application/sym/PLC_PRG/ScriptOutData.Value")
14
data.append(plcdata)
15
time.sleep(0.3)
16
โ€‹
17
plcdata = datalayer.read("plc/app/Application/sym/PLC_PRG/ScriptOutData.Value")
18
data.append(plcdata)
19
time.sleep(0.3)
20
โ€‹
21
plcdata = datalayer.read("plc/app/Application/sym/PLC_PRG/ScriptOutData.Value")
22
data.append(plcdata)
23
time.sleep(0.3)
24
โ€‹
25
plcdata = datalayer.read("plc/app/Application/sym/PLC_PRG/ScriptOutData.Value")
26
data.append(plcdata)
27
time.sleep(0.3)
28
โ€‹
29
#Average function
30
def avg(num):
31
sumOfNumbers = 0
32
for t in num:
33
sumOfNumbers = sumOfNumbers + t
34
avg = sumOfNumbers / len(num)
35
return avg
36
โ€‹
37
#Calculate Max
38
max_value = max(data)
39
โ€‹
40
#Calculate Min
41
min_value = min(data)
42
โ€‹
43
#Calculate Average
44
avg_value = avg(data)
45
โ€‹
46
#JSON to String
47
max_value_json = json.dumps({"type": "int16", "value": max_value})
48
min_value_json = json.dumps({"type": "int16", "value": min_value})
49
avg_value_json = json.dumps({"type": "float", "value": avg_value})
50
โ€‹
51
#Writing to PLC variable in ctrlX Data Layer
52
datalayer.write_json("plc/app/Application/sym/PLC_PRG/ScriptMax.Value",max_value_json )
53
datalayer.write_json("plc/app/Application/sym/PLC_PRG/ScriptMin.Value",min_value_json )
54
datalayer.write_json("plc/app/Application/sym/PLC_PRG/ScriptAvg.Value",avg_value_json )
55
โ€‹
56
sys.exit(0)
Example5
1
import sys
2
โ€‹
3
motion.attach_obj("X")
4
motion.attach_obj("Y")
5
motion.attach_obj("Z")
6
motion.attach_obj("Kinematics")
7
โ€‹
8
#Position1
9
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[-100,-100,100],coord_sys="PCS",vel=2000,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
10
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[-100,-100,0],coord_sys="PCS",vel=1500,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
11
while motion.get_axs_ipo_values("Z").get("pos") != 0:
12
pass
13
โ€‹
14
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[-100,-100,100],coord_sys="PCS",vel=1500,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
15
#Position2
16
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[250,250,100],coord_sys="PCS",vel=2000,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
17
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[250,250,50],coord_sys="PCS",vel=1500,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
18
while motion.get_axs_ipo_values("Z").get("pos") != 50:
19
pass
20
โ€‹
21
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[250,250,100],coord_sys="PCS",vel=1500,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
22
#Home position
23
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[-400,-400,100],coord_sys="PCS",vel=2000,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
24
while motion.get_axs_ipo_values("X").get("pos") != -400:
25
pass
26
โ€‹
27
motion.detach_obj("X")
28
motion.detach_obj("Y")
29
motion.detach_obj("Z")
30
motion.detach_obj("Kinematics")
31
โ€‹
32
sys.exit(0)
Example6.py
1
import time
2
import paho.mqtt.client as mqtt #import the client1
3
broker_address="192.168.0.112"
4
โ€‹
5
โ€‹
6
#broker_address="iot.eclipse.org" #use external broker
7
client = mqtt.Client("ctrlX CORE") #create new instance
8
#client.username_pw_set("boschrexroth","boschrexroth")
9
client.connect(broker_address,port=1884) #connect to broker
10
#client.subscribe("ctrlxon")
11
client.publish("ctrlxon","Hello ctrlX World")#publish
12
print("closing")
13
sys.exit(0)
Example7.py
1
import sys
2
import time
3
import paho.mqtt.client as mqtt_client #import the client1
4
โ€‹
5
โ€‹
6
broker = '192.168.0.112'
7
port = 1884
8
topic = "ctrlxon"
9
client_id = 'ctrlX CORE'
10
#username = 'boschrexroth'
11
#password = 'boschrexroth'
12
โ€‹
13
motion.attach_obj("X")
14
motion.attach_obj("Y")
15
motion.attach_obj("Z")
16
motion.attach_obj("Kinematics")
17
โ€‹
18
def connect_mqtt():
19
def on_connect(client, userdata, flags, rc):
20
if rc == 0:
21
print("Connected to MQTT Broker!")
22
else:
23
print("Failed to connect, return code %d\n", rc)
24
# Set Connecting Client ID
25
client = mqtt_client.Client(client_id)
26
#client.username_pw_set(username, password)
27
client.on_connect = on_connect
28
client.connect(broker, port)
29
return client
30
โ€‹
31
#Connect to MQTT Broker
32
client = connect_mqtt()
33
client.loop_start()
34
client.publish(topic, "Program Starts")
35
โ€‹
36
#Position1
37
client.publish(topic, "Going to Position 1")
38
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[-100,-100,100],coord_sys="PCS",vel=2000,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
39
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[-100,-100,0],coord_sys="PCS",vel=1500,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
40
while motion.get_axs_ipo_values("Z").get("pos") != 0:
41
time.sleep(1)
42
pass
43
client.publish(topic,"Picked object")
44
client.publish(topic,"Going to Position 2")
45
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[-100,-100,100],coord_sys="PCS",vel=1500,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
46
#Position2
47
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[250,250,100],coord_sys="PCS",vel=2000,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
48
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[250,250,50],coord_sys="PCS",vel=1500,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
49
while motion.get_axs_ipo_values("Z").get("pos") != 50:
50
time.sleep(1)
51
pass
52
client.publish(topic,"Placed Object")
53
client.publish(topic,"Going to Home position")
54
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[250,250,100],coord_sys="PCS",vel=1500,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
55
#Home position
56
motion.kin_cmd_move_lin_abs(kin="Kinematics",pos=[-400,-400,100],coord_sys="PCS",vel=2000,acc=1000,dec=1000,jrk_acc=0,jrk_dec=0)
57
while motion.get_axs_ipo_values("X").get("pos") != -400:
58
time.sleep(1)
59
pass
60
client.publish(topic,"Program finished") # working
61
time.sleep(1)
62
motion.detach_obj("X")
63
motion.detach_obj("Y")
64
motion.detach_obj("Z")
65
motion.detach_obj("Kinematics")
66
sys.exit(0)

Variable declaration
1
PROGRAM PLC_PRG
2
VAR
3
fbIL_ScriptInstance: IL_ScriptInstance;
4
StateInstance: CXA_PYTHON.INSTANCE_STATE;
5
bEnableInstance: BOOL;
6
bInOperationInstance: BOOL;
7
bErrorInstance: BOOL;
8
ErrorIDInstance: CXA_PYTHON.ERROR_CODE;
9
ErrorIdentInstance: CXA_PYTHON.ERROR_STRUCT;
10
strInstanceName: STRING := 'PLCInstance'; //Name of script instance to be created
11
bResetInstance: BOOL;
12
bAbortScript: BOOL;
13
14
fbIL_StartScriptFile: IL_StartScriptFile;
15
bExecuteFile: BOOL;
16
bDoneFile: BOOL;
17
bActiveFile: BOOL;
18
bErrorFile: BOOL;
19
ErrorIDFile: CXA_PYTHON.ERROR_CODE;
20
ErrorIdentFile: CXA_PYTHON.ERROR_STRUCT;
21
strFilePath: STRING(255) := 'activeConfiguration/script/Example2.py'; //Path to the file to be executed e.g. root folder of the active configuration
22
//strFilePath: STRING(255) := 'activeConfiguration/script/Example4PLC.py'; //Path to the file to be executed e.g. root folder of the active configuration
23
ParametersFile: ARRAY [0..9] OF STRING;
24
ScriptOutData : INT;
25
ScriptMax : INT;
26
ScriptMin : INT;
27
ScriptAvg : REAL;
28
END_VAR
PLC Program
1
fbIL_ScriptInstance(
2
Enable:= bEnableInstance,
3
InOperation=> bInOperationInstance,
4
Error=> bErrorInstance,
5
ErrorID=> ErrorIDInstance,
6
ErrorIdent=> ErrorIdentInstance,
7
InstanceName:= strInstanceName,
8
ResetInstance:= bResetInstance,
9
AbortScript:= bAbortScript,
10
State=> StateInstance);
11
12
fbIL_StartScriptFile(
13
Execute:= bExecuteFile,
14
Done=> bDoneFile,
15
Active=> bActiveFile,
16
Error=> bErrorFile,
17
ErrorID=> ErrorIDFile,
18
ErrorIdent=> ErrorIdentFile,
19
InstanceName:= strInstanceName,
20
FileName:= strFilePath,
21
Parameters:= ParametersFile);
22
โ€‹
23
PLC_PRG.ScriptOutData := PLC_PRG.ScriptOutData + 1;
24
IF PLC_PRG.ScriptOutData >= 200 THEN
25
PLC_PRG.ScriptOutData := 0;
26
END_IF
27
โ€‹
28
IF PLC_PRG.ScriptOutData <= 100 THEN
29
PLC_PRG.ScriptOutData := 100;
30
END_IF

GitHub - thegeterrdone/ctrlX-CORE-IDE
GitHub
Node-RED flows used in the video
Copy link
On this page
๐ŸŽฅ Introducing ctrlX CORE IDE App
๐Ÿ“— Resource files used in the video
๐Ÿ“ƒ Python scripts
๐Ÿ“ƒ PLC Engineering Program
๐Ÿ“ƒ Node-RED Program