|
| 1 | +from _pybullet_demo_helper import trajectory_replay |
| 2 | +from compas.geometry import Box |
| 3 | +from compas.geometry import Frame |
| 4 | +from compas.geometry import Point |
| 5 | +from compas.geometry import Vector |
| 6 | + |
| 7 | +from compas_fab.backends import MPNoIKSolutionError |
| 8 | +from compas_fab.backends import MPNoPlanFoundError |
| 9 | +from compas_fab.backends import PyBulletClient |
| 10 | +from compas_fab.backends import PyBulletPlanner |
| 11 | +from compas_fab.robots import ConfigurationTarget |
| 12 | +from compas_fab.robots import RigidBody |
| 13 | +from compas_fab.robots import RigidBodyState |
| 14 | +from compas_fab.robots import RobotCellLibrary |
| 15 | +from compas_fab.robots import RobotCellState |
| 16 | + |
| 17 | +with PyBulletClient("gui") as client: |
| 18 | + planner = PyBulletPlanner(client) |
| 19 | + |
| 20 | + # The robot cell in this example is loaded from RobotCellLibrary |
| 21 | + # The printing tool TCP is defined with its Z axis pointing out of nozzle |
| 22 | + robot_cell, robot_cell_state = RobotCellLibrary.abb_irb4600_40_255_printing_tool() |
| 23 | + |
| 24 | + # Add a box (without collision geometry) for demonstration visualization |
| 25 | + # Printing tool will trace a square around this box |
| 26 | + # box_size = 0.6 will demonstrate a successful planning |
| 27 | + # box_size = 0.8 will demonstrate a failure because it is out of reach |
| 28 | + box_size = 0.4 |
| 29 | + box = Box.from_corner_corner_height([1.0, 0.0, 0], [1.0 + box_size, 0.0 + box_size, 0], 1.50) |
| 30 | + rigidbody = RigidBody(box.to_mesh(True), None) |
| 31 | + robot_cell.rigid_body_models["box"] = rigidbody |
| 32 | + robot_cell_state.rigid_body_states["box"] = RigidBodyState(Frame.worldXY()) |
| 33 | + |
| 34 | + planner.set_robot_cell(robot_cell) |
| 35 | + |
| 36 | + # Set the robot's initial configuration |
| 37 | + start_state = robot_cell_state.copy() # type: RobotCellState |
| 38 | + start_state.robot_configuration.joint_values = [-0.5, 0.5, 0.0, 1.0, -0.7, 0.0] |
| 39 | + end_state = robot_cell_state.copy() # type: RobotCellState |
| 40 | + end_state.robot_configuration.joint_values = [0.5, 0.5, 0.0, 1.0, -0.7, 0.0] |
| 41 | + |
| 42 | + planner.set_robot_cell_state(start_state) |
| 43 | + planner.check_collision(start_state) |
| 44 | + input("Observe the start state in PyBullet's GUI, Press Enter to continue...") |
| 45 | + |
| 46 | + planner.set_robot_cell_state(end_state) |
| 47 | + planner.check_collision(end_state) |
| 48 | + input("Observe the end state in PyBullet's GUI, Press Enter to continue...") |
| 49 | + |
| 50 | + |
| 51 | + # -------------------------------------- |
| 52 | + # Plan Free Motion with ConfigurationTarget |
| 53 | + # --------------------------------------------- |
| 54 | + |
| 55 | + target = ConfigurationTarget(end_state.robot_configuration) |
| 56 | + try: |
| 57 | + trajectory = planner.plan_motion(target, start_state) |
| 58 | + print("Planned trajectory has {} points.".format(len(trajectory.points))) |
| 59 | + except MPNoIKSolutionError as e: |
| 60 | + # This exception is raised when part of the trajectory has no IK solution, |
| 61 | + # either due to collision or it is not reachable. |
| 62 | + print("No IK solution found. Reason:", e.message) |
| 63 | + print("Target that could not be reached:", e.target) |
| 64 | + trajectory = e.partial_trajectory |
| 65 | + print("Partial trajectory returned has {} points.".format(len(trajectory.points))) |
| 66 | + except MPNoPlanFoundError as e: |
| 67 | + # This exception is raised when no plan could be found, the IK solutions along the |
| 68 | + # trajectory are valid, but the planner could not find a continuous path between them. |
| 69 | + print("No plan found. Reason:", e.message) |
| 70 | + trajectory = e.partial_trajectory |
| 71 | + print("Partial trajectory returned has {} points.".format(len(trajectory.points))) |
| 72 | + |
| 73 | + # # ------------------------------------------------ |
| 74 | + # # Replay the trajectory in the PyBullet simulation |
| 75 | + # # ------------------------------------------------ |
| 76 | + |
| 77 | + trajectory_replay(planner, robot_cell_state, trajectory) |
0 commit comments