feat: add all code
This commit is contained in:
37
chapt10/chapt10_ws/src/learn_qos_cpp/CMakeLists.txt
Normal file
37
chapt10/chapt10_ws/src/learn_qos_cpp/CMakeLists.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
cmake_minimum_required(VERSION 3.8)
|
||||
project(learn_qos_cpp)
|
||||
|
||||
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(nav_msgs REQUIRED)
|
||||
|
||||
add_executable(reliability_test src/reliability_test.cpp)
|
||||
ament_target_dependencies(reliability_test
|
||||
rclcpp nav_msgs
|
||||
)
|
||||
|
||||
install(TARGETS reliability_test
|
||||
DESTINATION lib/${PROJECT_NAME})
|
||||
|
||||
ament_package()
|
||||
18
chapt10/chapt10_ws/src/learn_qos_cpp/package.xml
Normal file
18
chapt10/chapt10_ws/src/learn_qos_cpp/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_qos_cpp</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,56 @@
|
||||
#include <nav_msgs/msg/odometry.hpp>
|
||||
#include <rclcpp/rclcpp.hpp>
|
||||
|
||||
class OdomPublisherSubscriber : public rclcpp::Node
|
||||
{
|
||||
public:
|
||||
OdomPublisherSubscriber() : Node("odom_publisher_subscriber")
|
||||
{
|
||||
rclcpp::QoS qos_profile(10); // 队列深度为10
|
||||
qos_profile.reliability(RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT); // 可靠性策略
|
||||
qos_profile.durability(RMW_QOS_POLICY_DURABILITY_TRANSIENT_LOCAL); // 持久性策略
|
||||
qos_profile.history(RMW_QOS_POLICY_HISTORY_KEEP_LAST); // 历史记录策略
|
||||
qos_profile.deadline(rclcpp::Duration(1, 0)); // 截止时间为1秒
|
||||
|
||||
odom_publisher_ = this->create_publisher<nav_msgs::msg::Odometry>(
|
||||
"odom", qos_profile);
|
||||
|
||||
// // 创建发布者并设置QoS为sensor
|
||||
// odom_publisher_ = this->create_publisher<nav_msgs::msg::Odometry>(
|
||||
// "odom", rclcpp::SensorDataQoS());
|
||||
|
||||
// 创建订阅者(默认QoS配置)队列深度设置为 5
|
||||
// odom_subscription_ = this->create_subscription<nav_msgs::msg::Odometry>(
|
||||
// "odom", 5,
|
||||
// [this](const nav_msgs::msg::Odometry::SharedPtr msg) {
|
||||
// (void)msg;
|
||||
// RCLCPP_INFO(this->get_logger(), "收到里程计消息");
|
||||
// });
|
||||
|
||||
odom_subscription_ = this->create_subscription<nav_msgs::msg::Odometry>(
|
||||
"odom", rclcpp::SensorDataQoS(),
|
||||
[this](const nav_msgs::msg::Odometry::SharedPtr msg)
|
||||
{
|
||||
(void)msg;
|
||||
RCLCPP_INFO(this->get_logger(), "收到里程计消息");
|
||||
});
|
||||
|
||||
// 创建一个1秒的定时器,并指定回调函数
|
||||
timer_ = this->create_wall_timer(std::chrono::seconds(1), [this]()
|
||||
{ odom_publisher_->publish(nav_msgs::msg::Odometry()); });
|
||||
}
|
||||
|
||||
private:
|
||||
rclcpp::Publisher<nav_msgs::msg::Odometry>::SharedPtr odom_publisher_;
|
||||
rclcpp::Subscription<nav_msgs::msg::Odometry>::SharedPtr odom_subscription_;
|
||||
rclcpp::TimerBase::SharedPtr timer_;
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
rclcpp::init(argc, argv);
|
||||
auto odom_node = std::make_shared<OdomPublisherSubscriber>();
|
||||
rclcpp::spin(odom_node);
|
||||
rclcpp::shutdown();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user