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 
55  MyNode() : self_test_()
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 
111  self_test_.checkTest();
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 }
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_
int main(int argc, char **argv)
void summaryf(unsigned char lvl, const char *format,...)
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 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