Priceless

[ROS] ROS2 빌드 설정 파일 본문

ROS1 & ROS2

[ROS] ROS2 빌드 설정 파일

Hyun__ 2022. 11. 17. 15:47

 

 

 

helloworld_publisher.cpp

#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using namespace std::chrono_literals;

class HelloworldPublisher : public rclcpp::Node

{
	public:
    HelloworldPublisher() 
    : Node("helloworld_publisher"), count_(0) 
    {
    auto qos_profile = rclcpp::QoS(rclcpp::KeepLast(10));
    helloworld_publisher_ = this->create_publisher("helloworld", qos_profile);
    timer_ = this->create_wall_timer( 1s, std::bind(&HelloworldPublisher::publish_helloworld_msg, this)); }

	private: 
    void publish_helloworld_msg() 
    { 
        auto msg = std_msgs::msg::String();
        msg.data = "Hello world: " + std::to_string(count_++); 
        RCLCPP_INFO(this->get_logger(), "Publishing message: '%s'", msg.data.c_str());
        helloworld_publisher_->publish(msg); } rclcpp::TimerBase::SharedPtr timer_;
        rclcpp::Publisher::SharedPtr helloworld_publisher_; 
        size_t count_; 
    };
    int main(int argc, char * argv[]) 
    {
    	rclcpp::init(argc, argv); 
        auto node = std::make_shared(); 
        rclcpp::spin(node); 
        rclcpp::shutdown(); 
        return 0; 
     }

 

 

helloworld_subscriber.cpp

#include <functional>
#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1;

class HelloworldSubscriber : public rclcpp::Node
{
public:
HelloworldSubscriber()
: Node("Helloworld_subscriber")
{
auto qos_profile = rclcpp::QoS(rclcpp::KeepLast(10));
helloworld_subscriber_ = this->create_subscription<std_msgs::msg::String>(
"helloworld", qos_profile, std::bind(&HelloworldSubscriber::subscribe_topic_message, this, 
_1));
}
private:
void subscribe_topic_message(const std_msgs::msg::String::SharedPtr msg) const
{
RCLCPP_INFO(this->get_logger(), "Received message: '%s'", msg->data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr helloworld_subscriber_;
};
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
auto node = std::make_shared<HelloworldSubscriber>();
rclcpp::spin(node);
rclcpp::shutdown();
return 0;
}

'ROS1 & ROS2' 카테고리의 다른 글

[ROS1] ROS1 launch  (0) 2023.10.08
[ROS1] ROS1 Package(패키지)  (0) 2023.10.08
[ROS1] ROS1 Publish, Subscribe, Topic 이해하기(with turtlesim)  (1) 2023.10.08
[ROS1] ROS1 기초  (0) 2023.10.06
[ROS]Ubuntu 20.04 & ROS 2 Foxy 설치  (0) 2022.09.18