Hello world example

A first motorcortex-python script that connects to a controller and prints the manipulator tool pose.

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.

Change the IP_ADDRESS, PATH_TO_CERTIFICATE, LOGIN and PASSWORD variables to match your Motorcortex controller’s settings. 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 the message types and an empty parameter tree
motorcortex_types = motorcortex.MessageTypes()
parameter_tree = motorcortex.ParameterTree()
# Open request and subscribe connection
try:
    req, sub = motorcortex.connect(IP_ADDRESS, motorcortex_types, parameter_tree,
                                  certificate=PATH_TO_CERTIFICATE, timeout_ms=1000,
                                  login=LOGIN, password=PASSWORD)
    tree = parameter_tree.getParameterTree()
    print(f"Parameters: {tree}")
except RuntimeError as err:
    print(err)
    exit(1)

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 outputs 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)