pybullet学习笔记(一)——Quick start with an example

目录

1.资源

2.Quick start——Hello pybullet world(一个示例)

connect,disconnect

getConnectionInfo

connect using DIRECT, GUI

connect using Shared Memory

connect using UDP or TCP networking

disconnect

setGravity

loadURDF, loadSDF, loadMJCF

stepSimulation, setRealTimeSimulation

getBasePositionAndOrientation,resetBasePositionAndOrientation

Transforms: Position and Orientation


1.资源

PyBullet Quick start Guide.pdf

安装:pybullet安装教程

2.Quick start——Hello pybullet world(一个示例)

PyBullet是一个易于使用的Python模块,用于机器人,游戏,视觉效果和机器学习的物理模拟。 使用PyBullet,可以从URDF,SDF,MJCF和其他文件格式加载关节体。 PyBullet提供运动学、动力学仿真计算,碰撞检测和干涉查询。Bullet Physics SDK包括PyBullet机器人示例,例如Minitaur四足机器人的仿真,tensorflow推理的人形机器人奔跑仿真和KUKA机械臂抓取物体。

这是一个pybullet脚本,下面将逐步解析。

import pybullet as p
import time
import pybullet_data
physicsClient = p.connect(p.GUI)  # or p.DIRECT for non-graphical version
p.setAdditionalSearchPath(pybullet_data.getDataPath())  # optionally
p.setGravity(0,0,-10)
planeId = p.loadURDF("plane.urdf")
cubeStartPos = [0,0,1]
cubeStartOrientation = p.getQuaternionFromEuler([0,0,0])
boxId = p.loadURDF("r2d2.urdf",cubeStartPos, cubeStartOrientation)
for i in range (10000):
p.stepSimulation()
time.sleep(1./240.)
cubePos, cubeOrn = p.getBasePositionAndOrientation(boxId)
print(cubePos,cubeOrn)
p.disconnect()

connect,disconnect

导入PyBullet模块后,首先要做的是“连接”物理模拟器。PyBullet是围绕客户端 - 服务器驱动的API设计的,客户端发送命令,物理服务器返回状态。

PyBullet有一些内置的物理服务器:DIRECTGUI。GUI和DIRECT执行物理模拟和渲染的过程相同。

请注意,在DIRECT模式下,无法访问OpenGL和VR硬件功能,如“Virtual Reality”和“Debug GUI, Lines, Text, Parameters”章节中所述。 DIRECT模式允许使用内置软件渲染器通过“getCameraImage”API渲染图像。This can be useful for running simulations in the cloud on servers without GPU.

您可以提供自己的数据文件,也可以使用PyBullet附带的PyBullet_data包。 为此,import pybullet_data 并使用pybullet.setAdditionalSearchPath(pybullet_data.getDataPath())注册该目录

getConnectionInfo

给定一个physicsClientId将返回列表[isConnected,connectionMethod]

connect using DIRECT, GUI

DIRECT连接将命令直接发送到物理引擎,而不使用任何传输层和图形可视化窗口,并在执行命令后直接返回状态。

GUI连接将在与PyBullet相同的进程空间内创建具有3D OpenGL渲染的新图形用户界面(GUI)。 在Linux和Windows上,GUI在单独的线程中运行,而在OSX上,由于操作系统的限制,它在同一个线程中运行。在Mac OSX上,您可能会在OpenGL窗口中看到一个旋转轮,直到运行'stepSimulation'或其他PyBullet命令。

在PyBullet客户端和GUI物理模拟服务器之间的命令和状态消息使用普通的内存缓冲区(memory buffer)传递。也可以使用SHARED_MEMORY,UDP或TCP网络在同一台机器或远程机器上连接到不同进程中的物理服务器。

与几乎所有其他方法不同,由于向后兼容性,此方法不解析关键字参数。

连接输入参数:

requiredconnection modeinteger:
DIRECT,
GUI,
SHARED_MEMORY,
UDP, TCP

DIRECT模式创建一个新的物理引擎并直接与之通信。

GUI将创建一个带有图形GUI前端的物理引擎并与之通信。
SHARED_MEMORY将连接到同一台机器上的现有物理引擎进程,并通过共享内存与之通信。

TCP或UDP将通过TCP或UDP网络连接到现有物理服务器

optionalkeyint在SHARED_MEMORY模式下,可选的共享内存密钥。启动ExampleBrowser或SharedMemoryPhysics_ *时,可以使用可选的命令行 - shared_memory_key来设置密钥。 这允许在同一台机器上运行多个服务器。
optionalUdpNetworkAddress
(UDP and TCP)
stringIP address or host name, for example "127.0.0.1" or "localhost" or "mymachine.domain.com
optionalUdpNetworkPort
(UDP and TCP)
integerUDP port number. Default UDP port is 1234, default TCP port is 6667 (matching the defaults in the server)
optionaloptionsstring命令行选项传递到GUI服务器。 目前,只启用了--opengl2标志:默认情况下,Bullet使用OpenGL3,但某些环境(如虚拟机或远程桌面客户端)仅支持OpenGL2。 目前只能传递一个命令行参数。

For example:

pybullet.connect(pybullet.DIRECT)
pybullet.connect(pybullet.GUI, options="--opengl2")
pybullet.connect(pybullet.SHARED_MEMORY,1234)
pybullet.connect(pybullet.UDP,"192.168.0.1")
pybullet.connect(pybullet.UDP,"localhost", 1234)
pybullet.connect(pybullet.TCP,"localhost", 6667)

connect返回物理客户端ID,如果未连接则返回-1。 物理客户端Id是大多数其他PyBullet命令的可选参数。 如果您不提供它,它将假设物理客户端id = 0.您可以连接到多个不同的物理服务器,GUI除外。

connect using Shared Memory

有一些物理服务器允许共享内存连接:App_SharedMemoryPhysics,App_SharedMemoryPhysics_GUI和Bullet Example
Browser在Experimental / Physics Server下有一个允许共享内存连接的示例。 这将允许您在独立的程序中执行物理模拟和渲染。

还可以通过共享内存连接到App_SharedMemoryPhysics_VR,Virtual Reality应用程序支持头戴式显示器和6-dof跟踪控制器,如HTC Vive和带触摸控制器的Oculus Rift。 由于Valve OpenVR SDK仅在Windows下正常工作,App_SharedMemoryPhysics_VR只能在Windows下使用premake(最好)或cmake构建.

connect using UDP or TCP networking

For UDP networking, there is a App_PhysicsServerUDP that listens to a certain UDP port. It uses the open source ​enet​ library for reliable UDP networking. This allows you to execute the physics simulation and rendering on a separate machine. For TCP PyBullet uses the ​clsocket library. This can be useful when using SSH tunneling from a machine behind a firewall to a robot simulation. For example you can run a control stack or machine learning using PyBullet on Linux, while running the physics server on Windows in Virtual Reality using HTC Vive or Rift. One more UDP application is the App_PhysicsServerSharedMemoryBridgeUDP application that acts as a bridge to an existing physics server: you can connect over UDP to this bridge, and the bridge connects to a physics server using shared memory: the bridge passes messages between client and server. In a similar way there is a TCP version (replace UDP by TCP).
Note: at the moment, both client and server need to be either 32bit or 64bit builds!

disconnect

可以使用connect调用返回的物理客户端Id(if non-negative)断开与物理服务器的连接。 “DIRECT”或“GUI”物理服务器将关闭。 一个单独的(进程外)物理服务器将继续运行。 另请参阅'resetSimulation'以删除所有项目。

setGravity

默认情况下,没有启用重力。 setGravity允许您为所有对象设置默认的重力。

pybullet.setGravity(0, 0, -9.80665)

loadURDF, loadSDF, loadMJCF

loadURDF将向物理服务器发送命令,以从通用机器人描述文件(URDF)加载物理模型。 URDF文件是ROS用来描述机器人对象的,它由WillowGarage和开源机器人基金会(OSRF)创建。许多机器人都有公共的URDF文件,你可以在这里找到URDF教程。

重要说明:大多数关节默认启动电机,防止自由运动。这类似于具有非常高摩擦谐波驱动的机器人关节。您可以使用 pybullet.setJointMotorControl2 设置关节电机控制模式和目标设置。 有关更多信息,请参阅 setJointMotorControl2 API。

警告:默认情况下,PyBullet会缓存一些文件以加快加载速度。 您可以使用 setPhysicsEngineParameter(enableFileCaching = 0)禁用文件缓存。

loadURDF返回一个非负整数作为body的唯一 ID。 如果无法加载URDF文件,则返回负数,这不是一个有效的ID。

您还可以从其他文件格式加载对象,例如.bullet,.sdf和.mjcf。 这些文件格式支持多个对象,因此返回值是对象唯一ID的列表。 SDF格式在http://sdformat.org上有详细解释。 loadSDF命令仅提取与机器人模型和几何相关的SDF的一些基本部分,并忽略与摄像机,灯光等相关的许多元素。 loadMJCF命令导入在OpenAI Gym中使用的MuJoCo MJCF xml文件。 另请参阅loadURDF下与默认关节电机设置相关的重要说明,并确保使用setJointMotorControl2。

stepSimulation, setRealTimeSimulation

stepSimulation将在单个正向动力学模拟步骤中执行所有操作,例如碰撞检测,约束求解和积分。

setRealTimeSimulation根据real-time clock 自动让物理服务器运行正向动力学仿真。

默认情况下,物理服务器不会执行模拟,除非您明确发送'stepSimulation'命令。 通过使用setRealTimeSimulation命令可以让物理服务器根据real-time-clock(RTC)实时运行仿真。 如果启用实时仿真,则无需调用“stepSimulation”。
请注意,setRealTimeSimulation在DIRECT模式下无效:在DIRECT模式下,物理服务器和客户端发生在同一个线程中,并触发每个命令。 在GUI模式和VR模式以及TCP / UDP模式下,物理服务器与客户端(PyBullet)在不同的线程中运行,setRealTimeSimulation允许physicsserver线程向stepSimulation添加其他调用。

getBasePositionAndOrientation,resetBasePositionAndOrientation

getBasePositionAndOrientation返回笛卡尔世界坐标中body的base(或root link)的当前位置和姿态。 位置是[x,y,z],姿态是[x,y,z,w]格式的四元数,可以使用getEulerFromQuaternion将四元数转换为Euler角。getBasePositionAndOrientation输入参数是:

requiredobjectUniqueIdintobject unique id, as returned from loadURDF.
optionalphysicsClientIdintif you are connected to multiple servers, you can pickone.

可以使用resetBasePositionAndOrientation方法重新设置物体base的位姿。这最好在初始化的时候设置好,不要在仿真运行过程中修改。线速度和角速度默认设置为0,可以通过resetBaseVelocity方法修改。

Transforms: Position and Orientation

物体的位置可以用笛卡尔空间坐标[x,y,z]表示。 物体的姿态可以使用四元数[x,y,z,w]、欧拉角[yaw, pitch, roll]或3×3矩阵来表示。 PyBullet提供了一些辅助函数进行四元数、欧拉角和3x3矩阵的转换。 如

getQuaternionFromEuler ​ # The quaternion format is [x,y,z,w]

getEulerFromQuaternion # X,Y,Z欧拉角以弧度为单位,accumulating 3 rotations expressing the roll around the X, pitch around Y and yaw around the Z axis.

getMatrixFromQuaternion  # 返回3x3矩阵( a list of 9 floats)

此外,还有一些函数可以进行乘法和逆变换。

multiplyTransforms  # Multiply two transform, provided as [positionA], [quaternionA], [positionB], [quaternionB]

invertTransform  # 逆变换 required [position], [quaternion]

 

  • 7
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值