wrap_python_robot_interface.cpp
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2013, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *********************************************************************/
00034 
00035 /* Author: Ioan Sucan */
00036 
00037 #include <moveit/common_planning_interface_objects/common_objects.h>
00038 #include <moveit/robot_state/conversions.h>
00039 #include <moveit/py_bindings_tools/roscpp_initializer.h>
00040 #include <moveit/py_bindings_tools/py_conversions.h>
00041 #include <moveit/py_bindings_tools/serialize_msg.h>
00042 #include <moveit_msgs/RobotState.h>
00043 
00044 #include <boost/python.hpp>
00045 #include <Python.h>
00046 
00049 namespace bp = boost::python;
00050 
00051 namespace moveit
00052 {
00053 class RobotInterfacePython : protected py_bindings_tools::ROScppInitializer
00054 {
00055 public:
00056   RobotInterfacePython(const std::string &robot_description) :
00057     py_bindings_tools::ROScppInitializer()
00058   {
00059     robot_model_ = planning_interface::getSharedRobotModel(robot_description);
00060     current_state_monitor_ = planning_interface::getSharedStateMonitor(robot_model_, planning_interface::getSharedTF());
00061   }
00062 
00063   bp::list getJointNames() const
00064   {
00065     return py_bindings_tools::listFromString(robot_model_->getJointModelNames());
00066   }
00067 
00068   bp::list getGroupJointNames(const std::string &group) const
00069   {
00070     const robot_model::JointModelGroup *jmg = robot_model_->getJointModelGroup(group);
00071     if (jmg)
00072       return py_bindings_tools::listFromString(jmg->getJointModelNames());
00073     else
00074       return bp::list();
00075   }
00076 
00077   bp::list getGroupJointTips(const std::string &group) const
00078   {
00079     const robot_model::JointModelGroup *jmg = robot_model_->getJointModelGroup(group);
00080     if (jmg)
00081     {
00082       std::vector<std::string> tips;
00083       jmg->getEndEffectorTips(tips);
00084       return py_bindings_tools::listFromString(tips);
00085     }
00086     else
00087       return bp::list();
00088   }
00089 
00090   bp::list getLinkNames() const
00091   {
00092     return py_bindings_tools::listFromString(robot_model_->getLinkModelNames());
00093   }
00094 
00095   bp::list getGroupLinkNames(const std::string &group) const
00096   {
00097     const robot_model::JointModelGroup *jmg = robot_model_->getJointModelGroup(group);
00098     if (jmg)
00099       return py_bindings_tools::listFromString(jmg->getLinkModelNames());
00100     else
00101       return bp::list();
00102   }
00103   
00104   bp::list getGroupNames() const
00105   {
00106     return py_bindings_tools::listFromString(robot_model_->getJointModelGroupNames());
00107   }
00108   
00109   bp::list getJointLimits(const std::string &name) const
00110   {
00111     bp::list result;
00112     const robot_model::JointModel *jm = robot_model_->getJointModel(name);
00113     if (jm)
00114     {
00115       const std::vector<moveit_msgs::JointLimits> &lim = jm->getVariableBoundsMsg();
00116       for (std::size_t i = 0 ; i < lim.size() ; ++i)
00117       {
00118         bp::list l;
00119         l.append(lim[i].min_position);
00120         l.append(lim[i].max_position);
00121         result.append(l);
00122       }
00123     }
00124     return result;
00125   }
00126   
00127   const char* getPlanningFrame() const
00128   {
00129     return robot_model_->getModelFrame().c_str();
00130   }
00131   
00132   bp::list getLinkPose(const std::string &name)
00133   {
00134     bp::list l;
00135     if (!ensureCurrentState())
00136       return l;
00137     robot_state::RobotStatePtr state = current_state_monitor_->getCurrentState();
00138     const robot_model::LinkModel *lm = state->getLinkModel(name);
00139     if (lm)
00140     {
00141       const Eigen::Affine3d &t = state->getGlobalLinkTransform(lm);
00142       std::vector<double> v(7);
00143       v[0] = t.translation().x();
00144       v[1] = t.translation().y();
00145       v[2] = t.translation().z();
00146       Eigen::Quaterniond q(t.rotation());
00147       v[3] = q.x();
00148       v[4] = q.y();
00149       v[5] = q.z();
00150       v[6] = q.w();
00151       l = py_bindings_tools::listFromDouble(v);
00152     }
00153     return l;
00154   }
00155   
00156   bp::list getCurrentJointValues(const std::string &name)
00157   {
00158     bp::list l;
00159     if (!ensureCurrentState())
00160       return l;
00161     robot_state::RobotStatePtr state = current_state_monitor_->getCurrentState();
00162     const robot_model::JointModel *jm = state->getJointModel(name);
00163     if (jm)
00164     {
00165       const double *pos = state->getJointPositions(jm);
00166       const unsigned int sz = jm->getVariableCount();
00167       for (unsigned int i = 0 ; i < sz ; ++i)
00168         l.append(pos[i]);
00169     }
00170     
00171     return l;
00172   }
00173   
00174   bool ensureCurrentState(double wait = 1.0)
00175   {
00176     if (!current_state_monitor_)
00177     {
00178       ROS_ERROR("Unable to get current robot state");
00179       return false;
00180     }
00181     
00182     // if needed, start the monitor and wait up to 1 second for a full robot state
00183     if (!current_state_monitor_->isActive())
00184     {
00185       current_state_monitor_->startStateMonitor();
00186       if (!current_state_monitor_->waitForCurrentState(wait))
00187         ROS_WARN("Joint values for monitored state are requested but the full state is not known");
00188     }
00189     return true;
00190   }
00191 
00192   std::string getCurrentState()
00193   {
00194     if (!ensureCurrentState())
00195       return "";
00196     robot_state::RobotStatePtr s = current_state_monitor_->getCurrentState();
00197     moveit_msgs::RobotState msg;
00198     robot_state::robotStateToRobotStateMsg(*s, msg);
00199     return py_bindings_tools::serializeMsg(msg);
00200   }
00201   
00202   bp::dict getCurrentVariableValues()
00203   {
00204     bp::dict d;
00205     
00206     if (!ensureCurrentState())
00207       return d;
00208     
00209     const std::map<std::string, double> &vars = current_state_monitor_->getCurrentStateValues();
00210     for (std::map<std::string, double>::const_iterator it = vars.begin() ; it != vars.end() ; ++it)
00211       d[it->first] = it->second;
00212 
00213     return d;
00214   }
00215 
00216   const char* getRobotRootLink() const
00217   {
00218     return robot_model_->getRootLinkName().c_str();
00219   }
00220 
00221   bool hasGroup(const std::string &group) const
00222   {
00223     return robot_model_->hasJointModelGroup(group);
00224   }
00225 
00226 private:
00227   robot_model::RobotModelConstPtr robot_model_;
00228   planning_scene_monitor::CurrentStateMonitorPtr current_state_monitor_;
00229 };
00230 }
00231 
00232 static void wrap_robot_interface()
00233 {
00234   using namespace moveit;
00235 
00236   bp::class_<RobotInterfacePython> RobotClass("RobotInterface", bp::init<std::string>());
00237 
00238   RobotClass.def("get_joint_names", &RobotInterfacePython::getJointNames);
00239   RobotClass.def("get_group_joint_names", &RobotInterfacePython::getGroupJointNames);
00240   RobotClass.def("get_group_joint_tips", &RobotInterfacePython::getGroupJointTips);
00241   RobotClass.def("get_group_names", &RobotInterfacePython::getGroupNames);
00242   RobotClass.def("get_link_names", &RobotInterfacePython::getLinkNames);
00243   RobotClass.def("get_group_link_names", &RobotInterfacePython::getGroupLinkNames);
00244   RobotClass.def("get_joint_limits", &RobotInterfacePython::getJointLimits);
00245   RobotClass.def("get_link_pose", &RobotInterfacePython::getLinkPose);
00246   RobotClass.def("get_planning_frame", &RobotInterfacePython::getPlanningFrame);
00247   RobotClass.def("get_current_state",  &RobotInterfacePython::getCurrentState);
00248   RobotClass.def("get_current_variable_values", &RobotInterfacePython::getCurrentVariableValues);
00249   RobotClass.def("get_current_joint_values",  &RobotInterfacePython::getCurrentJointValues);
00250   RobotClass.def("get_robot_root_link", &RobotInterfacePython::getRobotRootLink);
00251   RobotClass.def("has_group", &RobotInterfacePython::hasGroup);
00252 }
00253 
00254 BOOST_PYTHON_MODULE(_moveit_robot_interface)
00255 {
00256   wrap_robot_interface();
00257 }
00258 


planning_interface
Author(s): Ioan Sucan
autogenerated on Wed Aug 26 2015 12:44:13