Background: I am working on a script that requires the TurtleBot to refresh its pose randomly in each of the next 50 frames, and I also want to print its pose for each frame.
Issue: While the TurtleBot can refresh its pose randomly without errors in the Script Editor, the system only prints and outputs the pose for the first frame. It doesn’t print or write the poses for the subsequent frames to a txt file. How can I resolve this issue?
Here are my code:
import omni.replicator.core as rep
from pxr import UsdGeom, Gf, Usd
Constants
WORKER_PRIM_PATH = “/World/turtlebot_tutorial_multi_sensor_publish_rates/turtlebot3_burger”
USD_SCENE_PATH = ‘/home/lenovo/JammingWong/FuhuaProject_9_6 (copy)/Turtlebot_Example.usd’
FILE_PATH = ‘/home/lenovo/JammingWong/Recorder/empty/position_orientation/turtlebot_positions_orientations.txt’
Counter = 1
Define the TurtleBot randomization function
def turtlebot():
global Counter
# Load the USD scene and get the prim
stage = Usd.Stage.Open(USD_SCENE_PATH)
turtlebot_prim = stage.GetPrimAtPath(WORKER_PRIM_PATH)
turtlebot_prim2 = rep.get.prim_at_path(WORKER_PRIM_PATH)
# Randomize position and rotation
with turtlebot_prim2:
rep.modify.pose(
position=rep.distribution.uniform((-1, -1, -0.75377), (1, 0, -0.75377)),
rotation=rep.distribution.uniform((0, 0, -180), (0, 0, 180))
)
# Extract the world transform matrix and get actual position and rotation
xformable = UsdGeom.Xformable(turtlebot_prim)
transform_matrix = xformable.ComputeLocalToWorldTransform(Usd.TimeCode.Default())
actual_rotation = transform_matrix.ExtractRotation()
actual_translation = transform_matrix.ExtractTranslation()
# Record the position and orientation to a file
with open(FILE_PATH, 'a') as f:
f.write(f"Frame {Counter}:\n")
f.write(f"Actual Position: {actual_translation}\n")
f.write(f"Actual Rotation: {actual_rotation}\n")
f.write("-" * 40 + "\n")
# Print debug information
print("Actual Position:", actual_translation)
print("Actual Rotation:", actual_rotation)
print("Counter =", Counter)
# Increment counter
Counter += 1
return turtlebot_prim2
Register the randomizer
rep.randomizer.register(turtlebot)
Trigger the randomization for 10,000 frames
with rep.trigger.on_frame(num_frames=50):
rep.randomizer.turtlebot()