test_self_test.cpp
Go to the documentation of this file.
00001 
00027 #include "sr_self_test/sr_self_test.hpp"
00028 #include <ros/ros.h>
00029 
00030 #include <diagnostic_msgs/SelfTest.h>
00031 #include <sr_robot_msgs/ManualSelfTest.h>
00032 
00033 class MyNode
00034 {
00035 public:
00036 
00037   // self_test::TestRunner is the handles sequencing driver self-tests.
00038   shadow_robot::SrTestRunner self_test_;
00039 
00040   ros::NodeHandle nh_;
00041 
00042   // A value showing statefulness of tests
00043   double some_val;
00044 
00045   MyNode() : self_test_()
00046   {
00047     self_test_.setID("12345");
00048 
00049     std::vector<std::string> services_to_test;
00050     services_to_test.push_back("sr_self_test_test/self_test");
00051     services_to_test.push_back("/rosout/get_loggers");
00052 
00053     self_test_.addServicesTest(services_to_test);
00054 
00055     self_test_.add("Testing plot - not saving", this, &MyNode::test_plot);
00056     self_test_.add("Testing plot - saving", this, &MyNode::test_plot_save);
00057     self_test_.add_diagnostic_parser();
00058     self_test_.addManualTests();
00059     self_test_.addSensorNoiseTest();
00060   }
00061 
00062   void test_plot(diagnostic_updater::DiagnosticStatusWrapper& status)
00063   {
00064     self_test_.plot(get_fake_joints(), true);
00065 
00066     status.summary(diagnostic_msgs::DiagnosticStatus::OK, "A plot should be displayed in a gnuplot window.");
00067   }
00068 
00069   void test_plot_save(diagnostic_updater::DiagnosticStatusWrapper& status)
00070   {
00071     std::string path = "/tmp/plot.png";
00072     self_test_.plot(get_fake_joints(), path, true);
00073 
00074     ros::Duration(0.5).sleep();
00075 
00076     std::ifstream test_file(path.c_str());
00077     if( test_file.good() )
00078       status.summary(diagnostic_msgs::DiagnosticStatus::OK, "The plot seems to have been saved in " + path);
00079     else
00080       status.summary(diagnostic_msgs::DiagnosticStatus::ERROR, "No file found at " + path + " the plot was probably not saved correctly.");
00081   }
00082 
00083   std::map<std::string, std::vector<double> > get_fake_joints()
00084   {
00085     std::map<std::string, std::vector<double> > joints;
00086 
00087     std::vector<double> ffj0_pos, ffj0_tar, ffj3_pos, ffj3_tar;
00088     for (int i = 0; i < 20; ++i)
00089     {
00090       ffj0_pos.push_back(1.0/float(i));
00091       ffj0_tar.push_back(1.0/float(i+1));
00092       ffj3_pos.push_back(1.0/float(i*2));
00093       ffj3_tar.push_back(2.0/float(i));
00094     }
00095 
00096     joints["FFJ0 positions"] = ffj0_pos;
00097     joints["FFJ0 targets"] = ffj0_tar;
00098     joints["FFJ3 positions"] = ffj3_pos;
00099     joints["FFJ3 targets"] = ffj3_tar;
00100 
00101     return joints;
00102   }
00103 
00104   bool spin()
00105   {
00106     while (nh_.ok())
00107     {
00108       ros::Duration(0.01).sleep();
00109 
00110       self_test_.checkTest();
00111 
00112       ros::spinOnce();
00113     }
00114     return true;
00115   }
00116 };
00117 
00118 int main(int argc, char **argv)
00119 {
00120   ros::init(argc, argv, "sr_self_test_test");
00121 
00122   MyNode n;
00123   n.spin();
00124 
00125   return(0);
00126 }
00127 
00128 /* For the emacs weenies in the crowd.
00129    Local Variables:
00130    c-basic-offset: 2
00131    End:
00132 */


sr_self_test
Author(s): Ugo Cupcic
autogenerated on Mon Oct 6 2014 07:52:52