|
| 1 | +from pycram.external_interfaces import giskard |
| 2 | +from pycram.robot_description import RobotDescription |
| 3 | +from pycram.world import World |
| 4 | +from pycram.local_transformer import LocalTransformer |
| 5 | +from pycram.datastructures.enums import MovementType, WaypointsMovementType |
| 6 | +from pycram.helpers import euler_from_quaternion |
| 7 | +from scipy.spatial.transform import Rotation as R |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +# Falls du eigene Exceptions hast: |
| 11 | +from pycram.failures import NavigationGoalNotReachedError, ToolPoseNotReachedError |
| 12 | + |
| 13 | +def map_MoveMotion(motion): |
| 14 | + giskard.avoid_all_collisions() |
| 15 | + giskard.achieve_cartesian_goal( |
| 16 | + motion.target, |
| 17 | + RobotDescription.current_robot_description.base_link, |
| 18 | + "map" |
| 19 | + ) |
| 20 | + if not World.current_world.robot.pose.almost_equal(motion.target, 0.05, 3): |
| 21 | + raise NavigationGoalNotReachedError(World.current_world.robot.pose, motion.target) |
| 22 | + |
| 23 | +def map_LookingMotion(motion): |
| 24 | + target = motion.target |
| 25 | + robot = World.robot |
| 26 | + |
| 27 | + local_transformer = LocalTransformer() |
| 28 | + neck = RobotDescription.current_robot_description.get_neck() |
| 29 | + pan_link = neck["yaw"][0] |
| 30 | + tilt_link = neck["pitch"][0] |
| 31 | + pan_joint = neck["yaw"][1] |
| 32 | + tilt_joint = neck["pitch"][1] |
| 33 | + |
| 34 | + pose_in_pan = local_transformer.transform_pose(target, robot.get_link_tf_frame(pan_link)).position.to_list() |
| 35 | + pose_in_tilt = local_transformer.transform_pose(target, robot.get_link_tf_frame(tilt_link)).position.to_list() |
| 36 | + new_pan = np.arctan2(pose_in_pan[1], pose_in_pan[0]) |
| 37 | + |
| 38 | + tilt_offset = RobotDescription.current_robot_description.get_offset(tilt_joint) |
| 39 | + quaternion_list = [0, 0, 0, 1] |
| 40 | + if tilt_offset: |
| 41 | + q = tilt_offset.pose.orientation |
| 42 | + quaternion_list = [q.x, q.y, q.z, q.w] |
| 43 | + |
| 44 | + tilt_offset_rotation = euler_from_quaternion(quaternion_list, axes='sxyz') |
| 45 | + adjusted_pose_in_tilt = R.from_euler('xyz', tilt_offset_rotation).apply(pose_in_tilt) |
| 46 | + new_tilt = -np.arctan2(adjusted_pose_in_tilt[2], np.sqrt(adjusted_pose_in_tilt[0] ** 2 + adjusted_pose_in_tilt[1] ** 2)) |
| 47 | + |
| 48 | + if RobotDescription.current_robot_description.name in {"iCub", "tiago_dual"}: |
| 49 | + new_tilt = -new_tilt |
| 50 | + |
| 51 | + current_pan = robot.get_joint_position(pan_joint) |
| 52 | + current_tilt = robot.get_joint_position(tilt_joint) |
| 53 | + |
| 54 | + giskard.avoid_all_collisions() |
| 55 | + giskard.achieve_joint_goal({ |
| 56 | + pan_joint: new_pan + current_pan, |
| 57 | + tilt_joint: new_tilt + current_tilt |
| 58 | + }) |
| 59 | + |
| 60 | +def map_MoveTCPMotion(motion): |
| 61 | + lt = LocalTransformer() |
| 62 | + pose_in_map = lt.transform_pose(motion.target, "map") |
| 63 | + tip_link = RobotDescription.current_robot_description.get_arm_chain(motion.arm).get_tool_frame() |
| 64 | + root_link = "map" |
| 65 | + |
| 66 | + gripper_that_can_collide = motion.arm if motion.allow_gripper_collision else None |
| 67 | + if motion.allow_gripper_collision: |
| 68 | + giskard.allow_gripper_collision(motion.arm) |
| 69 | + |
| 70 | + if motion.movement_type == MovementType.STRAIGHT_TRANSLATION: |
| 71 | + giskard.achieve_straight_translation_goal(pose_in_map.position.to_list(), tip_link, root_link) |
| 72 | + elif motion.movement_type == MovementType.STRAIGHT_CARTESIAN: |
| 73 | + giskard.achieve_straight_cartesian_goal(pose_in_map, tip_link, root_link) |
| 74 | + elif motion.movement_type == MovementType.TRANSLATION: |
| 75 | + giskard.achieve_translation_goal(pose_in_map.position.to_list(), tip_link, root_link) |
| 76 | + elif motion.movement_type == MovementType.CARTESIAN: |
| 77 | + giskard.achieve_cartesian_goal(pose_in_map, tip_link, root_link, |
| 78 | + grippers_that_can_collide=gripper_that_can_collide) |
| 79 | + |
| 80 | + if not World.current_world.robot.get_link_pose(tip_link).almost_equal(motion.target, 0.3, 3): |
| 81 | + raise ToolPoseNotReachedError(World.current_world.robot.get_link_pose(tip_link), motion.target) |
| 82 | + |
| 83 | +def map_MoveArmJointsMotion(motion): |
| 84 | + joint_goals = {} |
| 85 | + if motion.left_arm_poses: |
| 86 | + joint_goals.update(motion.left_arm_poses) |
| 87 | + if motion.right_arm_poses: |
| 88 | + joint_goals.update(motion.right_arm_poses) |
| 89 | + giskard.avoid_all_collisions() |
| 90 | + giskard.achieve_joint_goal(joint_goals) |
| 91 | + |
| 92 | +def map_MoveJointsMotion(motion): |
| 93 | + name_to_position = dict(zip(motion.names, motion.positions)) |
| 94 | + giskard.avoid_all_collisions() |
| 95 | + giskard.achieve_joint_goal( |
| 96 | + name_to_position, |
| 97 | + align=motion.align, |
| 98 | + tip_link=motion.tip_link, |
| 99 | + tip_normal=motion.tip_normal, |
| 100 | + root_link=motion.root_link, |
| 101 | + root_normal=motion.root_normal |
| 102 | + ) |
| 103 | + |
| 104 | +def map_OpeningMotion(motion): |
| 105 | + giskard.achieve_open_container_goal( |
| 106 | + RobotDescription.current_robot_description.get_arm_chain(motion.arm).get_tool_frame(), |
| 107 | + motion.object_part.name |
| 108 | + ) |
| 109 | + |
| 110 | +def map_ClosingMotion(motion): |
| 111 | + giskard.achieve_close_container_goal( |
| 112 | + RobotDescription.current_robot_description.get_arm_chain(motion.arm).get_tool_frame(), |
| 113 | + motion.object_part.name |
| 114 | + ) |
| 115 | + |
| 116 | +def map_MoveTCPWaypointsMotion(motion): |
| 117 | + lt = LocalTransformer() |
| 118 | + waypoints = [lt.transform_pose(x, "map") for x in motion.waypoints] |
| 119 | + tip_link = RobotDescription.current_robot_description.get_arm_chain(motion.arm).get_tool_frame() |
| 120 | + root_link = "map" |
| 121 | + |
| 122 | + giskard.avoid_all_collisions() |
| 123 | + if motion.allow_gripper_collision: |
| 124 | + giskard.allow_gripper_collision(motion.arm) |
| 125 | + |
| 126 | + giskard.achieve_cartesian_waypoints_goal( |
| 127 | + waypoints=waypoints, |
| 128 | + tip_link=tip_link, |
| 129 | + root_link=root_link, |
| 130 | + enforce_final_orientation=True if motion.movement_type == WaypointsMovementType.ENFORCE_ORIENTATION_FINAL_POINT else False |
| 131 | + ) |
0 commit comments