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

[ROS2] Configure a Package having IDL files (srv, msg etc.) along with CPP

asked 2022-06-11 02:01:14 -0500

ravijoshi gravatar image

I am trying to configure a package that contains srv, msg, etc., files along with CPP. However, the compiler can not find generated files. Below is the complete error:

ravi@dell:~/ros2_ws$ colcon build --allow-overriding action_tutorials_cpp
Starting >>> action_tutorials_cpp
--- stderr: action_tutorials_cpp                             
/home/ravi/ros2_ws/src/action_tutorials_cpp/src/fibonacci_action_server.cpp:17:10: fatal error: action_tutorials_cpp/action/fibonacci.hpp: No such file or directory
   17 | #include "action_tutorials_cpp/action/fibonacci.hpp"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/action_tutorials.dir/build.make:63: CMakeFiles/action_tutorials.dir/src/fibonacci_action_server.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:152: CMakeFiles/action_tutorials.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
make: *** [Makefile:141: all] Error 2
---
Failed   <<< action_tutorials_cpp [0.68s, exited with code 2]

Summary: 0 packages finished [0.84s]
  1 package failed: action_tutorials_cpp
  1 package had stderr output: action_tutorials_cpp

Please note that, the colcon was able to generate header file successfully, as shown below:

ravi@dell:~/ros2_ws$ find . -name fibonacci.hpp
./build/action_tutorials_cpp/rosidl_generator_cpp/action_tutorials_cpp/action/fibonacci.hpp

Surprisingly, the local_setup.bash shows the following error:

ravi@dell:~/ros2_ws$ source install/local_setup.bash
not found: "/home/ravi/ros2_ws/install/action_tutorials_cpp/share/action_tutorials_cpp/local_setup.bash"

Please see an example below (merged from action_tutorials_interfaces and action_tutorials_cpp):

CMakeLists.txt

cmake_minimum_required(VERSION 3.5)
project(action_tutorials_cpp)

find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(rclcpp_components REQUIRED)
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "action/Fibonacci.action")

include_directories(include)

add_library(action_tutorials SHARED
  src/fibonacci_action_server.cpp)
rclcpp_components_register_node(action_tutorials PLUGIN "action_tutorials_cpp::FibonacciActionServer" 
  EXECUTABLE fibonacci_action_server)
target_compile_definitions(action_tutorials
  PRIVATE "ACTION_TUTORIALS_CPP_BUILDING_DLL")
ament_target_dependencies(action_tutorials
  "rclcpp"
  "rclcpp_action"
  "rclcpp_components")

install(TARGETS
  action_tutorials
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

ament_package()

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>action_tutorials_cpp</name>
  <version>0.9.3</version>
  <description>C++ action tutorial cpp code</description>
  <maintainer email="mabel@openrobotics.org">Mabel Zhang</maintainer>
  <maintainer email="michael.jeronimo@openrobotics.org">Michael Jeronimo</maintainer>
  <license>Apache License 2.0</license>
  <author email="jacob@openrobotics.org">Jacob Perron</author>

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

  <depend>rclcpp</depend>
  <depend>rclcpp_action</depend>
  <depend>rclcpp_components</depend>

  <exec_depend>rosidl_default_runtime</exec_depend>

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

  <member_of_group>rosidl_interface_packages</member_of_group>
  <export>
    <build_type>ament_cmake</build_type>
  </export>
</package>

CPP (head only)

#include "action_tutorials_cpp/action/fibonacci.hpp"
#include "rclcpp/rclcpp.hpp"
#include "action_tutorials_cpp/visibility_control.h"
#include "rclcpp_action/rclcpp_action.hpp"
#include "rclcpp_components/register_node_macro.hpp"

namespace action_tutorials_cpp {
class FibonacciActionServer : public rclcpp::Node {
 public:
  using Fibonacci = action_tutorials_cpp::action::Fibonacci;
  using GoalHandleFibonacci = rclcpp_action::ServerGoalHandle<Fibonacci>;

  ACTION_TUTORIALS_CPP_PUBLIC
  explicit FibonacciActionServer(
      const rclcpp::NodeOptions& options = rclcpp::NodeOptions())
      : Node("fibonacci_action_server", options) {
    using namespace std::placeholders;

    this->action_server_ = rclcpp_action::create_server<Fibonacci>(
        this->get_node_base_interface(), this->get_node_clock_interface(),
        this->get_node_logging_interface(),
        this->get_node_waitables_interface(), "fibonacci",
        std::bind(&FibonacciActionServer::handle_goal, this, _1, _2),
        std::bind(&FibonacciActionServer::handle_cancel, this, _1),
        std::bind(&FibonacciActionServer::handle_accepted, this, _1));
  }

Package Structure

└── action_tutorials_cpp
    ├── action
    │   └── Fibonacci.action
    ├── CMakeLists.txt
    ├── include
    │   └── action_tutorials_cpp
    │       └── visibility_control.h
    ├── package.xml
    └── src
        └── fibonacci_action_server.cpp

5 directories, 5 files

I want to know how to configure such a package in ROS2.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2022-06-12 08:07:16 -0500

ijnek gravatar image

It is possible to have msg/action/srv files in the same package as where you are using them, but the CMakeLists.txt is slightly different to the case where they are in separate packages.

The Expanding on ROS Interfaces (Foxy) covers this exact case.

Modify your CMakeLists.txt as following:

// Your existing code
install(TARGETS
  action_tutorials
  ARCHIVE DESTINATION lib
  LIBRARY DESTINATION lib
  RUNTIME DESTINATION bin)

// ---- Insert Folowing ----

// Humble onwards
rosidl_get_typesupport_target(cpp_typesupport_target ${PROJECT_NAME} "rosidl_typesupport_cpp")
target_link_libraries(action_tutorials "${cpp_typesupport_target}")

// Foxy, Galactic
rosidl_target_interfaces(action_tutorials ${PROJECT_NAME} "rosidl_typesupport_cpp")

// ---- Up to here ----

// Your existing code
if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  ament_lint_auto_find_test_dependencies()
endif()

For more info, see this solution:

https://robotics.stackexchange.com/qu...

edit flag offensive delete link more

Comments

Sweet and simple. The compilation error has gone! On a side note, I must say that ROS2 documentation is a bit difficult to follow. However, I hope, over time, the documentation (and API also) gets easier to find. Finger crossed.

ravijoshi gravatar image ravijoshi  ( 2022-06-12 20:53:03 -0500 )edit

Question Tools

3 followers

Stats

Asked: 2022-06-11 02:01:14 -0500

Seen: 1,310 times

Last updated: Jun 12 '22