selftest_example.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, Willow Garage, Inc.
00003  * All rights reserved.
00004  * 
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  * 
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the <ORGANIZATION> nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  * 
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 
00031 #include <ros/ros.h>
00032 
00033 #include "diagnostic_msgs/SelfTest.h"
00034 
00035 #include "self_test/self_test.h"
00036 
00037 #include <stdexcept>
00038 
00039 //  using namespace std;
00040 
00041 class MyNode
00042 {
00043 public:
00044 
00045   // self_test::TestRunner is the handles sequencing driver self-tests.
00046   self_test::TestRunner self_test_;
00047 
00048   // A value showing statefulness of tests
00049   double some_val;
00050 
00051   ros::NodeHandle nh_;
00052 
00053   MyNode() : self_test_()
00054   {
00055     // If any setup work needs to be done before running the tests,
00056     // a pretest can be defined. It is just like any other test, but
00057     // doesn't actually do any testing.
00058     self_test_.add("Pretest", this, &MyNode::pretest );
00059 
00060     // Tests added will be run in the order in which they are added. Each
00061     // test has a name that will be automatically be filled in the
00062     // DiagnosticStatus message.
00063     //
00064     // NOTE: self_test::TestRunner inherits its add() methods from
00065     // diagnostic_updater::DiagnosticTaskVector. You will have to refer to
00066     // the diagnostic_updater doxygen documentation to find them:
00067     // http://www.ros.org/doc/api/diagnostic_updater/html/classdiagnostic__updater_1_1DiagnosticTaskVector.html
00068     self_test_.add("ID Lookup",                 this, &MyNode::test1);
00069     self_test_.add("Exception generating test", this, &MyNode::test2);
00070     self_test_.add("Value generating test",     this, &MyNode::test3);
00071     self_test_.add("Value testing test",        this, &MyNode::test4);
00072     self_test_.add("Value testing test2",        this, &MyNode::test4);
00073     
00074     // You can remove a test by using its name (documented in
00075     // diagnostic_updater).
00076     if (!self_test_.removeByName("Value testing test2"))
00077       ROS_ERROR("Value testing test2 was not found when trying to remove it.");
00078 
00079     // If any cleanup work needs to be done after running the tests,
00080     // a posttest can be defined. It is just like any other test, but 
00081     // doesn't actually do any testing.
00082     self_test_.add("Posttest", this, &MyNode::pretest );
00083   }
00084 
00085   void pretest(diagnostic_updater::DiagnosticStatusWrapper& status)
00086   {
00087     ROS_INFO("Doing preparation stuff before we run our test.\n");
00088     status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Pretest completed successfully.");
00089     
00090     some_val = 1.0;
00091   }
00092 
00093   // All tests take a reference to a DiagnosticStatusWrapper message which they should populate
00094   // The default values are status.level = 2 (ERROR), and status.message = "No message was set"
00095   // The status.name is automatically set to the name that was passed to add.
00096   // A DiagnosticStatusWrapper is used instead of a DiagnosticStatus
00097   // because it provides useful convenience methods.
00098   void test1(diagnostic_updater::DiagnosticStatusWrapper& status)
00099   {
00100     // Look up ID here
00101     char ID[] = "12345";
00102     bool lookup_successful = true;
00103 
00104     if (lookup_successful)
00105     {
00106       status.summary(diagnostic_msgs::DiagnosticStatus::OK, "ID Lookup successful");
00107       
00108       // One of the tests should call setID() to fill the hardware
00109       // identifier in the self-test output. In cases that do not involve
00110       // hardware, or for which there is no hardware identifier, an
00111       // identifier of "none" should be used.
00112       // For hardware devices that have a hardware identifier, the setID()
00113       // method should be called
00114       self_test_.setID(ID);
00115 
00116     } else {
00117       status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "ID Lookup failed");
00118     }
00119   }
00120 
00121   // Tests do not necessarily need to catch their exceptions.
00122   void test2(diagnostic_updater::DiagnosticStatusWrapper& status)
00123   {
00124     // Note, we start setting our status to success.  Since our
00125     // exception is not caught, however, the SelfTest class will
00126     // change level to ERROR.  This wouldn't be common practice And I
00127     // would instead recommend not changing the status to success
00128     // until after the exception might be generated.
00129 
00130     status.level = 0;
00131 
00132     // Exceptions of time runtime_error will actually propagate messages
00133     throw std::runtime_error("we did something that threw an exception");
00134 
00135     // Here's where we would report success if we'd made it past
00136     status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We made it past the exception throwing statement.");
00137   }
00138 
00139   // The state of the node can be changed as the tests are operating
00140   void test3(diagnostic_updater::DiagnosticStatusWrapper& status)
00141   {
00142     // Do something that changes the state of the node
00143     some_val += 41.0;
00144 
00145     status.add("some value", some_val);
00146     status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We successfully changed the value.");
00147   }
00148 
00149   void test4(diagnostic_updater::DiagnosticStatusWrapper& status)
00150   {
00151     if (some_val == 42.0)
00152     {
00153       status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We observed the change in value");
00154     } 
00155     else
00156     {
00157       status.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "We failed to observe the change in value, it is currently %f.", some_val);
00158     }
00159   }
00160 
00161   void posttest(diagnostic_updater::DiagnosticStatusWrapper& status)
00162   {
00163     ROS_INFO("Doing cleanup stuff after we run our test.\n");
00164     status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Posttest completed successfully.");
00165   }
00166 
00167   bool spin()
00168   {
00169     while (nh_.ok())
00170     {
00171       //Do something important...
00172       ros::Duration(1).sleep();
00173       
00174       // Calling checkTest tells the SelfTest class that it's ok
00175       // to actually run the test.  This gives you flexibility to
00176       // keep the SelfTest service from necessarily interrupting
00177       // other operations.
00178       self_test_.checkTest();
00179     }
00180     return true;
00181   }
00182 };
00183 
00184 int
00185 main(int argc, char** argv)
00186 {
00187   ros::init(argc, argv, "my_node");
00188 
00189   MyNode n;
00190 
00191   n.spin();
00192 
00193   return(0);
00194 }


self_test
Author(s): Jeremy Leibs and Blaise Gassend
autogenerated on Fri Jan 3 2014 11:19:02