|
| 1 | +import sys |
| 2 | +import time |
| 3 | +import tyro |
| 4 | + |
| 5 | +import pinocchio as pin |
| 6 | +from common import MODELS, ROBOPLAN_EXAMPLES_DIR |
| 7 | +from roboplan import ( |
| 8 | + shortcutPath, |
| 9 | + JointConfiguration, |
| 10 | + Scene, |
| 11 | + RRTOptions, |
| 12 | + RRT, |
| 13 | +) |
| 14 | +from roboplan.viser_visualizer import ViserVisualizer, visualizePath, visualizeTree |
| 15 | + |
| 16 | + |
| 17 | +def main( |
| 18 | + model: str = "ur5", |
| 19 | + max_connection_distance: float = 1.0, |
| 20 | + collision_check_step_size: float = 0.05, |
| 21 | + goal_biasing_probability: float = 0.15, |
| 22 | + max_nodes: int = 1000, |
| 23 | + max_planning_time: float = 3.0, |
| 24 | + rrt_connect: bool = False, |
| 25 | + include_shortcutting: bool = False, |
| 26 | + host: str = "localhost", |
| 27 | + port: str = "8000", |
| 28 | +): |
| 29 | + """ |
| 30 | + Run the RRT example with the provided parameters. |
| 31 | +
|
| 32 | +
|
| 33 | + Parameters: |
| 34 | + model: The name of the model to user (ur5 or franka). |
| 35 | + max_connection_distance: Maximum connection distance between two search nodes. |
| 36 | + collision_check_step_size: Configuration-space step size for collision checking along edges. |
| 37 | + goal_biasing_probability: Weighting of the goal node during random sampling. |
| 38 | + max_nodes: The maximum number of nodes to add to the search tree. |
| 39 | + max_planning_time: The maximum time (in seconds) to search for a path. |
| 40 | + rrt_connect: Whether or not to use RRT-Connect. |
| 41 | + include_shortcutting: Whether or not to include path shortcutting for found paths. |
| 42 | + host: The host for the ViserVisualizer. |
| 43 | + port: The port for the ViserVisualizer. |
| 44 | + """ |
| 45 | + |
| 46 | + if model not in MODELS: |
| 47 | + print(f"Invalid model requested: {model}") |
| 48 | + sys.exit(1) |
| 49 | + |
| 50 | + urdf_path, srdf_path, ee_name, _, _ = MODELS[model] |
| 51 | + package_paths = [ROBOPLAN_EXAMPLES_DIR] |
| 52 | + |
| 53 | + scene = Scene("test_scene", urdf_path, srdf_path, package_paths) |
| 54 | + |
| 55 | + # Create a redundant Pinocchio model just for visualization. |
| 56 | + # When Pinocchio 4.x releases nanobind bindings, we should be able to directly grab the model from the scene instead. |
| 57 | + model, collision_model, visual_model = pin.buildModelsFromUrdf( |
| 58 | + urdf_path, package_dirs=package_paths |
| 59 | + ) |
| 60 | + viz = ViserVisualizer(model, collision_model, visual_model) |
| 61 | + viz.initViewer(open=True, loadModel=True, host=host, port=port) |
| 62 | + |
| 63 | + # Optionally include path shortening |
| 64 | + include_shortcutting = True |
| 65 | + |
| 66 | + # Set up an RRT and perform path planning. |
| 67 | + options = RRTOptions() |
| 68 | + options.max_connection_distance = max_connection_distance |
| 69 | + options.collision_check_step_size = collision_check_step_size |
| 70 | + options.goal_biasing_probability = goal_biasing_probability |
| 71 | + options.max_nodes = max_nodes |
| 72 | + options.max_planning_time = max_planning_time |
| 73 | + options.rrt_connect = rrt_connect |
| 74 | + rrt = RRT(scene, options) |
| 75 | + |
| 76 | + start = JointConfiguration() |
| 77 | + start.positions = scene.randomCollisionFreePositions() |
| 78 | + assert start.positions is not None |
| 79 | + |
| 80 | + goal = JointConfiguration() |
| 81 | + goal.positions = scene.randomCollisionFreePositions() |
| 82 | + assert goal.positions is not None |
| 83 | + |
| 84 | + path = rrt.plan(start, goal) |
| 85 | + assert path is not None |
| 86 | + |
| 87 | + if include_shortcutting: |
| 88 | + shortcut_path = shortcutPath( |
| 89 | + scene, path, options.collision_check_step_size, 1000 |
| 90 | + ) |
| 91 | + |
| 92 | + # Visualize the tree and path |
| 93 | + print(path) |
| 94 | + viz.display(start.positions) |
| 95 | + visualizePath(viz, scene, path, ee_name, 0.05) |
| 96 | + visualizeTree(viz, scene, rrt, ee_name, 0.05) |
| 97 | + |
| 98 | + if include_shortcutting: |
| 99 | + print("Shortcutted path:") |
| 100 | + print(shortcut_path) |
| 101 | + visualizePath( |
| 102 | + viz, scene, shortcut_path, ee_name, 0.05, (0, 100, 0), "/rrt/shortcut_path" |
| 103 | + ) |
| 104 | + |
| 105 | + try: |
| 106 | + while True: |
| 107 | + time.sleep(10.0) |
| 108 | + except KeyboardInterrupt: |
| 109 | + pass |
| 110 | + |
| 111 | + |
| 112 | +if __name__ == "__main__": |
| 113 | + tyro.cli(main) |
0 commit comments