Hello World Example
This section will show a simple example of Motorcortex Python that connects and prints the manipulatorToolPose
2 minute read
This example shows how to create a simple “Hello World” application using the motorcortex-python library. The application connects to a Motorcortex controller and retrieves the current pose of the manipulator tool, printing it to the console.
Make sure you change the IP_ADDRESS, PATH_TO_CERTIFICATE, LOGIN, and PASSWORD variables to match your Motorcortex controller’s settings. Also, update the parameter path if you want to read a different parameter.
# import the motorcortex library
import motorcortex
import time
IP_ADDRESS = "wss://192.168.2.100"
PATH_TO_CERTIFICATE = "mcx.cert.crt"
LOGIN = "admin"
PASSWORD = "vectioneer"
# Create a parameter tree object
parameter_tree = motorcortex.ParameterTree()
# Open request and subscribe connection
try:
req, sub = motorcortex.connect(IP_ADDRESS, motorcortex.MessageTypes(), parameter_tree,
certificate=PATH_TO_CERTIFICATE, timeout_ms=300,
login=LOGIN, password=PASSWORD)
tree = parameter_tree.getParameterTree()
print(f"Parameters: {tree}")
except RuntimeError as err:
print(err)
while True:
# get the parameter value
position = req.getParameter('root/ManipulatorControl/manipulatorToolPoseActual').get()
if position is not None:
print(f"EE Position: {position.value}")
else:
print("Failed to get parameter value")
# wait for 1 second before requesting again
time.sleep(1)
This will output the current end-effector position of the manipulator tool every second:
EE Position: (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
EE Position: (0.0, 0.0, 0.0, 0.0, 0.0, 0.0)
EE Position: (0.4250000000011572, 0.0, 0.33500000000203173, 3.141592653589793, 5.78571669060653e-12, 3.141592653589793)
EE Position: (0.4250000000011572, 0.0, 0.33500000000203173, 3.141592653589793, 5.78571669060653e-12, 3.141592653589793)
EE Position: (0.4250000000011572, 0.0, 0.33500000000203173, 3.141592653589793, 5.78571669060653e-12, 3.141592653589793)
EE Position: (0.4250000000011572, 0.0, 0.33500000000203173, 3.141592653589793, 5.78571669060653e-12, 3.141592653589793)
EE Position: (0.4250000000011572, 0.0, 0.33500000000203173, 3.141592653589793, 5.78571669060653e-12, 3.141592653589793)