camera.h
Go to the documentation of this file.
1 #ifndef GAZEBO_PLUGINS_TEST_CAMERA_CAMERA_H
2 #define GAZEBO_PLUGINS_TEST_CAMERA_CAMERA_H
3 
4 #include <gtest/gtest.h>
6 #include <ros/ros.h>
7 
8 class CameraTest : public testing::Test
9 {
10 protected:
11  virtual void SetUp()
12  {
13  has_new_image_ = false;
14  }
15 
20 public:
21  void subscribeTest();
22 
23  void imageCallback(const sensor_msgs::ImageConstPtr& msg)
24  {
25  image_stamp_ = msg->header.stamp;
26  has_new_image_ = true;
27  }
28 };
29 
30 // Test if the camera image is published at all, and that the timestamp
31 // is not too long in the past.
33 {
35  cam_sub_ = it.subscribe("camera1/image_raw", 1,
37  dynamic_cast<CameraTest*>(this));
38 
39 #if 0
40  // wait for gazebo to start publishing
41  // TODO(lucasw) this isn't really necessary since this test
42  // is purely passive
43  bool wait_for_topic = true;
44  while (wait_for_topic)
45  {
46  // @todo this fails without the additional 0.5 second sleep after the
47  // publisher comes online, which means on a slower or more heavily
48  // loaded system it may take longer than 0.5 seconds, and the test
49  // would hang until the timeout is reached and fail.
50  if (cam_sub_.getNumPublishers() > 0)
51  wait_for_topic = false;
52  ros::Duration(0.5).sleep();
53  }
54 #endif
55 
56  while (!has_new_image_)
57  {
58  ros::spinOnce();
59  ros::Duration(0.1).sleep();
60  }
61 
62  // This check depends on the update period being much longer
63  // than the expected difference between now and the received image time
64  // TODO(lucasw)
65  // this likely isn't that robust - what if the testing system is really slow?
66  double time_diff = (ros::Time::now() - image_stamp_).toSec();
67  ROS_INFO_STREAM(time_diff);
68  EXPECT_LT(time_diff, 1.0);
70 
71  // make sure nothing is subscribing to image_trigger topic
72  // there is no easy API, so call getSystemState
73  XmlRpc::XmlRpcValue args, result, payload;
74  args[0] = ros::this_node::getName();
75  EXPECT_TRUE(ros::master::execute("getSystemState", args, result, payload, true));
76  // [publishers, subscribers, services]
77  // subscribers in index 1 of payload
78  for (int i = 0; i < payload[1].size(); ++i)
79  {
80  // [ [topic1, [topic1Subscriber1...topic1SubscriberN]] ... ]
81  // topic name i is in index 0
82  std::string topic = payload[1][i][0];
83  EXPECT_EQ(topic.find("image_trigger"), std::string::npos);
84  }
85 }
86 
87 #endif
Subscriber subscribe(const std::string &base_topic, uint32_t queue_size, const boost::function< void(const sensor_msgs::ImageConstPtr &)> &callback, const ros::VoidPtr &tracked_object=ros::VoidPtr(), const TransportHints &transport_hints=TransportHints())
int size() const
bool sleep() const
ROSCPP_DECL const std::string & getName()
void subscribeTest()
Definition: camera.h:32
ROSCPP_DECL bool execute(const std::string &method, const XmlRpc::XmlRpcValue &request, XmlRpc::XmlRpcValue &response, XmlRpc::XmlRpcValue &payload, bool wait_for_master)
uint32_t getNumPublishers() const
virtual void SetUp()
Definition: camera.h:11
#define ROS_INFO_STREAM(args)
static Time now()
image_transport::Subscriber cam_sub_
Definition: camera.h:17
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
Definition: camera.h:23
ros::NodeHandle nh_
Definition: camera.h:16
ROSCPP_DECL void spinOnce()
ros::Time image_stamp_
Definition: camera.h:19
bool has_new_image_
Definition: camera.h:18


gazebo_plugins
Author(s): John Hsu
autogenerated on Tue Mar 26 2019 02:31:27