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 
53  MyNode() : self_test_()
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 
126  self_test_.checkTest();
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 }
double some_val
void test4(diagnostic_updater::DiagnosticStatusWrapper &status)
void summary(unsigned char lvl, const std::string s)
bool sleep() const
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.
#define ROS_INFO(...)
bool spin()
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)
bool ok() const
void add(const std::string &key, const T &val)
self_test::TestRunner self_test_
int main(int argc, char **argv)


self_test
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs and Blaise Gassend
autogenerated on Thu Oct 8 2020 03:19:39