当前位置:网站首页>pybullet机器人仿真环境搭建 5.机器人位姿可视化
pybullet机器人仿真环境搭建 5.机器人位姿可视化
2022-06-26 16:05:00 【RuiH.AI】
前言
本篇记录一下如何在pybullet中可视化机器人的位姿。
在仿真环境中画线
pybullet提供了在仿真环境中添加点线文本的api,比如addUserDebugLine, addUserDebugPoints
等,并返回这些点线的id,可用于后续的删除修改。
这里我写了一个画物体坐标系的函数,来可视化位姿,代码应该很容易懂:
def draw_pose_in_pybullet(*pose):
""" *Draw pose frame in pybullet* :param pose: np.ndarray, shape=[4, 4] or tuple of (position, orientation) """
if len(pose) == 1:
position = pose[0][:3, 3]
rotation = pose[0][:3, :3]
else:
position, orientation = pose
print(orientation)
rotation = np.array(p.getMatrixFromQuaternion(orientation)).reshape([3, 3])
print(rotation)
start_point = position
end_point_x = position + rotation[:, 0] * 2
end_point_y = position + rotation[:, 1] * 2
end_point_z = position + rotation[:, 2] * 2
p.addUserDebugLine(start_point, end_point_x, [1, 0, 0])
p.addUserDebugLine(start_point, end_point_y, [0, 1, 0])
p.addUserDebugLine(start_point, end_point_z, [0, 0, 1])
代码例程
把上面的函数与上一篇pybullet环境的博客结合,给出机器人当前的位姿:
import time
import numpy as np
import pybullet
import pybullet_data
def draw_pose_in_pybullet(*pose):
""" *Draw pose frame in pybullet* :param pose: np.ndarray, shape=[4, 4] or tuple of (position, orientation) """
if len(pose) == 1:
position = pose[0][:3, 3]
rotation = pose[0][:3, :3]
else:
position, orientation = pose
print(orientation)
rotation = np.array(pybullet.getMatrixFromQuaternion(orientation)).reshape([3, 3])
print(rotation)
start_point = position
end_point_x = position + rotation[:, 0] * 2
end_point_y = position + rotation[:, 1] * 2
end_point_z = position + rotation[:, 2] * 2
pybullet.addUserDebugLine(start_point, end_point_x, [1, 0, 0])
pybullet.addUserDebugLine(start_point, end_point_y, [0, 1, 0])
pybullet.addUserDebugLine(start_point, end_point_z, [0, 0, 1])
if __name__ == '__main__':
client = pybullet.connect(pybullet.GUI)
pybullet.setAdditionalSearchPath(pybullet_data.getDataPath())
pybullet.setPhysicsEngineParameter(numSolverIterations=10)
pybullet.configureDebugVisualizer(pybullet.COV_ENABLE_RENDERING, 0)
pybullet.configureDebugVisualizer(pybullet.COV_ENABLE_GUI, 0)
pybullet.configureDebugVisualizer(pybullet.COV_ENABLE_TINY_RENDERER, 0)
pybullet.setGravity(0, 0, -9.8)
# pybullet.setRealTimeSimulation(1)
shift = [0, 0, 0]
scale = [1, 1, 1]
visual_shape_id = pybullet.createVisualShape(
shapeType=pybullet.GEOM_MESH,
fileName="sphere_smooth.obj",
rgbaColor=[1, 1, 1, 1],
specularColor=[0.4, 0.4, 0],
visualFramePosition=[0, 0, 0],
meshScale=scale)
collision_shape_id = pybullet.createCollisionShape(
shapeType=pybullet.GEOM_MESH,
fileName="sphere_smooth.obj",
collisionFramePosition=[0, 0, 0],
meshScale=scale)
pybullet.createMultiBody(
baseMass=1,
baseCollisionShapeIndex=collision_shape_id,
baseVisualShapeIndex=visual_shape_id,
basePosition=[-2, -1, 1],
useMaximalCoordinates=True)
plane_id = pybullet.loadURDF("plane100.urdf", useMaximalCoordinates=True)
cube_ind = pybullet.loadURDF('cube.urdf', (3, 1, 1), pybullet.getQuaternionFromEuler([0, 0, 0]))
r_ind = pybullet.loadURDF('r2d2.urdf', (1, 1, 1), pybullet.getQuaternionFromEuler([0, 0, 1.57]))
# 创建结束,重新开启渲染
pybullet.configureDebugVisualizer(pybullet.COV_ENABLE_RENDERING, 1)
num_joints = pybullet.getNumJoints(r_ind)
# 获得各关节的信息
joint_infos = []
for i in range(num_joints):
joint_info = pybullet.getJointInfo(r_ind, i)
if joint_info[2] != pybullet.JOINT_FIXED:
if 'wheel' in str(joint_info[1]):
print(joint_info)
joint_infos.append(joint_info)
maxforce = 10
velocity = 31.4
while True:
pybullet.removeAllUserDebugItems() # 把之前的线删除,否则会一直在仿真环境中出现
for i in range(len(joint_infos)):
pybullet.setJointMotorControl2(bodyUniqueId=r_ind,
jointIndex=joint_infos[i][0],
controlMode=pybullet.VELOCITY_CONTROL,
targetVelocity=velocity,
force=maxforce)
position, orientation = pybullet.getBasePositionAndOrientation(r_ind)
draw_pose_in_pybullet(position, orientation)
pybullet.stepSimulation()
time.sleep(1./240)
可视化效果如下:
需要注意,画线操作和删除线操作都会严重影响pybullet引擎的运行速度,实际感觉一卡一卡的。
边栏推荐
- Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
- R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
- Redis 迁移(操作流程建议)
- How to implement interface current limiting?
- 6 自定义层
- Tsinghua's "magic potion" is published in nature: reversing stem cell differentiation, and the achievements of the Nobel Prize go further. Netizen: life can be created without sperm and eggs
- IAR engineering adapts gd32 chip
- # 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
- What is the difference between stm32f1 and gd32f1?
- Kept to implement redis autofailover (redisha) 1
猜你喜欢
JS教程之使用 ElectronJS、VueJS、SQLite 和 Sequelize ORM 从 A 到 Z 创建多对多 CRUD 应用程序
Lifeifei's team applied vit to the robot, increased the maximum speed of planning reasoning by 512 times, and also cued hekaiming's Mae
【力扣刷题】二分查找:4. 寻找两个正序数组的中位数
Oilfield exploration problems
(1) Keras handwritten numeral recognition and recognition of self written numbers
Ten thousand words! In depth analysis of the development trend of multi-party data collaborative application and privacy computing under the data security law
Anaconda3安装tensorflow 2.0版本cpu和gpu安装,Win10系统
What is the process of switching C # read / write files from user mode to kernel mode?
JS text scrolling scattered animation JS special effect
# 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
随机推荐
[time complexity and space complexity]
Redis migration (recommended operation process) 1
100+数据科学面试问题和答案总结 - 基础知识和数据分析
Failed to get convolution algorithm. This is probably because cuDNN failed to initialize
Comprehensive analysis of discord security issues
大话领域驱动设计——表示层及其他
R language plotly visualization: Violin graph, multi category variable violin graph, grouped violin graph, split grouped violin graph, two groups of data in each violin graph, each group accounts for
Development, deployment and online process of NFT project (1)
Net基于girdview控件实现删除与编辑行数据
R语言plotly可视化:小提琴图、多分类变量小提琴图、分组(grouped)小提琴图、分裂的分组小提琴图、每个小提琴图内部分为两组数据、每个分组占小提琴图的一半、自定义小提琴图的调色板、抖动数据点
人人都当科学家之免Gas体验mint爱死机
Arduino UNO + DS1302简单获取时间并串口打印
Redis的ACID
Quickly get started with federal learning -- the practice of Tencent's self-developed federal learning platform powerfl
Hyperf框架使用阿里云OSS上传失败
R language generalized linear model function GLM, GLM function to build logistic regression model, analyze whether the model is over discrete, and use the ratio of residual deviation and residual degr
Panoramic analysis of upstream, middle and downstream industrial chain of "dry goods" NFT
8 user defined evaluation function
振动式液量检测装置
Anaconda3 installation tensorflow version 2.0 CPU and GPU installation, win10 system