2
$\begingroup$

I am receiving a sensor_msgs::msg::PointCloud (base_link frame) in my callback. I want to transform this PointCloud to the camera frame, so that I can do a projection of that point cloud view.

I am having problems to convert a sensor_msgs::msg::PointCloud into a pcl::PointCloud<pcl::PointXYZ> cloud; with ROS 2 Foxy. How can I do it?

My CPP code:

void my_callback(const sensor_msgs::msg::PointCloud::SharedPtr msg)
{
    // cloud_in should be the PointCloud message
    pcl::PointCloud<pcl::PointXYZ> cloud_in;
    pcl::PointCloud<pcl::PointXYZ> cloud_out;

    geometry_msgs::msg::TransformStamped transform;

    try
    {
        transform = tfBuffer_->lookupTransform("camera_frame", msg->header.frame_id, tf2::TimePointZero);
        pcl_ros::transformPointCloud(cloud_in, cloud_out, transform);
    }
    catch (tf2::TransformException &ex)
    {
        RCLCPP_ERROR(this->get_logger(), "Transform error: %s", ex.what());
        return;
    }
}
$\endgroup$
2
  • $\begingroup$ What seems to be the problem in this function? Can you provide the logs of the execution of this node? $\endgroup$
    – ssarkar
    Oct 5, 2023 at 11:51
  • $\begingroup$ as it is it won't do nothing, because my PointCloud msg isn't converted in a pcl::PointCloud<pcl::PointXYZ> as desired $\endgroup$
    – renan
    Oct 5, 2023 at 12:52

2 Answers 2

3
$\begingroup$

Welcome to Robotics Stack Exchange!

I have noticed several concerns with your code snippet, which I would like to address below:

  1. Change the signature of your callback to receive sensor_msgs::msg::PointCloud2 instead of sensor_msgs::msg::PointCloud. Below is an example:
    void my_callback(const sensor_msgs::msg::PointCloud2::SharedPtr msg)
    
  2. Use pcl::PCLPointCloud2 instead of pcl::PointCloud<pcl::PointXYZ>.
  3. Use pcl_conversions::toPCL to convert sensor_msgs::msg::PointCloud2 to pcl::PCLPointCloud2 as shown below:
    pcl::PCLPointCloud2::Ptr cloud (new pcl::PCLPointCloud2());
    // Convert ROS2 Pointcloud2 to PCL Pointcloud2
    //   Don't foerget to add followings:
    //     #include <pcl_conversions/pcl_conversions.h>
    //     find_package(pcl_conversions REQUIRED)
    pcl_conversions::toPCL(*msg,*cloud);
    
  4. Finally, you should be able to use pcl_ros::transformPointCloud to transform your point cloud from base_link to camera_link.
$\endgroup$
3
  • $\begingroup$ Thanks for your reply, ravi! I am subscribing a topic that publishes a sensor_msgs::msg::PointCloud. I get following output after running ros2 topic list -t: /my/point_cloud [sensor_msgs/msg/PointCloud]. Shouldn't I use then sensor_msgs::msg::PointCloud? $\endgroup$
    – renan
    Oct 5, 2023 at 12:25
  • $\begingroup$ @renan: I am worried seeing old style sensor_msgs::msg::PointCloud in your package. First please subscribe to sensor_msgs::msg::PointCloud2. If it does not work, please see here. On a side note, ROS 2 Foxy is EOL. and will no longer receive support $\endgroup$
    – ravi
    Oct 6, 2023 at 2:58
  • $\begingroup$ With convertPointCloudToPointCloud2 I can now convert my PC to PC2. Thank you. $\endgroup$
    – renan
    Oct 6, 2023 at 8:13
1
$\begingroup$

Maybe a good first step is to convert to PointCloud2 using the snippet from convertPointCloudtoPointCloud2 from the package sensor_msgs.

then you can use pcl_conversions::moveToPCL or pcl_conversions::toPCL

$\endgroup$
1
  • 1
    $\begingroup$ Thank you for your input. As @ravi gave me the same answer and some other suggestions, I accepted his, but I appreciate your efforts. Cheers! $\endgroup$
    – renan
    Oct 6, 2023 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.