Skip to content

Commit 4c68104

Browse files
committed
update for new api
1 parent 120f941 commit 4c68104

File tree

2 files changed

+82
-77
lines changed

2 files changed

+82
-77
lines changed

doc/move_group_python_interface/scripts/move_group_python_interface_tutorial.py

Lines changed: 75 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3333
# POSSIBILITY OF SUCH DAMAGE.
3434
#
35-
# Author: Acorn Pooley, Mike Lautman
35+
# Author: Acorn Pooley, Mike Lautman, Peter Mitrano
3636

3737
## BEGIN_SUB_TUTORIAL imports
3838
##
@@ -41,14 +41,12 @@
4141
## and a `RobotCommander`_ class. More on these below. We also import `rospy`_ and some messages that we will use:
4242
##
4343

44-
# Python 2/3 compatibility imports
45-
from __future__ import print_function
46-
from six.moves import input
47-
4844
import sys
4945
import copy
5046
import rospy
51-
import moveit_commander
47+
from moveit_commander import roscpp_initializer
48+
from moveit.core.conversions import pose_to_list
49+
from moveit.planning_interface import planning_interface
5250
import moveit_msgs.msg
5351
import geometry_msgs.msg
5452
try:
@@ -63,34 +61,38 @@ def dist(p, q):
6361
## END_SUB_TUTORIAL
6462

6563

64+
65+
## END_SUB_TUTORIAL
66+
67+
6668
def all_close(goal, actual, tolerance):
67-
"""
68-
Convenience method for testing if the values in two lists are within a tolerance of each other.
69-
For Pose and PoseStamped inputs, the angle between the two quaternions is compared (the angle
70-
between the identical orientations q and -q is calculated correctly).
71-
@param: goal A list of floats, a Pose or a PoseStamped
72-
@param: actual A list of floats, a Pose or a PoseStamped
73-
@param: tolerance A float
74-
@returns: bool
75-
"""
76-
if type(goal) is list:
77-
for index in range(len(goal)):
78-
if abs(actual[index] - goal[index]) > tolerance:
79-
return False
80-
81-
elif type(goal) is geometry_msgs.msg.PoseStamped:
82-
return all_close(goal.pose, actual.pose, tolerance)
83-
84-
elif type(goal) is geometry_msgs.msg.Pose:
85-
x0, y0, z0, qx0, qy0, qz0, qw0 = pose_to_list(actual)
86-
x1, y1, z1, qx1, qy1, qz1, qw1 = pose_to_list(goal)
87-
# Euclidean distance
88-
d = dist((x1, y1, z1), (x0, y0, z0))
89-
# phi = angle between orientations
90-
cos_phi_half = fabs(qx0*qx1 + qy0*qy1 + qz0*qz1 + qw0*qw1)
91-
return d <= tolerance and cos_phi_half >= cos(tolerance / 2.0)
92-
93-
return True
69+
"""
70+
Convenience method for testing if the values in two lists are within a tolerance of each other.
71+
For Pose and PoseStamped inputs, the angle between the two quaternions is compared (the angle
72+
between the identical orientations q and -q is calculated correctly).
73+
@param: goal A list of floats, a Pose or a PoseStamped
74+
@param: actual A list of floats, a Pose or a PoseStamped
75+
@param: tolerance A float
76+
@returns: bool
77+
"""
78+
if type(goal) is list:
79+
for index in range(len(goal)):
80+
if abs(actual[index] - goal[index]) > tolerance:
81+
return False
82+
83+
elif type(goal) is geometry_msgs.msg.PoseStamped:
84+
return all_close(goal.pose, actual.pose, tolerance)
85+
86+
elif type(goal) is geometry_msgs.msg.Pose:
87+
x0, y0, z0, qx0, qy0, qz0, qw0 = pose_to_list(actual)
88+
x1, y1, z1, qx1, qy1, qz1, qw1 = pose_to_list(goal)
89+
# Euclidean distance
90+
d = dist((x1, y1, z1), (x0, y0, z0))
91+
# phi = angle between orientations
92+
cos_phi_half = fabs(qx0 * qx1 + qy0 * qy1 + qz0 * qz1 + qw0 * qw1)
93+
return d <= tolerance and cos_phi_half >= cos(tolerance / 2.0)
94+
95+
return True
9496

9597

9698
class MoveGroupPythonInterfaceTutorial(object):
@@ -175,7 +177,7 @@ def go_to_joint_state(self):
175177
## Planning to a Joint Goal
176178
## ^^^^^^^^^^^^^^^^^^^^^^^^
177179
## The Panda's zero configuration is at a `singularity <https://www.quora.com/Robotics-What-is-meant-by-kinematic-singularity>`_, so the first
178-
## thing we want to do is move it to a slightly better configuration.
180+
## thing we want to do is move it to a slightly better configuration.
179181
## We use the constant `tau = 2*pi <https://en.wikipedia.org/wiki/Turn_(angle)#Tau_proposals>`_ for convenience:
180182
# We get the joint values from the group and change some of the values:
181183
joint_goal = move_group.get_current_joint_values()
@@ -466,55 +468,58 @@ def remove_box(self, timeout=4):
466468

467469

468470
def main():
469-
try:
470-
print("")
471-
print("----------------------------------------------------------")
472-
print("Welcome to the MoveIt MoveGroup Python Interface Tutorial")
473-
print("----------------------------------------------------------")
474-
print("Press Ctrl-D to exit at any time")
475-
print("")
476-
input("============ Press `Enter` to begin the tutorial by setting up the moveit_commander ...")
477-
tutorial = MoveGroupPythonInterfaceTutorial()
471+
try:
472+
print("")
473+
print("----------------------------------------------------------")
474+
print("Welcome to the MoveIt MoveGroup Python Interface Tutorial")
475+
print("----------------------------------------------------------")
476+
print("Press Ctrl-D to exit at any time")
477+
print("")
478+
input("============ Press `Enter` to begin the tutorial by setting up the moveit_commander ...")
479+
tutorial = MoveGroupPythonInterfaceTutorial()
480+
481+
input("============ Press `Enter` to execute a movement using a joint state goal ...")
482+
tutorial.go_to_joint_state()
483+
484+
input("============ Press `Enter` to execute a movement using a pose goal ...")
485+
tutorial.go_to_pose_goal()
478486

479-
input("============ Press `Enter` to execute a movement using a joint state goal ...")
480-
tutorial.go_to_joint_state()
487+
input("============ Press `Enter` to plan and display a Cartesian path ...")
488+
cartesian_plan, fraction = tutorial.plan_cartesian_path()
481489

482-
input("============ Press `Enter` to execute a movement using a pose goal ...")
483-
tutorial.go_to_pose_goal()
490+
input("============ Press `Enter` to display a saved trajectory (this will replay the Cartesian path) ...")
491+
tutorial.display_trajectory(cartesian_plan)
484492

485-
input("============ Press `Enter` to plan and display a Cartesian path ...")
486-
cartesian_plan, fraction = tutorial.plan_cartesian_path()
493+
input("============ Press `Enter` to execute a saved path ...")
494+
tutorial.execute_plan(cartesian_plan)
487495

488-
input("============ Press `Enter` to display a saved trajectory (this will replay the Cartesian path) ...")
489-
tutorial.display_trajectory(cartesian_plan)
496+
input("============ Press `Enter` to add a box to the planning scene ...")
497+
tutorial.add_box()
490498

491-
input("============ Press `Enter` to execute a saved path ...")
492-
tutorial.execute_plan(cartesian_plan)
499+
input("============ Press `Enter` to attach a Box to the Panda robot ...")
500+
tutorial.attach_box()
493501

494-
input("============ Press `Enter` to add a box to the planning scene ...")
495-
tutorial.add_box()
502+
input("============ Press `Enter` to plan and execute a path with an attached collision object ...")
503+
cartesian_plan, fraction = tutorial.plan_cartesian_path(scale=-1)
504+
tutorial.execute_plan(cartesian_plan)
496505

497-
input("============ Press `Enter` to attach a Box to the Panda robot ...")
498-
tutorial.attach_box()
506+
input("============ Press `Enter` to detach the box from the Panda robot ...")
507+
tutorial.detach_box()
499508

500-
input("============ Press `Enter` to plan and execute a path with an attached collision object ...")
501-
cartesian_plan, fraction = tutorial.plan_cartesian_path(scale=-1)
502-
tutorial.execute_plan(cartesian_plan)
509+
input("============ Press `Enter` to remove the box from the planning scene ...")
510+
tutorial.remove_box()
503511

504-
input("============ Press `Enter` to detach the box from the Panda robot ...")
505-
tutorial.detach_box()
512+
print("============ Python tutorial demo complete!")
506513

507-
input("============ Press `Enter` to remove the box from the planning scene ...")
508-
tutorial.remove_box()
514+
roscpp_initializer.roscpp_shutdown()
515+
except rospy.ROSInterruptException:
516+
return
517+
except KeyboardInterrupt:
518+
return
509519

510-
print("============ Python tutorial demo complete!")
511-
except rospy.ROSInterruptException:
512-
return
513-
except KeyboardInterrupt:
514-
return
515520

516521
if __name__ == '__main__':
517-
main()
522+
main()
518523

519524
## BEGIN_TUTORIAL
520525
## .. _moveit_commander:

doc/planning_scene_python/src/planning_scene_tutorial.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import numpy as np
66
import rospy
77
from geometry_msgs.msg import PoseStamped
8-
from pymoveit import kinematic_constraints, collision_detection, robot_model, random_numbers
9-
from pymoveit.planning_scene import PlanningScene
8+
import moveit.core
9+
from moveit.core import kinematic_constraints, collision_detection, robot_model
10+
from moveit.core.planning_scene import PlanningScene
1011

1112

1213
def main():
@@ -30,7 +31,7 @@ def main():
3031

3132
urdf_path = '/opt/ros/noetic/share/moveit_resources_panda_description/urdf/panda.urdf'
3233
srdf_path = '/opt/ros/noetic/share/moveit_resources_panda_moveit_config/config/panda.srdf'
33-
kinematic_model = robot_model.load_robot_model(urdf_path, srdf_path)
34+
kinematic_model = moveit.core.load_robot_model(urdf_path, srdf_path)
3435
planning_scene = PlanningScene(kinematic_model, collision_detection.World())
3536
current_state = planning_scene.getCurrentStateNonConst()
3637
joint_model_group = current_state.getJointModelGroup("panda_arm")
@@ -83,8 +84,7 @@ def main():
8384
# the collision_result before making a new collision checking
8485
# request.
8586

86-
rng = random_numbers.RandomNumbers(0)
87-
current_state.setToRandomPositions(joint_model_group, rng)
87+
current_state.setToRandomPositions(joint_model_group)
8888

8989
collision_result.clear()
9090
planning_scene.checkSelfCollision(collision_request, collision_result)
@@ -100,7 +100,7 @@ def main():
100100
# collision request.
101101

102102
collision_request.group_name = "hand"
103-
current_state.setToRandomPositions(joint_model_group, rng)
103+
current_state.setToRandomPositions(joint_model_group)
104104
collision_result.clear()
105105
planning_scene.checkSelfCollision(collision_request, collision_result)
106106
rospy.loginfo(f"Test 3: Current state is {'in' if collision_result.collision else 'not in'} self collision")
@@ -195,7 +195,7 @@ def main():
195195

196196
# Now, we can check a state against this constraint using the
197197
# isStateConstrained functions in the PlanningScene class.
198-
copied_state.setToRandomPositions(joint_model_group, rng)
198+
copied_state.setToRandomPositions(joint_model_group)
199199
copied_state.update()
200200
constrained = planning_scene.isStateConstrained(copied_state, goal_constraint)
201201
rospy.loginfo(f"Test 8: Random state is {'constrained' if constrained else 'not constrained'}")

0 commit comments

Comments
 (0)