analogue_to_position.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 
00024 #include "sr_ronex_transmissions/mapping/general_io/analogue_to_position.hpp"
00025 #include <ros_ethercat_model/robot_state.hpp>
00026 #include <boost/lexical_cast.hpp>
00027 
00028 namespace ronex
00029 {
00030 namespace mapping
00031 {
00032 namespace general_io
00033 {
00034 AnalogueToPosition::AnalogueToPosition(TiXmlElement* mapping_el, ros_ethercat_model::RobotState* robot)
00035   : pin_out_of_bound_(true)
00036 {
00037   const char *ronex_name = mapping_el ? mapping_el->Attribute("ronex") : NULL;
00038   if (!ronex_name)
00039   {
00040     ROS_ERROR("RonexTransmission transmission did not specify the ronex name");
00041     return;
00042   }
00043 
00044   init_timer_ = nh_.createTimer(ros::Duration(0.01),
00045                                 boost::bind(&AnalogueToPosition::try_init_cb_, this, _1, mapping_el,
00046                                 robot, ronex_name));
00047 }
00048 
00049 bool AnalogueToPosition::try_init_cb_(const ros::TimerEvent&, TiXmlElement* mapping_el,
00050                                       ros_ethercat_model::RobotState* robot, const char* ronex_name)
00051 {
00052   // has the ronex been added by the driver?
00053   if ( robot->getCustomHW(ronex_name) == NULL )
00054   {
00055     return false;
00056   }
00057 
00058   // @todo: when we have multiple available module types check the module type when casting
00059   general_io_ = static_cast<ronex::GeneralIO*>(robot->getCustomHW(ronex_name));
00060   if (!general_io_)
00061   {
00062     ROS_ERROR_STREAM("The RoNeX: " << ronex_name << " was not found on the system.");
00063     return false;
00064   }
00065 
00066   // read ronex pin from urdf
00067   const char *ronex_pin = mapping_el ? mapping_el->Attribute("analogue_pin") : NULL;
00068   if (!ronex_pin)
00069   {
00070     ROS_ERROR("RonexTransmission transmission did not specify the ronex pin.");
00071     return false;
00072   }
00073   // convert pin to size_t
00074   try
00075   {
00076     pin_index_ = boost::lexical_cast<size_t>(ronex_pin);
00077   }
00078   catch( boost::bad_lexical_cast const& )
00079   {
00080     ROS_ERROR("RonexTransmission: Couldn't parse pin to an int.");
00081     return false;
00082   }
00083 
00084   // read scale
00085   const char *scale = mapping_el ? mapping_el->Attribute("scale") : NULL;
00086   if (!scale)
00087   {
00088     ROS_WARN("RonexTransmission transmission did not specify the scale, using 1.0.");
00089     scale = "1.0";
00090   }
00091   // convert scale to double
00092   try
00093   {
00094     scale_ = boost::lexical_cast<double>(scale);
00095   }
00096   catch( boost::bad_lexical_cast const& )
00097   {
00098     ROS_WARN("RonexTransmission: Couldn't parse scale to a double, using 1.0.");
00099     scale_ = 1.0;
00100   }
00101 
00102   // read offset
00103   const char *offset = mapping_el ? mapping_el->Attribute("offset") : NULL;
00104   if (!offset)
00105   {
00106     ROS_WARN("RonexTransmission transmission did not specify the offset, using 0.0.");
00107     offset = "0.0";
00108   }
00109   // convert offset to double
00110   try
00111   {
00112     offset_ = boost::lexical_cast<double>(offset);
00113   }
00114   catch( boost::bad_lexical_cast const& )
00115   {
00116     ROS_WARN("RonexTransmission: Couldn't parse offset to a double, using 0.0.");
00117     offset_ = 0.0;
00118   }
00119 
00120   ROS_DEBUG_STREAM("RoNeX" << ronex_name << " is initialised now.");
00121   // stopping timer
00122   init_timer_.stop();
00123 
00124   is_initialized_ = true;
00125   return true;
00126 }
00127 
00128 void AnalogueToPosition::propagateFromRonex(ros_ethercat_model::JointState *js)
00129 {
00130   if ( !is_initialized_ )
00131     return;
00132 
00133   if ( check_pin_in_bound_() )
00134   {
00135     js->position_ = compute_scaled_data_();
00136   }
00137 }
00138 
00139 bool AnalogueToPosition::check_pin_in_bound_()
00140 {
00141   // we ignore the first iteration as the array is not yet initialised.
00142   if ( first_iteration_ )
00143   {
00144     pin_out_of_bound_ = true;
00145     first_iteration_ = false;
00146     return false;
00147   }
00148 
00149   // we have to check here for the size otherwise the general io hasn't been updated.
00150   if ( pin_out_of_bound_ )
00151   {
00152     if ( pin_index_ >= general_io_->state_.analogue_.size() )
00153     {
00154       // size_t is always >= 0 so no need to check lower bound
00155       ROS_ERROR_STREAM("Specified pin is out of bound: " << pin_index_ << " / max = " <<
00156                                general_io_->state_.analogue_.size() <<
00157                                ", not propagating the RoNeX data to the joint position.");
00158 
00159       pin_out_of_bound_ = true;
00160       return false;
00161     }
00162   }
00163 
00164   pin_out_of_bound_ = false;
00165   return true;
00166 }
00167 
00168 double AnalogueToPosition::compute_scaled_data_()
00169 {
00170   return general_io_->state_.analogue_[pin_index_]*scale_ + offset_;
00171 }
00172 }  // namespace general_io
00173 }  // namespace mapping
00174 }  // namespace ronex
00175 /* For the emacs weenies in the crowd.
00176    Local Variables:
00177    c-basic-offset: 2
00178    End:
00179 */


sr_ronex_transmissions
Author(s): Ugo Cupcic, Toni Oliver, Mark Pitchless
autogenerated on Thu Jun 6 2019 21:21:55