nominal_selftest.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, 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 #include "diagnostic_msgs/SelfTest.h"
00033 #include "self_test/self_test.h"
00034 #include <stdexcept>
00035 
00036 /*
00037  *\author Kevin Watts
00038  *\brief Returns nominal self-test values
00039  */
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     self_test_.add("Pretest", this, &MyNode::pretest );
00056 
00057     self_test_.add("ID Lookup",                 this, &MyNode::test1);
00058     self_test_.add("Exception generating test", this, &MyNode::test2);
00059     self_test_.add("Value generating test",     this, &MyNode::test3);
00060     self_test_.add("Value testing test",        this, &MyNode::test4);
00061 
00062     self_test_.add("Posttest", this, &MyNode::pretest );
00063   }
00064 
00065   void pretest(diagnostic_updater::DiagnosticStatusWrapper& status)
00066   {
00067     ROS_INFO("Doing preparation stuff before we run our test.\n");
00068     status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Pretest completed successfully.");
00069     
00070     some_val = 1.0;
00071   }
00072 
00073   void test1(diagnostic_updater::DiagnosticStatusWrapper& status)
00074   {
00075     // Look up ID here
00076     char ID[] = "12345";
00077     bool lookup_successful = true;
00078 
00079     if (lookup_successful)
00080     {
00081       status.summary(diagnostic_msgs::DiagnosticStatus::OK, "ID Lookup successful");
00082       
00083       self_test_.setID(ID);
00084 
00085     } else {
00086       status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "ID Lookup failed");
00087     }
00088   }
00089 
00090   // Tests do not necessarily need to catch their exceptions.
00091   void test2(diagnostic_updater::DiagnosticStatusWrapper& status)
00092   {
00093     status.level = 0;
00094 
00095     // Here's where we would report success if we'd made it past
00096     status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We made it past the exception throwing statement.");
00097   }
00098 
00099   void test3(diagnostic_updater::DiagnosticStatusWrapper& status)
00100   {
00101     some_val += 41.0;
00102 
00103     status.add("some value", some_val);
00104     status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We successfully changed the value.");
00105   }
00106 
00107   void test4(diagnostic_updater::DiagnosticStatusWrapper& status)
00108   {
00109     if (some_val == 42.0)
00110     {
00111       status.summary(diagnostic_msgs::DiagnosticStatus::OK, "We observed the change in value");
00112     } 
00113     else
00114     {
00115       status.summaryf(diagnostic_msgs::DiagnosticStatus::ERROR, "We failed to observe the change in value, it is currently %f.", some_val);
00116     }
00117   }
00118 
00119   void posttest(diagnostic_updater::DiagnosticStatusWrapper& status)
00120   {
00121     ROS_INFO("Doing cleanup stuff after we run our test.\n");
00122     status.summary(diagnostic_msgs::DiagnosticStatus::OK, "Posttest completed successfully.");
00123   }
00124 
00125   bool spin()
00126   {
00127     while (nh_.ok())
00128     {
00129       ros::Duration(1).sleep();
00130       
00131       self_test_.checkTest();
00132     }
00133     return true;
00134   }
00135 };
00136 
00137 int main(int argc, char** argv)
00138 {
00139   ros::init(argc, argv, "my_node");
00140 
00141   MyNode n;
00142 
00143   n.spin();
00144 
00145   return(0);
00146 }


self_test
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs and Blaise Gassend
autogenerated on Fri Aug 28 2015 10:30:02