service_server_test.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
3 
5 
6 #include <std_srvs/Empty.h>
7 
8 namespace du = diagnostic_updater;
9 
10 // Alias type for easier access to DiagnosticStatus enumerations.
11 typedef diagnostic_msgs::DiagnosticStatus DS;
12 
14 {
18 
20 
24 
26 
27  public:
29  :
30  test1_result_(true)
31  {
32  // Setup a one-shot timer to initialize the node after a brief
33  // delay so that /rosout is always fully initialized.
34  ROS_INFO("Starting initialization timer...");
35  init_timer_ = nh_.createWallTimer(ros::WallDuration(1.0),
37  this,
38  true);
39  }
40 
41  void initialize(const ros::WallTimerEvent &ignored)
42  {
43  test1_srv_.setInstrumentPerClient(true);
44  test1_srv_ = swri::ServiceServer(nh_, "test_service1",
46  this);
47 
48  test2_srv_.setInstrumentPerClient(false);
49  test2_srv_ = swri::ServiceServer(nh_, "test_service2",
51  this);
52 
53  test3_srv_.setLogCalls(true);
54  test3_srv_.setInstrumentPerClient(false);
55  test3_srv_ = swri::ServiceServer(nh_, "test_service3",
57  this);
58 
59 
60  diagnostic_updater_.setHardwareID("none");
61  diagnostic_updater_.add(
62  "swri::ServiceServer test service 1", this,
64 
65  diagnostic_updater_.add(
66  "swri::ServiceServer test service 2", this,
68 
69  diagnostic_updater_.add(
70  "swri::ServiceServer test service 3", this,
72 
73  diag_timer_ = nh_.createTimer(ros::Duration(1.0),
75  this);
76  }
77 
78  bool handleService1(std_srvs::Empty::Request &req,
79  std_srvs::Empty::Response &res)
80  {
81  ROS_INFO("test service 1 called. returning %s",
82  test1_result_ ? "true" : "false");
83  return test1_result_;
84  }
85 
86  bool handleService2(ros::ServiceEvent<std_srvs::Empty::Request,
87  std_srvs::Empty::Response> &event)
88  {
89  ROS_INFO("test service 2 called");
90  test1_result_ = !test1_result_;
91  return true;
92  }
93 
94  bool handleService3(const std::string &name,
95  const std_srvs::Empty::Request &request,
96  std_srvs::Empty::Response &response)
97  {
98  ROS_INFO("test service 3 called by %s", name.c_str());
99  return true;
100  }
101 
103  {
104  diagnostic_updater_.update();
105  }
106 
108  {
109  status.summary(DS::OK, "No errors reported.");
110  test1_srv_.appendDiagnostics(status, "Test Service 1",
112  }
113 
115  {
116  status.summary(DS::OK, "No errors reported.");
117  test2_srv_.appendDiagnostics(status, "Test Service 2",
119  }
120 
122  {
123  status.summary(DS::OK, "No errors reported.");
124  test3_srv_.appendDiagnostics(status, "Test Service 3",
126  }
127 }; // class ServiceServerTest
128 
129 int main(int argc, char **argv)
130 {
131  ros::init(argc, argv, "service_server_test");
132 
133  ServiceServerTest node;
134  ros::spin();
135 
136  return 0;
137 }
bool handleService1(std_srvs::Empty::Request &req, std_srvs::Empty::Response &res)
void appendDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &status, const std::string &name, const int flags)
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
void summary(unsigned char lvl, const std::string s)
void setHardwareID(const std::string &hwid)
void setLogCalls(bool enable)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void add(const std::string &name, TaskFunction f)
int main(int argc, char **argv)
swri::ServiceServer test3_srv_
swri::ServiceServer test1_srv_
WallTimer createWallTimer(WallDuration period, void(T::*callback)(const WallTimerEvent &), T *obj, bool oneshot=false, bool autostart=true) const
void service2Diagnostics(du::DiagnosticStatusWrapper &status)
ros::WallTimer init_timer_
swri::ServiceServer test2_srv_
void service3Diagnostics(du::DiagnosticStatusWrapper &status)
#define ROS_INFO(...)
bool handleService3(const std::string &name, const std_srvs::Empty::Request &request, std_srvs::Empty::Response &response)
ROSCPP_DECL void spin()
void service1Diagnostics(du::DiagnosticStatusWrapper &status)
void handleDiagnosticsTimer(const ros::TimerEvent &ignored)
void setInstrumentPerClient(bool enable)
void initialize(const ros::WallTimerEvent &ignored)
bool handleService2(ros::ServiceEvent< std_srvs::Empty::Request, std_srvs::Empty::Response > &event)
diagnostic_msgs::DiagnosticStatus DS
du::Updater diagnostic_updater_


swri_roscpp
Author(s):
autogenerated on Sat Jan 21 2023 03:13:16