Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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
00053 if ( robot->getCustomHW(ronex_name) == NULL )
00054 {
00055 return false;
00056 }
00057
00058
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
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
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
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
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
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
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
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
00142 if ( first_iteration_ )
00143 {
00144 pin_out_of_bound_ = true;
00145 first_iteration_ = false;
00146 return false;
00147 }
00148
00149
00150 if ( pin_out_of_bound_ )
00151 {
00152 if ( pin_index_ >= general_io_->state_.analogue_.size() )
00153 {
00154
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 }
00173 }
00174 }
00175
00176
00177
00178
00179