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

How to include a srv from another pkg in ROS2 - fatal error: No such file or directory

asked 2022-04-26 02:17:57 -0600

Peanpepu gravatar image

I have followed the basic srv example for ROS2 from the official website. However, i tried to create myself the service instead of using example_interfaces (intern from ROS2). I first tried to create the service inside my pkg code (as in ROS1) but i couldnt include it correctly. So i tried to create the service in an extern package (i know its the best practice in ROS2) but i got the same problem when including. My srv file is Suma.srv, inside dev_ws/src/custom_pkg/srv. I will appreciate the solution either for include my inside srv file either for the external srv pkg. My srv pkg can compile correctly but the one with the code cant. Here i share my custom_pkg with the srv:

  cmake_minimum_required(VERSION 3.5)
project(custom_interfaces)

# 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)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "srv/Suma.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_export_dependencies(rosidl_default_runtime)

ament_package()

Now i show 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_interfaces</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="pedro@todo.todo">pedro</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>
  <buildtool_depend>rosidl_default_generators</buildtool_depend>

  <exec_depend>rosidl_default_runtime</exec_depend>

  <member_of_group>rosidl_interface_packages</member_of_group>

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

  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

Then, i will show my cpp_srvcli package (it works with the predetermined pkg example_interfaces): CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(cpp_srvcli)

# 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(rclcpp REQUIRED)
find_package(custom_interfaces REQUIRED)
find_package(example_interfaces REQUIRED)
# find_package(rosidl_cmake REQUIRED)
# find_package(rosidl_default_generators REQUIRED)

# rosidl_generate_interfaces(${PROJECT_NAME}
#   "srv/Suma.srv"
# )

# include_directories(include)

#ament_export_dependencies(rosidl_default_runtime)
#include_directories(include ${Boost_INCLUDE_DIRS})

add_executable(server 
  src/add_two_ints_server.cpp
  #src/main_server.cpp
  )

add_executable(client
  src/add_two_ints_client.cpp
  # src/main_client.cpp
  )

# rosidl_target_interfaces(server
#   ${PROJECT_NAME} "rosidl_typesupport_cpp")
# rosidl_target_interfaces(client
#   ${PROJECT_NAME} "rosidl_typesupport_cpp")

ament_target_dependencies(server custom_interfaces example_interfaces rclcpp)
ament_target_dependencies(client custom_interfaces example_interfaces rclcpp)

install(TARGETS
  server
  client
  DESTINATION lib/${PROJECT_NAME}
)

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 ...
(more)
edit retag flag offensive close merge delete

Comments

I have already found the problem. Although the srv name file must start with a capital letter, when you use the include inside the file.cpp you have to include starting with lowercase letter: #include "cpp_srvcli/srv/suma.hpp"

Peanpepu gravatar image Peanpepu  ( 2022-04-26 02:48:27 -0600 )edit

Can you check each step with this tutorial?

ljaniec gravatar image ljaniec  ( 2022-04-26 03:09:27 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-04-26 03:00:02 -0600

Joe28965 gravatar image

updated 2022-04-26 07:47:38 -0600

It's not Suma.hpp, it's suma.hpp.

You can also see this for yourself if you go to ~/dev_ws/install/custom_interfaces/include/custom_interfaces/srv You will see the files suma.hpp there.

EDIT interestingly enough, naming the actual files should be CamelCased (it doesn't accept underscores) while the resulting hpp is under_scored

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-04-26 02:17:57 -0600

Seen: 910 times

Last updated: Apr 26 '22