MATLAB Answers

Problems with ROS service client in MATLAB

10 views (last 30 days)
Massimo Satler
Massimo Satler on 28 May 2021
Answered: Massimo Satler on 31 May 2021
Hi everybody,
I'm having trouble trying to connect to an external (i.e. a ROS MASTER running outside the Matlab environment) ROS service server.
In particular, I am trying to connect to a mavros service server (mavros/cmd/takeoff) but I have several problems.
Being a custom message type, I followed the Matlab documentation to generate custom messages (rosgenmsg). Here I had a different generated directory structure, so I modified the path in the addpath command in the following way: ..matlab_msg_gen_ros1/glnxa64/install/m
Typing the command "rosmsg list", I can see all the generated custom messages. Fine!
Then I moved to the service client generation for the /mavros/cmd/takeoff server, as described in the documentation:
client = rossvcclient('/mavros/cmd/takeoff');
testreq = rosmessage(client);
response = call(testreq,'TimeOut',3);
I got the following error: "Failed to create a service client"
I investigated a lot the problem, but I am not being able to find a solution.
Can someone help me?
What am I doing wrong?
Additional information:
rosservice type /mavros/cmd/takeoff
=> mavros_msgs/CommandTOL
rosservice info /mavros/cmd/takeoff
=> initially doesn't work (error message: Unrecognized function or variable 'ros.internal.ros.messages.mavros_msgs.commandTOLRequest' )
I had to rename the generated custom_message folder in the following way:
original: ~./matlab_msg_gen_ros1/glnxa64/install/m/+ros/+internal/+ros/+custommessages/...
working: ~./matlab_msg_gen_ros1/glnxa64/install/m/+ros/+internal/+ros/+messages/...
Now it finds the function and provides the expected output:
Node: /mavros
URI: rospc://user-PC:43679
Type: mavros_msgs/CommandTOL
Args: MessageType MinPitch Yaw Latitude Longitude Altitude

Answers (2)

Cam Salzberger
Cam Salzberger on 28 May 2021
Hello Massimo,
I attempted to reproduce this by taking this mavros_msgs/CommandTOL service defintiion. Fortunately it doesn't even need the whole package, since the service stands on its own, so it was a quick generation.
current_dir/
|-mavros_msgs/
|-srv/
|-CommandTOL.srv
And ran this code:
rosshutdown
rosgenmsg(pwd)
addpath('path\to\current_dir\matlab_msg_gen_ros1\win64\install\m')
clear classes
rehash toolboxcache
rosinit
s = rossvcserver('/mavros/cmd/takeoff', 'mavros_msgs/CommandTOL', @(~,~,r)r);
c = rossvcclient('/mavros/cmd/takeoff');
rosservice type /mavros/cmd/takeoff
rosservice info /mavros/cmd/takeoff
call(c)
It didn't have any issues. I also didn't have to edit the path to the struct definition file; "+custommessages" worked fine for me. I suspect that editing that is preventing the call command from working, if it is expecting +custommessages in the package. I'm not sure why your "rosservice" command was failing though. Can you provide the output of "ver" so I can see what update version of R2021a you are working with? There was an issue in the original release of R2020b that could be related, but it was fixed by R2021a.
Also, can you try shutting down (rosshutdown) and restarting (rosinit) ROS on MATLAB, and just communicating between a MATLAB server and client and see if that works, rather than your external server? One possibility is that the service definition may be different on your external system in comparison to MATLAB. It's worth checking that by running this on both MATLAB and your external system:
rosmsg md5 mavros_msgs/CommandTOLRequest
rosmsg md5 mavros_msgs/CommandTOLResponse
They must be the same to communicate.
-Cam
  2 Comments
Massimo Satler
Massimo Satler on 28 May 2021
Hi, I have tested the communication between a service server and client inside MATLAB, it works.
Then I switched again to the communication with a service server running in a ROS environment and it didn't work. I tested other services but the resul didn't change.

Sign in to comment.


Massimo Satler
Massimo Satler on 31 May 2021
Brief Update
I have switched to a Windows machine running Matlab2020b and I repeated all the required steps to firstly generate custom messages, then to create a client service to a ROS server service.
It works without any problems. I also didn't have to edit the path to the struct definition file as required in Linux.
So I guess in Linux there are some bugs in the ros.internal... functions.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!