position_joint_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/position_joint_interface_provider.h>
00034 
00035 namespace transmission_interface
00036 {
00037 
00038 bool PositionJointInterfaceProvider::updateJointInterfaces(const TransmissionInfo&      transmission_info,
00039                                                            hardware_interface::RobotHW* robot_hw,
00040                                                            JointInterfaces&             joint_interfaces,
00041                                                            RawJointDataMap&             raw_joint_data_map)
00042 {
00043   // Setup joint state interface first
00044   if (!JointStateInterfaceProvider::updateJointInterfaces(transmission_info,
00045                                                           robot_hw,
00046                                                           joint_interfaces,
00047                                                           raw_joint_data_map)) {return false;}
00048 
00049   // If interface does not yet exist in robot hardware abstraction, add it and use internal data structures
00050   using hardware_interface::PositionJointInterface;
00051   if (!robot_hw->get<PositionJointInterface>())
00052   {
00053     robot_hw->registerInterface(&joint_interfaces.position_joint_interface);
00054   }
00055   PositionJointInterface& interface = *(robot_hw->get<PositionJointInterface>());
00056 
00057   // Register joints on the hardware interface
00058   BOOST_FOREACH(const JointInfo& joint_info, transmission_info.joints_)
00059   {
00060     const std::string& name = joint_info.name_;
00061 
00062     // Do nothing if joint already exists on the hardware interface
00063     if (hasResource(name, interface)) {continue;}
00064 
00065     // Update hardware interface
00066     using hardware_interface::JointHandle;
00067     RawJointData& raw_joint_data = raw_joint_data_map[name];
00068     JointHandle handle(joint_interfaces.joint_state_interface.getHandle(joint_info.name_),
00069                        &raw_joint_data.position_cmd);
00070     interface.registerHandle(handle);
00071   }
00072   return true;
00073 }
00074 
00075 bool PositionJointInterfaceProvider::getJointCommandData(const TransmissionInfo& transmission_info,
00076                                                          const RawJointDataMap&  raw_joint_data_map,
00077                                                          JointData&              jnt_cmd_data)
00078 {
00079   const unsigned int dim = transmission_info.joints_.size();
00080   jnt_cmd_data.position.resize(dim);
00081 
00082   for (unsigned int i = 0; i < dim; ++i)
00083   {
00084     const std::string& joint_name = transmission_info.joints_[i].name_;
00085     RawJointDataMap::const_iterator raw_joint_data_it = raw_joint_data_map.find(joint_name);
00086     if (raw_joint_data_it == raw_joint_data_map.end()) {return false;} // Joint name not found!
00087     const RawJointData& raw_joint_data = raw_joint_data_it->second;
00088 
00089     // TODO: Get rid of these const casts!
00090     jnt_cmd_data.position[i] = const_cast<double*>(&(raw_joint_data.position_cmd));
00091   }
00092 
00093   return true;
00094 }
00095 
00096 bool PositionJointInterfaceProvider::getActuatorCommandData(const TransmissionInfo&      transmission_info,
00097                                                             hardware_interface::RobotHW* robot_hw,
00098                                                             ActuatorData&                act_cmd_data)
00099 {
00100   using hardware_interface::PositionActuatorInterface;
00101   using hardware_interface::ActuatorHandle;
00102 
00103   // Get handles to all required actuators
00104   std::vector<ActuatorHandle> handles;
00105   if (!this->getActuatorHandles<PositionActuatorInterface, ActuatorHandle>(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_cmd_data.position.resize(dim);
00112 
00113   for (unsigned int i = 0; i < dim; ++i)
00114   {
00115     // TODO: Get rid of these const casts!
00116     act_cmd_data.position[i] = const_cast<double*>(handles[i].getCommandPtr());
00117   }
00118   return true;
00119 }
00120 
00121 bool PositionJointInterfaceProvider::registerTransmission(TransmissionLoaderData& loader_data,
00122                                                           TransmissionHandleData& handle_data)
00123 {
00124   // Setup joint state interface first (if not yet done)
00125   if (!hasResource(handle_data.name, loader_data.transmission_interfaces.act_to_jnt_state))
00126   {
00127     if (!JointStateInterfaceProvider::registerTransmission(loader_data, handle_data)) {return false;}
00128   }
00129 
00130   // If command interface does not yet exist in the robot transmissions, add it and use internal data structures
00131   if (!loader_data.robot_transmissions->get<JointToActuatorPositionInterface>())
00132   {
00133     loader_data.robot_transmissions->registerInterface(&loader_data.transmission_interfaces.jnt_to_act_pos_cmd);
00134   }
00135   JointToActuatorPositionInterface& interface = *(loader_data.robot_transmissions->get<JointToActuatorPositionInterface>());
00136 
00137   // Setup command interface
00138   JointToActuatorPositionHandle handle(handle_data.name,
00139                                        handle_data.transmission.get(),
00140                                        handle_data.act_cmd_data,
00141                                        handle_data.jnt_cmd_data);
00142   interface.registerHandle(handle);
00143   return true;
00144 }
00145 
00146 } // namespace
00147 
00148 PLUGINLIB_EXPORT_CLASS(transmission_interface::PositionJointInterfaceProvider,
00149                        transmission_interface::RequisiteProvider)


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