test_tango_ros_msg.cpp
Go to the documentation of this file.
00001 // Copyright 2016 Intermodalics All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      http://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 #include <time.h>
00015 
00016 #include <gtest/gtest.h>
00017 #include <image_transport/image_transport.h>
00018 #include <ros/ros.h>
00019 #include <sensor_msgs/CompressedImage.h>
00020 #include <sensor_msgs/PointCloud2.h>
00021 #include <tango_ros_native/tango_ros_nodelet.h>
00022 #include <tf/transform_listener.h>
00023 #include <tf2_msgs/TFMessage.h>
00024 
00025 constexpr double TF_RATE = 150.; // in Hz.
00026 constexpr double POINT_CLOUD_RATE = 4.; // in Hz.
00027 constexpr double FISHEYE_IMAGE_RATE = 25.; // in Hz.
00028 constexpr double COLOR_IMAGE_RATE = 8.; // in Hz.
00029 constexpr double RATE_TOLERANCE_RATIO = 0.2;
00030 
00031 constexpr int SLEEP_TIME_UNTIL_FIRST_MESSAGE = 2; // in second.
00032 constexpr double DURATION_RATE_TEST = 10; // in second.
00033 
00034 class TangoRosTest : public ::testing::Test {
00035  public:
00036   ros::NodeHandle nh_;
00037   tf::TransformListener tf_listener_;
00038   ros::Subscriber sub_tf_;
00039   ros::Subscriber sub_point_cloud_;
00040   image_transport::Subscriber sub_fisheye_image_;
00041   image_transport::Subscriber sub_color_image_;
00042 
00043   bool tf_message_received_;
00044   bool point_cloud_received_;
00045   bool fisheye_image_received_;
00046   bool color_image_received_;
00047 
00048   int tf_message_count_;
00049   int point_cloud_message_count_;
00050   int fisheye_image_message_count_;
00051   int color_image_message_count_;
00052 
00053   TangoRosTest(): tf_message_received_(false), point_cloud_received_(false),
00054       fisheye_image_received_(false), color_image_received_(false),
00055       tf_message_count_(0), point_cloud_message_count_(0),
00056       fisheye_image_message_count_(0), color_image_message_count_(0) {}
00057 
00058  protected:
00059   virtual void SetUp() {
00060     sub_tf_ = nh_.subscribe<tf2_msgs::TFMessage>(
00061         "/tf", 1, boost::bind(&TangoRosTest::TfCallback, this, _1));
00062 
00063     sub_point_cloud_ = nh_.subscribe<sensor_msgs::PointCloud2>(
00064         "/tango/" + tango_ros_native::POINT_CLOUD_TOPIC_NAME, 1,
00065         boost::bind(&TangoRosTest::PointCloudCallback, this, _1));
00066 
00067     image_transport::ImageTransport it(nh_);
00068     sub_fisheye_image_ =
00069         it.subscribe("/tango/" + tango_ros_native::FISHEYE_IMAGE_TOPIC_NAME, 1,
00070                      &TangoRosTest::FisheyeImageCallback, this,
00071                      image_transport::TransportHints("compressed"));
00072 
00073     sub_color_image_ =
00074         it.subscribe("/tango/" + tango_ros_native::COLOR_IMAGE_TOPIC_NAME, 1,
00075                      &TangoRosTest::ColorImageCallback, this,
00076                      image_transport::TransportHints("compressed"));
00077   }
00078 
00079  private:
00080   void TfCallback(const boost::shared_ptr<const tf2_msgs::TFMessage> msg) {
00081     tf_message_received_ = true;
00082     tf_message_count_++;
00083   }
00084 
00085   void PointCloudCallback(const boost::shared_ptr<const sensor_msgs::PointCloud2> msg) {
00086     point_cloud_received_ = true;
00087     point_cloud_message_count_ ++;
00088   }
00089 
00090   void FisheyeImageCallback(const sensor_msgs::ImageConstPtr& image) {
00091     fisheye_image_received_ = true;
00092     fisheye_image_message_count_++;
00093   }
00094 
00095   void ColorImageCallback(const sensor_msgs::ImageConstPtr& image) {
00096     color_image_received_ = true;
00097     color_image_message_count_++;
00098   }
00099 };
00100 
00101 TEST_F(TangoRosTest, TestMessagesArePublished) {
00102   // Sleep some time to be sure that data is published.
00103   sleep(SLEEP_TIME_UNTIL_FIRST_MESSAGE);
00104   ros::spinOnce();
00105 
00106   tf::StampedTransform transform;
00107   EXPECT_NO_THROW(tf_listener_.lookupTransform("/start_of_service", "/device",
00108                                            ros::Time(0), transform));
00109   EXPECT_NO_THROW(tf_listener_.lookupTransform("/area_description", "/device",
00110                                              ros::Time(0), transform));
00111   EXPECT_NO_THROW(tf_listener_.lookupTransform("/device", "/camera_depth",
00112                                                ros::Time(0), transform));
00113   EXPECT_NO_THROW(tf_listener_.lookupTransform("/device", "/camera_fisheye",
00114                                                ros::Time(0), transform));
00115   EXPECT_NO_THROW(tf_listener_.lookupTransform("/device", "/camera_color",
00116                                                ros::Time(0), transform));
00117   EXPECT_TRUE(tf_message_received_);
00118   EXPECT_TRUE(point_cloud_received_);
00119   EXPECT_TRUE(fisheye_image_received_);
00120   EXPECT_TRUE(color_image_received_);
00121 }
00122 
00123 TEST_F(TangoRosTest, TestMessageRates) {
00124   time_t current_time = time(NULL);
00125   time_t end = current_time + DURATION_RATE_TEST;
00126   while (current_time < end) {
00127     ros::spinOnce();
00128     current_time = time(NULL);
00129   }
00130   double tf_rate = tf_message_count_ / DURATION_RATE_TEST;
00131   double point_cloud_rate = point_cloud_message_count_ / DURATION_RATE_TEST;
00132   double fisheye_image_rate = fisheye_image_message_count_ / DURATION_RATE_TEST;
00133   double color_image_rate = color_image_message_count_ / DURATION_RATE_TEST;
00134   EXPECT_NEAR(TF_RATE, tf_rate, RATE_TOLERANCE_RATIO * TF_RATE);
00135   EXPECT_NEAR(POINT_CLOUD_RATE, point_cloud_rate, RATE_TOLERANCE_RATIO * POINT_CLOUD_RATE);
00136   EXPECT_NEAR(FISHEYE_IMAGE_RATE, fisheye_image_rate, RATE_TOLERANCE_RATIO * FISHEYE_IMAGE_RATE);
00137   EXPECT_NEAR(COLOR_IMAGE_RATE, color_image_rate, RATE_TOLERANCE_RATIO * COLOR_IMAGE_RATE);
00138 }
00139 
00140 // Run all the tests that were declared with TEST()
00141 int main(int argc, char **argv){
00142   testing::InitGoogleTest(&argc, argv);
00143   ros::init(argc, argv, "tango_ros_test");
00144   return RUN_ALL_TESTS();
00145 }


tango_ros_native
Author(s):
autogenerated on Thu Jun 6 2019 19:49:54