test_assembler.cpp
Go to the documentation of this file.
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 /* Author: Vijay Pradeep */
00036 
00037 #include <string>
00038 #include <gtest/gtest.h>
00039 #include <ros/ros.h>
00040 #include "sensor_msgs/PointCloud.h"
00041 #include "sensor_msgs/LaserScan.h"
00042 #include "laser_assembler/AssembleScans.h"
00043 #include <boost/thread.hpp>
00044 #include <boost/thread/condition.hpp>
00045 
00046 using namespace ros;
00047 using namespace sensor_msgs;
00048 using namespace std;
00049 
00050 static const string SERVICE_NAME = "assemble_scans";
00051 
00052 class TestAssembler : public testing::Test
00053 {
00054 public:
00055 
00056   NodeHandle n_;
00057 
00058   void SetUp()
00059   {
00060     ROS_INFO("Waiting for service [%s]", SERVICE_NAME.c_str());
00061     ros::service::waitForService(SERVICE_NAME);
00062     ROS_INFO("Service [%s] detected", SERVICE_NAME.c_str());
00063     received_something_ = false;
00064     got_cloud_ = false;
00065     scan_sub_  = n_.subscribe("dummy_scan",  10, &TestAssembler::ScanCallback,  (TestAssembler*)this);
00066   }
00067 
00068   void ScanCallback(const LaserScanConstPtr& scan_msg)
00069   {
00070     boost::mutex::scoped_lock lock(mutex_);
00071     if (!received_something_)
00072     {
00073       // Store the time of this first scan. Will be needed when we make the service call
00074       start_time_ = scan_msg->header.stamp;
00075       received_something_ = true;
00076     }
00077     else
00078     {
00079       // Make the call to get a point cloud
00080       laser_assembler::AssembleScans assemble_scans;
00081       assemble_scans.request.begin = start_time_;
00082       assemble_scans.request.end = scan_msg->header.stamp;
00083       EXPECT_TRUE(ros::service::call(SERVICE_NAME, assemble_scans));
00084       if(assemble_scans.response.cloud.points.size() > 0)
00085       {
00086         ROS_INFO("Got a cloud with [%u] points. Saving the cloud", (uint32_t)(assemble_scans.response.cloud.points.size()));
00087         cloud_ = assemble_scans.response.cloud;
00088         got_cloud_ = true;
00089         cloud_condition_.notify_all();
00090       }
00091       else
00092         ROS_INFO("Got an empty cloud. Going to try again on the next scan");
00093     }
00094   }
00095 
00096 protected:
00097   ros::Subscriber scan_sub_;
00098   bool received_something_;
00099   ros::Time start_time_;
00100   bool got_cloud_;
00101   sensor_msgs::PointCloud cloud_;
00102   boost::mutex mutex_;
00103   boost::condition cloud_condition_;
00104 };
00105 
00106 
00107 void spinThread()
00108 {
00109   ros::spin();
00110 }
00111 
00112 // Check to make sure we can get a point cloud with at least 1 point in it
00113 TEST_F(TestAssembler, non_zero_cloud_test)
00114 {
00115   // Wait until we get laser scans
00116   boost::mutex::scoped_lock lock(mutex_);
00117 
00118   while(n_.ok() && !got_cloud_)
00119   {
00120     cloud_condition_.timed_wait(lock, boost::posix_time::milliseconds(1000.0f));
00121   }
00122 
00123   EXPECT_LT((unsigned int) 0, cloud_.points.size());
00124 
00125   SUCCEED();
00126 }
00127 
00128 int main(int argc, char** argv)
00129 {
00130   printf("******* Starting application *********\n");
00131 
00132   testing::InitGoogleTest(&argc, argv);
00133   ros::init(argc, argv, "test_assembler_node");
00134 
00135   boost::thread spin_thread(spinThread);
00136 
00137   int result = RUN_ALL_TESTS();
00138 
00139   ros::shutdown();
00140 
00141   spin_thread.join();
00142   return result;
00143 }


laser_assembler
Author(s): Vijay Pradeep
autogenerated on Mon Oct 6 2014 01:43:03