ROS 系列学习教程(总目录)
ROS2 系列学习教程(总目录)
目录
- 1. 构造函数
- 2. 节点名称相关
- 3. 获取log对象句柄
- 4. 回调组相关
- 5. Topic发布与订阅
- 6. Service服务端与客户端
1. 构造函数
public:Node(const std::string & node_name, const NodeOptions & options = NodeOptions());Node(const std::string & node_name, const std::string & namespace_, const NodeOptions & options = NodeOptions());
protected:Node(const Node & other, const std::string & sub_namespace);
2. 节点名称相关
获取节点名称
const char *get_name() const;
获取节点名空间
const char *get_namespace() const;
获取包含名空间和名称的全名
const char *get_fully_qualified_name() const;
获取当前 ROS 2 系统中所有活跃节点的名称列表
std::vector<std::string> get_node_names() const;
获取 ROS 2 系统中当前活跃的 主题(Topic)和 服务(Service)的名称及其关联的消息或服务类型
std::map<std::string, std::vector<std::string>> get_topic_names_and_types() const;
std::map<std::string, std::vector<std::string>> get_service_names_and_types() const;
获取指定节点提供的所有服务名称及其对应的服务类型列表
std::map<std::string, std::vector<std::string>>
get_service_names_and_types_by_node(const std::string & node_name,const std::string & namespace_) const;
3. 获取log对象句柄
rclcpp::Logger get_logger() const;
4. 回调组相关
回调组是ROS 2中管理回调函数执行的重要机制,它允许开发者控制不同回调之间的执行关系。(后面会详细介绍)
创建并返回一个回调组指针
rclcpp::CallbackGroup::SharedPtr create_callback_group(// 指定回调组的类型,枚举值,决定回调组如何被调度执行rclcpp::CallbackGroupType group_type,// 决定是否自动将这个回调组添加到与节点关联的执行器(executor)中bool automatically_add_to_executor_with_node = true);
遍历节点中的所有回调组,并对每个有效的回调组执行给定的函数
void for_each_callback_group(const node_interfaces::NodeBaseInterface::CallbackGroupFunction & func);
5. Topic发布与订阅
创建并返回一个发布者指针
std::shared_ptr<PublisherT>
create_publisher(const std::string & topic_name,const rclcpp::QoS & qos,const PublisherOptionsWithAllocator<AllocatorT> & options =PublisherOptionsWithAllocator<AllocatorT>()
);
参数说明:
topic_name
是topic的名字
qos
是服务质量策略设置(后面会详细介绍)
options
高级选项配置。(高级应用场景)
使用示例:
auto node = std::make_shared<rclcpp::Node>("my_node");
// 创建一个发布者,发布 String 类型消息到 "/chatter" 主题
auto publisher = node->create_publisher<std_msgs::msg::String>("/chatter",rclcpp::QoS(10).reliable() // QoS 配置// 使用默认的 options
);
创建并返回一个订阅者指针
std::shared_ptr<SubscriptionT>
create_subscription(const std::string & topic_name,const rclcpp::QoS & qos,CallbackT && callback,const SubscriptionOptionsWithAllocator<AllocatorT> & options =SubscriptionOptionsWithAllocator<AllocatorT>(),typename MessageMemoryStrategyT::SharedPtr msg_mem_strat = (MessageMemoryStrategyT::create_default())
);
参数说明:
topic_name
是topic的名字
qos
是服务质量策略设置(后面会详细介绍)
callback
是回调函数。
options
高级选项配置。(高级应用场景)
msg_mem_strat
用于设置消息内存策略。(高级应用场景)
使用示例:
auto node = std::make_shared<rclcpp::Node>("subscriber_node");
auto callback = [](const std_msgs::msg::String::SharedPtr msg) { // Lambda 回调RCLCPP_INFO(node->get_logger(), "Received: %s", msg->data.c_str());
};// 创建一个订阅者,接收 String 类型消息
auto subscription = node->create_subscription<std_msgs::msg::String>("/chatter",rclcpp::QoS(10),callback// 使用默认的 options 和 msg_mem_strat
);
统计当前订阅到指定主题(Topic)的发布者(Publisher)数量
size_t count_publishers(const std::string & topic_name) const;
使用场景
-
动态拓扑监控
- 检查某个主题是否有发布者,避免订阅“无人发布”的主题。
- 示例:机器人传感器主题
/lidar/data
的发布者是否在线。
-
条件性逻辑
- 根据发布者数量决定是否启用某些功能。
if (node->count_publishers("/cmd_vel") > 0) {// 安全地订阅 /cmd_vel,因为有发布者存在 }
-
调试与日志
- 记录主题的发布者数量变化,诊断通信问题。
统计当前有多少订阅者(Subscribers)正在订阅指定的主题(Topic)
size_t count_subscribers(const std::string & topic_name) const;
使用场景
-
动态发布决策
- 检查是否有订阅者,避免无意义的数据发布(节省资源)
if (node->count_subscribers("/sensor_data") > 0) {publisher->publish(data); // 只在有订阅者时发布 }
-
调试与监控
- 诊断通信问题(例如订阅者未正确连接)。
- 日志记录订阅者数量变化。
-
条件性初始化
- 延迟初始化资源密集型组件,直到有订阅者存在。
6. Service服务端与客户端
创建Service客户端
template<typename ServiceT>
typename rclcpp::Client<ServiceT>::SharedPtr
create_client(const std::string & service_name,const rmw_qos_profile_t & qos_profile = rmw_qos_profile_services_default,rclcpp::CallbackGroup::SharedPtr group = nullptr);
参数说明:
service_name
:服务名称
qos_profile
:服务质量(QoS)配置,默认值为 rmw_qos_profile_services_default
(ROS 2 默认的服务 QoS)
group
:回调组的智能指针,默认值为 nullptr
使用示例:
// 假设有一个服务类型:example_interfaces::srv::AddTwoInts
auto client = node->create_client<example_interfaces::srv::AddTwoInts>("/add_two_ints");// 发送请求
auto request = std::make_shared<example_interfaces::srv::AddTwoInts::Request>();
request->a = 2;
request->b = 3;
auto future = client->async_send_request(request);
创建Service服务端
template<typename ServiceT, typename CallbackT>
typename rclcpp::Service<ServiceT>::SharedPtr
create_service(const std::string & service_name,CallbackT && callback,const rmw_qos_profile_t & qos_profile = rmw_qos_profile_services_default,rclcpp::CallbackGroup::SharedPtr group = nullptr);
参数说明:
service_name
:服务名称
callback
:Service的处理函数
qos_profile
:服务质量(QoS)配置,默认值为 rmw_qos_profile_services_default
(ROS 2 默认的服务 QoS)
group
:回调组的智能指针,默认值为 nullptr
使用示例:
auto node = std::make_shared<rclcpp::Node>("my_node");// 定义回调函数
auto callback = [](const std::shared_ptr<example_interfaces::srv::AddTwoInts::Request> request,std::shared_ptr<example_interfaces::srv::AddTwoInts::Response> response
) {response->sum = request->a + request->b;
};// 创建服务
auto service = node->create_service<example_interfaces::srv::AddTwoInts>("/add_two_ints", callback);