test_tango_ros_msg.cpp
Go to the documentation of this file.
1 // Copyright 2016 Intermodalics All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include <time.h>
15 
16 #include <gtest/gtest.h>
18 #include <ros/ros.h>
19 #include <sensor_msgs/CompressedImage.h>
20 #include <sensor_msgs/PointCloud2.h>
22 #include <tf/transform_listener.h>
23 #include <tf2_msgs/TFMessage.h>
24 
25 constexpr double TF_RATE = 150.; // in Hz.
26 constexpr double POINT_CLOUD_RATE = 4.; // in Hz.
27 constexpr double FISHEYE_IMAGE_RATE = 25.; // in Hz.
28 constexpr double COLOR_IMAGE_RATE = 8.; // in Hz.
29 constexpr double RATE_TOLERANCE_RATIO = 0.2;
30 
31 constexpr int SLEEP_TIME_UNTIL_FIRST_MESSAGE = 2; // in second.
32 constexpr double DURATION_RATE_TEST = 10; // in second.
33 
34 class TangoRosTest : public ::testing::Test {
35  public:
42 
47 
52 
53  TangoRosTest(): tf_message_received_(false), point_cloud_received_(false),
54  fisheye_image_received_(false), color_image_received_(false),
55  tf_message_count_(0), point_cloud_message_count_(0),
56  fisheye_image_message_count_(0), color_image_message_count_(0) {}
57 
58  protected:
59  virtual void SetUp() {
60  sub_tf_ = nh_.subscribe<tf2_msgs::TFMessage>(
61  "/tf", 1, boost::bind(&TangoRosTest::TfCallback, this, _1));
62 
63  sub_point_cloud_ = nh_.subscribe<sensor_msgs::PointCloud2>(
65  boost::bind(&TangoRosTest::PointCloudCallback, this, _1));
66 
68  sub_fisheye_image_ =
69  it.subscribe("/tango/" + tango_ros_native::FISHEYE_IMAGE_TOPIC_NAME, 1,
71  image_transport::TransportHints("compressed"));
72 
73  sub_color_image_ =
74  it.subscribe("/tango/" + tango_ros_native::COLOR_IMAGE_TOPIC_NAME, 1,
76  image_transport::TransportHints("compressed"));
77  }
78 
79  private:
81  tf_message_received_ = true;
82  tf_message_count_++;
83  }
84 
86  point_cloud_received_ = true;
87  point_cloud_message_count_ ++;
88  }
89 
90  void FisheyeImageCallback(const sensor_msgs::ImageConstPtr& image) {
91  fisheye_image_received_ = true;
92  fisheye_image_message_count_++;
93  }
94 
95  void ColorImageCallback(const sensor_msgs::ImageConstPtr& image) {
96  color_image_received_ = true;
97  color_image_message_count_++;
98  }
99 };
100 
101 TEST_F(TangoRosTest, TestMessagesArePublished) {
102  // Sleep some time to be sure that data is published.
104  ros::spinOnce();
105 
106  tf::StampedTransform transform;
107  EXPECT_NO_THROW(tf_listener_.lookupTransform("/start_of_service", "/device",
108  ros::Time(0), transform));
109  EXPECT_NO_THROW(tf_listener_.lookupTransform("/area_description", "/device",
110  ros::Time(0), transform));
111  EXPECT_NO_THROW(tf_listener_.lookupTransform("/device", "/camera_depth",
112  ros::Time(0), transform));
113  EXPECT_NO_THROW(tf_listener_.lookupTransform("/device", "/camera_fisheye",
114  ros::Time(0), transform));
115  EXPECT_NO_THROW(tf_listener_.lookupTransform("/device", "/camera_color",
116  ros::Time(0), transform));
117  EXPECT_TRUE(tf_message_received_);
118  EXPECT_TRUE(point_cloud_received_);
119  EXPECT_TRUE(fisheye_image_received_);
120  EXPECT_TRUE(color_image_received_);
121 }
122 
123 TEST_F(TangoRosTest, TestMessageRates) {
124  time_t current_time = time(NULL);
125  time_t end = current_time + DURATION_RATE_TEST;
126  while (current_time < end) {
127  ros::spinOnce();
128  current_time = time(NULL);
129  }
130  double tf_rate = tf_message_count_ / DURATION_RATE_TEST;
131  double point_cloud_rate = point_cloud_message_count_ / DURATION_RATE_TEST;
132  double fisheye_image_rate = fisheye_image_message_count_ / DURATION_RATE_TEST;
133  double color_image_rate = color_image_message_count_ / DURATION_RATE_TEST;
134  EXPECT_NEAR(TF_RATE, tf_rate, RATE_TOLERANCE_RATIO * TF_RATE);
135  EXPECT_NEAR(POINT_CLOUD_RATE, point_cloud_rate, RATE_TOLERANCE_RATIO * POINT_CLOUD_RATE);
136  EXPECT_NEAR(FISHEYE_IMAGE_RATE, fisheye_image_rate, RATE_TOLERANCE_RATIO * FISHEYE_IMAGE_RATE);
137  EXPECT_NEAR(COLOR_IMAGE_RATE, color_image_rate, RATE_TOLERANCE_RATIO * COLOR_IMAGE_RATE);
138 }
139 
140 // Run all the tests that were declared with TEST()
141 int main(int argc, char **argv){
142  testing::InitGoogleTest(&argc, argv);
143  ros::init(argc, argv, "tango_ros_test");
144  return RUN_ALL_TESTS();
145 }
constexpr double POINT_CLOUD_RATE
void PointCloudCallback(const boost::shared_ptr< const sensor_msgs::PointCloud2 > msg)
void FisheyeImageCallback(const sensor_msgs::ImageConstPtr &image)
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
tf::TransformListener tf_listener_
int main(int argc, char **argv)
constexpr int SLEEP_TIME_UNTIL_FIRST_MESSAGE
image_transport::Subscriber sub_fisheye_image_
const std::string COLOR_IMAGE_TOPIC_NAME
void TfCallback(const boost::shared_ptr< const tf2_msgs::TFMessage > msg)
constexpr double COLOR_IMAGE_RATE
virtual void SetUp()
void ColorImageCallback(const sensor_msgs::ImageConstPtr &image)
constexpr double DURATION_RATE_TEST
ros::Subscriber sub_point_cloud_
ros::Subscriber sub_tf_
void lookupTransform(const std::string &target_frame, const std::string &source_frame, const ros::Time &time, StampedTransform &transform) const
const std::string POINT_CLOUD_TOPIC_NAME
ros::NodeHandle nh_
const std::string FISHEYE_IMAGE_TOPIC_NAME
constexpr double TF_RATE
constexpr double RATE_TOLERANCE_RATIO
ROSCPP_DECL void spinOnce()
TEST_F(TangoRosTest, TestMessagesArePublished)
image_transport::Subscriber sub_color_image_
constexpr double FISHEYE_IMAGE_RATE


tango_ros_native
Author(s):
autogenerated on Mon Jun 10 2019 15:37:51