ridgeback_base.cpp
Go to the documentation of this file.
00001 
00036 #include <string>
00037 #include <boost/asio.hpp>
00038 #include <boost/bind.hpp>
00039 #include <boost/thread.hpp>
00040 #include <boost/chrono.hpp>
00041 #include <boost/foreach.hpp>
00042 #include <boost/assign/list_of.hpp>
00043 
00044 #include "controller_manager/controller_manager.h"
00045 #include "ridgeback_base/ridgeback_diagnostic_updater.h"
00046 #include "ridgeback_base/ridgeback_hardware.h"
00047 #include "ridgeback_base/ridgeback_cooling.h"
00048 #include "ridgeback_base/ridgeback_lighting.h"
00049 #include "ridgeback_base/passive_joint_publisher.h"
00050 #include "puma_motor_driver/diagnostic_updater.h"
00051 #include "ros/ros.h"
00052 #include "rosserial_server/serial_session.h"
00053 
00054 typedef boost::chrono::steady_clock time_source;
00055 
00056 void controlThread(ros::Rate rate, ridgeback_base::RidgebackHardware* robot, controller_manager::ControllerManager* cm)
00057 {
00058   time_source::time_point last_time = time_source::now();
00059 
00060   while (1)
00061   {
00062     // Calculate monotonic time elapsed
00063     time_source::time_point this_time = time_source::now();
00064     boost::chrono::duration<double> elapsed_duration = this_time - last_time;
00065     ros::Duration elapsed(elapsed_duration.count());
00066     last_time = this_time;
00067 
00068     if (robot->isActive())
00069     {
00070       robot->powerHasNotReset();
00071       robot->updateJointsFromHardware();
00072     }
00073     else
00074     {
00075       robot->configure();
00076     }
00077 
00078     robot->canSend();
00079 
00080     cm->update(ros::Time::now(), elapsed, robot->inReset());
00081 
00082     if (robot->isActive())
00083     {
00084       robot->command();
00085       robot->requestData();
00086     }
00087     else
00088     {
00089       robot->verify();
00090     }
00091 
00092     robot->canSend();
00093     rate.sleep();
00094   }
00095 }
00096 
00097 void canReadThread(ros::Rate rate, ridgeback_base::RidgebackHardware* robot)
00098 {
00099   while (1)
00100   {
00101     robot->canRead();
00102     rate.sleep();
00103   }
00104 }
00105 
00106 int main(int argc, char* argv[])
00107 {
00108   // Initialize ROS node.
00109   ros::init(argc, argv, "ridgeback_node");
00110   ros::NodeHandle nh, pnh("~");
00111 
00112   // Create the socket rosserial server in a background ASIO event loop.
00113   boost::asio::io_service io_service;
00114 
00115   // For use the serial rosserial server to connect over socat. Future work
00116   // will create a native UDP rosserial server that this can migrate to. Specify
00117   // baud rate because we have to, but in reality it doesn't matter.
00118   new rosserial_server::SerialSession(io_service, "/dev/ttyrosserial", 115200);
00119   boost::thread(boost::bind(&boost::asio::io_service::run, &io_service));
00120 
00121   std::string canbus_dev;
00122   pnh.param<std::string>("canbus_dev", canbus_dev, "can0");
00123   puma_motor_driver::SocketCANGateway gateway(canbus_dev);
00124 
00125   ridgeback_base::RidgebackHardware ridgeback(nh, pnh, gateway);
00126 
00127   // Configure the CAN connection
00128   ridgeback.init();
00129   // Create a thread to start reading can messages.
00130   boost::thread canReadT(&canReadThread, ros::Rate(200), &ridgeback);
00131 
00132   // Background thread for the controls callback.
00133   ros::NodeHandle controller_nh("");
00134   controller_manager::ControllerManager cm(&ridgeback, controller_nh);
00135   boost::thread controlT(&controlThread, ros::Rate(25), &ridgeback, &cm);
00136 
00137   // Lighting control.
00138   ridgeback_base::RidgebackLighting lighting(&nh);
00139 
00140   // Create diagnostic updater, to update itself on the ROS thread.
00141   ridgeback_base::RidgebackDiagnosticUpdater ridgeback_diagnostic_updater;
00142   puma_motor_driver::PumaMotorDriverDiagnosticUpdater puma_motor_driver_diagnostic_updater;
00143 
00144   // Joint state publisher for passive front axle.
00145   ros::V_string passive_joint = boost::assign::list_of("front_rocker");
00146   ridgeback_base::PassiveJointPublisher passive_joint_publisher(nh, passive_joint, 50);
00147 
00148   // Cooling control for the fans.
00149   ridgeback_base::RidgebackCooling cooling(&nh);
00150 
00151   // Foreground ROS spinner for ROS callbacks, including rosserial, diagnostics
00152   ros::spin();
00153 
00154   return 0;
00155 }


ridgeback_base
Author(s): Mike Purvis , Tony Baltovski
autogenerated on Sun Mar 24 2019 03:01:13