no_id_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 
35 #include <ros/ros.h>
36 
37 #include "diagnostic_msgs/SelfTest.h"
38 
39 #include "self_test/self_test.h"
40 
41 #include <stdexcept>
42 
43 class MyNode
44 {
45 public:
46 
47  // self_test::TestRunner is the handles sequencing driver self-tests.
49 
50  // A value showing statefulness of tests
51  double some_val;
52 
54 
56  {
57  self_test_.add("Pretest", this, &MyNode::pretest );
58 
59  self_test_.add("ID Lookup", this, &MyNode::test1);
60  self_test_.add("Value generating test", this, &MyNode::test3);
61  self_test_.add("Value testing test", this, &MyNode::test4);
62 
63  self_test_.add("Posttest", this, &MyNode::pretest );
64  }
65 
67  {
68  ROS_INFO("Doing preparation stuff before we run our test.\n");
69  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Pretest completed successfully.");
70 
71  some_val = 1.0;
72  }
73 
75  {
76  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "ID not set");
77  }
78 
80  {
81  some_val += 41.0;
82 
83  status.add("some value", some_val);
84  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We successfully changed the value.");
85  }
86 
88  {
89  if (some_val == 42.0)
90  {
91  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We observed the change in value");
92  }
93  else
94  {
95  status.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "We failed to observe the change in value, it is currently %f.", some_val);
96  }
97  }
98 
100  {
101  ROS_INFO("Doing cleanup stuff after we run our test.\n");
102  status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Posttest completed successfully.");
103  }
104 
105  bool spin()
106  {
107  while (nh_.ok())
108  {
109  ros::Duration(1).sleep();
110 
112  }
113  return true;
114  }
115 };
116 
117 int
118 main(int argc, char** argv)
119 {
120  ros::init(argc, argv, "my_node");
121 
122  MyNode n;
123 
124  n.spin();
125 
126  return(0);
127 }
MyNode::spin
bool spin()
Definition: no_id_selftest.cpp:105
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)
main
int main(int argc, char **argv)
Definition: no_id_selftest.cpp:118
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
MyNode::posttest
void posttest(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition: no_id_selftest.cpp:99
ros::NodeHandle::ok
bool ok() const
MyNode::test3
void test3(diagnostic_updater::DiagnosticStatusWrapper &status)
Definition: selftest_example.cpp:140
self_test.h
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::MyNode
MyNode()
Definition: no_id_selftest.cpp:55
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:24