mirror of
https://github.com/bulletphysics/bullet3.git
synced 2026-10-25 18:15:38 +00:00
29 KiB
29 KiB
In [ ]:
# Install the PyBullet module.
!pip install -U pybulletIn [ ]:
import pybullet as p
p.connect(p.DIRECT)0
In [ ]:
import pybullet_data
p.setAdditionalSearchPath(pybullet_data.getDataPath())In [ ]:
# First, let's make sure we start with a fresh new simulation.
# Otherwise, we can keep adding objects by running this cell over again.
p.resetSimulation()
# Load our simulation floor plane at the origin (0, 0, 0).
p.loadURDF('plane.urdf')
# Load an R2D2 droid at the position at 0.5 meters height in the z-axis.
r2d2 = p.loadURDF('r2d2.urdf', [0, 0, 0.5])
# We can check the number of bodies we have in the simulation.
p.getNumBodies()2
In [ ]:
# First let's define a class for the JointInfo.
from dataclasses import dataclass
@dataclass
class Joint:
index: int
name: str
type: int
gIndex: int
uIndex: int
flags: int
damping: float
friction: float
lowerLimit: float
upperLimit: float
maxForce: float
maxVelocity: float
linkName: str
axis: tuple
parentFramePosition: tuple
parentFrameOrientation: tuple
parentIndex: int
def __post_init__(self):
self.name = str(self.name, 'utf-8')
self.linkName = str(self.linkName, 'utf-8')
# Let's analyze the R2D2 droid!
print(f"r2d2 unique ID: {r2d2}")
for i in range(p.getNumJoints(r2d2)):
joint = Joint(*p.getJointInfo(r2d2, i))
print(joint)r2d2 unique ID: 1 Joint(index=0, name='base_to_right_leg', type=4, gIndex=-1, uIndex=-1, flags=0, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=0.0, maxVelocity=0.0, linkName='right_leg', axis=(0.0, 0.0, 0.0), parentFramePosition=(0.22, 0.0, 0.25), parentFrameOrientation=(0.0, -0.7070904020014416, 0.0, 0.7071231599922604), parentIndex=-1) Joint(index=1, name='right_base_joint', type=4, gIndex=-1, uIndex=-1, flags=0, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=0.0, maxVelocity=0.0, linkName='right_base', axis=(0.0, 0.0, 0.0), parentFramePosition=(0.2999999996780742, 0.0, -1.3898038463944216e-05), parentFrameOrientation=(0.0, 0.7070904020014416, 0.0, 0.7071231599922604), parentIndex=0) Joint(index=2, name='right_front_wheel_joint', type=0, gIndex=7, uIndex=6, flags=1, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=100.0, maxVelocity=100.0, linkName='right_front_wheel', axis=(0.0, 0.0, 1.0), parentFramePosition=(0.0, 0.133333333333, -0.085), parentFrameOrientation=(0.0, -0.7070904020014416, 0.0, 0.7071231599922604), parentIndex=1) Joint(index=3, name='right_back_wheel_joint', type=0, gIndex=8, uIndex=7, flags=1, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=100.0, maxVelocity=100.0, linkName='right_back_wheel', axis=(0.0, 0.0, 1.0), parentFramePosition=(0.0, -0.133333333333, -0.085), parentFrameOrientation=(0.0, -0.7070904020014416, 0.0, 0.7071231599922604), parentIndex=1) Joint(index=4, name='base_to_left_leg', type=4, gIndex=-1, uIndex=-1, flags=0, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=0.0, maxVelocity=0.0, linkName='left_leg', axis=(0.0, 0.0, 0.0), parentFramePosition=(-0.22, 0.0, 0.25), parentFrameOrientation=(0.0, -0.7070904020014416, 0.0, 0.7071231599922604), parentIndex=-1) Joint(index=5, name='left_base_joint', type=4, gIndex=-1, uIndex=-1, flags=0, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=0.0, maxVelocity=0.0, linkName='left_base', axis=(0.0, 0.0, 0.0), parentFramePosition=(0.2999999996780742, 0.0, -1.3898038463944216e-05), parentFrameOrientation=(0.0, 0.7070904020014416, 0.0, 0.7071231599922604), parentIndex=4) Joint(index=6, name='left_front_wheel_joint', type=0, gIndex=9, uIndex=8, flags=1, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=100.0, maxVelocity=100.0, linkName='left_front_wheel', axis=(0.0, 0.0, 1.0), parentFramePosition=(0.0, 0.133333333333, -0.085), parentFrameOrientation=(0.0, -0.7070904020014416, 0.0, 0.7071231599922604), parentIndex=5) Joint(index=7, name='left_back_wheel_joint', type=0, gIndex=10, uIndex=9, flags=1, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=100.0, maxVelocity=100.0, linkName='left_back_wheel', axis=(0.0, 0.0, 1.0), parentFramePosition=(0.0, -0.133333333333, -0.085), parentFrameOrientation=(0.0, -0.7070904020014416, 0.0, 0.7071231599922604), parentIndex=5) Joint(index=8, name='gripper_extension', type=1, gIndex=11, uIndex=10, flags=1, damping=0.0, friction=0.0, lowerLimit=-0.38, upperLimit=0.0, maxForce=1000.0, maxVelocity=0.5, linkName='gripper_pole', axis=(1.0, 0.0, 0.0), parentFramePosition=(0.0, 0.19, 0.2), parentFrameOrientation=(0.0, 0.0, -0.7070904020014416, 0.7071231599922604), parentIndex=-1) Joint(index=9, name='left_gripper_joint', type=0, gIndex=12, uIndex=11, flags=1, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=0.548, maxForce=1000.0, maxVelocity=0.5, linkName='left_gripper', axis=(0.0, 0.0, 1.0), parentFramePosition=(0.2, 0.02, 0.0), parentFrameOrientation=(0.0, 0.0, 0.0, 1.0), parentIndex=8) Joint(index=10, name='left_tip_joint', type=4, gIndex=-1, uIndex=-1, flags=0, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=0.0, maxVelocity=0.0, linkName='left_tip', axis=(0.0, 0.0, 0.0), parentFramePosition=(0.0, 0.0, 0.0), parentFrameOrientation=(0.0, 0.0, 0.0, 1.0), parentIndex=9) Joint(index=11, name='right_gripper_joint', type=0, gIndex=13, uIndex=12, flags=1, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=0.548, maxForce=1000.0, maxVelocity=0.5, linkName='right_gripper', axis=(0.0, 0.0, -1.0), parentFramePosition=(0.2, -0.02, 0.0), parentFrameOrientation=(0.0, 0.0, 0.0, 1.0), parentIndex=8) Joint(index=12, name='right_tip_joint', type=4, gIndex=-1, uIndex=-1, flags=0, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=0.0, maxVelocity=0.0, linkName='right_tip', axis=(0.0, 0.0, 0.0), parentFramePosition=(0.0, 0.0, 0.0), parentFrameOrientation=(0.0, 0.0, 0.0, 1.0), parentIndex=11) Joint(index=13, name='head_swivel', type=0, gIndex=14, uIndex=13, flags=1, damping=0.0, friction=0.0, lowerLimit=-0.38, upperLimit=0.0, maxForce=1000.0, maxVelocity=0.5, linkName='head', axis=(0.0, 0.0, 1.0), parentFramePosition=(0.0, 0.0, 0.3), parentFrameOrientation=(0.0, 0.0, 0.0, 1.0), parentIndex=-1) Joint(index=14, name='tobox', type=4, gIndex=-1, uIndex=-1, flags=0, damping=0.0, friction=0.0, lowerLimit=0.0, upperLimit=-1.0, maxForce=0.0, maxVelocity=0.0, linkName='box', axis=(0.0, 0.0, 0.0), parentFramePosition=(0.0, 0.1214, 0.1214), parentFrameOrientation=(0.0, 0.0, 0.0, 1.0), parentIndex=13)
In [ ]:
# Set the gravity to Earth's gravity.
p.setGravity(0, 0, -9.807)
# Run the simulation for a fixed amount of steps.
for i in range(20):
position, orientation = p.getBasePositionAndOrientation(r2d2)
x, y, z = position
roll, pitch, yaw = p.getEulerFromQuaternion(orientation)
print(f"{i:3}: x={x:0.10f}, y={y:0.10f}, z={z:0.10f}), roll={roll:0.10f}, pitch={pitch:0.10f}, yaw={yaw:0.10f}")
p.stepSimulation()0: x=0.0000000000, y=0.0000000000, z=0.5000000000), roll=0.0000000000, pitch=-0.0000000000, yaw=0.0000000000 1: x=-0.0000000000, y=0.0000000000, z=0.4998297396), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 2: x=-0.0000000000, y=0.0000000000, z=0.4994892187), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 3: x=-0.0000000000, y=0.0000000000, z=0.4989784375), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 4: x=-0.0000000000, y=0.0000000000, z=0.4982973958), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 5: x=-0.0000000000, y=0.0000000000, z=0.4974460937), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 6: x=-0.0000000000, y=0.0000000000, z=0.4964245312), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 7: x=-0.0000000000, y=0.0000000000, z=0.4952327083), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 8: x=-0.0000000000, y=0.0000000000, z=0.4938706250), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 9: x=-0.0000000000, y=0.0000000000, z=0.4923382813), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 10: x=-0.0000000000, y=0.0000000000, z=0.4906356771), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 11: x=-0.0000000000, y=0.0000000000, z=0.4887628125), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 12: x=-0.0000000000, y=0.0000000000, z=0.4867196875), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 13: x=-0.0000000000, y=0.0000000000, z=0.4845063021), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 14: x=-0.0000000000, y=0.0000000000, z=0.4821226563), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 15: x=-0.0000000000, y=0.0000000000, z=0.4795687500), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 16: x=-0.0000000000, y=0.0000000000, z=0.4768445833), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 17: x=-0.0000000000, y=0.0000000000, z=0.4739501562), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 18: x=-0.0000000000, y=0.0000000000, z=0.4708854687), roll=0.0000000000, pitch=-0.0000000000, yaw=-0.0000000000 19: x=0.0000001305, y=-0.0000077238, z=0.4708941546), roll=0.0000021144, pitch=0.0000002247, yaw=0.0000001176
In [ ]:
import numpy as np
from PIL import Image
from IPython.display import display
width = 320
height = 200
img_arr = p.getCameraImage(
width,
height,
viewMatrix=p.computeViewMatrixFromYawPitchRoll(
cameraTargetPosition=[0, 0, 0],
distance=4,
yaw=60,
pitch=-10,
roll=0,
upAxisIndex=2,
),
projectionMatrix=p.computeProjectionMatrixFOV(
fov=60,
aspect=width/height,
nearVal=0.01,
farVal=100,
),
shadow=True,
lightDirection=[1, 1, 1],
)
width, height, rgba, depth, mask = img_arr
print(f"rgba shape={rgba.shape}, dtype={rgba.dtype}")
display(Image.fromarray(rgba, 'RGBA'))
print(f"depth shape={depth.shape}, dtype={depth.dtype}, as values from 0.0 (near) to 1.0 (far)")
display(Image.fromarray((depth*255).astype('uint8')))
print(f"mask shape={mask.shape}, dtype={mask.dtype}, as unique values from 0 to N-1 entities, and -1 as None")
display(Image.fromarray(np.interp(mask, (-1, mask.max()), (0, 255)).astype('uint8')))image size: 320x200 rgba shape=(200, 320, 4) dtype=uint8
depth shape=(200, 320) dtype=float32 as values from 0.0 (near) to 1.0 (far)
mask shape=(200, 320) dtype=int32 as unique values from 0 to N-1 entities, and -1 as None