[ros2 foxy] change subscriber callback based on topic remapping

asked 2021-10-01 08:12:08 -0500

morten gravatar image

I am trying to write some general purpose point cloud filters in ros2, utilizing PCL. The cpp file essentially just calls the relevant pcl function and manages conversions from the subscriber to the pcl format, and back to ros msg when publishing.

The problem is: this should hopefully be able to be applied to both radar and velodyne pointclouds, but the fields contained in each respective sensor_msgs::msg::PointCloud2 are not the same. The velodyne one has x, y, z, intensity, ring and time whereas the radar has x, y, z, rcs, and radial speed.

I have created unique point types fitting to each of the sensors, e.g.

namespace velodyne {
struct PointXYZIRT {
  float intensity;
  std::uint16_t ring;
  float time;
} // namespace velodyne
    (float, x, x)(float, y, y)(float, z, z)(float, intensity,
                                            intensity)(std::uint16_t, ring,
                                                       ring)(float, time, time))

I was thinking perhaps I could template the callback as the change is very minor, really only the pcl pointcloud initialization changes. But, since this function is general the publish and subscribe topics are remapped from /input and /output to the relevant topics. This means that I have to call this->create_subscription before I know what the topic remaps to.

Is it possible to change the callback associated to the rclcpp:Subscription shared pointer? Or is there such a thing as a remap callback? Maybe I am going about this the wrong way, I would just like to avoid making two nodes applying the same filter with a total of 2 lines of difference.

edit retag flag offensive close merge delete