Skip to content

Commit 430a69f

Browse files
committed
Adapt python scripts to ROS2 API changes
1 parent 29d1219 commit 430a69f

20 files changed

+157
-73
lines changed

core/python/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ add_subdirectory(bindings)
2727

2828
if(BUILD_TESTING)
2929
find_package(ament_cmake_pytest REQUIRED)
30-
set(_pytest_tests
31-
test/test_mtc.py
32-
test/rostest_mtc.py
33-
)
30+
find_package(launch_testing_ament_cmake)
31+
set(_pytest_tests test/test_mtc.py)
3432
foreach(_test_path ${_pytest_tests})
3533
get_filename_component(_test_name ${_test_path} NAME_WE)
3634
ament_add_pytest_test(${_test_name} ${_test_path}
@@ -39,4 +37,8 @@ if(BUILD_TESTING)
3937
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
4038
)
4139
endforeach()
40+
41+
# add_launch_test(test/test.launch.py TARGET rostest_mtc ARGS "exe:=${CMAKE_CURRENT_SOURCE_DIR}/test/rostest_mtc.py")
42+
# add_launch_test(test/test.launch.py TARGET rostest_mps ARGS "exe:=${CMAKE_CURRENT_SOURCE_DIR}/test/rostest_mps.py")
43+
# add_launch_test(test/test.launch.py TARGET rostest_trampoline ARGS "exe:=${CMAKE_CURRENT_SOURCE_DIR}/test/rostest_trampoline.py")
4244
endif()

core/python/test/rostest_mps.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
#! /usr/bin/env python3
22

33
import unittest
4-
import rostest
5-
from py_binding_tools import roscpp_init
6-
from moveit_commander import PlanningSceneInterface
4+
import rclcpp
75
from moveit.task_constructor import core, stages
86
from geometry_msgs.msg import PoseStamped
97

108

119
def setUpModule():
12-
roscpp_init("test_mtc")
10+
rclcpp.init()
1311

1412

1513
def make_pose(x, y, z):
@@ -31,7 +29,9 @@ def setUp(self):
3129
self.make_box = self.psi._PlanningSceneInterface__make_box
3230
# insert a box to collide with
3331
self.psi.add_box("box", make_pose(0.8, 0.25, 1.25), [0.2, 0.2, 0.2])
32+
self.node = rclcpp.Node("test_mtc")
3433
self.task = task = core.Task()
34+
self.task.loadRobotModel(self.node)
3535
task.add(stages.CurrentState("current"))
3636

3737
def insertMove(self, position=-1):
@@ -116,4 +116,4 @@ def test_bw_remove_object(self):
116116

117117

118118
if __name__ == "__main__":
119-
rostest.rosrun("mtc", "mps", TestModifyPlanningScene)
119+
unittest.main()

core/python/test/rostest_mtc.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
#! /usr/bin/env python3
22

33
import unittest
4-
import rostest
5-
from py_binding_tools import roscpp_init
4+
import rclcpp
65
from moveit.task_constructor import core, stages
76
from geometry_msgs.msg import PoseStamped
87
from geometry_msgs.msg import Vector3Stamped, Vector3
98
from std_msgs.msg import Header
109

1110

1211
def setUpModule():
13-
roscpp_init("test_mtc")
12+
rclcpp.init()
1413

1514

1615
class Test(unittest.TestCase):
1716
PLANNING_GROUP = "manipulator"
1817

18+
def setUp(self):
19+
self.node = rclcpp.Node("test_mtc")
20+
1921
def test_MoveAndExecute(self):
2022
moveRel = stages.MoveRelative("moveRel", core.JointInterpolationPlanner())
2123
moveRel.group = self.PLANNING_GROUP
@@ -26,6 +28,8 @@ def test_MoveAndExecute(self):
2628
moveTo.setGoal("all-zeros")
2729

2830
task = core.Task()
31+
task.loadRobotModel(self.node)
32+
2933
task.add(stages.CurrentState("current"), moveRel, moveTo)
3034

3135
self.assertTrue(task.plan())
@@ -45,6 +49,8 @@ def createDisplacement(group, displacement):
4549
return move
4650

4751
task = core.Task()
52+
task.loadRobotModel(self.node)
53+
4854
task.add(stages.CurrentState("current"))
4955
merger = core.Merger("merger")
5056
merger.insert(createDisplacement(self.PLANNING_GROUP, [-0.2, 0, 0]))
@@ -57,4 +63,4 @@ def createDisplacement(group, displacement):
5763

5864

5965
if __name__ == "__main__":
60-
rostest.rosrun("mtc", "base", Test)
66+
unittest.main()

core/python/test/rostest_trampoline.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
import unittest
5-
import rostest
6-
from py_binding_tools import roscpp_init
5+
import rclcpp
76
from moveit.task_constructor import core, stages
87
from moveit.core.planning_scene import PlanningScene
98
from geometry_msgs.msg import Vector3Stamped, Vector3, PoseStamped
@@ -13,7 +12,7 @@
1312

1413

1514
def setUpModule():
16-
roscpp_init("test_mtc")
15+
rclcpp.init()
1716

1817

1918
def pybind11_versions():
@@ -100,9 +99,11 @@ class TestTrampolines(unittest.TestCase):
10099
def setUp(self):
101100
self.cartesian = core.CartesianPath()
102101
self.jointspace = core.JointInterpolationPlanner()
102+
self.node = rclcpp.Node("test_mtc")
103103

104104
def create(self, *stages):
105105
task = core.Task()
106+
task.loadRobotModel(self.node)
106107
task.enableIntrospection()
107108
for stage in stages:
108109
task.add(stage)
@@ -140,4 +141,4 @@ def test_propagator(self):
140141

141142

142143
if __name__ == "__main__":
143-
rostest.rosrun("mtc", "trampoline", TestTrampolines)
144+
unittest.main()

core/python/test/test.launch.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import unittest
2+
3+
import launch_testing
4+
import pytest
5+
from launch import LaunchDescription
6+
from launch.actions import DeclareLaunchArgument
7+
from launch.substitutions import LaunchConfiguration
8+
from launch_ros.actions import Node
9+
from launch_testing.actions import ReadyToTest
10+
from launch_testing.util import KeepAliveProc
11+
from moveit_configs_utils import MoveItConfigsBuilder
12+
13+
14+
@pytest.mark.launch_test
15+
def generate_test_description():
16+
moveit_config = MoveItConfigsBuilder("moveit_resources_fanuc").to_moveit_configs()
17+
18+
test_exec = Node(
19+
executable=[LaunchConfiguration("exe")],
20+
output="screen",
21+
parameters=[
22+
moveit_config.robot_description,
23+
moveit_config.robot_description_semantic,
24+
moveit_config.robot_description_kinematics,
25+
moveit_config.joint_limits,
26+
],
27+
)
28+
29+
return (
30+
LaunchDescription(
31+
[
32+
DeclareLaunchArgument(name="exe"),
33+
test_exec,
34+
KeepAliveProc(),
35+
ReadyToTest(),
36+
]
37+
),
38+
{"test_exec": test_exec},
39+
)
40+
41+
42+
class TestTerminatingProcessStops(unittest.TestCase):
43+
def test_gtest_run_complete(self, proc_info, test_exec):
44+
proc_info.assertWaitForShutdown(process=test_exec, timeout=4000.0)
45+
46+
47+
@launch_testing.post_shutdown_test()
48+
class TaskModelTestAfterShutdown(unittest.TestCase):
49+
def test_exit_code(self, proc_info):
50+
# Check that all processes in the launch exit with code 0
51+
launch_testing.asserts.assertExitCodes(proc_info)

core/python/test/test_mtc.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33

44
import sys
55
import unittest
6+
import rclcpp
67
from geometry_msgs.msg import Pose, PoseStamped, PointStamped, TwistStamped, Vector3Stamped
78
from moveit_msgs.msg import RobotState, Constraints, MotionPlanRequest
89
from moveit.task_constructor import core, stages
910

1011

12+
def setUpModule():
13+
rclcpp.init()
14+
15+
1116
class TestPropertyMap(unittest.TestCase):
1217
def setUp(self):
18+
self.node = rclcpp.Node("test_mtc_props")
1319
self.props = core.PropertyMap()
1420

1521
def _check(self, name, value):
@@ -28,7 +34,7 @@ def test_assign(self):
2834
self.assertRaises(TypeError, self._check, "request", MotionPlanRequest())
2935

3036
def test_assign_in_reference(self):
31-
planner = core.PipelinePlanner()
37+
planner = core.PipelinePlanner(self.node)
3238
props = planner.properties
3339

3440
props["goal_joint_tolerance"] = 3.14
@@ -40,21 +46,15 @@ def test_assign_in_reference(self):
4046

4147
props["planner"] = "planner"
4248
self.assertEqual(props["planner"], "planner")
43-
self.assertEqual(planner.planner, "planner")
4449

4550
props["double"] = 3.14
4651
a = props
4752
props["double"] = 2.71
4853
self.assertEqual(a["double"], 2.71)
4954

50-
planner.planner = "other"
51-
self.assertEqual(props["planner"], "other")
52-
self.assertEqual(planner.planner, "other")
53-
5455
del planner
5556
# We can still access props, because actual destruction of planner is delayed
5657
self.assertEqual(props["goal_joint_tolerance"], 2.71)
57-
self.assertEqual(props["planner"], "other")
5858

5959
def test_iter(self):
6060
# assign values so we can iterate over them
@@ -116,7 +116,8 @@ def test_allow_collisions(self):
116116

117117
class TestStages(unittest.TestCase):
118118
def setUp(self):
119-
self.planner = core.PipelinePlanner()
119+
self.node = rclcpp.Node("test_mtc_stages")
120+
self.planner = core.PipelinePlanner(self.node)
120121

121122
def _check(self, stage, name, value):
122123
self._check_assign(stage, name, value)
@@ -198,8 +199,7 @@ def test_MoveRelative(self):
198199
stage.setDirection({"joint": 0.1})
199200

200201
def test_Connect(self):
201-
planner = core.PipelinePlanner()
202-
stage = stages.Connect("connect", [("group1", planner), ("group2", planner)])
202+
stage = stages.Connect("connect", [("group1", self.planner), ("group2", self.planner)])
203203

204204
def test_FixCollisionObjects(self):
205205
stage = stages.FixCollisionObjects("collision")

demo/scripts/alternatives.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22
# -*- coding: utf-8 -*-
33

44
from moveit.task_constructor import core, stages
5-
from py_binding_tools import roscpp_init
5+
import rclcpp
66
import time
77

8-
roscpp_init("mtc_tutorial")
8+
rclcpp.init()
9+
node = rclcpp.Node("mtc_tutorial")
910

1011
# Use the joint interpolation planner
1112
jointPlanner = core.JointInterpolationPlanner()
1213

1314
# Create a task
1415
task = core.Task()
1516
task.name = "alternatives"
17+
task.loadRobotModel(node)
1618

1719
# Start from current robot state
1820
currentState = stages.CurrentState("current state")

demo/scripts/cartesian.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
#! /usr/bin/env python3
22
# -*- coding: utf-8 -*-
33

4+
from math import pi
5+
import time
6+
47
from std_msgs.msg import Header
58
from geometry_msgs.msg import TwistStamped, Twist, Vector3Stamped, Vector3
69
from moveit.task_constructor import core, stages
7-
from math import pi
8-
import time
910

10-
from py_binding_tools import roscpp_init
11+
import rclcpp
1112

12-
roscpp_init("mtc_tutorial")
13+
rclcpp.init()
14+
node = rclcpp.Node("mtc_tutorial")
1315

1416
# [cartesianTut1]
1517
group = "panda_arm"
@@ -24,6 +26,7 @@
2426
# [cartesianTut3]
2527
task = core.Task()
2628
task.name = "cartesian"
29+
task.loadRobotModel(node)
2730

2831
# start from current robot state
2932
task.add(stages.CurrentState("current state"))
@@ -34,23 +37,25 @@
3437
move = stages.MoveRelative("x +0.2", cartesian)
3538
move.group = group
3639
header = Header(frame_id="world")
37-
move.setDirection(Vector3Stamped(header=header, vector=Vector3(0.2, 0, 0)))
40+
move.setDirection(Vector3Stamped(header=header, vector=Vector3(x=0.2, y=0.0, z=0.0)))
3841
task.add(move)
3942
# [initAndConfigMoveRelative]
4043

4144
# [cartesianTut4]
4245
# move along y
4346
move = stages.MoveRelative("y -0.3", cartesian)
4447
move.group = group
45-
move.setDirection(Vector3Stamped(header=header, vector=Vector3(0, -0.3, 0)))
48+
move.setDirection(Vector3Stamped(header=header, vector=Vector3(x=0.0, y=-0.3, z=0.0)))
4649
task.add(move)
4750
# [cartesianTut4]
4851

4952
# [cartesianTut5]
5053
# rotate about z
5154
move = stages.MoveRelative("rz +45°", cartesian)
5255
move.group = group
53-
move.setDirection(TwistStamped(header=header, twist=Twist(angular=Vector3(0, 0, pi / 4.0))))
56+
move.setDirection(
57+
TwistStamped(header=header, twist=Twist(angular=Vector3(x=0.0, y=0.0, z=pi / 4.0)))
58+
)
5459
task.add(move)
5560
# [cartesianTut5]
5661

demo/scripts/compute_ik.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,24 @@
66
from std_msgs.msg import Header
77
import time
88

9-
from py_binding_tools import roscpp_init
9+
import rclcpp
1010

11-
roscpp_init("mtc_tutorial")
11+
rclcpp.init()
12+
node = rclcpp.Node("mtc_tutorial")
1213

1314
# Specify the planning group
1415
group = "panda_arm"
1516

1617
# Create a task
1718
task = core.Task()
1819
task.name = "compute IK"
20+
task.loadRobotModel(node)
1921

2022
# Add a stage to retrieve the current state
2123
task.add(stages.CurrentState("current state"))
2224

2325
# Add a planning stage connecting the generator stages
24-
planner = core.PipelinePlanner() # create default planning pipeline
26+
planner = core.PipelinePlanner(node) # create default planning pipeline
2527
task.add(stages.Connect("connect", [(group, planner)])) # operate on group
2628
del planner # Delete PipelinePlanner when not explicitly needed anymore
2729

0 commit comments

Comments
 (0)