joint_state_interface_provider.cpp
Go to the documentation of this file.
00001 
00002 // Copyright (C) 2013, PAL Robotics S.L.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are met:
00006 //   * Redistributions of source code must retain the above copyright notice,
00007 //     this list of conditions and the following disclaimer.
00008 //   * Redistributions in binary form must reproduce the above copyright
00009 //     notice, this list of conditions and the following disclaimer in the
00010 //     documentation and/or other materials provided with the distribution.
00011 //   * Neither the name of PAL Robotics S.L. nor the names of its
00012 //     contributors may be used to endorse or promote products derived from
00013 //     this software without specific prior written permission.
00014 //
00015 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025 // POSSIBILITY OF SUCH DAMAGE.
00027 
00028 
00029 // Pluginlib
00030 #include <pluginlib/class_list_macros.h>
00031 
00032 // ros_control
00033 #include <transmission_interface/joint_state_interface_provider.h>
00034 
00035 namespace transmission_interface
00036 {
00037 
00038 bool JointStateInterfaceProvider::updateJointInterfaces(const TransmissionInfo&      transmission_info,
00039                                                         hardware_interface::RobotHW* robot_hw,
00040                                                         JointInterfaces&             joint_interfaces,
00041                                                         RawJointDataMap&             raw_joint_data_map)
00042 {
00043   // If interface does not yet exist in robot hardware abstraction, add it and use internal data structures
00044   using hardware_interface::JointStateInterface;
00045   if (!robot_hw->get<JointStateInterface>())
00046   {
00047     robot_hw->registerInterface(&joint_interfaces.joint_state_interface);
00048   }
00049   JointStateInterface& interface = *(robot_hw->get<JointStateInterface>());
00050 
00051   // Register joints on the hardware interface
00052   BOOST_FOREACH(const JointInfo& joint_info, transmission_info.joints_)
00053   {
00054     const std::string& name = joint_info.name_;
00055 
00056     // Do nothing if joint already exists on the hardware interface
00057     if (hasResource(name, interface)) {continue;}
00058 
00059     // Update hardware interface
00060     using hardware_interface::JointStateHandle;
00061     RawJointData& raw_joint_data = raw_joint_data_map[name]; // Add joint if it does not yet exist
00062     JointStateHandle handle(name,
00063                             &raw_joint_data.position,
00064                             &raw_joint_data.velocity,
00065                             &raw_joint_data.effort);
00066     interface.registerHandle(handle);
00067   }
00068   return true;
00069 }
00070 
00071 bool JointStateInterfaceProvider::getJointStateData(const TransmissionInfo& transmission_info,
00072                                                     const RawJointDataMap&  raw_joint_data_map,
00073                                                     JointData&              jnt_state_data)
00074 {
00075   const unsigned int dim = transmission_info.joints_.size();
00076   jnt_state_data.position.resize(dim);
00077   jnt_state_data.velocity.resize(dim);
00078   jnt_state_data.effort.resize(dim);
00079 
00080   for (unsigned int i = 0; i < dim; ++i)
00081   {
00082     const std::string& joint_name = transmission_info.joints_[i].name_;
00083     RawJointDataMap::const_iterator raw_joint_data_it = raw_joint_data_map.find(joint_name);
00084     if (raw_joint_data_it == raw_joint_data_map.end()) {return false;} // Joint name not found!
00085     const RawJointData& raw_joint_data = raw_joint_data_it->second;
00086 
00087     // TODO: Get rid of these const casts!
00088     jnt_state_data.position[i] = const_cast<double*>(&(raw_joint_data.position));
00089     jnt_state_data.velocity[i] = const_cast<double*>(&(raw_joint_data.velocity));
00090     jnt_state_data.effort[i]   = const_cast<double*>(&(raw_joint_data.effort));
00091   }
00092 
00093   return true;
00094 }
00095 
00096 bool JointStateInterfaceProvider::getActuatorStateData(const TransmissionInfo&      transmission_info,
00097                                                        hardware_interface::RobotHW* robot_hw,
00098                                                        ActuatorData&                act_state_data)
00099 {
00100   using hardware_interface::ActuatorStateInterface;
00101   using hardware_interface::ActuatorStateHandle;
00102 
00103   // Get handles to all required actuators
00104   std::vector<ActuatorStateHandle> handles;
00105   if (!this->getActuatorHandles<ActuatorStateInterface, ActuatorStateHandle>(transmission_info.actuators_,
00106                                                                              robot_hw,
00107                                                                              handles)) {return false;}
00108 
00109   // Populate actuator data
00110   const unsigned int dim = transmission_info.actuators_.size();
00111   act_state_data.position.resize(dim);
00112   act_state_data.velocity.resize(dim);
00113   act_state_data.effort.resize(dim);
00114 
00115   for (unsigned int i = 0; i < dim; ++i)
00116   {
00117     // TODO: Get rid of these const casts!
00118     act_state_data.position[i] = const_cast<double*>(handles[i].getPositionPtr());
00119     act_state_data.velocity[i] = const_cast<double*>(handles[i].getVelocityPtr());
00120     act_state_data.effort[i]   = const_cast<double*>(handles[i].getEffortPtr());
00121   }
00122   return true;
00123 }
00124 
00125 bool JointStateInterfaceProvider::registerTransmission(TransmissionLoaderData& loader_data,
00126                                                        TransmissionHandleData& handle_data)
00127 {
00128   // If interface does not yet exist in the robot transmissions, add it and use internal data structures
00129   if (!loader_data.robot_transmissions->get<ActuatorToJointStateInterface>())
00130   {
00131     loader_data.robot_transmissions->registerInterface(&loader_data.transmission_interfaces.act_to_jnt_state);
00132   }
00133   ActuatorToJointStateInterface& interface = *(loader_data.robot_transmissions->get<ActuatorToJointStateInterface>());
00134 
00135   // Update transmission interface
00136   ActuatorToJointStateHandle handle(handle_data.name,
00137                                     handle_data.transmission.get(),
00138                                     handle_data.act_state_data,
00139                                     handle_data.jnt_state_data);
00140   interface.registerHandle(handle);
00141   return true;
00142 }
00143 
00144 } // namespace
00145 
00146 PLUGINLIB_EXPORT_CLASS(transmission_interface::JointStateInterfaceProvider,
00147                        transmission_interface::RequisiteProvider)


transmission_interface
Author(s): Adolfo Rodriguez Tsouroukdissian
autogenerated on Sat Jun 8 2019 20:09:31