1- from pathlib import Path
1+ import sys
22import time
3+ import tyro
34
4- import numpy as np
55import pinocchio as pin
6+ from common import MODELS , ROBOPLAN_EXAMPLES_DIR
67from roboplan import (
7- computeFramePath ,
8- get_package_share_dir ,
8+ shortcutPath ,
99 JointConfiguration ,
10- JointPath ,
1110 Scene ,
1211 RRTOptions ,
1312 RRT ,
1413)
15- from roboplan .viser_visualizer import ViserVisualizer
16-
17-
18- def visualizePath (
19- viz : ViserVisualizer ,
20- scene : Scene ,
21- rrt : RRT ,
22- path : JointPath | None ,
23- frame_name : str ,
24- max_step_size : float ,
25- ) -> None :
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+ ):
2629 """
27- Helper function to visualize the RRT path.
28- TODO: Move this to the actual Python package itself.
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.
2944 """
30- start_nodes , goal_nodes = rrt .getNodes ()
31-
32- start_segments = []
33- for node in start_nodes [1 :]:
34- q_start = start_nodes [node .parent_id ].config
35- q_end = node .config
36- frame_path = computeFramePath (scene , q_start , q_end , frame_name , max_step_size )
37- for idx in range (len (frame_path ) - 1 ):
38- start_segments .append ([frame_path [idx ][:3 , 3 ], frame_path [idx + 1 ][:3 , 3 ]])
39-
40- goal_segments = []
41- for node in goal_nodes [1 :]:
42- q_start = goal_nodes [node .parent_id ].config
43- q_end = node .config
44- frame_path = computeFramePath (scene , q_start , q_end , frame_name , max_step_size )
45- for idx in range (len (frame_path ) - 1 ):
46- goal_segments .append ([frame_path [idx ][:3 , 3 ], frame_path [idx + 1 ][:3 , 3 ]])
47-
48- path_segments = []
49- if path is not None :
50- for idx in range (len (path .positions ) - 1 ):
51- q_start = path .positions [idx ]
52- q_end = path .positions [idx + 1 ]
53- frame_path = computeFramePath (
54- scene , q_start , q_end , frame_name , max_step_size
55- )
56- for idx in range (len (frame_path ) - 1 ):
57- path_segments .append (
58- [frame_path [idx ][:3 , 3 ], frame_path [idx + 1 ][:3 , 3 ]]
59- )
60-
61- if start_segments :
62- viz .viewer .scene .add_line_segments (
63- "/rrt/start_tree" ,
64- points = np .array (start_segments ),
65- colors = (0 , 100 , 100 ),
66- line_width = 1.0 ,
67- )
68- if goal_segments :
69- viz .viewer .scene .add_line_segments (
70- "/rrt/goal_tree" ,
71- points = np .array (goal_segments ),
72- colors = (100 , 0 , 100 ),
73- line_width = 1.0 ,
74- )
75-
76- if path_segments :
77- viz .viewer .scene .add_line_segments (
78- "/rrt/path" ,
79- points = np .array (path_segments ),
80- colors = (100 , 100 , 0 ),
81- line_width = 3.0 ,
82- )
83-
8445
85- if __name__ == "__main__" :
46+ if model not in MODELS :
47+ print (f"Invalid model requested: { model } " )
48+ sys .exit (1 )
8649
87- roboplan_examples_dir = Path (get_package_share_dir ())
88- urdf_path = roboplan_examples_dir / "ur_robot_model" / "ur5_gripper.urdf"
89- srdf_path = roboplan_examples_dir / "ur_robot_model" / "ur5_gripper.srdf"
90- package_paths = [roboplan_examples_dir ]
50+ urdf_path , srdf_path , ee_name , _ , _ = MODELS [model ]
51+ package_paths = [ROBOPLAN_EXAMPLES_DIR ]
9152
9253 scene = Scene ("test_scene" , urdf_path , srdf_path , package_paths )
9354
@@ -97,14 +58,19 @@ def visualizePath(
9758 urdf_path , package_dirs = package_paths
9859 )
9960 viz = ViserVisualizer (model , collision_model , visual_model )
100- viz .initViewer (open = True , loadModel = True )
61+ viz .initViewer (open = True , loadModel = True , host = host , port = port )
62+
63+ # Optionally include path shortening
64+ include_shortcutting = True
10165
10266 # Set up an RRT and perform path planning.
10367 options = RRTOptions ()
104- options .max_connection_distance = 1.0
105- options .collision_check_step_size = 0.05
106- options .max_planning_time = 3.0
107- options .rrt_connect = True
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
10874 rrt = RRT (scene , options )
10975
11076 start = JointConfiguration ()
@@ -118,14 +84,30 @@ def visualizePath(
11884 path = rrt .plan (start , goal )
11985 assert path is not None
12086
121- # WHOOPSIE DAISEY
122- goal .positions [0 ] = - 6
123- path = rrt .plan_expected (start , goal )
87+ if include_shortcutting :
88+ shortcut_path = shortcutPath (
89+ scene , path , options .collision_check_step_size , 1000
90+ )
12491
12592 # Visualize the tree and path
12693 print (path )
12794 viz .display (start .positions )
128- visualizePath (viz , scene , rrt , path , "tool0" , 0.05 )
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+ )
129104
130- while True :
131- time .sleep (10.0 )
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