timer_test.cpp
Go to the documentation of this file.
00001 #include <ros/ros.h>
00002 #include <swri_roscpp/timer.h>
00003 #include <swri_roscpp/parameters.h>
00004 
00005 #include <diagnostic_updater/diagnostic_updater.h>
00006 
00007 namespace du = diagnostic_updater;
00008 
00009 // Alias type for easier access to DiagnosticStatus enumerations.
00010 typedef diagnostic_msgs::DiagnosticStatus DS;
00011 
00012 class TimerTest
00013 {
00014   ros::NodeHandle nh_;
00015   ros::WallTimer init_timer_;
00016   swri::Timer update_timer_;
00017   swri::Timer diag_timer_;
00018 
00019   du::Updater diagnostic_updater_;
00020 
00021   int fibonacci_index_;
00022   
00023  public:
00024   TimerTest()
00025   {
00026     // Setup a one-shot timer to initialize the node after a brief
00027     // delay so that /rosout is always fully initialized.
00028     ROS_INFO("Starting initialization timer...");
00029     init_timer_ = nh_.createWallTimer(ros::WallDuration(1.0),
00030                                       &TimerTest::initialize,
00031                                       this,
00032                                       true);
00033   }
00034 
00035   void initialize(const ros::WallTimerEvent &ignored)
00036   {
00037     update_timer_ = swri::Timer(nh_, ros::Duration(1.0/50.0),
00038                                 &TimerTest::handleUpdateTimer,
00039                                 this);
00040 
00041     ros::NodeHandle pnh("~");
00042     swri::param(pnh, "fibonacci_index", fibonacci_index_, 30);
00043     
00044     diagnostic_updater_.setHardwareID("none");
00045     diagnostic_updater_.add(
00046       "swri::Timer test", this,
00047       &TimerTest::timerDiagnostics);
00048     
00049     diag_timer_ = swri::Timer(nh_, ros::Duration(1.0),
00050                               &TimerTest::handleDiagnosticsTimer,
00051                               this);
00052   }
00053 
00054   void handleUpdateTimer(const ros::TimerEvent &ignored)
00055   {
00056     // Do some work to give us a measurable time.
00057     size_t number = super_slow_fibonacci(fibonacci_index_);
00058     ROS_INFO("The %d-th number of the fibonacci sequence is %lu",
00059              fibonacci_index_, number);
00060   }
00061 
00062   int super_slow_fibonacci(int x)
00063   {
00064     if (x <= 0) {
00065       return 0;
00066     } else if (x == 1) {
00067       return 1;
00068     } else {
00069       return super_slow_fibonacci(x-1) + super_slow_fibonacci(x-2);
00070     }
00071   }
00072 
00073   void handleDiagnosticsTimer(const ros::TimerEvent &ignored)
00074   {
00075     diagnostic_updater_.update();
00076   }
00077 
00078   void timerDiagnostics(du::DiagnosticStatusWrapper& status) // NOLINT
00079   {
00080     status.summary(DS::OK, "No errors reported.");
00081 
00082     // This example uses swri::Timer's appendDiagnostics method to
00083     // include diagnostics using a common format and common summary
00084     // semantics.  If we didn't care about specific bits of
00085     // information, we use more specific flags.
00086     update_timer_.appendDiagnostics(status, "Update",
00087                                     swri::Timer::DIAG_ALL);
00088   }
00089   
00090 };  // class TimerTest
00091 
00092 int main(int argc, char **argv)
00093 {
00094   ros::init(argc, argv, "timer_test");
00095 
00096   TimerTest node;
00097   ros::spin();
00098 
00099   return 0;
00100 }


swri_roscpp
Author(s):
autogenerated on Tue Oct 3 2017 03:19:27