0

Previously this was posted on the PyBullet forums but I haven't gotten a response so I'm looking for assistance here. I am currently working on a project where I have trained my own AI to simulate a specific movement and have recorded the movement data onto a JSON file. I am now trying to replicate this animation in PyBullet, but have encountered some issues.

When attempting to use the recorded joint information, the animation runs through all the movements in a second. To address this, I also recorded the time of each movement, but Python is too slow to execute the movements at the correct time. Attempts at using linear interpolation, setting the time step, and various combinations of time calculations have not yielded successful results.

I am reaching out to the community for guidance, as I believe someone may have encountered a similar problem in the past. If you have experience replicating animations, particularly in Python (C++ is not my strong suit), I would greatly appreciate any advice or tips you may have.

To provide a bit more context, the model I am using is a XSensStyleModel generated using the motion capture data from the Human Gazebo repository on Github (https://github.com/robotology/human-gazebo). The model has 50 joints, and while some are a bit glitchy on their own, they did not cause any issues during simulations as the movements were designed to not exceed their joint angles.

Here's a brief sample of a one fifth of a key inside the JSON file. I want to also note that recorded time periods were 0.5-1 second intervals apart from each other since that's all that could be gotten during the simulation and the animation is supposed to go on for 150 seconds.

 {"1.2839901447296143": {"0": {"joint_positions": -0.2660407240964722}, "1": {"joint_positions": 0.5304539359064856}, "2": {"joint_positions": -0.44852838864481787}, "3": {"joint_positions": -0.14016010671763662}, "4": {"joint_positions": 0.5558784919638431}, "5": {"joint_positions": 0.463813963178519}, "6": {"joint_positions": 0.1232185850913412}, "7": {"joint_positions": 0.5509734074329725}, "8": {"joint_positions": -0.5095558255751145}, "9": {"joint_positions": 0.08173290868566063}, "10": {"joint_positions": 0.3949344400183078}

Here's the script current rendition after reverting back to the logic I first tried to implement after numerous fails with other ones..

import pybullet as p
import time
import json
# Connect to the PyBullet physics server
p.connect(p.GUI)
p.setRealTimeSimulation(0)

# Load the humanoid model
skeleton = p.loadURDF("skeleton.urdf", useFixedBase=False)
p.setTimeStep(0.01)

# add a ground plane
plane_id = p.createCollisionShape(p.GEOM_PLANE)
ground = p.createMultiBody(baseCollisionShapeIndex=plane_id, basePosition=[0, 0, 0])

# enable collisions between the humanoid model and the ground plane
p.setCollisionFilterPair(skeleton, ground, -1, -1, 1)

# set the gravity
p.setGravity(0, 0, -9.8)

# load the joint angles from json file
with open("joint_angles.json", "r") as file:
    joint_angles = json.load(file)

# Initialize a dictionary to store the current joint angles
current_joint_angles = {joint_num: 0 for joint_num in range(p.getNumJoints(skeleton))}

# Get the start time
start_time = time.time()  +
offset = [0, 0, 1]
position, orientation = p.getBasePositionAndOrientation(skeleton)
new_position = [position[i] + offset[i] for i in range(3)]
p.resetBasePositionAndOrientation(skeleton, new_position, orientation)
n = 1

# Get the list of all time frames
time_frames = list(joint_angles.keys())

# Iterate through the time frames and set the joint angls
for i, keyframe_time in enumerate(time_frames):
    keyframe_time = float(keyframe_time)
    current_time = time.time() - start_time 
    time_to_sleep = keyframe_time - current_time
    while time_to_sleep > 0:
        time_to_sleep = keyframe_time - current_time
    # Sleep for the calculated amount of time
    time.sleep(time_to_sleep)

    # Get the joint angles for this time frame
    for joint_num, joint_data in joint_angles[str(keyframe_time)].items():
        joint_positions = joint_data['joint_positions']
        p.setJointMotorControl2(bodyIndex=0, jointIndex=int(joint_num), controlMode=p.POSITION_CONTROL, targetPosition=joint_positions)

# Step the simulation
    p.stepSimulation()

print(time.time() - start_time)
p.disconnect()

Thank you in advance for any help or advice you can provide.

Best, Drum

Drum
  • 9
  • 3

0 Answers0