Commit ef9ff632 authored by CANNERE Xavier's avatar CANNERE Xavier
Browse files

topic5 final

parent 5cd1f9a4
......@@ -129,7 +129,7 @@ And these lines define the service on the package.xml::
Step 3 : Create the producer
----------------------------
The producer is the node who'll publish information
The producer is the node who'll publish information.
Open QtCreator, create a new file called ``producer.cpp`` into ``/src`` of ``pkg_producer`` and paste block by block::
......@@ -143,9 +143,18 @@ Open QtCreator, create a new file called ``producer.cpp`` into ``/src`` of ``pk
#include "std_msgs/msg/int64.hpp"
#include "pkg_srv/srv/raz.hpp"
This is part of the code where we include libraries::
This is part of the code where we include libraries and global variables::
using namespace std::chrono_literals;
size_t count_;
rclcpp::Service<pkg_srv::srv::Raz>::SharedPtr raz_;
This is the "raz" function who allow the reset of counter::
void raz(const std::shared_ptr<pkg_srv::srv::Raz::Request> request, std::shared_ptr<pkg_srv::srv::Raz::Response> response)
{
count_ = request->a;
}
This will define the namespace by default::
......@@ -153,10 +162,11 @@ This will define the namespace by default::
{
public:
Producer()
: Node("producer"), count_(0)
: Node("producer")
{
producer_ = this->create_publisher<std_msgs::msg::String>("topic", 10);
timer_ = this->create_wall_timer(500ms, std::bind(&Producer::timer_callback, this));
producer_ = this->create_publisher<std_msgs::msg::Int64>("topic", 10);
timer_ = this->create_wall_timer(2000ms, std::bind(&Producer::timer_callback, this));
raz_ = this->create_service<pkg_srv::srv::Raz>("raz", &raz);
}
This is the constructor of the class, the topics are created here::
......@@ -164,14 +174,14 @@ This is the constructor of the class, the topics are created here::
private:
void timer_callback()
{
auto message = std_msgs::msg::String();
message.data = "value : " + std::to_string(count_++);
RCLCPP_INFO(this->get_logger(), "Send: '%s'", message.data.c_str());
auto message = std_msgs::msg::Int64();
message.data = count_++;
RCLCPP_INFO(this->get_logger(), "Send: '%ld'", message.data);
producer_->publish(message);
}
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<std_msgs::msg::String>::SharedPtr producer_;
size_t count_;
rclcpp::Publisher<std_msgs::msg::Int64>::SharedPtr producer_;
};
The method timer_callback put a message in the console, it is called in the constructor for the topic timer::
......@@ -184,12 +194,12 @@ The method timer_callback put a message in the console, it is called in the cons
return 0;
}
This is the main function.
This is the main function who call the constructor.
Step 3 : Create the consumer
Step 4 : Create the consumer
----------------------------
The consumer is a listener who'll collect information from the producer
The consumer is a listener who'll collect information from the producer. He don't has a link with the service.
Create new node called consumer.cpp and paste these code::
......@@ -232,34 +242,31 @@ This is the private method of the topic::
return 0;
}
This is the main function.
This is the main function who call the constructor.
Step 4 : Test it
----------------
Before test it, verify your CMakeLists seems like that::
Now edit the CMakeLists of your ``pkg_producer``::
cmake_minimum_required(VERSION 3.5)
cmake_minimum_required(VERSION 3.8)
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 dependencies
find_package(ament_cmake REQUIRED)
# uncomment the following section in order to fill in
# further dependencies manually.
# find_package(<dependency> REQUIRED)
find_package(rclcpp REQUIRED)
find_package(std_msgs REQUIRED)
add_executable(producer src/producer.cpp)
ament_target_dependencies(producer rclcpp std_msgs)
find_package(pkg_srv REQUIRED)
add_executable(consumer src/consumer.cpp)
ament_target_dependencies(consumer rclcpp std_msgs)
ament_target_dependencies(consumer rclcpp std_msgs pkg_srv)
add_executable(producer src/producer.cpp)
ament_target_dependencies(producer rclcpp std_msgs pkg_srv)
install(TARGETS
producer
......@@ -268,31 +275,37 @@ Before test it, verify your CMakeLists seems like that::
ament_package()
And your package.xml like that::
And your package.xml::
<?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>
<name>pkg_consumer</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>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<depend>rclcpp</depend>
<depend>std_msgs</depend>
<depend>pkg_srv</depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<test_depend>ament_lint_auto</test_depend>
<test_depend>ament_lint_common</test_depend>
<export>
<build_type>ament_cmake</build_type>
</export>
<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::
You can now ``colcon build``.
Step 5 : Test it
----------------
Your nodes are ready, don't forget to source your workspace and run the topics::
cd ~/colcon_ws
. install/setup.bash
......@@ -303,3 +316,14 @@ Nothing happend, don't care, the consumer need the producer to work. Open a new
cd ~/colcon_ws
. install/setup.bash
ros2 run pkg_producer producer
Type that on a new terminal to see the input and output of "Raz" service::
. install/setup.bash
ros2 interface show pkg_srv/srv/Raz
And if you want reset the counter type that::
ros2 service call /raz pkg_srv/srv/Raz "{a: 0}"
And you can see the counter is reset, you can change the value of ``a`` to other value, i let you try it.
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