force_torque_sensor_controller.cpp
Go to the documentation of this file.
00001 
00002 // Copyright (C) 2012, hiDOF INC.
00003 // Copyright (C) 2013, PAL Robotics S.L.
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //   * Redistributions of source code must retain the above copyright notice,
00008 //     this list of conditions and the following disclaimer.
00009 //   * Redistributions in binary form must reproduce the above copyright
00010 //     notice, this list of conditions and the following disclaimer in the
00011 //     documentation and/or other materials provided with the distribution.
00012 //   * Neither the name of PAL Robotics S.L. nor the names of its
00013 //     contributors may be used to endorse or promote products derived from
00014 //     this software without specific prior written permission.
00015 //
00016 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00020 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026 // POSSIBILITY OF SUCH DAMAGE.
00028 
00030 
00031 
00032 #include "force_torque_sensor_controller/force_torque_sensor_controller.h"
00033 
00034 
00035 
00036 namespace force_torque_sensor_controller
00037 {
00038 
00039   bool ForceTorqueSensorController::init(hardware_interface::ForceTorqueSensorInterface* hw, ros::NodeHandle &root_nh, ros::NodeHandle& controller_nh)
00040   {
00041     // get all joint states from the hardware interface
00042     const std::vector<std::string>& sensor_names = hw->getNames();
00043     for (unsigned i=0; i<sensor_names.size(); i++)
00044       ROS_DEBUG("Got sensor %s", sensor_names[i].c_str());
00045 
00046     // get publishing period
00047     if (!controller_nh.getParam("publish_rate", publish_rate_)){
00048       ROS_ERROR("Parameter 'publish_rate' not set");
00049       return false;
00050     }
00051 
00052     for (unsigned i=0; i<sensor_names.size(); i++){
00053       // sensor handle
00054       sensors_.push_back(hw->getHandle(sensor_names[i]));
00055 
00056       // realtime publisher
00057       RtPublisherPtr rt_pub(new realtime_tools::RealtimePublisher<geometry_msgs::WrenchStamped>(root_nh, sensor_names[i], 4));
00058       realtime_pubs_.push_back(rt_pub);
00059     }
00060 
00061     // Last published times
00062     last_publish_times_.resize(sensor_names.size());
00063     return true;
00064   }
00065 
00066   void ForceTorqueSensorController::starting(const ros::Time& time)
00067   {
00068     // initialize time
00069     for (unsigned i=0; i<last_publish_times_.size(); i++){
00070       last_publish_times_[i] = time;
00071     }
00072   }
00073 
00074   void ForceTorqueSensorController::update(const ros::Time& time, const ros::Duration& period)
00075   {
00076     // limit rate of publishing
00077     for (unsigned i=0; i<realtime_pubs_.size(); i++){
00078       if (publish_rate_ > 0.0 && last_publish_times_[i] + ros::Duration(1.0/publish_rate_) < time){
00079         // try to publish
00080         if (realtime_pubs_[i]->trylock()){
00081           // we're actually publishing, so increment time
00082           last_publish_times_[i] = last_publish_times_[i] + ros::Duration(1.0/publish_rate_);
00083 
00084           // populate message
00085           realtime_pubs_[i]->msg_.header.stamp = time;
00086           realtime_pubs_[i]->msg_.header.frame_id = sensors_[i].getFrameId();
00087 
00088           realtime_pubs_[i]->msg_.wrench.force.x  = sensors_[i].getForce()[0];
00089           realtime_pubs_[i]->msg_.wrench.force.y  = sensors_[i].getForce()[1];
00090           realtime_pubs_[i]->msg_.wrench.force.z  = sensors_[i].getForce()[2];
00091           realtime_pubs_[i]->msg_.wrench.torque.x = sensors_[i].getTorque()[0];
00092           realtime_pubs_[i]->msg_.wrench.torque.y = sensors_[i].getTorque()[1];
00093           realtime_pubs_[i]->msg_.wrench.torque.z = sensors_[i].getTorque()[2];
00094 
00095           realtime_pubs_[i]->unlockAndPublish();
00096         }
00097       }
00098     }
00099   }
00100 
00101   void ForceTorqueSensorController::stopping(const ros::Time& time)
00102   {}
00103 
00104 }
00105 
00106 PLUGINLIB_EXPORT_CLASS(force_torque_sensor_controller::ForceTorqueSensorController, controller_interface::ControllerBase)


force_torque_sensor_controller
Author(s): Adolfo Rodriguez Tsouroukdissian
autogenerated on Fri Aug 28 2015 12:36:57