Commit 89cc0701 authored by CANNERE Xavier's avatar CANNERE Xavier
Browse files

ajout topic5

parent 2036e077
......@@ -13,4 +13,5 @@ Welcome to my ROS2 Tutorial!
./topic2/t2.rst
./topic3/t3.rst
./topic4/t4.rst
./topic5/t5.rst
......@@ -235,3 +235,8 @@ And CMakeLists.txt by that::
ament_package()
Now you can ``colcon build`` in the root of your workspace and your follower node compile correctly.
Step 4 : Create the launcher
----------------------------
Producer / Consumer
===================
Step 1 : Create the package
---------------------------
First we'll create the package with these following lines::
cd ~/colcon_ws/src
ros2 pkg create --build-type ament_cmake --node-name follower pkg_follower
Step 2 : Create the producer
----------------------------
Open QtCreator, create a new file called ``producer.cpp`` into ``/src`` of ``pkg_producer`` and paste block by block::
#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
This is part of the code where we include libraries::
using namespace std::chrono_literals;
This will define the namespace by default::
class Producer : public rclcpp::Node
{
public:
Producer()
: Node("producer"), count_(0)
{
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
timer_ = this->create_wall_timer(500ms, std::bind(&Producer::timer_callback, this));
}
This is the constructor of the class, the topics are created here::
private:
void timer_callback()
{
auto message = std_msgs::msg::String();
message.data = "echo : " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Publishing: '%s'", message.data.c_str());
publisher_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
size_t count_;
};
The method timer_callback put a message in the console, it is called in the constructor for the topic timer::
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<Producer>());
rclcpp::shutdown();
return 0;
}
This is the main function.
Step 3 : Create the consumer
----------------------------
Create new node called consumer.cpp and paste these code::
#include <memory>
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
using std::placeholders::_1;
Here are define and declared external nodes used in this node::
class Consumer : public rclcpp::Node
{
public:
Consumer()
: Node("consumer")
{
subscription_ = this->create_subscription<std_msgs::msg::String>(
"topic", 10, std::bind(&Consumer::topic_callback, this, _1));
}
This is the constructor who create one topic in subscription::
private:
void topic_callback(const std_msgs::msg::String & msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
};
This is the private method of the topic::
int main(int argc, char * argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<Consumer>());
rclcpp::shutdown();
return 0;
}
This is the main function.
Step 4 : Test it
----------------
Don't forget to build nodes and in the terminal write these lines to test your nodes::
cd ~/colcon_ws
. install/setup.bash
ros2 run pkg_producer listener
Nothing happend, don't care, the listener need the publisher to work. Open a new terminal and type these lines::
cd ~/colcon_ws
. install/setup.bash
ros2 run pkg_producer talker
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment