PyBullet wasteland notes


Robot development based on gazebo_ros is not very suitable for RL research, so I looked at several mainstream robot simulation platforms on the Internet. Generally speaking, everyone will do the first instance of RL on the gym. Later, I found out that opencv developed a robot simulation platform based on the bullet physics engine on the basis of gym: roboschool, but I am on their github homepage I found that roboschool has been discarded. I was disappointed, but a new link gave me hope. This is the PyBullet I am currently learning below.


Install

First of all, the biggest pitfall is the installation, which is also one of the advantages of PyBullet. Unlike openai_ros or other platforms where the demo cannot run when it is installed to autism, the installation of PyBullet is very simple:

python3 -m pip install pybullet

The platform I use is Ubuntu18, please install pybullet according to your own platform.

Then some demos inside PyBullet need to be downloaded additionally, that is, the baselines module of pybullet, which can be downloaded by the following command:

git clone https://github.com/openai/baselines.git
cd baselines
pip3 install -e .

Since some out-of-the-box RL algorithms inside pybullet are implemented by tensorflow, you need to ensure that tensorflow is installed before running the ready-made algorithms inside pybullet.


first demo

Let's build our first pybullet program, summoning an r2d2 droid from Star Wars:

#!/usr/bin/python3
import pybullet as p
import pybullet_data
from time import sleep

# 链接物理引擎
p.connect(p.GUI)

# 设置模型加载路径
datapath = pybullet_data.getDataPath()
p.setAdditionalSearchPath(datapath)

# 加载模型
robot_id = p.loadURDF("r2d2.urdf", [0, 0, 0.5])

# 在这停顿
sleep(1000)

out:

The mouse wheel can adjust the distance of observation, Ctrl+drag the mouse to adjust the observation position


View robot information

getNumJoints()There are mainly two functions for viewing robot information getJointInfo().

getNumJoints()It accepts two parameters, the first parameter is the robot ID, and the second parameter is optional. Generally, it does not need to be selected, because it is only used in multi-platform development. This function returns the number of joints for that ID robot.

getJointInfo()It accepts three parameters, of which the third parameter is the same as the second parameter of the above function. It is not necessary to fill in the relevant platform. The first two parameters are the ID of the robot to be viewed and the joint number of the robot to be viewed. The function returns a tuple whose length is 17. The specific meaning can be seen in the following code:

You can try to print information about the robot:

#!/usr/bin/python3
import pybullet as p
import pybullet_data
from time import sleep

# 链接物理引擎
p.connect(p.GUI)

# 设置模型加载路径
datapath = pybullet_data.getDataPath()
p.setAdditionalSearchPath(datapath)

# 加载模型
robot_id = p.loadURDF("r2d2.urdf", [0, 0, 0.5])

# 输出基本信息
joint_num = p.getNumJoints(robot_id)
print("r2d2的节点数量为:", joint_num)

print("r2d2的信息:")
for joint_index in range(joint_num):
    info_tuple = p.getJointInfo(robot_id, joint_index)
    print(f"关节序号:{
    
    info_tuple[0]}\n\
            关节名称:{
    
    info_tuple[1]}\n\
            关节类型:{
    
    info_tuple[2]}\n\
            机器人第一个位置的变量索引:{
    
    info_tuple[3]}\n\
            机器人第一个速度的变量索引:{
    
    info_tuple[4]}\n\
            保留参数:{
    
    info_tuple[5]}\n\
            关节的阻尼大小:{
    
    info_tuple[6]}\n\
            关节的摩擦系数:{
    
    info_tuple[7]}\n\
            slider和revolute(hinge)类型的位移最小值:{
    
    info_tuple[8]}\n\
            slider和revolute(hinge)类型的位移最大值:{
    
    info_tuple[9]}\n\
            关节驱动的最大值:{
    
    info_tuple[10]}\n\
            关节的最大速度:{
    
    info_tuple[11]}\n\
            节点名称:{
    
    info_tuple[12]}\n\
            局部框架中的关节轴系:{
    
    info_tuple[13]}\n\
            父节点frame的关节位置:{
    
    info_tuple[14]}\n\
            父节点frame的关节方向:{
    
    info_tuple[15]}\n\
            父节点的索引,若是基座返回-1{
    
    info_tuple[16]}\n\n")

# 在这停顿
sleep(1000)

In addition to the basic environment connection and termination information, the printed information is as follows:

r2d2的节点数量为: 15
r2d2的信息:
关节序号:0
            关节名称:b'base_to_right_leg'
            关节类型:4
            机器人第一个位置的变量索引:-1
            机器人第一个速度的变量索引:-1
            保留参数:0
            关节的阻尼大小:0.0
            关节的摩擦系数:0.0
            slider和revolute(hinge)类型的位移最小值:0.0
            slider和revolute(hinge)类型的位移最大值:-1.0
            关节驱动的最大值:0.0
            关节的最大速度:0.0
            节点名称:b'right_leg'
            局部框架中的关节轴系:(0.0, 0.0, 0.0)
            父节点frame的关节位置:(0.22, 0.0, 0.25)
            父节点frame的关节方向:(0.0, -0.7070904020014416, 0.0, 0.7071231599922604)
            父节点的索引,若是基座返回-1:-1


关节序号:1
            关节名称:b'right_base_joint'
            关节类型:4
            机器人第一个位置的变量索引:-1
            机器人第一个速度的变量索引:-1
            保留参数:0
            关节的阻尼大小:0.0
            关节的摩擦系数:0.0
            slider和revolute(hinge)类型的位移最小值:0.0
            slider和revolute(hinge)类型的位移最大值:-1.0
            关节驱动的最大值:0.0
            关节的最大速度:0.0
            节点名称:b'right_base'
            局部框架中的关节轴系:(0.0, 0.0, 0.0)
            父节点frame的关节位置:(0.2999999996780742, 0.0, -1.3898038463944216e-05)
            父节点frame的关节方向:(0.0, 0.7070904020014416, 0.0, 0.7071231599922604)
            父节点的索引,若是基座返回-1:0


关节序号:2
            关节名称:b'right_front_wheel_joint'
            关节类型:0
            机器人第一个位置的变量索引:7
            机器人第一个速度的变量索引:6
            保留参数:1
            关节的阻尼大小:0.0
            关节的摩擦系数:0.0
            slider和revolute(hinge)类型的位移最小值:0.0
            slider和revolute(hinge)类型的位移最大值:-1.0
            关节驱动的最大值:100.0
            关节的最大速度:100.0
            节点名称:b'right_front_wheel'
            局部框架中的关节轴系:(0.0, 0.0, 1.0)
            父节点frame的关节位置:(0.0, 0.133333333333, -0.085)
            父节点frame的关节方向:(0.0, -0.7070904020014416, 0.0, 0.7071231599922604)
            父节点的索引,若是基座返回-1:1


关节序号:3
            关节名称:b'right_back_wheel_joint'
            关节类型:0
            机器人第一个位置的变量索引:8
            机器人第一个速度的变量索引:7
            保留参数:1
            关节的阻尼大小:0.0
            关节的摩擦系数:0.0
            slider和revolute(hinge)类型的位移最小值:0.0
            slider和revolute(hinge)类型的位移最大值:-1.0
            关节驱动的最大值:100.0
            关节的最大速度:100.0
            节点名称:b'right_back_wheel'
            局部框架中的关节轴系:(0.0, 0.0, 1.0)
            父节点frame的关节位置:(0.0, -0.133333333333, -0.085)
            父节点frame的关节方向:(0.0, -0.7070904020014416, 0.0, 0.7071231599922604)
            父节点的索引,若是基座返回-1:1

......


My Zhihu Series Notes

Huh~, I have written a lot on Zhihu, let me post it here:

PyBullet Notes (1) Installation of pybullet and its dependencies, a preliminary study of pybullet

PyBullet Notes (2) Miscellaneous talk starting from hello world (engine connection, URDF model loading, viewing information)

PyBullet notes (3) createMultiBody creates model objects with collision box properties

PyBullet Notes (4) Robot Control, Camera Tracking and Status Recording

PyBullet notes (5) collision detection and simulated laser detection

PyBullet Notes (6) Correct eating posture of Debug module and GUI

PyBullet notes (seven) get keyboard events and mouse events

PyBullet notes (8) introduction and training of reinforcement learning environment

Guess you like

Origin blog.csdn.net/weixin_45576923/article/details/113140247