ROS2架构和实现

ROS2是机器人领域的著名SDK,可以加速学习效率。ROS2主要解决了两个问题: 通信:基于Node,包含topic、service、action通信方式 代码架构:模块化开发,简化维护 一、架构 userland: code client: rclc rclcpp rclpy etc… abstract: rcl middleware: rmw rmw_implementation rmw_fastrtps etc … dds: fastdds Context_RTI etc… 二、实现 ROS2代码惯例: ./ ../ include/... # 头文件 src/... # 实现 1、Node实现 推荐继承Node实现业务功能, 主要选择合理的通信方式,注册回调函数。 namespace palomino { class VincentDriver : public rclcpp::Node { // ... }; } int main(int argc, char * argv[]) { rclcpp::init(argc, argv); rclcpp::spin(std::make_shared<palomino::VincentDriver>()); rclcpp::shutdown(); return 0; } Node的声明: // node.hpp class Node : public std::enable_shared_from_this<Node> { public: explicit Node( const std::string & node_name, const NodeOptions & options = NodeOptions()); explicit Node( const std::string & node_name, const std::string & namespace_, const NodeOptions & options = NodeOptions()); virtual ~Node(); // 创建发布者 template< typename MessageT, typename AllocatorT = std::allocator<void>, typename PublisherT = rclcpp::Publisher<MessageT, AllocatorT>> std::shared_ptr<PublisherT> create_publisher( const std::string & topic_name, const rclcpp::QoS & qos, const PublisherOptionsWithAllocator<AllocatorT> & options = PublisherOptionsWithAllocator<AllocatorT>() ); // 创建订阅者 template< typename MessageT, typename CallbackT, typename AllocatorT = std::allocator<void>, typename SubscriptionT = rclcpp::Subscription<MessageT, AllocatorT>, typename MessageMemoryStrategyT = typename SubscriptionT::MessageMemoryStrategyType > 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() ) ); // 创建service的客户端 template<typename ServiceT> typename rclcpp::Client<ServiceT>::SharedPtr create_client( const std::string & service_name, const rclcpp::QoS & qos = rclcpp::ServicesQoS(), rclcpp::CallbackGroup::SharedPtr group = nullptr); // 创建service的服务端 template<typename ServiceT, typename CallbackT> typename rclcpp::Service<ServiceT>::SharedPtr create_service( const std::string & service_name, CallbackT && callback, const rclcpp::QoS & qos = rclcpp::ServicesQoS(), rclcpp::CallbackGroup::SharedPtr group = nullptr); // 参数相关操作方法 // 其他服务方法 protected: Node( const Node & other, const std::string & sub_namespace); private: rclcpp::node_interfaces::NodeBaseInterface::SharedPtr node_base_; rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph_; rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr node_logging_; rclcpp::node_interfaces::NodeTimersInterface::SharedPtr node_timers_; rclcpp::node_interfaces::NodeTopicsInterface::SharedPtr node_topics_; rclcpp::node_interfaces::NodeServicesInterface::SharedPtr node_services_; rclcpp::node_interfaces::NodeClockInterface::SharedPtr node_clock_; rclcpp::node_interfaces::NodeParametersInterface::SharedPtr node_parameters_; rclcpp::node_interfaces::NodeTimeSourceInterface::SharedPtr node_time_source_; rclcpp::node_interfaces::NodeTypeDescriptionsInterface::SharedPtr node_type_descriptions_; rclcpp::node_interfaces::NodeWaitablesInterface::SharedPtr node_waitables_; const rclcpp::NodeOptions node_options_; const std::string sub_namespace_; const std::string effective_namespace_; class NodeImpl; // This member is meant to be a place to backport features into stable distributions, // and new features targeting Rolling should not use this. // See the comment in node.cpp for more information. std::shared_ptr<NodeImpl> hidden_impl_{nullptr}; }; 2、启动 上面的代码示例中,main函数调用了三个ROS2 C++客户端库的函数: ...

2025-05-07 · Jerry Wang