You can perform online trajectory planning that accounts for both robot and environmental collisions. This involves defining a robot model, collision geometries (like HalfSpace or Sphere), and an IK target. The planning loop typically uses a solver to find a trajectory that moves the robot's target link to a desired position and orientation while avoiding the specified collision objects.
To run these examples, ensure you have cloned the PyRoki repository to access pyroki_snippets implementation details used in the demonstration.
import time
import numpy as np
import pyroki as pk
import viser
from pyroki.collision import HalfSpace, RobotCollision, Sphere
from robot_descriptions.loaders.yourdfpy import load_robot_description
from viser.extras import ViserUrdf
import pyroki_snippets as pks
def main():
# 1. Setup Robot and Collisions
urdf = load_robot_description("panda_description")
target_link_name = "panda_hand"
robot = pk.Robot.from_urdf(urdf)
robot_coll = RobotCollision.from_urdf(urdf)
plane_coll = HalfSpace.from_point_and_normal(
np.array([0.0, 0.0, 0.0]), np.array([0.0, 0.0, 1.0])
)
sphere_coll = Sphere.from_center_and_radius(
np.array([0.0, 0.0, 0.0]), np.array([0.05])
)
# 2. Planning Parameters
len_traj, dt = 5, 0.1
# 3. Visualization Setup (Viser)
server = viser.ViserServer()
urdf_vis = ViserUrdf(server, urdf, root_node_name="/robot")
ik_target_handle = server.scene.add_transform_controls(
"/ik_target", scale=0.2, position=(0.3, 0.0, 0.5), wxyz=(0, 0, 1, 0)
)
sphere_handle = server.scene.add_transform_controls(
"/obstacle", scale=0.2, position=(0.4, 0.3, 0.4)
)
# 4. Planning Loop
sol_traj = np.array(
robot.joint_var_cls.default_factory()[None].repeat(len_traj, axis=0)
)
while True:
# Transform obstacle to world coordinates
sphere_coll_world_current = sphere_coll.transform_from_wxyz_position(
wxyz=np.array(sphere_handle.wxyz),
position=np.array(sphere_handle.position),
)
world_coll_list = [plane_coll, sphere_coll_world_current]
# Solve online planning
sol_traj, sol_pos, sol_wxyz = pks.solve_online_planning(
robot=robot,
robot_coll=robot_coll,
world_coll=world_coll_list,
target_link_name=target_link_name,
target_position=np.array(ik_target_handle.position),
target_wxyz=np.array(ik_target_handle.wxyz),
timesteps=len_traj,
dt=dt,
start_cfg=sol_traj[0],
prev_sols=sol_traj,
)
# Update visualization
urdf_vis.update_cfg(sol_traj[0])
if __name__ == "__main__":
main()