赞
踩
Autoware.Auto是在ROS2上开发的一个自动驾驶平台。其基本的运行单位与ROS2相同为节点。一个节点相当于一个完整的应用,大部分通过“RCLCPP_COMPONENTS_REGISTER_NODE”组件编译,循环执行。Autoware中绝大多数节点采用的是事件驱动,即订阅事件触发整个应用进行计算。此处以开始的point_cloud_filter_transform_nodes为例,将其改为周期性的时间触发机制。
- class POINT_CLOUD_FILTER_TRANSFORM_NODES_PUBLIC PointCloud2FilterTransformNode
- : public rclcpp::Node
- {
- public:
- /// \brief Parameter constructor
- /// \param node_options Additional options to control creation of the node.
- explicit PointCloud2FilterTransformNode(const rclcpp::NodeOptions & node_options);
-
- protected:
- /// \brief Call distance & angle filter and then static transformer for all the points
- /// \param msg Raw point cloud
- /// \return Filtered and Transformed point cloud.
- /// \throws std::runtime_error on unexpected input contents or not enough output capacity
- const PointCloud2 & filter_and_transform(const PointCloud2 & msg);
-
- /// \brief Run main subscribe -> filter & transform -> publish loop
- void process_filtered_transformed_message(
- const PointCloud2::SharedPtr msg);
-
- template<typename PointType>
- /// \brief Check if the point is within the specified angle and radius limits
- /// \tparam PointType type with x, y, z.
- /// \param pt point with x, y, z
- /// \return True if the point is within the desired radius and angle limits. False otherwise.
- bool8_t point_not_filtered(const PointType & pt) const
- {
- return m_angle_filter(pt) && m_distance_filter(pt);
- }
-
- template<typename PointType>
- /// \brief Apply static transform to the given point
- /// \tparam PointType type with x, y, z
- /// \param pt point with x, y, z
- /// \return Transformed point
- PointType transform_point(const PointType & pt) const
- {
- PointType pt_final;
- m_static_transformer->transform(pt, pt_final);
- return pt_final;
- }
-
- private:
- using AngleFilter = autoware::common::lidar_utils::AngleFilter;
- using DistanceFilter = autoware::common::lidar_utils::DistanceFilter;
- using StaticTransformer = autoware::common::lidar_utils::StaticTransformer;
- AngleFilter m_angle_filter;
- DistanceFilter m_distance_filter;
- const std::string m_input_frame_id;
- const std::string m_output_frame_id;
- std::unique_ptr<StaticTransformer> m_static_transformer;
- const std::chrono::nanoseconds m_init_timeout;
- const std::chrono::nanoseconds m_timeout;
- const typename rclcpp::Subscription<PointCloud2>::SharedPtr m_sub_ptr;
- const typename std::shared_ptr<rclcpp::Publisher<PointCloud2>> m_pub_ptr;
- const size_t m_expected_num_publishers;
- const size_t m_expected_num_subscribers;
- const std::size_t m_pcl_size;
- PointCloud2 m_filtered_transformed_msg;
- };

其继承自rclcpp::Node父类。改成时间驱动首先要添加头文件
- #include <chrono>
- #include <functional>
其次在私有属性里添加计时器timer_,私有方法里添加time_callback回调函数声明。
- const rclcpp::TimerBase::SharedPtr timer_;
-
-
- void time_callback();
实现时在PointCloud2FilterTransformNode的构造函数里,添加timer的初始化。
- PointCloud2FilterTransformNode::PointCloud2FilterTransformNode(
- const rclcpp::NodeOptions & node_options)
- : Node("point_cloud_filter_transform_node", node_options),
- m_angle_filter{
- static_cast<float32_t>(declare_parameter("start_angle").get<float64_t>()),
- static_cast<float32_t>(declare_parameter("end_angle").get<float64_t>())},
- m_distance_filter{
- static_cast<float32_t>(declare_parameter("min_radius").get<float64_t>()),
- static_cast<float32_t>(declare_parameter("max_radius").get<float64_t>())},
- m_input_frame_id{declare_parameter("input_frame_id").get<std::string>()},
- m_output_frame_id{declare_parameter("output_frame_id").get<std::string>()},
- m_init_timeout{std::chrono::milliseconds{declare_parameter("init_timeout_ms").get<int32_t>()}},
- m_sub_ptr{create_subscription<PointCloud2>(
- "points_in", rclcpp::QoS{10},
- std::bind(
- &PointCloud2FilterTransformNode::process_filtered_transformed_message, this, _1))},
- m_timeout{std::chrono::milliseconds{declare_parameter("timeout_ms").get<int32_t>()}},
- m_pub_ptr{create_publisher<PointCloud2>("points_filtered", rclcpp::QoS{10})},
- m_expected_num_publishers{
- static_cast<size_t>(declare_parameter("expected_num_publishers").get<int32_t>())},
- m_expected_num_subscribers{
- static_cast<size_t>(declare_parameter("expected_num_subscribers").get<int32_t>())},
- m_pcl_size{static_cast<size_t>(declare_parameter("pcl_size").get<int32_t>())},
- timer_{create_wall_timer(200ms, std::bind(&PointCloud2FilterTransformNode::time_callback,this))}

把订阅和发布的初始化放到时间回调函数time_callback中
- void PointCloud2FilterTransformNode::time_callback()
- {
- m_pub_ptr=this->create_publisher<PointCloud2>("points_filtered", rclcpp::QoS{10});
- m_sub_ptr=this->create_subscription<PointCloud2>(
- "points_in", rclcpp::QoS{10},
- std::bind(
- &PointCloud2FilterTransformNode::process_filtered_transformed_message, this, _1));
- }
由此便实现了将节点改为以时间周期驱动的节点。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。