husky_base.cpp
Go to the documentation of this file.
1 
34 #include "ros/callback_queue.h"
35 
36 #include <boost/chrono.hpp>
37 
38 typedef boost::chrono::steady_clock time_source;
39 
45  time_source::time_point &last_time)
46 {
47 
48  // Calculate monotonic time difference
49  time_source::time_point this_time = time_source::now();
50  boost::chrono::duration<double> elapsed_duration = this_time - last_time;
51  ros::Duration elapsed(elapsed_duration.count());
52  last_time = this_time;
53 
54  // Process control loop
55  husky.reportLoopDuration(elapsed);
57  cm.update(ros::Time::now(), elapsed);
59 }
60 
65 {
66  husky.updateDiagnostics();
67 }
68 
69 int main(int argc, char *argv[])
70 {
71  ros::init(argc, argv, "husky_base");
72  ros::NodeHandle nh, private_nh("~");
73 
74  double control_frequency, diagnostic_frequency;
75  private_nh.param<double>("control_frequency", control_frequency, 10.0);
76  private_nh.param<double>("diagnostic_frequency", diagnostic_frequency, 1.0);
77 
78  // Initialize robot hardware and link to controller manager
79  husky_base::HuskyHardware husky(nh, private_nh, control_frequency);
81 
82  // Setup separate queue and single-threaded spinner to process timer callbacks
83  // that interface with Husky hardware - libhorizon_legacy not threadsafe. This
84  // avoids having to lock around hardware access, but precludes realtime safety
85  // in the control loop.
86  ros::CallbackQueue husky_queue;
87  ros::AsyncSpinner husky_spinner(1, &husky_queue);
88 
89  time_source::time_point last_time = time_source::now();
90  ros::TimerOptions control_timer(
91  ros::Duration(1 / control_frequency),
92  boost::bind(controlLoop, boost::ref(husky), boost::ref(cm), boost::ref(last_time)),
93  &husky_queue);
94  ros::Timer control_loop = nh.createTimer(control_timer);
95 
96  ros::TimerOptions diagnostic_timer(
97  ros::Duration(1 / diagnostic_frequency),
98  boost::bind(diagnosticLoop, boost::ref(husky)),
99  &husky_queue);
100  ros::Timer diagnostic_loop = nh.createTimer(diagnostic_timer);
101 
102  husky_spinner.start();
103 
104  // Process remainder of ROS callbacks separately, mainly ControlManager related
105  ros::spin();
106 
107  return 0;
108 }
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
void reportLoopDuration(const ros::Duration &duration)
ROSCPP_DECL void spin(Spinner &spinner)
int main(int argc, char *argv[])
Definition: husky_base.cpp:69
bool param(const std::string &param_name, T &param_val, const T &default_val) const
void diagnosticLoop(husky_base::HuskyHardware &husky)
Definition: husky_base.cpp:64
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
boost::chrono::steady_clock time_source
Definition: husky_base.cpp:38
static Time now()
void update(const ros::Time &time, const ros::Duration &period, bool reset_controllers=false)
void controlLoop(husky_base::HuskyHardware &husky, controller_manager::ControllerManager &cm, time_source::time_point &last_time)
Definition: husky_base.cpp:43


husky_base
Author(s): Mike Purvis , Paul Bovbel
autogenerated on Fri Oct 2 2020 03:40:07