main.cpp 6.57 KB
Newer Older
Marius's avatar
ad  
Marius committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <ros/ros.h>
#include <tf/transform_broadcaster.h>
#include <geometry_msgs/Pose.h>
#include <chrono>
#include <comon/clockhelper.hpp>
#include <pcl_ros/point_cloud.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/io/pcd_io.h>
#include <pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/statistical_outlier_removal.h>
#include <pcl/filters/passthrough.h>
#include <pcl/registration/icp.h>
#include <pcl/registration/ia_ransac.h>
#include <pcl/features/pfh.h>
#include <pcl/features/fpfh.h>
#include <pcl/features/normal_3d.h>
#include <Eigen/Geometry>
#include <pcl/common/transforms.h>
#include <comon/rosbaghelper.hpp>
#include <comon/pclhelper.hpp>

Marius's avatar
aswsd  
Marius committed
26

Marius's avatar
ad  
Marius committed
27 28 29 30 31 32 33
// Less typing
using CloudRGBA = pcl::PointCloud<pcl::PointXYZRGBA>;

void setCloudColor(CloudRGBA::Ptr cloud, int r, int g, int b) {
    for (int i = 0; i < cloud->size(); ++i) {
        auto& point = (*cloud)[i];
        point.r = r;
Marius's avatar
aswsd  
Marius committed
34 35
        /* MISSING - Task 2 */
        /* MISSING - Task 2 */
Marius's avatar
ad  
Marius committed
36 37 38 39 40
    }
}

void prepareHeader(CloudRGBA::Ptr cloud, const std::string frameID) {
    pcl_conversions::toPCL(ros::Time::now(), cloud->header.stamp);
Marius's avatar
aswsd  
Marius committed
41
    cloud->header.frame_id = ""; /* FIX ME - Task 2 */
Marius's avatar
ad  
Marius committed
42 43 44
}

CloudRGBA::Ptr downsample(CloudRGBA::Ptr frame, float leafSize) {
Marius's avatar
aswsd  
Marius committed
45
    auto downsampledCloud = boost::make_shared<CloudRGBA>();
Marius's avatar
ad  
Marius committed
46 47 48 49 50 51
    if (leafSize == 0) {
        *downsampledCloud = *frame;
        return downsampledCloud;
    }
    // Downsample using voxelgrid
    pcl::VoxelGrid<pcl::PointXYZRGBA> downsample_filter;
Marius's avatar
aswsd  
Marius committed
52
    downsample_filter.setLeafSize(0, 0, 0); /* FIX ME - Task 2 */
Marius's avatar
ad  
Marius committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    downsample_filter.setInputCloud(frame);
    downsample_filter.filter(*downsampledCloud);

    return downsampledCloud;
}

int main(int argc, char **argv) {
    // The name of this node
    std::string nodeName = "crs_tf_publisher";

    ros::init(argc, argv, nodeName);
    auto node = ros::NodeHandle();

    // The used bag file
    std::string bagFilePath = "/home/marius/bockdata/bags/room2.bag";


Marius's avatar
aswsd  
Marius committed
70
    ros::Publisher framePublisher, modelPublisher, alignedPublisher, completeCloudPublisher;
Marius's avatar
ad  
Marius committed
71
    framePublisher = node.advertise<CloudRGBA>("/crs/frame", 5);
Marius's avatar
aswsd  
Marius committed
72 73 74
    modelPublisher = node.advertise<CloudRGBA>("", 5); /* FIX ME - Task 3 */
    alignedPublisher = node.advertise<CloudRGBA>("", 5); /* FIX ME - Task 3 */
    completeCloudPublisher = node.advertise<CloudRGBA>("", 5); /* FIX ME - Task 3 */
Marius's avatar
ad  
Marius committed
75 76 77 78

    // The topic we subscribe to
    std::string defaultCloudFrame = "kinect2_ir_optical_frame";

Marius's avatar
aswsd  
Marius committed
79
    // Some ICP Parameters. DO NOT CHANGE THEM!
Marius's avatar
ad  
Marius committed
80 81 82 83
    double ICPMaxCorrespondenceDistance = 0.2;
    double ICPTransformationEpsilon = 0.00001;
    double ICPEuclidianFitnessEpsilon = 0.1;

Marius's avatar
aswsd  
Marius committed
84

Marius's avatar
ad  
Marius committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    // The leaf size of the VoxelGrid Filter
    double ICPDownsampleLeafSize = 0.1;

    // The maximal of ICP iterations
    int ICPMaxIterations = 20;

    // The ROS Bag Helper.
    BagFileHelper helper;
    helper.openBagFile(bagFilePath);
    helper.initTopic(cloudSubscribeTopic);

    // Create an empty cloud that is going to contain ALL registered point clouds
    auto completeCloud = boost::make_shared<CloudRGBA>();

    // Create a matrix that is going to contain the transformation from the CURRENT frame to the FIRST Model
    // At the beginning it is an identiy matrix -> no transformation at all
    Eigen::Matrix4f completeTransform;
    completeTransform.setIdentity();

    // Variables for the model and the frame
    CloudRGBA::Ptr model, frame;

    // The main loop runs as long as there are new clouds avaiable and [CRTL + c] has not been pressed
Marius's avatar
aswsd  
Marius committed
108
    while (helper.hasTopicAvailableMessages("/kinect2/sd/points") && ros::ok()) {
Marius's avatar
ad  
Marius committed
109
        // Gather the next cloud from our bag file
Marius's avatar
aswsd  
Marius committed
110
        frame = helper.getNextCloudRGBAForTopic("/kinect2/sd/points");
Marius's avatar
ad  
Marius committed
111 112 113 114 115 116

        if (model == nullptr) {
            // Since it is the first loop iteration we do not have a model yet.
            // The current frame will be the model for the next frame
            model = frame;
        } else {
Marius's avatar
aswsd  
Marius committed
117
            // Downsample the input clouds
Marius's avatar
ad  
Marius committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
            auto downsampledModel = downsample(model, ICPDownsampleLeafSize);
            auto downsampledFrame = downsample(frame, ICPDownsampleLeafSize);

            // Create the ICP object
            IterativeClosestPoint icp;

            // Set the ICP parameters
            icp.setMaxCorrespondenceDistance(ICPMaxCorrespondenceDistance);
            icp.setMaximumIterations(ICPMaxIterations);
            icp.setTransformationEpsilon(ICPTransformationEpsilon);
            icp.setEuclideanFitnessEpsilon(ICPEuclidianFitnessEpsilon);

            // Set ICP data sources
            icp.setInputCloud(downsampledFrame);
            icp.setInputTarget(downsampledModel);

            // Run ICP Alligment
            icp.align(*downsampledFrame);

            // The ICP algorithm has finished. Print the convergence criteria!
            ROS_INFO("ICP terminated with criteria: %s", getCriteria(icp));

            // Receive the local transformation from the current frame to the model (the last frame)
            auto transform = icp.getFinalTransformation();

            // Calculate the complete transformation from the CURRENT frame to the FIRST model
            completeTransform = transform * completeTransform;

            // Transform the current frame and add it to the complete model
            auto transformedCloud = boost::make_shared<CloudRGBA>();
            pcl::transformPointCloud(*frame, *transformedCloud, completeTransform);
            *completeCloud += *transformedCloud;

            // Publish the complete model
            prepareHeader(completeCloud, defaultCloudFrame);
            completeCloudPublisher.publish(completeCloud);

            // Publish the current model
Marius's avatar
aswsd  
Marius committed
156
            setCloudColor(downsampledModel, 0, 0, 0); /* FIX ME - Task 3 */
Marius's avatar
ad  
Marius committed
157 158 159 160
            prepareHeader(downsampledModel, defaultCloudFrame);
            modelPublisher.publish(downsampledModel);

            // Publish the current frame
Marius's avatar
aswsd  
Marius committed
161
            setCloudColor(downsampledFrame, 0, 0, 0); /* FIX ME - Task 3 */
Marius's avatar
ad  
Marius committed
162 163 164 165 166
            prepareHeader(downsampledFrame, defaultCloudFrame);
            framePublisher.publish(downsampledFrame);

            // Publish the current, donwsampled aligned frame
            auto downsampledAlign = downsample(transformedCloud, ICPDownsampleLeafSize);
Marius's avatar
aswsd  
Marius committed
167
            setCloudColor(downsampledAlign, 0, 0, 0); /* FIX ME - Task 3 */
Marius's avatar
ad  
Marius committed
168 169 170 171 172 173 174 175 176
            prepareHeader(downsampledAlign, defaultCloudFrame);
            alignedPublisher.publish(downsampledAlign);

            // The current frame will be the model for the next frame
            model = frame;
        }
    }
    return 0;
}