srh_fake_joint_calibration_controller.cpp
Go to the documentation of this file.
00001 
00027 #include "sr_mechanism_controllers/srh_fake_joint_calibration_controller.h"
00028 #include "ros/time.h"
00029 #include "pluginlib/class_list_macros.h"
00030 #include <boost/algorithm/string.hpp>
00031 #include <string>
00032 #include <std_srvs/Empty.h>
00033 
00034 PLUGINLIB_EXPORT_CLASS( controller::SrhFakeJointCalibrationController, pr2_controller_interface::Controller)
00035 
00036 using namespace std;
00037 
00038 namespace controller {
00039 
00040   SrhFakeJointCalibrationController::SrhFakeJointCalibrationController()
00041     : robot_(NULL), last_publish_time_(0), state_(IS_INITIALIZED),
00042       actuator_(NULL), joint_(NULL)
00043   {
00044   }
00045 
00046   SrhFakeJointCalibrationController::~SrhFakeJointCalibrationController()
00047   {
00048   }
00049 
00050   bool SrhFakeJointCalibrationController::init(pr2_mechanism_model::RobotState *robot, ros::NodeHandle &n)
00051   {
00052     robot_ = robot;
00053     node_ = n;
00054     // Joint
00055 
00056     std::string joint_name;
00057     if (!node_.getParam("joint", joint_name))
00058     {
00059       ROS_ERROR("No joint given (namespace: %s)", node_.getNamespace().c_str());
00060       return false;
00061     }
00062     if (!(joint_ = robot->getJointState(joint_name)))
00063     {
00064       ROS_ERROR("Could not find joint %s (namespace: %s)",
00065                 joint_name.c_str(), node_.getNamespace().c_str());
00066       return false;
00067     }
00068     joint_name_ = joint_name;
00069 
00070     // Actuator
00071     std::string actuator_name;
00072     if (!node_.getParam("actuator", actuator_name))
00073     {
00074       ROS_ERROR("No actuator given (namespace: %s)", node_.getNamespace().c_str());
00075       return false;
00076     }
00077     if (!(actuator_ = robot->model_->getActuator(actuator_name)))
00078     {
00079       ROS_ERROR("Could not find actuator %s (namespace: %s)",
00080                 actuator_name.c_str(), node_.getNamespace().c_str());
00081       return false;
00082     }
00083     actuator_name_ = actuator_name;
00084 
00085     // Transmission
00086     std::string transmission_name;
00087     if (!node_.getParam("transmission", transmission_name))
00088     {
00089       ROS_ERROR("No transmission given (namespace: %s)", node_.getNamespace().c_str());
00090       return false;
00091     }
00092     transmission_ = robot->model_->getTransmission(transmission_name);
00093     if (!transmission_)
00094     {
00095       ROS_ERROR("Could not find transmission %s (namespace: %s)",
00096                 transmission_name.c_str(), node_.getNamespace().c_str());
00097       return false;
00098     }
00099 
00100     // "Calibrated" topic
00101     pub_calibrated_.reset(
00102       new realtime_tools::RealtimePublisher<std_msgs::Empty>(node_, "calibrated", 1));
00103 
00104     return true;
00105   }
00106 
00107 
00108   void SrhFakeJointCalibrationController::update()
00109   {
00110     assert(joint_);
00111     assert(actuator_);
00112 
00113     switch(state_)
00114     {
00115     case IS_INITIALIZED:
00116       state_ = BEGINNING;
00117       break;
00118     case BEGINNING:
00119       initialize_pids();
00120       joint_->calibrated_ = true;
00121       state_ = CALIBRATED;
00122       //We add the following line to delay for some time the first publish and allow the correct initialization of the subscribers in calibrate.py
00123       last_publish_time_ = robot_->getTime();
00124       break;
00125     case CALIBRATED:
00126       if (pub_calibrated_)
00127       {
00128         if (last_publish_time_ + ros::Duration(0.5) < robot_->getTime())
00129         {
00130           assert(pub_calibrated_);
00131           if (pub_calibrated_->trylock())
00132           {
00133             last_publish_time_ = robot_->getTime();
00134             pub_calibrated_->unlockAndPublish();
00135           }
00136         }
00137       }
00138       break;
00139     }
00140   }
00141 
00142   void SrhFakeJointCalibrationController::initialize_pids()
00143   {
00145     std::string service_name = "realtime_loop/reset_motor_" + boost::to_upper_copy(actuator_name_);
00146     if( ros::service::waitForService (service_name, ros::Duration(2.0)) )
00147     {
00148       std_srvs::Empty srv;
00149       if (ros::service::call(service_name, srv))
00150       {
00151         return;
00152       }
00153       else
00154       {
00155         ROS_WARN("Reset failed: %s", service_name.c_str());
00156       }
00157     }
00158   }
00159 
00160 } // namespace
00161 
00162 /* For the emacs weenies in the crowd.
00163 Local Variables:
00164    c-basic-offset: 2
00165 End:
00166 */


sr_mechanism_controllers
Author(s): Ugo Cupcic
autogenerated on Fri Aug 28 2015 13:09:56