ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

Importing and defining custom services in ROS 2 Foxy

asked 2022-04-06 15:13:06 -0500

pablo.arandarod gravatar image

updated 2022-04-07 04:34:23 -0500

I have been trying to use some custom services that I created to read some sensor data in an UR5e. This service is declared in a package called custom_msgs_srvs, inside a folder called srv, with the name SensorCall.srv. This service is the used in nodes from different packages, not in that same package.

The package.xml file of the custom_msgs_srvs is:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>custom_msgs_srvs</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="pablo.arandarod@gmail.com">pablo</maintainer>
<license>TODO: License declaration</license>

<depend>rclpy</depend>

<test_depend>ament_copyright</test_depend>
<test_depend>ament_flake8</test_depend>
<test_depend>ament_pep257</test_depend>
<test_depend>python3-pytest</test_depend>

<export>
   <build_type>ament_python</build_type>
</export>
</package>

In the setup.py of the package, in the data_files section of the setup, I had the following element:

(os.path.join('share', package_name, 'srv'), ["srv/SensorCall.srv"])

I import the service in the node that I'm using it by putting from custom_msgs_srvs.srv import SensorCall.

I'm able to build everything with no problem, but when I try to execute the node that uses this service, I get the following error: ModuleNotFoundError: No module named 'custom_msgs_srvs.srv'.

----Edit:

Adding the CMakeLists.txt file:

cmake_minimum_required(VERSION 3.5)
project(custom_msg_srv)

# Default to C99
if(NOT CMAKE_C_STANDARD)
  set(CMAKE_C_STANDARD 99)
endif()

# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
  set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/SensorCall.srv"
)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # uncomment the line when a copyright and license is not present in all source files
  #set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # uncomment the line when this package is not in a git repo
  #set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

   ament_package()

And the package.xml:

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>custom_msg_srv</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="pablo.arandarod@gmail.com">pablo</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <test_depend>ament_lint_auto</test_depend>
  <test_depend>ament_lint_common</test_depend>

  <build_depend>rosidl_default_generators</build_depend>
  <exec_depend>rosidl_default_runtime</exec_depend>
  <member_of_group>rosidl_interface_packages</member_of_group>

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>
edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2022-04-07 11:54:11 -0500

pablo.arandarod gravatar image

updated 2022-04-07 12:16:41 -0500

Firstly, huge thanks to ljaniec.

The problem was solved by doing the following changes:

  1. Changes in the service declaration. When using types that are declared in other packages, it is needed to indicate it in the message/service declaration. In my case, instead of writing Float32MultiArray, it is needed to write it the following way std_msgs/Float32MultiArray.

  2. Changes in the package.xml file. Add the dependency of the std_msgs by writing <depend>std_msgs</depend>.

  3. Changes in the CMakeLists.txt file. Add the dependency of the std_msgs by writing find_package(std_msgs REQUIRED). Also, as it can be seen in this post, it is needed to add those dependencies declarations in the rosidl_generate_interfaces section of the CMakeLists file. In my particular case: rosidl_generate_interfaces(${PROJECT_NAME} "srv/SensorCall.srv" DEPENDENCIES std_msgs )

edit flag offensive delete link more
1

answered 2022-04-06 17:33:05 -0500

ljaniec gravatar image

updated 2022-04-06 17:33:31 -0500

Hello,

As there https://docs.ros.org/en/foxy/Tutorial...:

tutorial_interfaces is the name of the new package. Note that it is a CMake package; there currently isn’t a way to generate a .msg or .srv file in a pure Python package. You can create a custom interface in a CMake package, and then use it in a Python node, which will be covered in the last section.

It looks like your custom_msgs_srvs is a pure Python package. Just redo it as a C++ one :)

edit flag offensive delete link more

Comments

I redid everything in C++ but now it seems that it is unable to find some header files. The error is:

fatal error: custom_msg_srv/msg/detail/float32_multi_array__struct.h: No such file or directory #include "custom_msg_srv/msg/detail/float32_multi_array__struct.h"

I tried to find some similar situations that other people encountered, but didn't find much

pablo.arandarod gravatar image pablo.arandarod  ( 2022-04-06 17:58:05 -0500 )edit

Can you add this C++ version of the package? Maybe put it on GitHub so we can see what settings are you missing

ljaniec gravatar image ljaniec  ( 2022-04-07 03:54:16 -0500 )edit

I will show the entire CMakeLists.txt and package.xml in an edit. The repository for the project is the following one: https://github.com/PabloDavidAR/encod.... The package for the service can be found inside the src folder

pablo.arandarod gravatar image pablo.arandarod  ( 2022-04-07 04:32:02 -0500 )edit

I didn't find in your CMakeLists.txt a line with a find_package(std_msgs REQUIRED) and in package.xml a line with <depend>std_msgs</depend> for use of Float32MultiArray. Use something similar to:

set(srv_files
    "srv/SensorCall.srv"
)

rosidl_generate_interfaces(${PROJECT_NAME}
  ${srv_files}
  DEPENDENCIES 
    std_msgs
    builtin_interfaces
)
ljaniec gravatar image ljaniec  ( 2022-04-07 06:16:01 -0500 )edit

I added the builtin_interafaces and the std_msgs to both the find_package and the rosidl_generate_interfaces parts of the CMakeLists file and to the package.xml file, but still not working.

The error that appears now is fatal error: custom_msg_srv/msg/detail/float32_multi_array__struct.hpp: No such file or directory 126 | #include "custom_msg_srv/msg/detail/float32_multi_array__struct.hpp"

pablo.arandarod gravatar image pablo.arandarod  ( 2022-04-07 06:38:00 -0500 )edit

Which file has this line? How did you get this error? Put commands down + the whole terminal output. Overall, this line #include "custom_msg_srv/msg/detail/float32_multi_array__struct.hpp" looks a bit weird too. It should look more like #include "tutorial_interfaces/srv/add_three_ints.hpp". Look at the tutorial again, this part: https://docs.ros.org/en/foxy/Tutorial... Remember about changes in the package.xml and CMakeLists.txt too

ljaniec gravatar image ljaniec  ( 2022-04-07 08:01:41 -0500 )edit

Okay, I managed to solve the problem, although the solution that I ended up finding is not what I would have preferred. The problem appears to be related to using a Float32MultiArray, and when using standard types, such as float32[], the problem disappears. I tried putting the std_msgs dependency, but it was not enough and the problem did not disappear.

pablo.arandarod gravatar image pablo.arandarod  ( 2022-04-07 08:35:49 -0500 )edit
1

Maybe this step by step tutorial will be easier for you: https://roboticsbackend.com/ros2-crea...

ljaniec gravatar image ljaniec  ( 2022-04-07 08:52:24 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2022-04-06 15:13:06 -0500

Seen: 627 times

Last updated: Apr 07 '22