roch_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  // Calculate monotonic time difference
48  time_source::time_point this_time = time_source::now();
49  boost::chrono::duration<double> elapsed_duration = this_time - last_time;
50  ros::Duration elapsed(elapsed_duration.count());
51  last_time = this_time;
52 
53  // Process control loop
54  roch.reportLoopDuration(elapsed);
56  cm.update(ros::Time::now(), elapsed);
57  roch.writeCommandsToHardware(); //for difference driver
58  //roch.writeOverallSpeedCommandsToHardware(); //for overall speed
59 }
60 
65 {
66  roch.updateDiagnostics();
67 }
68 
69 int main(int argc, char *argv[])
70 {
71  ros::init(argc, argv, "roch_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  roch_base::rochHardware roch(nh, private_nh, control_frequency);
81  // Setup separate queue and single-threaded spinner to process timer callbacks
82  // that interface with roch hardware - libcore not threadsafe. This
83  // avoids having to lock around hardware access, but precludes realtime safety
84  // in the control loop.
85  ros::CallbackQueue roch_queue;
86  ros::AsyncSpinner roch_spinner(1, &roch_queue);
87 
88  time_source::time_point last_time = time_source::now();
89  ros::TimerOptions control_timer(
90  ros::Duration(1 / control_frequency),
91  boost::bind(controlLoop, boost::ref(roch), boost::ref(cm), boost::ref(last_time)),
92  &roch_queue);
93  ros::Timer control_loop = nh.createTimer(control_timer);
94 
95  ros::TimerOptions diagnostic_timer(
96  ros::Duration(1 / diagnostic_frequency),
97  boost::bind(diagnosticLoop, boost::ref(roch)),
98  &roch_queue);
99  ros::Timer diagnostic_loop = nh.createTimer(diagnostic_timer);
100  roch_spinner.start();
101  // Process remainder of ROS callbacks separately, mainly ControlManager related
102  ros::spin();
103 
104  return 0;
105 }
void diagnosticLoop(roch_base::rochHardware &roch)
Definition: roch_base.cpp:64
void reportLoopDuration(const ros::Duration &duration)
int main(int argc, char *argv[])
Definition: roch_base.cpp:69
ROSCPP_DECL void init(int &argc, char **argv, const std::string &name, uint32_t options=0)
ROSCPP_DECL void spin(Spinner &spinner)
boost::chrono::steady_clock time_source
Definition: roch_base.cpp:38
void controlLoop(roch_base::rochHardware &roch, controller_manager::ControllerManager &cm, time_source::time_point &last_time)
Definition: roch_base.cpp:43
bool param(const std::string &param_name, T &param_val, const T &default_val) const
Timer createTimer(Rate r, Handler h, Obj o, bool oneshot=false, bool autostart=true) const
static Time now()
void update(const ros::Time &time, const ros::Duration &period, bool reset_controllers=false)


roch_base
Author(s): Mike Purvis , Paul Bovbel , Chen
autogenerated on Mon Jun 10 2019 14:41:14