feat: add all code
This commit is contained in:
56
chapt10/chapt10_ws/src/learn_compose/CMakeLists.txt
Normal file
56
chapt10/chapt10_ws/src/learn_compose/CMakeLists.txt
Normal file
@@ -0,0 +1,56 @@
|
||||
cmake_minimum_required(VERSION 3.8)
|
||||
project(learn_compose)
|
||||
|
||||
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)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
find_package(ament_lint_auto REQUIRED)
|
||||
# the following line skips the linter which checks for copyrights
|
||||
# comment the line when a copyright and license is added to all source files
|
||||
set(ament_cmake_copyright_FOUND TRUE)
|
||||
# the following line skips cpplint (only works in a git repo)
|
||||
# comment the line when this package is in a git repo and when
|
||||
# a copyright and license is added to all source files
|
||||
set(ament_cmake_cpplint_FOUND TRUE)
|
||||
ament_lint_auto_find_test_dependencies()
|
||||
endif()
|
||||
|
||||
find_package(rclcpp REQUIRED)
|
||||
find_package(std_msgs REQUIRED)
|
||||
find_package(rclcpp_components REQUIRED)
|
||||
include_directories(include)
|
||||
add_executable(intra_process_pubsub
|
||||
src/intra_process_pubsub.cpp
|
||||
src/talker.cpp
|
||||
src/listener.cpp
|
||||
)
|
||||
ament_target_dependencies(intra_process_pubsub std_msgs rclcpp rclcpp_components)
|
||||
|
||||
install(TARGETS intra_process_pubsub
|
||||
DESTINATION lib/${PROJECT_NAME})
|
||||
|
||||
|
||||
find_package(rclcpp_components REQUIRED)
|
||||
add_library(talker_component SHARED src/talker.cpp)
|
||||
ament_target_dependencies(talker_component "std_msgs" "rclcpp" "rclcpp_components")
|
||||
rclcpp_components_register_nodes(talker_component "learn_compose::Talker")
|
||||
|
||||
add_library(listener_component SHARED src/listener.cpp)
|
||||
ament_target_dependencies(listener_component "std_msgs" "rclcpp" "rclcpp_components")
|
||||
rclcpp_components_register_nodes(listener_component "learn_compose::Listener")
|
||||
|
||||
install(TARGETS talker_component listener_component
|
||||
ARCHIVE DESTINATION lib
|
||||
LIBRARY DESTINATION lib
|
||||
RUNTIME DESTINATION bin
|
||||
)
|
||||
|
||||
ament_package()
|
||||
@@ -0,0 +1,19 @@
|
||||
#ifndef LEARN_COMPOSE__LISTENER_COMPONENT_HPP_
|
||||
#define LEARN_COMPOSE__LISTENER_COMPONENT_HPP_
|
||||
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "std_msgs/msg/int32.hpp"
|
||||
|
||||
namespace learn_compose {
|
||||
|
||||
class Listener : public rclcpp::Node {
|
||||
public:
|
||||
explicit Listener(const rclcpp::NodeOptions &options);
|
||||
|
||||
private:
|
||||
rclcpp::Subscription<std_msgs::msg::Int32>::SharedPtr sub_;
|
||||
};
|
||||
|
||||
} // namespace learn_compose
|
||||
|
||||
#endif // LEARN_COMPOSE__LISTENER_COMPONENT_HPP_
|
||||
@@ -0,0 +1,21 @@
|
||||
#ifndef LEARN_COMPOSE__TALKER_COMPONENT_HPP_
|
||||
#define LEARN_COMPOSE__TALKER_COMPONENT_HPP_
|
||||
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
#include "std_msgs/msg/int32.hpp"
|
||||
|
||||
namespace learn_compose {
|
||||
|
||||
class Talker : public rclcpp::Node {
|
||||
public:
|
||||
explicit Talker(const rclcpp::NodeOptions &options);
|
||||
|
||||
private:
|
||||
int32_t count_;
|
||||
rclcpp::Publisher<std_msgs::msg::Int32>::SharedPtr pub_;
|
||||
rclcpp::TimerBase::SharedPtr timer_;
|
||||
};
|
||||
|
||||
} // namespace learn_compose
|
||||
|
||||
#endif // LEARN_COMPOSE__TALKER_COMPONENT_HPP_
|
||||
18
chapt10/chapt10_ws/src/learn_compose/package.xml
Normal file
18
chapt10/chapt10_ws/src/learn_compose/package.xml
Normal file
@@ -0,0 +1,18 @@
|
||||
<?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>learn_compose</name>
|
||||
<version>0.0.0</version>
|
||||
<description>TODO: Package description</description>
|
||||
<maintainer email="87068644+fishros@users.noreply.github.com">fishros</maintainer>
|
||||
<license>TODO: License declaration</license>
|
||||
|
||||
<buildtool_depend>ament_cmake</buildtool_depend>
|
||||
|
||||
<test_depend>ament_lint_auto</test_depend>
|
||||
<test_depend>ament_lint_common</test_depend>
|
||||
|
||||
<export>
|
||||
<build_type>ament_cmake</build_type>
|
||||
</export>
|
||||
</package>
|
||||
@@ -0,0 +1,21 @@
|
||||
#include "learn_compose/listener.hpp"
|
||||
#include "learn_compose/talker.hpp"
|
||||
#include "rclcpp/rclcpp.hpp"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
rclcpp::init(argc, argv);
|
||||
rclcpp::executors::SingleThreadedExecutor executor;
|
||||
|
||||
rclcpp::NodeOptions options; // 创建节点选项
|
||||
options.use_intra_process_comms(true); // 使用进程内通信
|
||||
auto talker = std::make_shared<learn_compose::Talker>(options);
|
||||
auto listener = std::make_shared<learn_compose::Listener>(options);
|
||||
|
||||
executor.add_node(talker);
|
||||
executor.add_node(listener);
|
||||
executor.spin();
|
||||
|
||||
rclcpp::shutdown();
|
||||
|
||||
return 0;
|
||||
}
|
||||
20
chapt10/chapt10_ws/src/learn_compose/src/listener.cpp
Normal file
20
chapt10/chapt10_ws/src/learn_compose/src/listener.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
#include "learn_compose/listener.hpp"
|
||||
#include <chrono>
|
||||
|
||||
namespace learn_compose {
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
Listener::Listener(const rclcpp::NodeOptions &options)
|
||||
: Node("listener", options) {
|
||||
sub_ = this->create_subscription<std_msgs::msg::Int32>(
|
||||
"count", 10, [&](const std_msgs::msg::Int32::UniquePtr msg) {
|
||||
RCLCPP_INFO(this->get_logger(), "收到数据:%d(0x%lX)", msg->data,
|
||||
reinterpret_cast<std::uintptr_t>(msg.get()));
|
||||
});
|
||||
}
|
||||
} // namespace learn_compose
|
||||
|
||||
|
||||
#include "rclcpp_components/register_node_macro.hpp"
|
||||
RCLCPP_COMPONENTS_REGISTER_NODE(learn_compose::Listener)
|
||||
24
chapt10/chapt10_ws/src/learn_compose/src/talker.cpp
Normal file
24
chapt10/chapt10_ws/src/learn_compose/src/talker.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <chrono>
|
||||
#include "learn_compose/talker.hpp"
|
||||
|
||||
namespace learn_compose {
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
Talker::Talker(const rclcpp::NodeOptions &options) : Node("talker", options) {
|
||||
pub_ = this->create_publisher<std_msgs::msg::Int32>("count", 10);
|
||||
auto callback = [&]() -> void {
|
||||
std_msgs::msg::Int32::UniquePtr msg(new std_msgs::msg::Int32());
|
||||
msg->data = count_++;
|
||||
RCLCPP_INFO(this->get_logger(), "发布数据:%d(0x%lX)", msg->data,
|
||||
reinterpret_cast<std::uintptr_t>(msg.get()));
|
||||
pub_->publish(std::move(msg));
|
||||
};
|
||||
timer_ = this->create_wall_timer(1s, callback);
|
||||
}
|
||||
} // namespace learn_compose
|
||||
|
||||
|
||||
|
||||
#include "rclcpp_components/register_node_macro.hpp"
|
||||
RCLCPP_COMPONENTS_REGISTER_NODE(learn_compose::Talker)
|
||||
Reference in New Issue
Block a user