Don't see scan message on Rviz even though it's published [closed]
I'm trying to solve this puzzle. It seems to be simple but it doesn't work.
So, the OdomNode is subscribed to the original LaserScan message on /sick_lrs_36x1/scan topic. It receives the message and call LaserScan publisher to publish the message on /scan topic. A customized message is published with a new frame_id and with now timestamp.
The original scan message is getting played using the bag tool.
Running
ros2 topic echo /sick_lrs_36x1/scan
seems to give exact message as
ros2 topic echo /scan message
Interestingly, I can observe the /sick_lrs_36x1/scan in rviz, but cannot see the customized message.
Below is my code:
class OdomNode : public rclcpp::Node
{
public:
OdomNode() : Node("odom")
{
scan_ = this->create_subscription<sensor_msgs::msg::LaserScan>("/sick_lrs_36x1/scan", rclcpp::SensorDataQoS(),
std::bind(&OdomNode::callbackPose, this, std::placeholders::_1));
publisher_ = this->create_publisher<sensor_msgs::msg::LaserScan>("scan", rclcpp::SensorDataQoS());
}
private:
void callbackPose(const sensor_msgs::msg::LaserScan::SharedPtr msg)
{
now = this->get_clock()->now();
sensor_msgs::msg::LaserScan laser;
laser.header.frame_id = "cloud";
laser.header.stamp = now;
laser.angle_min = msg->angle_min;
laser.angle_max = msg->angle_max;
laser.angle_increment = msg->angle_increment;
laser.scan_time = msg->scan_time;
laser.time_increment = msg->time_increment;
laser.range_min = msg->range_min;
laser.range_max = msg->range_max;
laser.ranges = msg->ranges;
publisher_->publish(laser);
}
rclcpp::Time now;
rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr scan_;
rclcpp::Publisher<sensor_msgs::msg::LaserScan>::SharedPtr publisher_;
rclcpp::TimerBase::SharedPtr transform_timer_;
};
int main(int argc, char **argv)
{
rclcpp::init(argc, argv);
auto node = std::make_shared<OdomNode>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}