forked from AndrejOrsula/pymoveit2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_ik.py
executable file
·79 lines (64 loc) · 2.53 KB
/
ex_ik.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
"""
Example of computing Inverse Kinematics.
- ros2 run pymoveit2 ex_ik.py --ros-args -p position:="[0.25, 0.0, 1.0]" -p quat_xyzw:="[0.0, 0.0, 0.0, 1.0]"
- ros2 run pymoveit2 ex_ik.py --ros-args -p position:="[0.25, 0.0, 1.0]" -p quat_xyzw:="[0.0, 0.0, 0.0, 1.0]" -p synchronous:=False
"""
from threading import Thread
import rclpy
from rclpy.callback_groups import ReentrantCallbackGroup
from rclpy.node import Node
from pymoveit2 import MoveIt2, MoveIt2State
from pymoveit2.robots import panda
def main():
rclpy.init()
# Create node for this example
node = Node("ex_ik")
# Declare parameters for position and orientation
node.declare_parameter("position", [0.5, 0.0, 0.25])
node.declare_parameter("quat_xyzw", [1.0, 0.0, 0.0, 0.0])
node.declare_parameter("synchronous", True)
# Create callback group that allows execution of callbacks in parallel without restrictions
callback_group = ReentrantCallbackGroup()
# Create MoveIt 2 interface
moveit2 = MoveIt2(
node=node,
joint_names=panda.joint_names(),
base_link_name=panda.base_link_name(),
end_effector_name=panda.end_effector_name(),
group_name=panda.MOVE_GROUP_ARM,
callback_group=callback_group,
)
# Spin the node in background thread(s) and wait a bit for initialization
executor = rclpy.executors.MultiThreadedExecutor(2)
executor.add_node(node)
executor_thread = Thread(target=executor.spin, daemon=True, args=())
executor_thread.start()
node.create_rate(1.0).sleep()
# Get parameters
position = node.get_parameter("position").get_parameter_value().double_array_value
quat_xyzw = node.get_parameter("quat_xyzw").get_parameter_value().double_array_value
synchronous = node.get_parameter("synchronous").get_parameter_value().bool_value
# Move to joint configuration
node.get_logger().info(
f"Computing IK for {{position: {list(position)}, quat_xyzw: {list(quat_xyzw)}}}"
)
retval = None
if synchronous:
retval = moveit2.compute_ik(position, quat_xyzw)
else:
future = moveit2.compute_ik_async(position, quat_xyzw)
if future is not None:
rate = node.create_rate(10)
while not future.done():
rate.sleep()
retval = moveit2.get_compute_ik_result(future)
if retval is None:
print("Failed.")
else:
print("Succeeded. Result: " + str(retval))
rclpy.shutdown()
executor_thread.join()
exit(0)
if __name__ == "__main__":
main()