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 the 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/py_bindings_tools/roscpp_initializer.h>
00039 #include <moveit/py_bindings_tools/py_conversions.h>
00040 
00041 #include <boost/python.hpp>
00042 #include <Python.h>
00043 
00046 namespace bp = boost::python;
00047 
00048 namespace moveit
00049 {
00050 class RobotInterfacePython : protected py_bindings_tools::ROScppInitializer
00051 {
00052 public:
00053   RobotInterfacePython(const std::string &robot_description) :
00054     py_bindings_tools::ROScppInitializer()
00055   {
00056     robot_model_ = planning_interface::getSharedRobotModel(robot_description);
00057     current_state_monitor_ = planning_interface::getSharedStateMonitor(robot_model_, planning_interface::getSharedTF());
00058   }
00059 
00060   bp::list getJointNames() const
00061   {
00062     return py_bindings_tools::listFromString(robot_model_->getJointModelNames());
00063   }
00064 
00065   bp::list getGroupJointNames(const std::string &group) const
00066   {
00067     const robot_model::JointModelGroup *jmg = robot_model_->getJointModelGroup(group);
00068     if (jmg)
00069       return py_bindings_tools::listFromString(jmg->getJointModelNames());
00070     else
00071       return bp::list();
00072   }
00073 
00074   bp::list getLinkNames() const
00075   {
00076     return py_bindings_tools::listFromString(robot_model_->getLinkModelNames());
00077   }
00078 
00079   bp::list getGroupLinkNames(const std::string &group) const
00080   {
00081     const robot_model::JointModelGroup *jmg = robot_model_->getJointModelGroup(group);
00082     if (jmg)
00083       return py_bindings_tools::listFromString(jmg->getLinkModelNames());
00084     else
00085       return bp::list();
00086   }
00087 
00088   bp::list getGroupNames() const
00089   {
00090     return py_bindings_tools::listFromString(robot_model_->getJointModelGroupNames());
00091   }
00092 
00093   bp::list getJointLimits(const std::string &name) const
00094   {
00095     bp::list result;
00096     const robot_model::JointModel *jm = robot_model_->getJointModel(name);
00097     if (jm)
00098     {
00099       const std::vector<moveit_msgs::JointLimits> &lim = jm->getVariableLimits();
00100       for (std::size_t i = 0 ; i < lim.size() ; ++i)
00101       {
00102         bp::list l;
00103         l.append(lim[i].min_position);
00104         l.append(lim[i].max_position);
00105         result.append(l);
00106       }
00107     }
00108     return result;
00109   }
00110 
00111   const char* getPlanningFrame() const
00112   {
00113     return robot_model_->getModelFrame().c_str();
00114   }
00115 
00116   bp::list getLinkPose(const std::string &name)
00117   {
00118     bp::list l;
00119     if (!ensureCurrentState())
00120       return l;
00121     robot_state::RobotStatePtr state = current_state_monitor_->getCurrentState();
00122     const robot_state::LinkState *ls = state->getLinkState(name);
00123     if (ls)
00124     {
00125       const Eigen::Affine3d &t = ls->getGlobalLinkTransform();
00126       std::vector<double> v(7);
00127       v[0] = t.translation().x();
00128       v[1] = t.translation().y();
00129       v[2] = t.translation().z();
00130       Eigen::Quaterniond q(t.rotation());
00131       v[3] = q.x();
00132       v[4] = q.y();
00133       v[5] = q.z();
00134       v[6] = q.w();
00135       l = py_bindings_tools::listFromDouble(v);
00136     }
00137     return l;
00138   }
00139 
00140   bp::list getCurrentJointValues(const std::string &name)
00141   {
00142     bp::list l;
00143     if (!ensureCurrentState())
00144       return l;
00145     robot_state::RobotStatePtr state = current_state_monitor_->getCurrentState();
00146     const robot_state::JointState *js = state->getJointState(name);
00147     if (js)
00148       l = py_bindings_tools::listFromDouble(js->getVariableValues());
00149     return l;
00150   }
00151 
00152   bool ensureCurrentState()
00153   {
00154     if (!current_state_monitor_)
00155     {
00156       ROS_ERROR("Unable to get current robot state");
00157       return false;
00158     }
00159 
00160     // if needed, start the monitor and wait up to 1 second for a full robot state
00161     if (!current_state_monitor_->isActive())
00162     {
00163       current_state_monitor_->startStateMonitor();
00164       if (!current_state_monitor_->waitForCurrentState(1.0))
00165         ROS_WARN("Joint values for monitored state are requested but the full state is not known");
00166     }
00167     return true;
00168   }
00169 
00170   bp::dict getCurrentVariableValues()
00171   {
00172     bp::dict d;
00173     if (!current_state_monitor_)
00174     {
00175       ROS_ERROR("Unable to get current robot state");
00176       return d;
00177     }
00178 
00179     // if needed, start the monitor and wait up to 1 second for a full robot state
00180     if (!current_state_monitor_->isActive())
00181       current_state_monitor_->startStateMonitor();
00182 
00183     if (!current_state_monitor_->waitForCurrentState(1.0))
00184       ROS_WARN("Joint values for monitored state are requested but the full state is not known");
00185 
00186     const std::map<std::string, double> &vars = current_state_monitor_->getCurrentStateValues();
00187     for (std::map<std::string, double>::const_iterator it = vars.begin() ; it != vars.end() ; ++it)
00188       d[it->first] = it->second;
00189 
00190     return d;
00191   }
00192 
00193   const char* getRobotRootLink() const
00194   {
00195     return robot_model_->getRootLinkName().c_str();
00196   }
00197 
00198   bool hasGroup(const std::string &group) const
00199   {
00200     return robot_model_->hasJointModelGroup(group);
00201   }
00202 
00203 private:
00204   robot_model::RobotModelConstPtr robot_model_;
00205   planning_scene_monitor::CurrentStateMonitorPtr current_state_monitor_;
00206 };
00207 }
00208 
00209 static void wrap_robot_interface()
00210 {
00211   using namespace moveit;
00212 
00213   bp::class_<RobotInterfacePython> RobotClass("RobotInterface", bp::init<std::string>());
00214 
00215   RobotClass.def("get_joint_names", &RobotInterfacePython::getJointNames);
00216   RobotClass.def("get_group_joint_names", &RobotInterfacePython::getGroupJointNames);
00217   RobotClass.def("get_group_names", &RobotInterfacePython::getGroupNames);
00218   RobotClass.def("get_link_names", &RobotInterfacePython::getLinkNames);
00219   RobotClass.def("get_group_link_names", &RobotInterfacePython::getGroupLinkNames);
00220   RobotClass.def("get_joint_limits", &RobotInterfacePython::getJointLimits);
00221   RobotClass.def("get_link_pose", &RobotInterfacePython::getLinkPose);
00222   RobotClass.def("get_planning_frame", &RobotInterfacePython::getPlanningFrame);
00223   RobotClass.def("get_current_variable_values", &RobotInterfacePython::getCurrentVariableValues);
00224   RobotClass.def("get_current_joint_values",  &RobotInterfacePython::getCurrentJointValues);
00225   RobotClass.def("get_robot_root_link", &RobotInterfacePython::getRobotRootLink);
00226   RobotClass.def("has_group", &RobotInterfacePython::hasGroup);
00227 }
00228 
00229 BOOST_PYTHON_MODULE(_moveit_robot_interface)
00230 {
00231   wrap_robot_interface();
00232 }
00233 


planning_interface
Author(s): Ioan Sucan
autogenerated on Mon Oct 6 2014 02:33:28