00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of the Willow Garage nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 #include <cstdio> 00036 #include <math.h> 00037 #include <ros/ros.h> 00038 #include <sensor_msgs/JointState.h> 00039 00040 // Services 00041 #include "laser_assembler/AssembleScans.h" 00042 00043 // Messages 00044 #include "sensor_msgs/PointCloud.h" 00045 00046 /*** 00047 * This a simple test app that requests a point cloud from the 00048 * point_cloud_assembler every 4 seconds, and then publishes the 00049 * resulting data 00050 */ 00051 namespace laser_assembler 00052 { 00053 00054 class PeriodicSnapshotter 00055 { 00056 00057 public: 00058 00059 PeriodicSnapshotter() 00060 { 00061 // Create a publisher for the clouds that we assemble 00062 pub_ = n_.advertise<sensor_msgs::PointCloud> ("assembled_cloud", 1); 00063 00064 sub_ = n_.subscribe("joint_states", 1000, &PeriodicSnapshotter::rotCallback, this); 00065 00066 // Create the service client for calling the assembler 00067 client_ = n_.serviceClient<AssembleScans>("assemble_scans"); 00068 00069 first_time_ = true; 00070 arm_ = false; 00071 } 00072 00076 int getIndex(const sensor_msgs::JointState::ConstPtr& e) 00077 { 00078 for (size_t i = 0; i < e->name.size(); ++i) 00079 { 00080 if (e->name[i] == "laser_rot_joint") 00081 return i; 00082 } 00083 return -1; 00084 } 00085 00086 void rotCallback(const sensor_msgs::JointState::ConstPtr& e) 00087 { 00088 00089 if(first_time_) { 00090 last_time_ = e->header.stamp; 00091 first_time_ = false; 00092 return; 00093 } 00094 00095 int index = getIndex(e); 00096 if (index < 0) 00097 return; 00098 00099 double position = fmod(e->position[index], 2 * M_PI); 00100 00101 if (!arm_ && position > 3) { 00102 arm_ = true; 00103 return; 00104 } 00105 00106 if(arm_ && position > 0 && position < 1) { 00107 00108 // Populate our service request based on our timer callback times 00109 AssembleScans srv; 00110 srv.request.begin = last_time_; 00111 srv.request.end = e->header.stamp; 00112 00113 // Make the service call 00114 if (client_.call(srv)) 00115 { 00116 ROS_INFO("Published Cloud with %zu points", srv.response.cloud.points.size()) ; 00117 pub_.publish(srv.response.cloud); 00118 } 00119 else 00120 { 00121 ROS_ERROR("Error making service call\n") ; 00122 } 00123 00124 arm_ = false; 00125 last_time_ = e->header.stamp; 00126 } 00127 } 00128 00129 private: 00130 ros::NodeHandle n_; 00131 ros::Publisher pub_; 00132 ros::Subscriber sub_; 00133 ros::ServiceClient client_; 00134 bool first_time_; 00135 bool arm_; 00136 ros::Time last_time_; 00137 } ; 00138 00139 } 00140 00141 using namespace laser_assembler ; 00142 00143 int main(int argc, char **argv) 00144 { 00145 ros::init(argc, argv, "rotunit_snapshotter"); 00146 ros::NodeHandle n; 00147 ROS_INFO("Waiting for [build_cloud] to be advertised"); 00148 ros::service::waitForService("build_cloud"); 00149 ROS_INFO("Found build_cloud! Starting the snapshotter"); 00150 PeriodicSnapshotter snapshotter; 00151 ros::spin(); 00152 return 0; 00153 }