fake_calibration_controller.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2013, Shadow Robot Company, All rights reserved.
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Lesser General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 3.0 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012  * Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public
00015  * License along with this library.
00016  */
00017 
00025 #include "sr_ronex_controllers/fake_calibration_controller.hpp"
00026 #include "pluginlib/class_list_macros.h"
00027 
00028 PLUGINLIB_EXPORT_CLASS( ronex::FakeCalibrationController, controller_interface::ControllerBase)
00029 
00030 namespace ronex
00031 {
00032   FakeCalibrationController::FakeCalibrationController()
00033     : robot_(NULL), last_publish_time_(0), state_(INITIALIZED),
00034       joint_(NULL)
00035   {
00036   }
00037 
00038   bool FakeCalibrationController::init(ros_ethercat_model::RobotState* robot, ros::NodeHandle &n)
00039   {
00040     robot_ = robot;
00041     node_ = n;
00042     // Joint
00043 
00044     std::string joint_name;
00045     if (!node_.getParam("joint", joint_name))
00046     {
00047       ROS_ERROR("No joint given (namespace: %s)", node_.getNamespace().c_str());
00048       return false;
00049     }
00050     if (!(joint_ = robot->getJointState(joint_name)))
00051     {
00052       ROS_ERROR("Could not find joint %s (namespace: %s)",
00053                 joint_name.c_str(), node_.getNamespace().c_str());
00054       return false;
00055     }
00056     joint_name_ = joint_name;
00057 
00058     // "Calibrated" topic
00059     pub_calibrated_.reset(
00060       new realtime_tools::RealtimePublisher<std_msgs::Bool>(node_, "/calibrated", 1));
00061 
00062     return true;
00063   }
00064 
00068   void FakeCalibrationController::update(const ros::Time&, const ros::Duration&)
00069   {
00070     assert(joint_);
00071 
00072     switch(state_)
00073     {
00074     case INITIALIZED:
00075       state_ = BEGINNING;
00076       break;
00077     case BEGINNING:
00078       joint_->calibrated_ = true;
00079       calib_msg_.data = true;
00080       state_ = CALIBRATED;
00081       //We add the following line to delay for some time the first publish and allow the correct initialization of the subscribers in calibrate.py
00082       last_publish_time_ = robot_->getTime();
00083       break;
00084     case CALIBRATED:
00085       if (pub_calibrated_)
00086       {
00087         if (last_publish_time_ + ros::Duration(0.5) < robot_->getTime())
00088         {
00089           assert(pub_calibrated_);
00090           if (pub_calibrated_->trylock())
00091           {
00092             last_publish_time_ = robot_->getTime();
00093             pub_calibrated_->msg_ = calib_msg_;
00094             pub_calibrated_->unlockAndPublish();
00095           }
00096         }
00097       }
00098       break;
00099     }
00100   }
00101 }
00102 
00103 /* For the emacs weenies in the crowd.
00104 Local Variables:
00105    c-basic-offset: 2
00106 End:
00107 */


sr_ronex_controllers
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless
autogenerated on Fri Aug 28 2015 13:12:37