Warning
This page is located in archive. Go to the latest version of this course pages. Go the latest version of this page.

Fast and easy simulation with Pybullet

For those of you requiring a simulator for a semestral work I suggest using Pybullet . It's probably the simplest and richest engine to use for python and it is open source. It takes less than a minute to install.

It can be installed by making sure you have the appropriate tools:

apt-get update
apt-get install build-essential

And doing a pip installation

pip install pybullet

You can try a hello-world after installation to check if it's working:

import pybullet as p
import pybullet_data # Additional model search path 
import time
 
# Initialize the simulation
physicsClient = p.connect(p.GUI) #or p.DIRECT for non-graphical version
 
# This is so that python can find ready models that are installed (such as the plane and r2d2).
p.setAdditionalSearchPath(pybullet_data.getDataPath()) 
 
# You can set various physical parameters
p.setGravity(0,0,-9.81)
 
# Load the plane (floor)
planeId = p.loadURDF("plane.urdf")
 
# Starting position (x,y,z) and orientation (quaternion). Euler angles should be avoided whenever possible. 
cubeStartPos = [0,0,1]
cubeStartOrientation = p.getQuaternionFromEuler([0,0,0])
 
# Load the r2d2
boxId = p.loadURDF("r2d2.urdf",cubeStartPos, cubeStartOrientation)
 
# Loop the simulation
for i in range(1000000):
    # If you had any actions or external forces that you wanted to add, they would be added here
    # ...
 
    # This does a single simulation step of size "timestep" which can be defined earlier.
    p.stepSimulation()
 
    # If you want to see the simulation in roughly realtime then add an appropriate length pause
    time.sleep(0.004)
 
cubePos, cubeOrn = p.getBasePositionAndOrientation(boxId)
print(cubePos,cubeOrn)
p.disconnect()

Quickstart guide (documentation):

A quick tutorial on how to set up a KUKA robot arm.

Several ready robotics examples that can be used:


Note:

Robot models in the simulators are loaded through description files which can be of the format .urdf, .xml, .mjcf, .sdf (and maybe several less popular ones). Pybullet knows how to load all of them although it might not be able to deal with some specifics of mujoco description files (.mjcf) so the model will be incomplete.


Tips:

Such a simulation engine can do many things “for free” other than the actual physical simulation. Some of these include forward and inverse kinematics, forward and inverse dynamics, ray intersection calculation, etc.


courses/b3b33vir/resources/pybullet_simulation/start.txt · Last modified: 2020/09/26 20:08 by azayetey