roch_base.cpp
Go to the documentation of this file.
00001 
00032 #include "roch_base/roch_hardware.h"
00033 #include "controller_manager/controller_manager.h"
00034 #include "ros/callback_queue.h"
00035 
00036 #include <boost/chrono.hpp>
00037 
00038 typedef boost::chrono::steady_clock time_source;
00039 
00043 void controlLoop(roch_base::rochHardware &roch,
00044                  controller_manager::ControllerManager &cm,
00045                  time_source::time_point &last_time)
00046 {
00047   // Calculate monotonic time difference
00048   time_source::time_point this_time = time_source::now();
00049   boost::chrono::duration<double> elapsed_duration = this_time - last_time;
00050   ros::Duration elapsed(elapsed_duration.count());
00051   last_time = this_time;
00052 
00053   // Process control loop
00054   roch.reportLoopDuration(elapsed);
00055   roch.updateJointsFromHardware();
00056   cm.update(ros::Time::now(), elapsed);
00057   roch.writeCommandsToHardware(); //for difference driver
00058   //roch.writeOverallSpeedCommandsToHardware(); //for overall speed
00059 }
00060 
00064 void diagnosticLoop(roch_base::rochHardware &roch)
00065 {
00066   roch.updateDiagnostics();
00067 }
00068 
00069 int main(int argc, char *argv[])
00070 {
00071   ros::init(argc, argv, "roch_base");
00072   ros::NodeHandle nh, private_nh("~");
00073 
00074   double control_frequency, diagnostic_frequency;
00075   private_nh.param<double>("control_frequency", control_frequency, 10.0);
00076   private_nh.param<double>("diagnostic_frequency", diagnostic_frequency, 1.0);
00077 
00078   // Initialize robot hardware and link to controller manager
00079   roch_base::rochHardware roch(nh, private_nh, control_frequency);
00080   controller_manager::ControllerManager cm(&roch, nh);
00081   // Setup separate queue and single-threaded spinner to process timer callbacks
00082   // that interface with roch hardware - libcore not threadsafe. This
00083   // avoids having to lock around hardware access, but precludes realtime safety
00084   // in the control loop.
00085   ros::CallbackQueue roch_queue;
00086   ros::AsyncSpinner roch_spinner(1, &roch_queue);
00087   
00088   time_source::time_point last_time = time_source::now();
00089   ros::TimerOptions control_timer(
00090     ros::Duration(1 / control_frequency),
00091     boost::bind(controlLoop, boost::ref(roch), boost::ref(cm), boost::ref(last_time)),
00092     &roch_queue);
00093   ros::Timer control_loop = nh.createTimer(control_timer);
00094   
00095   ros::TimerOptions diagnostic_timer(
00096     ros::Duration(1 / diagnostic_frequency),
00097     boost::bind(diagnosticLoop, boost::ref(roch)),
00098     &roch_queue);
00099   ros::Timer diagnostic_loop = nh.createTimer(diagnostic_timer);
00100   roch_spinner.start();
00101   // Process remainder of ROS callbacks separately, mainly ControlManager related
00102   ros::spin();
00103 
00104   return 0;
00105 }


roch_base
Author(s): Mike Purvis , Paul Bovbel , Carl
autogenerated on Sat Jun 8 2019 20:32:33