42 #include <moveit_msgs/RobotState.h> 43 #include <visualization_msgs/MarkerArray.h> 46 #include <boost/python.hpp> 55 class RobotInterfacePython :
protected py_bindings_tools::ROScppInitializer
58 RobotInterfacePython(
const std::string& robot_description,
const std::string& ns =
"")
59 : py_bindings_tools::ROScppInitializer()
63 throw std::runtime_error(
"RobotInterfacePython: invalid robot model");
64 current_state_monitor_ =
68 const char* getRobotName()
const 70 return robot_model_->getName().c_str();
73 bp::list getJointNames()
const 78 bp::list getGroupJointNames(
const std::string&
group)
const 80 const robot_model::JointModelGroup* jmg = robot_model_->getJointModelGroup(group);
87 bp::list getGroupJointTips(
const std::string& group)
const 89 const robot_model::JointModelGroup* jmg = robot_model_->getJointModelGroup(group);
92 std::vector<std::string> tips;
93 jmg->getEndEffectorTips(tips);
100 bp::list getLinkNames()
const 105 bp::list getGroupLinkNames(
const std::string& group)
const 107 const robot_model::JointModelGroup* jmg = robot_model_->getJointModelGroup(group);
114 bp::list getGroupNames()
const 119 bp::list getJointLimits(
const std::string& name)
const 122 const robot_model::JointModel* jm = robot_model_->getJointModel(name);
125 const std::vector<moveit_msgs::JointLimits>& lim = jm->getVariableBoundsMsg();
126 for (std::size_t i = 0; i < lim.size(); ++i)
129 l.append(lim[i].min_position);
130 l.append(lim[i].max_position);
137 const char* getPlanningFrame()
const 139 return robot_model_->getModelFrame().c_str();
142 bp::list getLinkPose(
const std::string& name)
145 if (!ensureCurrentState())
147 robot_state::RobotStatePtr state = current_state_monitor_->getCurrentState();
148 const robot_model::LinkModel* lm = state->getLinkModel(name);
151 const Eigen::Affine3d& t = state->getGlobalLinkTransform(lm);
152 std::vector<double>
v(7);
153 v[0] = t.translation().x();
154 v[1] = t.translation().y();
155 v[2] = t.translation().z();
156 Eigen::Quaterniond
q(t.linear());
166 bp::list getDefaultStateNames(
const std::string& group)
169 const robot_model::JointModelGroup* jmg = robot_model_->getJointModelGroup(group);
172 for (
auto& known_state : jmg->getDefaultStateNames())
174 l.append(known_state);
180 bp::list getCurrentJointValues(
const std::string& name)
183 if (!ensureCurrentState())
185 robot_state::RobotStatePtr state = current_state_monitor_->getCurrentState();
186 const robot_model::JointModel* jm = state->getJointModel(name);
189 const double* pos = state->getJointPositions(jm);
190 const unsigned int sz = jm->getVariableCount();
191 for (
unsigned int i = 0; i < sz; ++i)
198 bp::dict getJointValues(
const std::string& group,
const std::string& named_state)
200 const robot_model::JointModelGroup* jmg = robot_model_->getJointModelGroup(group);
202 return boost::python::dict();
203 std::map<std::string, double>
values;
204 jmg->getVariableDefaultPositions(named_state, values);
208 bool ensureCurrentState(
double wait = 1.0)
210 if (!current_state_monitor_)
212 ROS_ERROR(
"Unable to get current robot state");
217 if (!current_state_monitor_->isActive())
219 current_state_monitor_->startStateMonitor();
220 if (!current_state_monitor_->waitForCompleteState(wait))
221 ROS_WARN(
"Joint values for monitored state are requested but the full state is not known");
226 std::string getCurrentState()
228 if (!ensureCurrentState())
230 robot_state::RobotStatePtr
s = current_state_monitor_->getCurrentState();
231 moveit_msgs::RobotState msg;
232 robot_state::robotStateToRobotStateMsg(*s, msg);
236 bp::tuple getEndEffectorParentGroup(std::string group)
240 const robot_state::JointModelGroup* jmg = robot_model_->getJointModelGroup(group);
242 return boost::python::make_tuple(
"",
"");
243 std::pair<std::string, std::string> parent_group = jmg->getEndEffectorParentGroup();
244 return boost::python::make_tuple(parent_group.first, parent_group.second);
247 std::string getRobotMarkersPythonDictList(bp::dict& values, bp::list& links)
249 robot_state::RobotStatePtr state;
250 if (ensureCurrentState())
252 state = current_state_monitor_->getCurrentState();
256 state.reset(
new robot_state::RobotState(robot_model_));
259 bp::list k = values.keys();
261 sensor_msgs::JointState joint_state;
262 joint_state.name.resize(l);
263 joint_state.position.resize(l);
264 for (
int i = 0; i < l; ++i)
266 joint_state.name[i] = bp::extract<std::string>(k[i]);
267 joint_state.position[i] = bp::extract<double>(values[k[i]]);
269 state->setVariableValues(joint_state);
270 visualization_msgs::MarkerArray msg;
276 std::string getRobotMarkersPythonDict(bp::dict& values)
279 return getRobotMarkersPythonDictList(values, links);
282 std::string getRobotMarkersFromMsg(
const std::string& state_str)
284 moveit_msgs::RobotState state_msg;
285 robot_state::RobotState state(robot_model_);
289 visualization_msgs::MarkerArray msg;
290 state.getRobotMarkers(msg, state.getRobotModel()->getLinkModelNames());
295 std::string getRobotMarkers()
297 if (!ensureCurrentState())
299 robot_state::RobotStatePtr s = current_state_monitor_->getCurrentState();
300 visualization_msgs::MarkerArray msg;
301 s->getRobotMarkers(msg, s->getRobotModel()->getLinkModelNames());
306 std::string getRobotMarkersPythonList(bp::list links)
308 if (!ensureCurrentState())
310 robot_state::RobotStatePtr s = current_state_monitor_->getCurrentState();
311 visualization_msgs::MarkerArray msg;
317 std::string getRobotMarkersGroup(std::string group)
319 if (!ensureCurrentState())
321 robot_state::RobotStatePtr s = current_state_monitor_->getCurrentState();
322 const robot_model::JointModelGroup* jmg = robot_model_->getJointModelGroup(group);
323 visualization_msgs::MarkerArray msg;
326 s->getRobotMarkers(msg, jmg->getLinkModelNames());
332 std::string getRobotMarkersGroupPythonDict(std::string group, bp::dict& values)
334 const robot_model::JointModelGroup* jmg = robot_model_->getJointModelGroup(group);
338 return getRobotMarkersPythonDictList(values, links);
341 bp::dict getCurrentVariableValues()
345 if (!ensureCurrentState())
348 const std::map<std::string, double>& vars = current_state_monitor_->getCurrentStateValues();
349 for (std::map<std::string, double>::const_iterator it = vars.begin(); it != vars.end(); ++it)
350 d[it->first] = it->second;
355 const char* getRobotRootLink()
const 357 return robot_model_->getRootLinkName().c_str();
360 bool hasGroup(
const std::string& group)
const 362 return robot_model_->hasJointModelGroup(group);
366 robot_model::RobotModelConstPtr robot_model_;
367 planning_scene_monitor::CurrentStateMonitorPtr current_state_monitor_;
372 static void wrap_robot_interface()
376 bp::class_<RobotInterfacePython> RobotClass(
"RobotInterface", bp::init<std::string, bp::optional<std::string>>());
378 RobotClass.def(
"get_joint_names", &RobotInterfacePython::getJointNames);
379 RobotClass.def(
"get_group_joint_names", &RobotInterfacePython::getGroupJointNames);
380 RobotClass.def(
"get_group_default_states", &RobotInterfacePython::getDefaultStateNames);
381 RobotClass.def(
"get_group_joint_tips", &RobotInterfacePython::getGroupJointTips);
382 RobotClass.def(
"get_group_names", &RobotInterfacePython::getGroupNames);
383 RobotClass.def(
"get_link_names", &RobotInterfacePython::getLinkNames);
384 RobotClass.def(
"get_group_link_names", &RobotInterfacePython::getGroupLinkNames);
385 RobotClass.def(
"get_joint_limits", &RobotInterfacePython::getJointLimits);
386 RobotClass.def(
"get_link_pose", &RobotInterfacePython::getLinkPose);
387 RobotClass.def(
"get_planning_frame", &RobotInterfacePython::getPlanningFrame);
388 RobotClass.def(
"get_current_state", &RobotInterfacePython::getCurrentState);
389 RobotClass.def(
"get_current_variable_values", &RobotInterfacePython::getCurrentVariableValues);
390 RobotClass.def(
"get_current_joint_values", &RobotInterfacePython::getCurrentJointValues);
391 RobotClass.def(
"get_joint_values", &RobotInterfacePython::getJointValues);
392 RobotClass.def(
"get_robot_root_link", &RobotInterfacePython::getRobotRootLink);
393 RobotClass.def(
"has_group", &RobotInterfacePython::hasGroup);
394 RobotClass.def(
"get_robot_name", &RobotInterfacePython::getRobotName);
395 RobotClass.def(
"get_robot_markers", &RobotInterfacePython::getRobotMarkers);
396 RobotClass.def(
"get_robot_markers", &RobotInterfacePython::getRobotMarkersPythonList);
397 RobotClass.def(
"get_robot_markers", &RobotInterfacePython::getRobotMarkersFromMsg);
398 RobotClass.def(
"get_robot_markers", &RobotInterfacePython::getRobotMarkersPythonDictList);
399 RobotClass.def(
"get_robot_markers", &RobotInterfacePython::getRobotMarkersPythonDict);
400 RobotClass.def(
"get_group_markers", &RobotInterfacePython::getRobotMarkersGroup);
401 RobotClass.def(
"get_group_markers", &RobotInterfacePython::getRobotMarkersGroupPythonDict);
402 RobotClass.def(
"get_parent_group", &RobotInterfacePython::getEndEffectorParentGroup);
407 wrap_robot_interface();
boost::shared_ptr< tf::Transformer > getSharedTF()
std::vector< double > values
BOOST_PYTHON_MODULE(_moveit_roscpp_initializer)
planning_scene_monitor::CurrentStateMonitorPtr getSharedStateMonitor(const robot_model::RobotModelConstPtr &kmodel, const boost::shared_ptr< tf::Transformer > &tf)
getSharedStateMonitor is a simpler version of getSharedStateMonitor(const robot_model::RobotModelCons...
bool robotStateMsgToRobotState(const Transforms &tf, const moveit_msgs::RobotState &robot_state, RobotState &state, bool copy_attached_bodies=true)
robot_model::RobotModelConstPtr getSharedRobotModel(const std::string &robot_description)