Commit 90c5a3c2 authored by CANNERE Xavier's avatar CANNERE Xavier
Browse files

modif v1 topic5

parent bc64e198
......@@ -38,7 +38,7 @@ This will define the namespace by default::
Producer()
: Node("producer"), count_(0)
{
publisher_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
producer_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
timer_ = this->create_wall_timer(500ms, std::bind(&Producer::timer_callback, this));
}
......@@ -48,12 +48,12 @@ This is the constructor of the class, the topics are created here::
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);
message.data = "value : " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Send: '%s'", message.data.c_str());
producer_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr publisher_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr producer_;
size_t count_;
};
......@@ -72,7 +72,7 @@ This is the main function.
Step 3 : Create the consumer
----------------------------
The consumer is a listener who'll collect information from the publisher
The consumer is a listener who'll collect information from the producer
Create new node called consumer.cpp and paste these code::
......@@ -91,18 +91,18 @@ Here are define and declared external nodes used in this node::
Consumer()
: Node("consumer")
{
subscription_ = this->create_subscription<std_msgs::msg::String>(
consumer_ = 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:
private:
void topic_callback(const std_msgs::msg::String & msg) const
{
RCLCPP_INFO(this->get_logger(), "I heard: '%s'", msg.data.c_str());
RCLCPP_INFO(this->get_logger(), "Receive: '%s'", msg.data.c_str());
}
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr subscription_;
rclcpp::Subscription<std_msgs::msg::String>::SharedPtr consumer_;
};
This is the private method of the topic::
......@@ -120,14 +120,69 @@ This is the main function.
Step 4 : Test it
----------------
Before test it, verify your CMakeLists seems like that::
cmake_minimum_required(VERSION 3.5)
project(pkg_producer)
# Default to C++14
if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(producer src/producer.cpp)
ament_target_dependencies(producer rclcpp std_msgs)
add_executable(consumer src/consumer.cpp)
ament_target_dependencies(consumer rclcpp std_msgs)
install(TARGETS
producer
consumer
DESTINATION lib/${PROJECT_NAME})
ament_package()
And your package.xml like that::
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>pkg_producer</name>
<version>0.0.0</version>
<description>TODO: Package description</description>
<maintainer email="Xavier.CANNERE@isae-supaero.fr">etdisc</maintainer>
<license>TODO: License declaration</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
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
ros2 run pkg_producer consumer
Nothing happend, don't care, the listener need the publisher to work. Open a new terminal and type these lines::
Nothing happend, don't care, the consumer need the producer to work. Open a new terminal and type these lines::
cd ~/colcon_ws
. install/setup.bash
ros2 run pkg_producer talker
ros2 run pkg_producer producer
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