The first lesson of pybullet learning
Installation of pybullet
First install pybullet, it is very simple and just pip. It’s better to use it on an Ubuntu computer that can render. Since I haven’t encountered any installation problems, I won’t go into details here.
After installing pybullet, you can render by running some of the demos it provides.
If run directly:
python -m pybullet_envs.examples.loadpanda
You can see a robotic arm performing manipulation:
After the installation is complete, you can see what pybullet provides:
These four types of files contain a variety of ready-made models for us to load and use, so let’s first familiarize ourselves with what is in these files:
pybullet_data: This folder contains a series of physical models. Here are a few:
type | Loading method |
---|---|
bike | bicycle/bike.urdf |
bipedal man | bipid/biped2d_pybullet.urdf |
flat | plane.urdf |
Various planes | plane100.urdf, plane_implicit.urdf, plane_transparent.urdf |
racing | racecar/racecar.urdf |
cup | urdf/mug.urdf |
Cup 2 | urdf/mug.urdf |
Robotic arm | franka_panda/panda.urdf |
gripper | gripper/various below |
pybullet_envs: There are some ready-made environments that can be used for operations.
pybullet_examples: There are some very good exmpales that can be used to learn how to structure tasks and use reinforcement learning to simulate and train.
pybullet_robots: It contains 2 types of robotic arms (xarm, pandas) and 1 type of mechanical dog (laikago). There are corresponding py files and related task examples showing how to use these components.
**pybullet_utils:**There are also related code examples.
After opening the urdf file, its writing format is similar to ROS, Mujoco, etc. You can improve the model by modifying the internal parameters.
Basic operations of pybullet
First import some packages:
import pybullet as p
import time
import pybullet_data
Connect to the physics engine:
physicsCilent = p.connect(p.GUI)
The connect function accepts a parameter, representing the physics engine server that the user chooses to connect to. p.GUI
Indicates renderable, p.DIRECT
indicates non-renderable.
If you want to cancel the surrounding control panel when rendering, you can use the following command to remove it:
p.configureDebugVisualizer(p.COV_ENABLE_GUI, 0)
Set gravity:
p.setGravity(0, 0, -10)
Use the loadURDF function to load the model:
The specific information of the loadURDF function is as follows:
filename
used to refer to the path of the loaded file, basePosition
the location of the model, baseOrientation
used to describe the orientation of the model, globalScaling
used to control the scale of the model, etc.
To load a model we usually load this line of code to read an existing model in the library using a relative path:
p.setAdditionalSearchPath(pybullet_data.getDataPath())
Note: In addition to using loadURDF to load the urdf model, we can also load the sdf model through loadSDF and the mjcf model through loadMJCF. Among them, sdf is the database file and mjcf is the robot description file of the MuJoCo platform.
Use the stepSimulation function to simulate:
p.stepSimulation()
time.sleep(1/240)
But the rendering looks like the gravitational acceleration has become smaller, and it can be added time.sleep(1/240)
to make the rendering more comfortable.
At the same time, you can also use the following function for real-time rendering, directly synchronizing the rendering time of the physics engine with the RTC (real time clock):
p.setRealTimeSimulation(1)
A simple demo:
import pybullet as p
import pybullet_data as pd
import math
import time
import numpy as np
import pybullet_robots.panda.panda_sim as panda_sim
import pybullet_data
p.connect(p.GUI)
p.setGravity(0, 0, -10)
p.configureDebugVisualizer(p.COV_ENABLE_RENDERING, 0)
p.configureDebugVisualizer(p.COV_ENABLE_RENDERING, 1)
p.resetSimulation()
p.setAdditionalSearchPath(pybullet_data.getDataPath())
planeId = p.loadURDF("plane.urdf")
tableUid = p.loadURDF("table/table.urdf", basePosition=[0, 0.3, -0.45], globalScaling=1)
mugid = p.loadURDF("urdf/mug.urdf", basePosition=[1, 1, 1], globalScaling=1)
cubeStartPos = [0, 0, 0,5]
cubeStartOrientation = p.getQuaternionFromEuler([0, 1, 1])
pandaUid = p.loadURDF('franka_panda/panda.urdf', cubeStartPos, cubeStartOrientation)
while 1:
p.stepSimulation()
time.sleep(1 / 240)
p.getCameraImage(320, 240)
A useful website for viewing urdf files
https://mymodelrobot.appspot.com/5629499534213120
Reference link:
https://zhuanlan.zhihu.com/p/347618698