error_selftest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019, 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 #include "diagnostic_msgs/SelfTest.h"
33 #include "self_test/self_test.h"
34 #include <stdexcept>
35 
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 
54  {
55  self_test_.add("Pretest", this, &MyNode::pretest );
56 
57  self_test_.add("ID Lookup", this, &MyNode::test1);
58  self_test_.add("Error test", this, &MyNode::test2);
59  self_test_.add("Value generating test", this, &MyNode::test3);
60  self_test_.add("Value testing test", this, &MyNode::test4);
61 
62  self_test_.add("Posttest", this, &MyNode::pretest );
63  }
64 
66  {
67  ROS_INFO("Doing preparation stuff before we run our test.\n");
68  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Pretest completed successfully.");
69 
70  some_val = 1.0;
71  }
72 
74  {
75  // Look up ID here
76  char ID[] = "12345";
77  bool lookup_successful = true;
78 
79  if (lookup_successful)
80  {
81  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "ID Lookup successful");
82  self_test_.setID(ID);
83  }
84  else
85  status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "ID Lookup failed");
86  }
87 
89  {
90  // Report an error, this should fail self test
91  status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "Reporting an error");
92  }
93 
95  {
96  some_val += 41.0;
97 
98  status.add("some value", some_val);
99  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We successfully changed the value.");
100  }
101 
103  {
104  if (some_val == 42.0)
105  {
106  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We observed the change in value");
107  }
108  else
109  {
110  status.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "We failed to observe the change in value, it is currently %f.", some_val);
111  }
112  }
113 
115  {
116  ROS_INFO("Doing cleanup stuff after we run our test.\n");
117  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Posttest completed successfully.");
118  }
119 
120  bool spin()
121  {
122  while (nh_.ok())
123  {
124  ros::Duration(1).sleep();
125 
127  }
128  return true;
129  }
130 };
131 
132 int
133 main(int argc, char** argv)
134 {
135  ros::init(argc, argv, "my_node");
136 
137  MyNode n;
138 
139  n.spin();
140 
141  return(0);
142 }
MyNode::spin
bool spin()
Definition: error_selftest.cpp:120
diagnostic_updater::DiagnosticStatusWrapper::add
void add(const std::string &key, const bool &b)
ros::init
ROSCPP_DECL void init(const M_string &remappings, const std::string &name, uint32_t options=0)
MyNode::some_val
double some_val
Definition: selftest_example.cpp:49
ros.h
MyNode::test4
void test4(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition: selftest_example.cpp:149
MyNode::test1
void test1(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition: selftest_example.cpp:98
self_test::TestRunner::add
void add(const std::string &name, TaskFunction f)
diagnostic_updater::DiagnosticStatusWrapper::summary
void summary(const diagnostic_msgs::DiagnosticStatus &src)
MyNode::nh_
ros::NodeHandle nh_
Definition: selftest_example.cpp:51
self_test::TestRunner
Class to facilitate the creation of component self-tests.
Definition: self_test.h:100
MyNode
Simple node with a self test that reports an error.
Definition: selftest_example.cpp:41
self_test::TestRunner::setID
void setID(std::string id)
Sets the ID of the part being tested.
Definition: self_test.h:153
MyNode::posttest
void posttest(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition: error_selftest.cpp:114
ros::NodeHandle::ok
bool ok() const
MyNode::test3
void test3(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition: selftest_example.cpp:140
self_test.h
main
int main(int argc, char **argv)
Definition: error_selftest.cpp:133
self_test::TestRunner::checkTest
void checkTest()
Check if a self-test is pending. If so, start it and wait for it to complete.
Definition: self_test.h:139
MyNode::pretest
void pretest(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition: selftest_example.cpp:85
diagnostic_updater::DiagnosticStatusWrapper
ros::Duration::sleep
bool sleep() const
MyNode::test2
void test2(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition: selftest_example.cpp:122
MyNode::MyNode
MyNode()
Definition: error_selftest.cpp:53
ROS_INFO
#define ROS_INFO(...)
ros::Duration
diagnostic_updater::DiagnosticStatusWrapper::summaryf
void summaryf(unsigned char lvl, const char *format,...)
ros::NodeHandle
MyNode::self_test_
self_test::TestRunner self_test_
Definition: selftest_example.cpp:46


self_test
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs and Blaise Gassend
autogenerated on Tue Nov 15 2022 03:17:23