Rosject - part 1 - subscription

Hi!
I have just written the code for the first part of the rosject (topics), but it does not compile.
It tells me there is a problem with the subscriber, but I think there is no error.
This is how I implemented the subscriber (hidding the core part):

#include "rclcpp/rclcpp.hpp"
#include "geometry_msgs/msg/twist.hpp"
#include "sensor_msgs/msg/laser_scan.hpp"
#include <chrono>

using namespace std::chrono_literals;
using std::placeholders::_1;

class WallFollow : public rclcpp::Node
{
public:
  WallFollow()
  : Node("pt1_wall_follow_node")
  {
    publisher_    = this->create_publisher<geometry_msgs::msg::Twist>("/cmd_vel", 10);
    subscription_ = this->create_subscription<sensor_msgs::msg::LaserScan>("/scan", 10, std::bind(&WallFollow::scan_callback, this, _1));

    timer_        = this->create_wall_timer(500ms, std::bind(&WallFollow::timer_callback, this));
    this->front       = 0.0;
    this->front_right = 0.0;
    this->front_left  = 0.0;
    this->left        = 0.0;
    this->right       = 0.0;

  }

private:

  void scan_callback(const sensor_msgs::msg::LaserScan::SharedPtr msg)
  {
    this->front       = msg->ranges[360];
    this->front_right = msg->ranges[365];
    this->front_left  = msg->ranges[355];
    this->left        = msg->ranges[540];
    this->right       = msg->ranges[180];

    RCLCPP_INFO(this->get_logger(), "[LEFT] = '%f'", this->left);
    RCLCPP_INFO(this->get_logger(), "[RIGHT] = '%f'", this->right);
    RCLCPP_INFO(this->get_logger(), "[FORWARD] = '%f'", this->front);
    
  }

  void timer_callback()
  { 
    [...]
  }
    publisher_->publish(this->twist_msg);  
  }

  rclcpp::TimerBase::SharedPtr timer_;
  rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_;
  rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr subscription_;
  geometry_msgs::msg::Twist twist_msg; 

  float front;
  float front_right;
  float front_left;
  float left;
  float right;
};

int main(int argc, char * argv[])
{
  rclcpp::init(argc, argv);
  rclcpp::spin(std::make_shared<WallFollow>());
  rclcpp::shutdown();
  return 0;
}

This is the error I have compiling:

Where is the problem? Thanks in advance

This is indicating an error while compiling your C++ source. It could be an error in your C++ source (most likely), or an error in your CMakeLists.txt file.

To be sure, you can start from a clean state by removing current build-related folders before recompiling.

cd ~/ros2_ws
rm -rf build/ install/ log/
colcon build

Thank you @bayodesegun for your response.
Even following your instructions I have the same error.
This is my CMakeLists:

cmake_minimum_required(VERSION 3.5)

project(ros2ject_pkg)

# 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(std_msgs REQUIRED)
find_package(geometry_msgs REQUIRED)
find_package(std_srvs REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(rosidl_default_generators REQUIRED)

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()

# Custom interfaces
rosidl_generate_interfaces(${PROJECT_NAME}
"srv/FindWall.srv"
)

# Main 1: Publishers&Subsribers
add_executable(pt1_wall_follow_node src/pt1_wall_follow.cpp)
ament_target_dependencies(pt1_wall_follow_node rclcpp std_msgs geometry_msgs)

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

# Install launch files.
install(DIRECTORY
launch
DESTINATION share/${PROJECT_NAME}/
)

ament_package()

If I comment on the line “subscription_ = this->create_subscription<sensor_msgs::msg::LaserScan>("/scan", 10, std::bind(&WallFollow::scan_callback, this, _1));” it works.
What am I missing?

We’ll look into that for you.

Hi @Simo97 ,

I just read through your code. I know of two reasons why you are having this subscription error (rosidl_typesupport).

  1. You might have forgotten to include sensor_msgs in to your package dependency (in your CMakeLists.txt and package.xml files).
    EDIT: I was correct - you don’t have sensor_msgs in your CMakeLists.txt. Also add that in package.xml file.
  2. You have forgotten to define the QoS for the laser scanner subscriber (so your program defaults to system settings - which probably does not match with sensor QoS).

You can get the laser scanner QoS info by doing ros2 topic info /scan -v.
You can get info on scan QoS such as history depth, reliability, durability and liveliness.

Once you have fixed the issues try to run your program. If you still have the issues let us know!

Regards,
Girish

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.