Robotics StackExchange | Archived questions

Failed to compile node with custom msg created in same package

This question is very similar to: https://answers.ros.org/question/306568/ros2-custom-messages-not-found/ but it dosen't solve my problem. The error that I get is that the custom message library #include "optimus_rover/msg/wheel_angles.hpp" is not found. I've checked that the library .hpp and .h is generated fine under

/home/$USER/dev_ws/install/optimus_rover/include/optimus_rover/msg

To explain I have 1 package called optimus_rover with the following layout:

/optimus_rover
     CMakeLists.txt
     /include
         /optimus_rover
     /msg
         WheelAngles.msg
     package.xml
     /src
         main.cpp

My CMakeLists.txt file looks like this:

cmake_minimum_required(VERSION 3.5)
project(optimus_rover)

# 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_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(sensor_msgs REQUIRED)
# find_package(builtin_interfaces REQUIRED)
find_package(rosidl_default_generators REQUIRED)

rosidl_generate_interfaces(${PROJECT_NAME}
  "msg/WheelAngles.msg"
  # DEPENDENCIES builtin_interfaces
)

add_executable(${PROJECT_NAME}_node src/main.cpp)
ament_target_dependencies(${PROJECT_NAME}_node rclcpp std_msgs geometry_msgs sensor_msgs)

install(TARGETS
  ${PROJECT_NAME}_node
  DESTINATION lib/${PROJECT_NAME})

ament_package()

My main.cpp node looks like this:

// Include important C++ header files that provide class
// templates for useful operations.
#include <functional> // Arithmetic, comparisons, and logical operations
#include <memory> // Dynamic memory management
#include <string> // String functions

// ROS Client Library for C++
// Allows use of the most common elements of ROS 2
#include "rclcpp/rclcpp.hpp"

// Built-in message type that will be used to publish data
#include "std_msgs/msg/string.hpp"
#include "geometry_msgs/msg/twist.hpp"
#include "sensor_msgs/msg/joint_state.hpp"
#include "std_msgs/msg/float32_multi_array.hpp"
#include "optimus_rover/msg/wheel_angles.hpp"

using std::placeholders::_1;

// Create the node class named PublishingSubscriber which inherits the attributes
// and methods of the rclcpp::Node class.
class PublishingSubscriber : public rclcpp::Node
{
  public:
    // Constructor creates a node named publishing_subscriber. 
    // The published message count is initialized to 0.
    PublishingSubscriber()
    : Node("publishing_subscriber")
    {
      // Create the subscription.
      // The topic_callback function executes whenever data is published
      // to the 'addison' topic.
      subscription_ = this->create_subscription<geometry_msgs::msg::Twist>(
      "cmd_vel", 10, std::bind(&PublishingSubscriber::topic_callback, this, _1));

      // Publisher publishes String messages to a topic named "addison2". 
      // The size of the queue is 10 messages.
      pose_publisher_ = this->create_publisher<std_msgs::msg::Float32MultiArray>("wheel_pose_setpoints",10);
      vel_publisher_ = this->create_publisher<std_msgs::msg::Float32MultiArray>("wheel_vel_setpoints",10);

    }

  private:
    // Receives the String message that is published over the topic
    void topic_callback(const geometry_msgs::msg::Twist::SharedPtr msg) const
    {

      RCLCPP_INFO(this->get_logger(), "LinSpeed: '%f', AngSpeed: '%f'",
        msg->linear.x,
        msg->angular.z
        );

      // Create a new message of type String
      auto pose_msg = std_msgs::msg::Float32MultiArray();
      auto vel_msg = std_msgs::msg::Float32MultiArray();


      // // Publish the message to the topic
      // pose_publisher_->publish(pose_msg);
      // vel_publisher_->publish(vel_msg);

      RCLCPP_INFO(this->get_logger(), "End callback");
    }
    // Declare the subscription attribute
    rclcpp::Subscription<geometry_msgs::msg::Twist>::SharedPtr subscription_;

    // Declaration of the publisher_ attribute      
    rclcpp::Publisher<std_msgs::msg::Float32MultiArray>::SharedPtr vel_publisher_;
    rclcpp::Publisher<std_msgs::msg::Float32MultiArray>::SharedPtr pose_publisher_;

};

// Node execution starts here
int main(int argc, char * argv[])
{
  // Initialize ROS 2
  rclcpp::init(argc, argv);

  // Start processing data from the node as well as the callbacks
  rclcpp::spin(std::make_shared<PublishingSubscriber>());

  // Shutdown the node when finished
  rclcpp::shutdown();
  return 0;
}

My package.xml looks like this:

<?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>optimus_rover</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="richard9661h@gmail.com">Richard</maintainer>
  <license>TODO: License declaration</license>

  <buildtool_depend>ament_cmake</buildtool_depend>

  <depend>rclcpp</depend>
  <depend>std_msgs</depend>
  <depend>geometry_msgs</depend>
  <depend>sensor_msgs</depend>

  <build_depend>rosidl_default_generators</build_depend>
  <!-- <build_depend>builtin_interfaces</build_depend> -->
  <exec_depend>rosidl_default_runtime</exec_depend>
  <!-- <exec_depend>builtin_interfaces</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>

In the package.xml and CMakeLists.txt there are commented lines that are used in on some tutorials on youtube by The Constructisim but they are from 1 year ago with a different distribution of ros2, on the official foxy tutorials on how to build a custom message they dont use these lines of code. Correct me if I'm wrong on that.

The difference I see with the previous question is that the custom msg is built in a seperate package, its it possible to build a single package to create a custom message and a node that uses that custom message?

Asked by Richard Mark Haes Ellis on 2021-06-26 12:59:12 UTC

Comments

Answers