When I run the code below on the CPU, things work as expected. However, when I run it on GPU, the robots do not seem to move at all! However, .get_joint_state()
shows they moved, which means the app is not rendering their joint states.
How can I render robots’ movements when running the simulation on GPU?
The screencast shows what I tried to explain in the post. I attach robots’ USD (in the .zip file because the forum doesn’t support .USD files upload) and a minimal reproducible example.
As a side note, based on this answer, I tried adding world.get_physics_context().enable_fabric(True)
but that didn’t make a difference.
robot.zip (1.2 MB)
from isaacsim import SimulationApp
simulation_app = SimulationApp({"headless":False})
from omni.isaac.core.world import World
from omni.isaac.core.utils.stage import add_reference_to_stage
from omni.isaac.core.prims import XFormPrim
from omni.isaac.core.articulations import ArticulationView
from omni.isaac.core.utils.types import ArticulationAction
import torch
import numpy as np
use_gpu = True
if use_gpu:
device='cuda:0'
backend='torch'
else:
device='cpu'
backend='numpy'
batch_size = 4
world=World(device=device, backend=backend)
if 'cuda' in device:
world.get_physics_context().enable_gpu_dynamics(True)
world.scene.add_default_ground_plane()
# add the robots
for i in range(batch_size):
robot_str = "/World/Robot_" + str(i + 1)
add_reference_to_stage(usd_path='robot.usd', prim_path=robot_str)
position = np.array([i,0,0])
if 'cuda' in backend:
position = torch.tensor(position).to(device)
world.scene.add(XFormPrim(prim_path=robot_str, name=robot_str, position=position))
world.initialize_physics()
robot_range = "/World/Robot_[1-9]|[1-9][0-9]{1,3}|9000"
robots_view = ArticulationView(prim_paths_expr=robot_range, name="robots_view")
robots_view.initialize()
world.scene.add(robots_view)
world.play()
q = torch.rand((batch_size, 23)).to(device)
if 'numpy' in backend:
q = q.cpu().numpy()
print("target q: ", q)
for i in range(240):
robots_view.apply_action(ArticulationAction(joint_positions=q))
world.step(render=True)
# same thing
#robots_view.set_joint_positions(q)
states = robots_view.get_joints_state()
if i % 24 == 0:
print(states.positions)
while True:
world.step(render=True)