Ask Your Question

Unable to import sensor_msgs in ros2 publisher code ?

asked 2021-02-11 08:18:15 -0500

Shiva_uchiha gravatar image

updated 2021-02-11 08:35:29 -0500

I am actually trying to wrap a IMU driver in ROS2 environment . I have the requirement to import sensor_msgs/IMU . However I am not able to import it in my current code.

NOTE:- I have installed foxy in 18.04 by source.

I get the following error

No such file or directory #include "sensor_msgs/msg/Imu.hpp" .

Attaching code for reference.

#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include "master_board_sdk/master_board_interface.h"
#include "master_board_sdk/defines.h"
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "sensor_msgs/msg/Imu.hpp"
using namespace std::chrono_literals;

/* This example creates a subclass of Node and uses std::bind() to register a
* member function as a callback from the timer. */

class MinimalPublisher : public rclcpp::Node
    : Node("minimal_publisher"), count_(0)
      publisher_ = this->create_publisher<sensor_msgs::msg::Imu>("/imu", 10);
      timer_ = this->create_wall_timer(
      1ms, std::bind(&MinimalPublisher::timer_callback, this));

    void timer_callback()
      auto message = sensor_msgs::msg::Imu();
      // = "Hello, world! " + std::to_string(count_++);
      //RCLCPP_INFO(this->get_logger(), "Publishing: '%s'",;
    rclcpp::TimerBase::SharedPtr timer_;
    rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
    size_t count_;

  int main(int argc, char * argv[])
    rclcpp::init(argc, argv);
    return 0;
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2021-02-11 21:15:17 -0500

Shiva_uchiha gravatar image

Hi ! It was a mistake from my side . I forgot to add dependency in my cmake file .

edit flag offensive delete link more

answered 2021-02-11 11:38:42 -0500

jacobperron gravatar image

In ROS 2, C++ headers for message types are all lowercase, snake_case. The same is true for services and actions. Try:

#include "sensor_msgs/msg/imu.hpp
edit flag offensive delete link more


Nope this is not working as well

Shiva_uchiha gravatar image Shiva_uchiha  ( 2021-02-11 21:07:44 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2021-02-11 08:18:15 -0500

Seen: 425 times

Last updated: Feb 11 '21