#include <chrono>
#include <functional>
#include <memory>

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

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

class Sample{
    public:
        Sample(): ranges_size(0){};
        void LaserScanCallback(const sensor_msgs::msg::LaserScan::SharedPtr _msg){
            this->ranges_size = _msg->ranges.size();
            std::cout << this->ranges_size << std::endl;
        }
    private:
        unsigned int ranges_size;
};
class MovingRobot: public rclcpp::Node{
    public:
        MovingRobot(): Node("moving_robot"){
            sample_ptr_ = std::make_shared<Sample>();

            laserscan_subscription_ = this->create_subscription<sensor_msgs::msg::LaserScan>(
                "/scan", rclcpp::QoS(rclcpp::SystemDefaultsQoS()),
                std::bind(&Sample::LaserScanCallback, sample_ptr_.get(), _1));
        }
    private:
        rclcpp::Subscription<sensor_msgs::msg::LaserScan>::SharedPtr laserscan_subscription_;

        std::shared_ptr<Sample> sample_ptr_;
};

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

    return 0;
}

핵심은 subscription callback 등록할때 bind 함수에서 this 포인터 대신 클래스의 포인터를 등록

'etc' 카테고리의 다른 글

Microservice Architecture on Robotics  (2) 2025.02.04
How to set log level  (0) 2025.02.04
colcon graph  (0) 2025.01.16
Python Package Offline Install  (0) 2024.01.18
Fix client coordinates and set camera projection in Gazebo  (0) 2023.11.20

+ Recent posts