timer_test.cpp
Go to the documentation of this file.
1 #include <ros/ros.h>
2 #include <swri_roscpp/timer.h>
4 
6 
7 namespace du = diagnostic_updater;
8 
9 // Alias type for easier access to DiagnosticStatus enumerations.
10 typedef diagnostic_msgs::DiagnosticStatus DS;
11 
12 class TimerTest
13 {
18 
20 
22 
23  public:
25  {
26  // Setup a one-shot timer to initialize the node after a brief
27  // delay so that /rosout is always fully initialized.
28  ROS_INFO("Starting initialization timer...");
29  init_timer_ = nh_.createWallTimer(ros::WallDuration(1.0),
31  this,
32  true);
33  }
34 
35  void initialize(const ros::WallTimerEvent &ignored)
36  {
37  update_timer_ = swri::Timer(nh_, ros::Duration(1.0/50.0),
39  this);
40 
41  ros::NodeHandle pnh("~");
42  swri::param(pnh, "fibonacci_index", fibonacci_index_, 30);
43 
44  diagnostic_updater_.setHardwareID("none");
45  diagnostic_updater_.add(
46  "swri::Timer test", this,
48 
49  diag_timer_ = swri::Timer(nh_, ros::Duration(1.0),
51  this);
52  }
53 
54  void handleUpdateTimer(const ros::TimerEvent &ignored)
55  {
56  // Do some work to give us a measurable time.
57  size_t number = super_slow_fibonacci(fibonacci_index_);
58  ROS_INFO("The %d-th number of the fibonacci sequence is %lu",
59  fibonacci_index_, number);
60  }
61 
63  {
64  if (x <= 0) {
65  return 0;
66  } else if (x == 1) {
67  return 1;
68  } else {
69  return super_slow_fibonacci(x-1) + super_slow_fibonacci(x-2);
70  }
71  }
72 
74  {
75  diagnostic_updater_.update();
76  }
77 
79  {
80  status.summary(DS::OK, "No errors reported.");
81 
82  // This example uses swri::Timer's appendDiagnostics method to
83  // include diagnostics using a common format and common summary
84  // semantics. If we didn't care about specific bits of
85  // information, we use more specific flags.
86  update_timer_.appendDiagnostics(status, "Update",
88  }
89 
90 }; // class TimerTest
91 
92 int main(int argc, char **argv)
93 {
94  ros::init(argc, argv, "timer_test");
95 
96  TimerTest node;
97  ros::spin();
98 
99  return 0;
100 }
int fibonacci_index_
Definition: timer_test.cpp:21
void appendDiagnostics(diagnostic_updater::DiagnosticStatusWrapper &status, const std::string &name, const int flags)
Definition: timer.h:228
void handleDiagnosticsTimer(const ros::TimerEvent &ignored)
Definition: timer_test.cpp:73
int main(int argc, char **argv)
Definition: timer_test.cpp:92
void timerDiagnostics(du::DiagnosticStatusWrapper &status)
Definition: timer_test.cpp:78
du::Updater diagnostic_updater_
Definition: timer_test.cpp:19
void summary(unsigned char lvl, const std::string s)
void setHardwareID(const std::string &hwid)
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void add(const std::string &name, TaskFunction f)
diagnostic_msgs::DiagnosticStatus DS
Definition: timer_test.cpp:10
ros::WallTimer init_timer_
Definition: timer_test.cpp:15
WallTimer createWallTimer(WallDuration period, void(T::*callback)(const WallTimerEvent &), T *obj, bool oneshot=false, bool autostart=true) const
#define ROS_INFO(...)
int super_slow_fibonacci(int x)
Definition: timer_test.cpp:62
void param(swri::NodeHandle &nh, const std::string name, std::string &value, const std::string def, const std::string description)
Definition: node_handle.h:1159
ROSCPP_DECL void spin()
swri::Timer diag_timer_
Definition: timer_test.cpp:17
void initialize(const ros::WallTimerEvent &ignored)
Definition: timer_test.cpp:35
swri::Timer update_timer_
Definition: timer_test.cpp:16
ros::NodeHandle nh_
Definition: timer_test.cpp:14
void handleUpdateTimer(const ros::TimerEvent &ignored)
Definition: timer_test.cpp:54


swri_roscpp
Author(s):
autogenerated on Sat Jan 21 2023 03:13:16