sr_tactile_sensor_controller.cpp
Go to the documentation of this file.
00001 
00012 
00013 // Copyright (C) 2012, hiDOF INC.
00014 // Copyright (C) 2013, PAL Robotics S.L.
00015 //
00016 // Redistribution and use in source and binary forms, with or without
00017 // modification, are permitted provided that the following conditions are met:
00018 // * Redistributions of source code must retain the above copyright notice,
00019 // this list of conditions and the following disclaimer.
00020 // * Redistributions in binary form must reproduce the above copyright
00021 // notice, this list of conditions and the following disclaimer in the
00022 // documentation and/or other materials provided with the distribution.
00023 // * Neither the name of PAL Robotics S.L. nor the names of its
00024 // contributors may be used to endorse or promote products derived from
00025 // this software without specific prior written permission.
00026 //
00027 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037 // POSSIBILITY OF SUCH DAMAGE.
00040 
00041 #include "sr_tactile_sensor_controller/sr_tactile_sensor_controller.hpp"
00042 #include <pluginlib/class_list_macros.h>
00043 #include <sr_tactile_sensor_controller/sr_pst_tactile_sensor_publisher.hpp>
00044 #include <sr_tactile_sensor_controller/sr_biotac_tactile_sensor_publisher.hpp>
00045 #include <sr_tactile_sensor_controller/sr_ubi_tactile_sensor_publisher.hpp>
00046 #include <string>
00047 
00048 
00049 namespace controller
00050 {
00051 SrTactileSensorController::SrTactileSensorController()
00052   : initialized_(false), sensors_(NULL)
00053 {}
00054 
00055 bool SrTactileSensorController::init(ros_ethercat_model::RobotStateInterface* hw,
00056                                      ros::NodeHandle &root_nh, ros::NodeHandle& controller_nh)
00057 {
00058   ROS_ASSERT(hw);
00059 
00060   ros_ethercat_model::RobotState* robot_state;
00061   std::string robot_state_name;
00062   controller_nh.param<std::string>("robot_state_name", robot_state_name, "unique_robot_hw");
00063 
00064   bool use_ns = true;
00065   ros::NodeHandle nh_priv("~");
00066 
00067   try
00068   {
00069     robot_state = hw->getHandle(robot_state_name).getState();
00070   }
00071   catch(const hardware_interface::HardwareInterfaceException& e)
00072   {
00073     ROS_ERROR_STREAM("Could not find robot state: " << robot_state_name << " Not loading the controller. " << e.what());
00074     return false;
00075   }
00076 
00077   if (!nh_priv.getParam("use_ns", use_ns))
00078   {
00079     ROS_INFO("Private parameter 'use_ns' not set, default is using namespace");
00080   }
00081 
00082   if (!controller_nh.getParam("prefix", prefix_))
00083   {
00084     ROS_ERROR("Parameter 'prefix' not set");
00085     return false;
00086   }
00087 
00088   // this should handle the case where we don't want a prefix
00089   if (!prefix_.empty())
00090   {
00091     if (use_ns)
00092       nh_prefix_ = ros::NodeHandle(root_nh, prefix_);
00093     else
00094       nh_prefix_ = ros::NodeHandle(root_nh);
00095 
00096     prefix_+="_";
00097   }
00098   else
00099   {
00100     nh_prefix_ = ros::NodeHandle(root_nh);
00101   }
00102 
00103   // get all sensors from the hardware interface
00104   // apparently all the actuators have the tactile data copied in, so take the first one.
00105   sr_actuator::SrMotorActuator* motor_actuator = static_cast<sr_actuator::SrMotorActuator*>(
00106     robot_state->getActuator(prefix_+"FFJ0"));
00107   if (motor_actuator)
00108   {
00109     sensors_ = motor_actuator->motor_state_.tactiles_;
00110 
00111     // get publishing period
00112     if (!controller_nh.getParam("publish_rate", publish_rate_))
00113     {
00114       ROS_ERROR("Parameter 'publish_rate' not set");
00115       return false;
00116     }
00117 
00118     return true;
00119   }
00120   else
00121   {
00122     ROS_ERROR_STREAM("Could not find the " << prefix_ << "FFJ0 actuator");
00123     return false;
00124   }
00125 }
00126 
00127 void SrTactileSensorController::update(const ros::Time& time, const ros::Duration& period)
00128 {
00129   if (!initialized_)
00130   {
00131     if (sensors_)
00132     {
00133       if (!sensors_->empty())
00134       {
00135   if (!sensors_->at(0).type.empty())
00136   {
00137     if (sensors_->at(0).type == "pst")
00138     {
00139       sensor_publisher_.reset(new SrPSTTactileSensorPublisher(sensors_, publish_rate_, nh_prefix_, prefix_));
00140     }
00141     else if (sensors_->at(0).type == "biotac")
00142     {
00143       sensor_publisher_.reset(new SrBiotacTactileSensorPublisher(sensors_, publish_rate_, nh_prefix_, prefix_));
00144     }
00145     else if (sensors_->at(0).type == "ubi")
00146     {
00147       sensor_publisher_.reset(new SrUbiTactileSensorPublisher(sensors_, publish_rate_, nh_prefix_, prefix_));
00148     }
00149     else
00150     {
00151       ROS_FATAL_STREAM("Unknown tactile sensor type: " << sensors_->at(0).type);
00152     }
00153 
00154     // initialize pusblisher and starting time
00155     sensor_publisher_->init(time);
00156     initialized_ = true;
00157   }
00158       }
00159     }
00160   }
00161   else
00162   {
00163     sensor_publisher_->update(time, period);
00164   }
00165 }
00166 
00167 void SrTactileSensorController::starting(const ros::Time& time)
00168 {
00169 }
00170 
00171 void SrTactileSensorController::stopping(const ros::Time& time)
00172 {
00173   // remove initialized flag to permit data type change and time resetting
00174   initialized_ = false;
00175 }
00176 }  // namespace controller
00177 
00178 
00179 PLUGINLIB_EXPORT_CLASS(controller::SrTactileSensorController, controller_interface::ControllerBase)
00180 
00181 /* For the emacs weenies in the crowd.
00182 Local Variables:
00183    c-basic-offset: 2
00184 End:
00185  */


sr_tactile_sensor_controller
Author(s): Guillaume Walck
autogenerated on Mon Jul 1 2019 20:06:36