00001 #include <ros/ros.h>
00002 #include <iostream>
00003 #include <strstream>
00004
00005 #include <nav_msgs/Odometry.h>
00006
00007 #include <tf/tf.h>
00008 #include <tf/transform_listener.h>
00009
00010 #include <pcl_ros/point_cloud.h>
00011 #include <pcl_ros/transforms.h>
00012 #include <pcl/point_types.h>
00013 #include <pcl/io/io.h>
00014 #include <pcl/io/pcd_io.h>
00015 using namespace std;
00016
00017 class myPC
00018 {
00019 public:
00020 myPC()
00021 {
00022 sub = n_.subscribe("hokuyo_point_cloud2", 10, &myPC::pointCloud2Callback,this);
00023 sub_stereo_odom_ = n_.subscribe("/stereo_odometer/odometry", 10, &myPC::stereoCallback,this);
00024 timer = n_.createTimer(ros::Duration(0.5),&myPC::timerCallback,this);
00025 }
00026 ~myPC()
00027 {
00028 writer.write("robot.pcd", sum_robot);
00029 writer.write("stereo.pcd", sum_stereo);
00030 }
00031 private:
00032 void pointCloud2Callback(const sensor_msgs::PointCloud2::ConstPtr& msg);
00033 void stereoCallback(const nav_msgs::Odometry::ConstPtr& msg);
00034 void timerCallback(const ros::TimerEvent&);
00035 ros::NodeHandle n_;
00036 ros::Subscriber sub;
00037 ros::Subscriber sub_stereo_odom_;
00038 ros::Timer timer;
00039 tf::TransformListener mylistener;
00040 tf::TransformListener mylistener1;
00041 tf::StampedTransform Laser2robot;
00042 tf::StampedTransform Laser2multisense;
00043 tf::StampedTransform Laser2multisense_odom;
00044
00045 pcl::PointCloud<pcl::PointXYZ> sum_robot;
00046 pcl::PointCloud<pcl::PointXYZ> sum_stereo;
00047 pcl::PCDWriter writer;
00048 };
00049
00050 void myPC::stereoCallback(const nav_msgs::Odometry::ConstPtr& msg)
00051 {
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074 }
00075
00076 void myPC::pointCloud2Callback(const sensor_msgs::PointCloud2::ConstPtr& msg)
00077 {
00078 std::string robot_name,stereo_name;
00079 strstream ss;
00080 ss << ros::Time::now().toNSec();
00081 ss >> robot_name;
00082 stereo_name = robot_name;
00083 robot_name += string("robot.pcd");
00084 stereo_name += string("stereo.pcd");
00085
00086 sensor_msgs::PointCloud2 robot_pointcloud;
00087 sensor_msgs::PointCloud2 stereo_pointcloud;
00088 pcl_ros::transformPointCloud("Laser", Laser2robot.inverse(), *msg, robot_pointcloud);
00089 pcl_ros::transformPointCloud("Laser", Laser2multisense.inverse(), *msg, stereo_pointcloud);
00090
00091 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_robot(new pcl::PointCloud<pcl::PointXYZ>);
00092 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_stereo(new pcl::PointCloud<pcl::PointXYZ>);
00093
00094 pcl::fromROSMsg(robot_pointcloud,*cloud_robot);
00095 pcl::fromROSMsg(stereo_pointcloud,*cloud_stereo);
00096
00097 sum_robot += *cloud_robot;
00098 sum_stereo += *cloud_stereo;
00099 writer.write(robot_name, *cloud_robot);
00100 writer.write(stereo_name, *cloud_stereo);
00101 }
00102
00103 void myPC::timerCallback(const ros::TimerEvent&)
00104 {
00105 try{
00106 (mylistener.waitForTransform("Laser_based_on_robot","Laser",ros::Time::now()-ros::Duration(0.1),ros::Duration(0.2)));
00107 {
00108 mylistener.lookupTransform("Laser_based_on_robot","Laser",ros::Time::now()-ros::Duration(0.1),Laser2robot);
00109 ROS_WARN_THROTTLE(2.0,"Laser2robot: %f,%f,%f",Laser2robot.getOrigin().x(),Laser2robot.getOrigin().y(),Laser2robot.getOrigin().z());
00110 }
00111 }
00112 catch(tf::TransformException ex)
00113 {
00114 ROS_WARN_THROTTLE(10.0, "Laser2robot. ex.what:%s",ex.what());
00115 }
00116
00117 try{
00118 (mylistener1.waitForTransform("Laser_based_on_stereo","Laser",ros::Time::now()-ros::Duration(0.8),ros::Duration(0.2)));
00119 {
00120 mylistener1.lookupTransform("Laser_based_on_stereo","Laser", ros::Time::now()-ros::Duration(0.8),Laser2multisense);
00121
00122 ROS_WARN_THROTTLE(1.0,"Laser2multisense Tran: %f,%f,%f",Laser2multisense.getOrigin().getX(),Laser2multisense.getOrigin().getY(),Laser2multisense.getOrigin().getZ());
00123 double r,p,y;
00124 Laser2multisense.getBasis().getEulerYPR(y,p,r);
00125 ROS_WARN_THROTTLE(1.0,"Laser2multisense RPY: %f,%f,%f",y,p,r);
00126 }
00127 }
00128 catch(tf::TransformException ex)
00129 {
00130 ROS_WARN_THROTTLE(10.0, "Laser2robot. ex.what:%s",ex.what());
00131 }
00132
00133 }
00134
00135 int main(int argc, char** argv)
00136 {
00137 ros::init(argc, argv, "pointcloud2pcd");
00138 ros::NodeHandle n;
00139 myPC myPC;
00140 ros::spin();
00141
00142 return 0;
00143 }