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 "boost/thread.hpp"
00043 
00044 using namespace ros;
00045 using namespace sensor_msgs;
00046 
00047 int g_argc;
00048 char** g_argv;
00049 
00050 class TestAssembler : public testing::Test
00051 {
00052 public:
00053 
00054   NodeHandle n_;
00055 
00056   sensor_msgs::PointCloud cloud_msg_ ;
00057   boost::mutex cloud_mutex_ ;
00058   sensor_msgs::PointCloud safe_cloud_ ;
00059   int cloud_counter_ ;
00060 
00061   LaserScan scan_msg_ ;
00062   boost::mutex scan_mutex_ ;
00063   LaserScan safe_scan_ ;
00064   int scan_counter_ ;
00065 
00066 
00067   void CloudCallback(const PointCloudConstPtr& cloud_msg)
00068   {
00069     cloud_mutex_.lock() ;
00070     cloud_counter_++ ;
00071     safe_cloud_ = *cloud_msg ;
00072     cloud_mutex_.unlock() ;
00073     ROS_INFO("Got Cloud with %u points", cloud_msg_.get_points_size()) ;
00074   }
00075 
00076   void ScanCallback(const LaserScanConstPtr& scan_msg)
00077   {
00078     scan_mutex_.lock() ;
00079     scan_counter_++ ;
00080     safe_scan_ = *scan_msg ;
00081     scan_mutex_.unlock() ;
00082     //ROS_INFO("Got Scan") ;
00083   }
00084 
00085 protected:
00087   TestAssembler()
00088   {
00089     cloud_counter_ = 0 ;
00090     scan_counter_ = 0 ;
00091   }
00092 
00094   ~TestAssembler()
00095   {
00096 
00097   }
00098 };
00099 
00100 
00101 TEST_F(TestAssembler, test)
00102 {
00103   ROS_INFO("Starting test F");
00104   ros::Subscriber cloud_sub = n_.subscribe("dummy_cloud", 10, &TestAssembler::CloudCallback, (TestAssembler*)this);
00105   ros::Subscriber scan_sub  = n_.subscribe("dummy_scan",  10, &TestAssembler::ScanCallback,  (TestAssembler*)this);
00106 
00107   bool waiting = true;
00108   while(n_.ok() && waiting )
00109   {
00110     ros::spinOnce();
00111     usleep(1e2) ;
00112 
00113     scan_mutex_.lock() ;
00114     waiting = (scan_counter_ < 55) ;
00115     scan_mutex_.unlock() ;
00116   }
00117 
00118   while(n_.ok() && cloud_counter_ == 0)
00119   {
00120     ROS_INFO("Waiting to get a cloud");  
00121     ros::spinOnce();
00122     usleep(5e5);
00123   }
00124 
00125   ASSERT_GT(cloud_counter_, 0) ;
00126   ROS_INFO("Got a cloud!");
00127 
00128   unsigned int cloud_size ;
00129   cloud_mutex_.lock() ;
00130   cloud_size = safe_cloud_.get_points_size() ;
00131   cloud_mutex_.unlock() ;
00132 
00133   EXPECT_LT((unsigned int) 100, cloud_size) ;
00134 
00135   SUCCEED();
00136 }
00137 
00138 int main(int argc, char** argv)
00139 {
00140   printf("******* Starting application *********\n");
00141 
00142   testing::InitGoogleTest(&argc, argv);
00143   ros::init(argc, argv, "test_assembler_node");
00144 
00145   return RUN_ALL_TESTS();
00146 }


pr2_laser_snapshotter
Author(s): Vijay Pradeep
autogenerated on Wed Dec 11 2013 14:17:19