husky_base.cpp
Go to the documentation of this file.
00001 
00032 #include "husky_base/husky_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(husky_base::HuskyHardware &husky,
00044                  controller_manager::ControllerManager &cm,
00045                  time_source::time_point &last_time)
00046 {
00047 
00048   // Calculate monotonic time difference
00049   time_source::time_point this_time = time_source::now();
00050   boost::chrono::duration<double> elapsed_duration = this_time - last_time;
00051   ros::Duration elapsed(elapsed_duration.count());
00052   last_time = this_time;
00053 
00054   // Process control loop
00055   husky.reportLoopDuration(elapsed);
00056   husky.updateJointsFromHardware();
00057   cm.update(ros::Time::now(), elapsed);
00058   husky.writeCommandsToHardware();
00059 }
00060 
00064 void diagnosticLoop(husky_base::HuskyHardware &husky)
00065 {
00066   husky.updateDiagnostics();
00067 }
00068 
00069 int main(int argc, char *argv[])
00070 {
00071   ros::init(argc, argv, "husky_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   husky_base::HuskyHardware husky(nh, private_nh, control_frequency);
00080   controller_manager::ControllerManager cm(&husky, nh);
00081 
00082   // Setup separate queue and single-threaded spinner to process timer callbacks
00083   // that interface with Husky hardware - libhorizon_legacy not threadsafe. This
00084   // avoids having to lock around hardware access, but precludes realtime safety
00085   // in the control loop.
00086   ros::CallbackQueue husky_queue;
00087   ros::AsyncSpinner husky_spinner(1, &husky_queue);
00088 
00089   time_source::time_point last_time = time_source::now();
00090   ros::TimerOptions control_timer(
00091     ros::Duration(1 / control_frequency),
00092     boost::bind(controlLoop, boost::ref(husky), boost::ref(cm), boost::ref(last_time)),
00093     &husky_queue);
00094   ros::Timer control_loop = nh.createTimer(control_timer);
00095 
00096   ros::TimerOptions diagnostic_timer(
00097     ros::Duration(1 / diagnostic_frequency),
00098     boost::bind(diagnosticLoop, boost::ref(husky)),
00099     &husky_queue);
00100   ros::Timer diagnostic_loop = nh.createTimer(diagnostic_timer);
00101 
00102   husky_spinner.start();
00103 
00104   // Process remainder of ROS callbacks separately, mainly ControlManager related
00105   ros::spin();
00106 
00107   return 0;
00108 }


husky_base
Author(s): Mike Purvis , Paul Bovbel
autogenerated on Sat Jun 8 2019 18:26:01