gazebo_hardware_sim.cpp
Go to the documentation of this file.
00001 
00025 #include "sr_gazebo_sim/gazebo_hardware_sim.h"
00026 
00027 #include <string>
00028 #include <vector>
00029 #include <map>
00030 
00031 namespace sr_gazebo_sim
00032 {
00033 
00034 const std::string SrGazeboHWSim::j0_transmission_name = "sr_mechanism_model/J0Transmission";
00035 const std::string SrGazeboHWSim::simple_transmission_name = "sr_mechanism_model/SimpleTransmission";
00036 
00037 SrGazeboHWSim::SrGazeboHWSim() :
00038     DefaultRobotHWSim(), fake_state_(NULL)
00039 {
00040 }
00041 
00042 template<class T>
00043 void SrGazeboHWSim::fixJointName(std::vector<T> *items, const std::string old_joint_name,
00044                                  const std::string new_joint_name) const
00045 {
00046   if (items->size() > 0)
00047   {
00048     items->at(0).name_ = new_joint_name;
00049 
00050     std::string xml_data = items->at(0).xml_element_;
00051     for (size_t index = xml_data.find(old_joint_name, 0); std::string::npos != index;
00052         index = xml_data.find(old_joint_name, index))
00053     {
00054       xml_data.replace(index, old_joint_name.length(), new_joint_name);
00055       index += new_joint_name.length();
00056     }
00057     items->at(0).xml_element_ = xml_data;
00058   }
00059 }
00060 
00061 void SrGazeboHWSim::addFakeTransmissionsForJ0(std::vector<transmission_interface::TransmissionInfo> *transmissions)
00062 {
00063   for (size_t i = 0; i < transmissions->size(); ++i)
00064   {
00065     if (j0_transmission_name == transmissions->at(i).type_)
00066     {
00067       if (transmissions->at(i).joints_.size() > 0)
00068       {
00069         const std::string joint_name = transmissions->at(i).joints_[0].name_;
00070         std::string second_joint_name = joint_name;
00071         second_joint_name[second_joint_name.size() - 1] = '2';
00072         this->j2_j1_joints_[second_joint_name] = joint_name;
00073       }
00074     }
00075   }
00076 
00077   for (size_t i = transmissions->size() - 1; i != static_cast<size_t>(-1); --i)
00078   {
00079     if (transmissions->at(i).joints_.size() > 0)
00080     {
00081       const std::string joint_name = transmissions->at(i).joints_[0].name_;
00082 
00083       if ((joint_name.size() > 0) && ('3' == joint_name[joint_name.size() - 1]))
00084       {
00085         std::string second_joint_name = joint_name;
00086         second_joint_name[second_joint_name.size() - 1] = '2';
00087 
00088         if (this->j2_j1_joints_.count(second_joint_name) > 0)
00089         {
00090           transmission_interface::TransmissionInfo new_transmission = transmissions->at(i);
00091 
00092           fixJointName(&new_transmission.joints_, joint_name, second_joint_name);
00093           fixJointName(&new_transmission.actuators_, joint_name, second_joint_name);
00094           transmissions->push_back(new_transmission);
00095         }
00096       }
00097     }
00098   }
00099 }
00100 
00101 bool SrGazeboHWSim::isHandJoint(const std::vector<transmission_interface::TransmissionInfo> &transmissions,
00102                                 const std::string &joint_name) const
00103 {
00104   // TODO(Andriy): Reimplement this function. It is using simple assumption that hand joint has one of
00105   // two types of transmission from sr_mechanism_model package.
00106   bool result = false;
00107   for (size_t i = 0; i < transmissions.size(); ++i)
00108   {
00109     if ((transmissions[i].joints_.size() > 0)
00110         && ((j0_transmission_name == transmissions[i].type_) || (simple_transmission_name == transmissions[i].type_)))
00111     {
00112       if (transmissions[i].joints_[0].name_ == joint_name)
00113       {
00114         result = true;
00115       }
00116     }
00117   }
00118 
00119   return result;
00120 }
00121 
00122 void SrGazeboHWSim::initializeFakeRobotState(const urdf::Model * const urdf_model,
00123                                              const std::vector<transmission_interface::TransmissionInfo> &transmissions)
00124 {
00125   this->fake_state_.robot_model_ = *urdf_model;
00126 
00127   for (std::map<std::string, boost::shared_ptr<urdf::Joint> >::const_iterator it = urdf_model->joints_.begin();
00128       it != urdf_model->joints_.end(); ++it)
00129   {
00130     if (this->isHandJoint(transmissions, it->first))
00131     {
00132       this->fake_state_.joint_states_[it->first].joint_ = it->second;
00133       this->fake_state_.joint_states_[it->first].calibrated_ = true;
00134     }
00135   }
00136 
00137   registerInterface(&this->fake_state_);
00138   ROS_INFO_STREAM("Registered fake state");
00139 }
00140 
00141 void SrGazeboHWSim::registerSecondHardwareInterface(std::vector<transmission_interface::TransmissionInfo> transmissions)
00142 {
00143   for (size_t j = 0; j < transmissions.size(); j++)
00144   {
00145     std::vector<std::string> joint_interfaces = transmissions[j].joints_[0].hardware_interfaces_;
00146     if (joint_interfaces.size() > 1)
00147     {
00148       // We use the second interface to allow to have the hand trajectory controller running on top of the hand
00149       // position controllers.
00150       // We use this trick because the DefaultRobotHWSim only deals with one (the first) hardware interface
00151       if (joint_interfaces[1] == "PositionJointInterface")
00152       {
00153         // Create position joint interface
00154         hardware_interface::JointHandle joint_handle = hardware_interface::JointHandle(
00155             js_interface_.getHandle(joint_names_[j]),
00156             &(this->fake_state_.getJointState(joint_names_[j])->commanded_position_));
00157         pj_interface_.registerHandle(joint_handle);
00158       }
00159     }
00160   }
00161 }
00162 
00163 bool SrGazeboHWSim::initSim(const std::string& robot_namespace, ros::NodeHandle model_nh,
00164                             gazebo::physics::ModelPtr parent_model, const urdf::Model * const urdf_model,
00165                             std::vector<transmission_interface::TransmissionInfo> transmissions)
00166 {
00167   this->addFakeTransmissionsForJ0(&transmissions);
00168   this->initializeFakeRobotState(urdf_model, transmissions);
00169 
00170   bool result = gazebo_ros_control::DefaultRobotHWSim::initSim(robot_namespace, model_nh, parent_model, urdf_model,
00171                                                                transmissions);
00172   if (result)
00173   {
00174     registerSecondHardwareInterface(transmissions);
00175   }
00176 
00177   return result;
00178 }
00179 
00180 void SrGazeboHWSim::readSim(ros::Time time, ros::Duration period)
00181 {
00182   gazebo_ros_control::DefaultRobotHWSim::readSim(time, period);
00183 
00184   for (unsigned int j = 0; j < n_dof_; ++j)
00185   {
00186     const std::string joint_name = joint_names_[j];
00187     if (NULL != this->fake_state_.getJointState(joint_name))
00188     {
00189       this->fake_state_.getJointState(joint_name)->position_ = this->joint_position_[j];
00190       this->fake_state_.getJointState(joint_name)->velocity_ = this->joint_velocity_[j];
00191       this->fake_state_.getJointState(joint_name)->effort_ = this->joint_effort_[j];
00192     }
00193   }
00194 
00195   this->fake_state_.current_time_ = time;
00196 }
00197 
00198 void SrGazeboHWSim::writeSim(ros::Time time, ros::Duration period)
00199 {
00200   for (unsigned int j = 0; j < n_dof_; ++j)
00201   {
00202     std::string joint_name = joint_names_[j];
00203     if (this->j2_j1_joints_.count(joint_name) > 0)
00204     {
00205       joint_name = this->j2_j1_joints_[joint_name];
00206     }
00207 
00208     if (NULL != this->fake_state_.getJointState(joint_name))
00209     {
00210       this->joint_position_command_[j] = this->fake_state_.getJointState(joint_name)->commanded_position_;
00211       this->joint_velocity_command_[j] = this->fake_state_.getJointState(joint_name)->commanded_velocity_;
00212       this->joint_effort_command_[j] = this->fake_state_.getJointState(joint_name)->commanded_effort_;
00213     }
00214   }
00215 
00216   gazebo_ros_control::DefaultRobotHWSim::writeSim(time, period);
00217 }
00218 
00219 }  // namespace sr_gazebo_sim
00220 
00221 PLUGINLIB_EXPORT_CLASS(sr_gazebo_sim::SrGazeboHWSim, gazebo_ros_control::RobotHWSim)


sr_gazebo_sim
Author(s): Andriy Petlovanyy
autogenerated on Fri Aug 21 2015 12:26:07