ROS toolbox stuck on service call

7 views (last 30 days)
I'm trying to make a service call from MATLAB to a ros service. The node with the service works from other nodes and from the command line, but when I try to call it from MATLAB, I never get a response. The same happens to all services I have tried when calling them from MATLAB. I'm using ros2 with the following code:
classdef Test < handle
%TEST Summary of this class goes here
% Detailed explanation goes here
function obj = Test()
%TEST Construct an instance of this class
% Detailed explanation goes here
obj.node = ros2node("api2");
obj.client = ros2svcclient(obj.node, "/planner/clear_state", "mtms_interfaces/ClearState");
function out = call_service(obj)
msg = ros2message(obj.client);
[response,status,statustext] = call(obj.client, msg);
out = res;
To call the service, I simply do
>> t = Test()
t =
Test with properties:
node: [1×1 ros2node]
client: [1×1 ros2svcclient]
>> t.call_service()
MessageType: 'mtms_interfaces/ClearStateRequest'
but as you can see, I get stuck on the service call. The service call never finishes. The ROS service receives the message and sends it back but MATLAB code's service call never finishes. The ClearState srv is simply as follows:
# ROS service for clearing state.
bool success
Is there something I'm doing wrong? I'm using Ubuntu 20.04.4.

Accepted Answer

Josh Chen
Josh Chen on 10 Aug 2022
Hi Kyösti,
The code looks correct for me except "res" should probably be "response". But I don't think that is the culprit.
I wonder if there is any network setup issue that's preventing this code from working properly. It is always recommanded to make sure the service server is visiableand available from client side before using the "call" method. Coud you run the following code on the same MATLAB session and see if the "connectionStatus" is true?
>> myNode = ros2node("api2");
>> myClient = ros2svcclient(myNode,'/test','test_msgs/BasicTypes');
>> [connectionStatus,connectionStatustext] = waitForServer(myClient)
As an alternative, you may also try:
>> isServerAvailable(myClient)
If everyting above is working fine, I am suspecting this is an issue caused by ROS version difference or DomainID difference. Which distribution of MATLAB and ROS are you using in your environment? This page listed out supported ROS distributions for different MATLAB releases.
Hope this helps,
Josh Chen
Josh Chen on 10 Aug 2022
Edited: Josh Chen on 10 Aug 2022
Thanks for providing this additional information.
As for now, ROS Toolbox does not support communication with ROS2 Galactic. Different DDS implementation (MATLAB R2022a uses fastrtps, while ROS Galactic uses cyclonedds) underneath cause this invisible issue.
As a workaround, please consider one of the following two solutions:
  1. Follow the steps listed here to use Foxy in MATLAB with Cyclone DDS, and keep using Cyclone DDS with Galactic
  2. Run the following command before launching the server to use fastrtps on ROS 2 Galactic to make it discoverable by MATLAB ROS Toolbox
$ export RMW_IMPLEMENTATION="rmw_fastrtps_cpp"
Kyösti Alkio
Kyösti Alkio on 11 Aug 2022
Thank you, this solved the problem! I used option 1:

Sign in to comment.

More Answers (0)


Find more on Network Connection and Exploration in Help Center and File Exchange




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!