Getting QoS error on running code [closed]
Hi everyone, I am trying to run obstacle avoidance cpp code in ROS 2 using turtlebot 3 in gazebo but on running the code I am getting the following error - [WARN] [1672798148.155781828] [obstacle_avoidance]: New publisher discovered on topic '/scan', offering incompatible QoS. No messages will be sent to it. Last incompatible policy: RELIABILITY_QOS_POLICY
Here is the entire script -
#include "rclcpp/rclcpp.hpp"
#include "geometry_msgs/msg/twist.hpp"
#include "sensor_msgs/msg/laser_scan.hpp"
using std::placeholders::_1;
class ObstacleAvoidance : public rclcpp::Node
{
public:
ObstacleAvoidance() : Node("obstacle_avoidance") {
velocity_publisher = this->create_publisher<geometry_msgs::msg::Twist>('/cmd_vel', 10);
auto default_qos = rclcpp::QoS(rclcpp::SystemDefaultsQoS());
subscription_ = this->create_subscription<sensor_msgs::msg::LaserScan>(
'/scan', default_qos,
std::bind(&ObstacleAvoidance::topic_callback, this, _1));
}
private:
void topic_callback(const sensor_msgs::msg::LaserScan::SharedPtr msg) {
int distToObstacle = 1;
auto move = geometry_msgs::msg::Twist();
if(msg->ranges[300] > distToObstacle) {
move.linear.x = 0.5;
move.angular.z = 0.0;
velocity_publisher->publish(move);
}
if(msg->ranges[300] <= distToObstacle) {
move.linear.x = 0.5;
move.angular.z = 3.5;
velocity_publisher->publish(move);
}
}
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr velocity_publisher;
rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr subscription_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<ObstacleAvoidance>());
rclcpp::shutdown();
return 0;
}
Can anybody tell me what is wrong in this script which is causing the above error?