当前位置:   article > 正文

创建一个周期性执行的Autoware节点_autoware::common::

autoware::common::

1.介绍

Autoware.Auto是在ROS2上开发的一个自动驾驶平台。其基本的运行单位与ROS2相同为节点。一个节点相当于一个完整的应用,大部分通过“RCLCPP_COMPONENTS_REGISTER_NODE”组件编译,循环执行。Autoware中绝大多数节点采用的是事件驱动,即订阅事件触发整个应用进行计算。此处以开始的point_cloud_filter_transform_nodes为例,将其改为周期性的时间触发机制。

2.Point_Cloud_Filter_Transform_Nodes基本结构

  1. class POINT_CLOUD_FILTER_TRANSFORM_NODES_PUBLIC PointCloud2FilterTransformNode
  2. : public rclcpp::Node
  3. {
  4. public:
  5. /// \brief Parameter constructor
  6. /// \param node_options Additional options to control creation of the node.
  7. explicit PointCloud2FilterTransformNode(const rclcpp::NodeOptions & node_options);
  8. protected:
  9. /// \brief Call distance & angle filter and then static transformer for all the points
  10. /// \param msg Raw point cloud
  11. /// \return Filtered and Transformed point cloud.
  12. /// \throws std::runtime_error on unexpected input contents or not enough output capacity
  13. const PointCloud2 & filter_and_transform(const PointCloud2 & msg);
  14. /// \brief Run main subscribe -> filter & transform -> publish loop
  15. void process_filtered_transformed_message(
  16. const PointCloud2::SharedPtr msg);
  17. template<typename PointType>
  18. /// \brief Check if the point is within the specified angle and radius limits
  19. /// \tparam PointType type with x, y, z.
  20. /// \param pt point with x, y, z
  21. /// \return True if the point is within the desired radius and angle limits. False otherwise.
  22. bool8_t point_not_filtered(const PointType & pt) const
  23. {
  24. return m_angle_filter(pt) && m_distance_filter(pt);
  25. }
  26. template<typename PointType>
  27. /// \brief Apply static transform to the given point
  28. /// \tparam PointType type with x, y, z
  29. /// \param pt point with x, y, z
  30. /// \return Transformed point
  31. PointType transform_point(const PointType & pt) const
  32. {
  33. PointType pt_final;
  34. m_static_transformer->transform(pt, pt_final);
  35. return pt_final;
  36. }
  37. private:
  38. using AngleFilter = autoware::common::lidar_utils::AngleFilter;
  39. using DistanceFilter = autoware::common::lidar_utils::DistanceFilter;
  40. using StaticTransformer = autoware::common::lidar_utils::StaticTransformer;
  41. AngleFilter m_angle_filter;
  42. DistanceFilter m_distance_filter;
  43. const std::string m_input_frame_id;
  44. const std::string m_output_frame_id;
  45. std::unique_ptr<StaticTransformer> m_static_transformer;
  46. const std::chrono::nanoseconds m_init_timeout;
  47. const std::chrono::nanoseconds m_timeout;
  48. const typename rclcpp::Subscription<PointCloud2>::SharedPtr m_sub_ptr;
  49. const typename std::shared_ptr<rclcpp::Publisher<PointCloud2>> m_pub_ptr;
  50. const size_t m_expected_num_publishers;
  51. const size_t m_expected_num_subscribers;
  52. const std::size_t m_pcl_size;
  53. PointCloud2 m_filtered_transformed_msg;
  54. };

其继承自rclcpp::Node父类。改成时间驱动首先要添加头文件

  1. #include <chrono>
  2. #include <functional>

其次在私有属性里添加计时器timer_,私有方法里添加time_callback回调函数声明。

  1. const rclcpp::TimerBase::SharedPtr timer_;
  2. void time_callback();

3.实现

实现时在PointCloud2FilterTransformNode的构造函数里,添加timer的初始化。

  1. PointCloud2FilterTransformNode::PointCloud2FilterTransformNode(
  2. const rclcpp::NodeOptions & node_options)
  3. : Node("point_cloud_filter_transform_node", node_options),
  4. m_angle_filter{
  5. static_cast<float32_t>(declare_parameter("start_angle").get<float64_t>()),
  6. static_cast<float32_t>(declare_parameter("end_angle").get<float64_t>())},
  7. m_distance_filter{
  8. static_cast<float32_t>(declare_parameter("min_radius").get<float64_t>()),
  9. static_cast<float32_t>(declare_parameter("max_radius").get<float64_t>())},
  10. m_input_frame_id{declare_parameter("input_frame_id").get<std::string>()},
  11. m_output_frame_id{declare_parameter("output_frame_id").get<std::string>()},
  12. m_init_timeout{std::chrono::milliseconds{declare_parameter("init_timeout_ms").get<int32_t>()}},
  13. m_sub_ptr{create_subscription<PointCloud2>(
  14. "points_in", rclcpp::QoS{10},
  15. std::bind(
  16. &PointCloud2FilterTransformNode::process_filtered_transformed_message, this, _1))},
  17. m_timeout{std::chrono::milliseconds{declare_parameter("timeout_ms").get<int32_t>()}},
  18. m_pub_ptr{create_publisher<PointCloud2>("points_filtered", rclcpp::QoS{10})},
  19. m_expected_num_publishers{
  20. static_cast<size_t>(declare_parameter("expected_num_publishers").get<int32_t>())},
  21. m_expected_num_subscribers{
  22. static_cast<size_t>(declare_parameter("expected_num_subscribers").get<int32_t>())},
  23. m_pcl_size{static_cast<size_t>(declare_parameter("pcl_size").get<int32_t>())},
  24. timer_{create_wall_timer(200ms, std::bind(&PointCloud2FilterTransformNode::time_callback,this))}

把订阅和发布的初始化放到时间回调函数time_callback中

  1. void PointCloud2FilterTransformNode::time_callback()
  2. {
  3. m_pub_ptr=this->create_publisher<PointCloud2>("points_filtered", rclcpp::QoS{10});
  4. m_sub_ptr=this->create_subscription<PointCloud2>(
  5. "points_in", rclcpp::QoS{10},
  6. std::bind(
  7. &PointCloud2FilterTransformNode::process_filtered_transformed_message, this, _1));
  8. }

由此便实现了将节点改为以时间周期驱动的节点。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/540464
推荐阅读
相关标签
  

闽ICP备14008679号