Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.

Commit 73416fb

Browse files
PeterMitranoPeter
and
Peter
authored
Python interface improvements. Fix moveit#1966, add enforceBounds (moveit#2356)
* implement get state and get state bounded * add enforceBounds Co-authored-by: Peter <pmitrano@storm20.04>
1 parent 03c24d0 commit 73416fb

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

moveit_commander/src/moveit_commander/move_group.py

+20-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,20 @@ def set_start_state(self, msg):
156156
"""
157157
self._g.set_start_state(conversions.msg_to_string(msg))
158158

159+
def get_current_state_bounded(self):
160+
""" Get the current state of the robot bounded."""
161+
s = RobotState()
162+
c_str = self._g.get_current_state_bounded()
163+
conversions.msg_from_string(s, c_str)
164+
return s
165+
166+
def get_current_state(self):
167+
""" Get the current state of the robot."""
168+
s = RobotState()
169+
c_str = self._g.get_current_state()
170+
conversions.msg_from_string(s, c_str)
171+
return s
172+
159173
def get_joint_value_target(self):
160174
return self._g.get_joint_value_target()
161175

@@ -230,7 +244,6 @@ def set_joint_value_target(self, arg1, arg2=None, arg3=None):
230244
else:
231245
raise MoveItCommanderException("Unsupported argument of type %s" % type(arg1))
232246

233-
234247
def set_rpy_target(self, rpy, end_effector_link=""):
235248
""" Specify a target orientation for the end-effector. Any position of the end-effector is acceptable."""
236249
if len(end_effector_link) > 0 or self.has_end_effector_link():
@@ -610,3 +623,9 @@ def get_jacobian_matrix(self, joint_values, reference_point=None):
610623
""" Get the jacobian matrix of the group as a list"""
611624
return self._g.get_jacobian_matrix(joint_values, [0.0, 0.0, 0.0] if reference_point is None else reference_point)
612625

626+
def enforce_bounds(self, robot_state_msg):
627+
""" Takes a moveit_msgs RobotState and enforces the state bounds, based on the C++ RobotState enforceBounds() """
628+
s = RobotState()
629+
c_str = self._g.enforce_bounds(conversions.msg_to_string(robot_state_msg))
630+
conversions.msg_from_string(s, c_str)
631+
return s

moveit_commander/test/python_moveit_commander.py

+29
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@
3535
# Author: William Baker
3636

3737
import unittest
38+
39+
import genpy
3840
import numpy as np
3941
import rospy
4042
import rostest
4143
import os
4244

45+
from moveit_msgs.msg import RobotState
46+
4347
from moveit_commander import RobotCommander, PlanningSceneInterface
4448

4549

@@ -55,6 +59,31 @@ def setUpClass(self):
5559
def tearDown(self):
5660
pass
5761

62+
def test_enforce_bounds_empty_state(self):
63+
in_msg = RobotState()
64+
with self.assertRaises(genpy.DeserializationError):
65+
self.group.enforce_bounds(in_msg)
66+
67+
def test_enforce_bounds(self):
68+
in_msg = RobotState()
69+
in_msg.joint_state.header.frame_id = 'base_link'
70+
in_msg.joint_state.name = ['joint_1', 'joint_2', 'joint_3', 'joint_4', 'joint_5', 'joint_6']
71+
in_msg.joint_state.position = [0] * 6
72+
in_msg.joint_state.position[0] = 1000
73+
74+
out_msg = self.group.enforce_bounds(in_msg)
75+
76+
self.assertEqual(in_msg.joint_state.position[0], 1000)
77+
self.assertLess(out_msg.joint_state.position[0], 1000)
78+
79+
def test_get_current_state(self):
80+
expected_state = RobotState()
81+
expected_state.joint_state.header.frame_id = 'base_link'
82+
expected_state.multi_dof_joint_state.header.frame_id = 'base_link'
83+
expected_state.joint_state.name = ['joint_1', 'joint_2', 'joint_3', 'joint_4', 'joint_5', 'joint_6']
84+
expected_state.joint_state.position = [0] * 6
85+
self.assertEqual(self.group.get_current_state(), expected_state)
86+
5887
def check_target_setting(self, expect, *args):
5988
if len(args) == 0:
6089
args = [expect]

moveit_ros/planning_interface/move_group_interface/src/wrap_python_move_group.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,14 @@ class MoveGroupInterfaceWrapper : protected py_bindings_tools::ROScppInitializer
342342
return output;
343343
}
344344

345+
py_bindings_tools::ByteString getCurrentStatePython()
346+
{
347+
moveit::core::RobotStatePtr current_state = getCurrentState();
348+
moveit_msgs::RobotState state_message;
349+
moveit::core::robotStateToRobotStateMsg(*current_state, state_message);
350+
return py_bindings_tools::serializeMsg(state_message);
351+
}
352+
345353
void setStartStatePython(const py_bindings_tools::ByteString& msg_str)
346354
{
347355
moveit_msgs::RobotState msg;
@@ -602,6 +610,24 @@ class MoveGroupInterfaceWrapper : protected py_bindings_tools::ROScppInitializer
602610
state.setJointGroupPositions(group, v);
603611
return state.getJacobian(group, Eigen::Map<Eigen::Vector3d>(&ref[0]));
604612
}
613+
614+
py_bindings_tools::ByteString enforceBoundsPython(const py_bindings_tools::ByteString& msg_str)
615+
{
616+
moveit_msgs::RobotState state_msg;
617+
py_bindings_tools::deserializeMsg(msg_str, state_msg);
618+
moveit::core::RobotState state(getRobotModel());
619+
if (moveit::core::robotStateMsgToRobotState(state_msg, state, true))
620+
{
621+
state.enforceBounds();
622+
moveit::core::robotStateToRobotStateMsg(state, state_msg);
623+
return py_bindings_tools::serializeMsg(state_msg);
624+
}
625+
else
626+
{
627+
ROS_ERROR("Unable to convert RobotState message to RobotState instance.");
628+
return py_bindings_tools::ByteString("");
629+
}
630+
}
605631
};
606632

607633
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(getJacobianMatrixOverloads, getJacobianMatrixPython, 1, 2)
@@ -742,8 +768,10 @@ static void wrap_move_group_interface()
742768
move_group_interface_class.def("get_named_targets", &MoveGroupInterfaceWrapper::getNamedTargetsPython);
743769
move_group_interface_class.def("get_named_target_values", &MoveGroupInterfaceWrapper::getNamedTargetValuesPython);
744770
move_group_interface_class.def("get_current_state_bounded", &MoveGroupInterfaceWrapper::getCurrentStateBoundedPython);
771+
move_group_interface_class.def("get_current_state", &MoveGroupInterfaceWrapper::getCurrentStatePython);
745772
move_group_interface_class.def("get_jacobian_matrix", &MoveGroupInterfaceWrapper::getJacobianMatrixPython,
746773
getJacobianMatrixOverloads());
774+
move_group_interface_class.def("enforce_bounds", &MoveGroupInterfaceWrapper::enforceBoundsPython);
747775
}
748776
} // namespace planning_interface
749777
} // namespace moveit

0 commit comments

Comments
 (0)