selftest_example.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the <ORGANIZATION> nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 
31 #include <ros/ros.h>
32 
33 #include "diagnostic_msgs/SelfTest.h"
34 
35 #include "self_test/self_test.h"
36 
37 #include <stdexcept>
38 
39 // using namespace std;
40 
41 class MyNode
42 {
43 public:
44 
45  // self_test::TestRunner is the handles sequencing driver self-tests.
47 
48  // A value showing statefulness of tests
49  double some_val;
50 
52 
53  MyNode() : self_test_()
54  {
55  // If any setup work needs to be done before running the tests,
56  // a pretest can be defined. It is just like any other test, but
57  // doesn't actually do any testing.
58  self_test_.add("Pretest", this, &MyNode::pretest );
59 
60  // Tests added will be run in the order in which they are added. Each
61  // test has a name that will be automatically be filled in the
62  // DiagnosticStatus message.
63  //
64  // NOTE: self_test::TestRunner inherits its add() methods from
65  // diagnostic_updater::DiagnosticTaskVector. You will have to refer to
66  // the diagnostic_updater doxygen documentation to find them:
67  // http://www.ros.org/doc/api/diagnostic_updater/html/classdiagnostic__updater_1_1DiagnosticTaskVector.html
68  self_test_.add("ID Lookup", this, &MyNode::test1);
69  self_test_.add("Exception generating test", this, &MyNode::test2);
70  self_test_.add("Value generating test", this, &MyNode::test3);
71  self_test_.add("Value testing test", this, &MyNode::test4);
72  self_test_.add("Value testing test2", this, &MyNode::test4);
73 
74  // You can remove a test by using its name (documented in
75  // diagnostic_updater).
76  if (!self_test_.removeByName("Value testing test2"))
77  ROS_ERROR("Value testing test2 was not found when trying to remove it.");
78 
79  // If any cleanup work needs to be done after running the tests,
80  // a posttest can be defined. It is just like any other test, but
81  // doesn't actually do any testing.
82  self_test_.add("Posttest", this, &MyNode::pretest );
83  }
84 
86  {
87  ROS_INFO("Doing preparation stuff before we run our test.\n");
88  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Pretest completed successfully.");
89 
90  some_val = 1.0;
91  }
92 
93  // All tests take a reference to a DiagnosticStatusWrapper message which they should populate
94  // The default values are status.level = 2 (ERROR), and status.message = "No message was set"
95  // The status.name is automatically set to the name that was passed to add.
96  // A DiagnosticStatusWrapper is used instead of a DiagnosticStatus
97  // because it provides useful convenience methods.
99  {
100  // Look up ID here
101  char ID[] = "12345";
102  bool lookup_successful = true;
103 
104  if (lookup_successful)
105  {
106  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "ID Lookup successful");
107 
108  // One of the tests should call setID() to fill the hardware
109  // identifier in the self-test output. In cases that do not involve
110  // hardware, or for which there is no hardware identifier, an
111  // identifier of "none" should be used.
112  // For hardware devices that have a hardware identifier, the setID()
113  // method should be called
114  self_test_.setID(ID);
115 
116  } else {
117  status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "ID Lookup failed");
118  }
119  }
120 
121  // Tests do not necessarily need to catch their exceptions.
123  {
124  // Note, we start setting our status to success. Since our
125  // exception is not caught, however, the SelfTest class will
126  // change level to ERROR. This wouldn't be common practice And I
127  // would instead recommend not changing the status to success
128  // until after the exception might be generated.
129 
130  status.level = 0;
131 
132  // Exceptions of time runtime_error will actually propagate messages
133  throw std::runtime_error("we did something that threw an exception");
134 
135  // Here's where we would report success if we'd made it past
136  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We made it past the exception throwing statement.");
137  }
138 
139  // The state of the node can be changed as the tests are operating
141  {
142  // Do something that changes the state of the node
143  some_val += 41.0;
144 
145  status.add("some value", some_val);
146  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We successfully changed the value.");
147  }
148 
150  {
151  if (some_val == 42.0)
152  {
153  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We observed the change in value");
154  }
155  else
156  {
157  status.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "We failed to observe the change in value, it is currently %f.", some_val);
158  }
159  }
160 
162  {
163  ROS_INFO("Doing cleanup stuff after we run our test.\n");
164  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Posttest completed successfully.");
165  }
166 
167  bool spin()
168  {
169  while (nh_.ok())
170  {
171  //Do something important...
172  ros::Duration(1).sleep();
173 
174  // Calling checkTest tells the SelfTest class that it's ok
175  // to actually run the test. This gives you flexibility to
176  // keep the SelfTest service from necessarily interrupting
177  // other operations.
178  self_test_.checkTest();
179  }
180  return true;
181  }
182 };
183 
184 int
185 main(int argc, char** argv)
186 {
187  ros::init(argc, argv, "my_node");
188 
189  MyNode n;
190 
191  n.spin();
192 
193  return(0);
194 }
double some_val
void test4(diagnostic_updater::DiagnosticStatusWrapper &status)
void summary(unsigned char lvl, const std::string s)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void add(const std::string &name, TaskFunction f)
void test3(diagnostic_updater::DiagnosticStatusWrapper &status)
void pretest(diagnostic_updater::DiagnosticStatusWrapper &status)
ros::NodeHandle nh_
void summaryf(unsigned char lvl, const char *format,...)
void test2(diagnostic_updater::DiagnosticStatusWrapper &status)
Simple node with a self test that reports an error.
int main(int argc, char **argv)
#define ROS_INFO(...)
bool removeByName(const std::string name)
void checkTest()
Check if a self-test is pending. If so, start it and wait for it to complete.
Definition: self_test.h:107
Class to facilitate the creation of component self-tests.
Definition: self_test.h:68
void setID(std::string id)
Sets the ID of the part being tested.
Definition: self_test.h:121
void test1(diagnostic_updater::DiagnosticStatusWrapper &status)
void posttest(diagnostic_updater::DiagnosticStatusWrapper &status)
void add(const std::string &key, const T &val)
bool sleep() const
bool ok() const
#define ROS_ERROR(...)
self_test::TestRunner self_test_


self_test
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs and Blaise Gassend
autogenerated on Mon Feb 28 2022 22:19:58