nominal_selftest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, 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 
36 /*
37  *\author Kevin Watts
38  *\brief Returns nominal self-test values
39  */
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  self_test_.add("Pretest", this, &MyNode::pretest );
56 
57  self_test_.add("ID Lookup", this, &MyNode::test1);
58  self_test_.add("Exception generating 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 
83  self_test_.setID(ID);
84 
85  } else {
86  status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "ID Lookup failed");
87  }
88  }
89 
90  // Tests do not necessarily need to catch their exceptions.
92  {
93  status.level = 0;
94 
95  // Here's where we would report success if we'd made it past
96  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We made it past the exception throwing statement.");
97  }
98 
100  {
101  some_val += 41.0;
102 
103  status.add("some value", some_val);
104  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We successfully changed the value.");
105  }
106 
108  {
109  if (some_val == 42.0)
110  {
111  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We observed the change in value");
112  }
113  else
114  {
115  status.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "We failed to observe the change in value, it is currently %f.", some_val);
116  }
117  }
118 
120  {
121  ROS_INFO("Doing cleanup stuff after we run our test.\n");
122  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Posttest completed successfully.");
123  }
124 
125  bool spin()
126  {
127  while (nh_.ok())
128  {
129  ros::Duration(1).sleep();
130 
131  self_test_.checkTest();
132  }
133  return true;
134  }
135 };
136 
137 int main(int argc, char** argv)
138 {
139  ros::init(argc, argv, "my_node");
140 
141  MyNode n;
142 
143  n.spin();
144 
145  return(0);
146 }
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(...)
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
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