Skip to main content
Version: 7

Invoking a Python Service from MoveIt Pro

This how-to guide shows how to use MoveIt Pro to invoke a service hosted by a rclpy node, allowing you to bridge your Python only ROS code into your MoveIt Pro applications.

We will use the CallTriggerService Behavior to call the Python service. The CallTriggerService Behavior expects a std_srvs/srv/Trigger service type.

Creating the MoveIt Pro Objective

Create a new Objective and name it Call Python Service.

Inside of the Objective, replace the AlwaysSuccess Behavior with the CallTriggerService Behavior and click Done on the top-right to save the Objective.

The Objective should look like this:

Call Python Service Behavior

Creating the rclpy node

The following code block creates a ROS 2 node using rclpy that advertises a service of type std_srvs/srv/Trigger. Save the following code block to <config>/scripts/trigger_service_node.py. Don't forget to make sure that the file can be run as an executable by running chmod +x on trigger_service_node.py.

#!/usr/bin/env python3
import rclpy
from rclpy.node import Node
from std_srvs.srv import Trigger

class TriggerService(Node):
def __init__(self):
super().__init__('trigger_service_node')
# Create a service with the name 'trigger_service'
self.srv = self.create_service(Trigger, 'trigger', self.trigger_callback)
self.get_logger().info('Trigger service is ready.')

def trigger_callback(self, request, response):
# This function is called whenever a Trigger request is received.
self.get_logger().info('Received Trigger request')
# Fill in the response; here we simply set success to True and provide a message.
message = 'Service triggered successfully!'
response.success = True
response.message = message
self.get_logger().info(message)
return response

def main(args=None):
rclpy.init(args=args)
trigger_service = TriggerService()
try:
rclpy.spin(trigger_service)
except KeyboardInterrupt:
pass
# Cleanup and shutdown
trigger_service.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main()

Calling the Python Service

In another terminal, run moveit_pro shell and then run the trigger_service_node.py file by executing

python3 trigger_service_node.py

You should see the following output indicating that the service is ready to be called:

[INFO] [1740497312.519518218] [trigger_service_node]: Trigger service is ready.

In MoveIt Pro, run the Call Python Service Objective. In the terminal where you are running trigger_service_node.py, you should see the following output indicating that the service was called and run successfully:

[INFO] [1740517185.300635854] [trigger_service_node]: Received Trigger request
[INFO] [1740517185.301189675] [trigger_service_node]: Service triggered successfully!

Congratulations, you now know how to invoke a service hosted by a rclpy node!

Adding the rclpy node to the MoveIt Pro Launch Configuration

To start the rclpy node when MoveIt Pro launches, the CMakeLists.txt file and launch/agent_bridge.launch.xml file need to be edited and a new launch file for the rclpy node needs to be created.

Add the following to the CMakeLists.txt file in the config:

install(FILES scripts/trigger_service_node.py
DESTINATION lib/${PROJECT_NAME})

Save the following code to <config>/launch/trigger_service_node.launch.py (Don't forget to replace <config-name> and run chmod +x on trigger_service_node.launch.py):

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():

return LaunchDescription(
[
Node(
package="<config-name>",
executable="trigger_service_node.py",
name="trigger_service_node",
),
]
)

Add the following snippet to <config>/launch/agent_bridge.launch.xml (Don't forget to replace config-name):

<include file="$(find-pkg-share config-name)/launch/trigger_service_node.launch.py"/>

From your terminal build the workspace by running moveit_pro build workspace.

To verify if this is working, run MoveIt Pro in verbose mode and run the Call Python Service Objective. You should see output indicating that the service was called and run successfully.

Next steps

You can try to create a new Behavior which calls a different type of ROS 2 service or a Behavior which calls a ROS 2 service where you also send information in the service request.