< >
Home » ROS与Matlab入门教程 » ROS与Matlab语言入门教程-从Gazebo读取模型和仿真性能

ROS与Matlab语言入门教程-从Gazebo读取模型和仿真性能

本例程展示如何与MATLAB中的Gazebo仿真器进行交互,展示如何暂停Gazebo仿真、读取物理特性和获取Gazebo环境中的对象信息。

预备知识“4.2开始使用Gazebo和仿真TurtleBot”。

连接到Gazebo环境

从虚拟机的桌面或者Linux设备打开Gazebo世界,如果用户使用“4.2开始使用Gazebo和仿真TurtleBot”中的Ubuntu虚拟机,使用“Gazebo Empty”或“Gazebo Playground”。

在宿主计算机上的MATLAB实例运行如下命令,使用虚拟机的IP地址替换命令中的IP地址(192.168.1.1)。该指令初始化ROS并连接用户MATLAB实例到指定IP地址的ROS主控节点。

ipaddress = '192.168.1.1' % Replace this with the IP address of your virtual machine
rosinit(ipaddress)

注意,如果用户正在使用的网络不是你的默认网络,那么你可以手动设定IP地址,如下所示的语法。用仿真的IP地址代替“IP_OF_VM”,宿主的IP地址代替“IP_OF_HOST_COMPUTER”。

rosinit('IP_OF_VM','NodeHost','IP_OF_HOST_COMPUTER');

通过输入下面的指令,显示所有可用的ROS话题:

rostopic list

如果未能显示话题列表,那么说明网络没有正确设置。关于网络设置步骤,参考“4.2开始使用Gazebo和仿真TurtleBot”。

获得的列表应类似于:

>> rosinit('192.168.147.128')
Initializing global node /matlab_global_node_02311 with NodeURI 
http://192.168.147.1:4651/

>> rostopic list
/camera/depth/camera_info
/camera/depth/image_raw
/camera/depth/points
/camera/parameter_descriptions
/camera/parameter_updates
/camera/rgb/camera_info
/camera/rgb/image_raw
/camera/rgb/image_raw/compressed
/camera/rgb/image_raw/compressed/parameter_descriptions
/camera/rgb/image_raw/compressed/parameter_updates
/clock
/cmd_vel_mux/active
/cmd_vel_mux/input/navi
/cmd_vel_mux/input/safety_controller
/cmd_vel_mux/input/teleop
/cmd_vel_mux/parameter_descriptions
/cmd_vel_mux/parameter_updates
/depthimage_to_laserscan/parameter_descriptions
/depthimage_to_laserscan/parameter_updates
/gazebo/link_states
/gazebo/model_states
/gazebo/parameter_descriptions
/gazebo/parameter_updates
/gazebo/set_link_state
/gazebo/set_model_state
/joint_states
/laserscan_nodelet_manager/bond
/mobile_base/commands/motor_power
/mobile_base/commands/reset_odometry
/mobile_base/commands/velocity
/mobile_base/events/bumper
/mobile_base/events/cliff
/mobile_base/sensors/bumper_pointcloud
/mobile_base/sensors/core
/mobile_base/sensors/imu_data
/mobile_base_nodelet_manager/bond
/odom
/rosout
/rosout_agg
/scan
/tf

通过“ExampleHelperGazeboCommunicator”类建立与Gazebo世界的连接,这将令与Gazebo的相互作用更简单。

gazebo = ExampleHelperGazeboCommunicator();

读取Gazebo引擎的物理特性

使用“ExampleHelperGazeboCommunicator”对象提取仿真的物理特性。

phys = readPhysics(gazebo)

输出与下面类似:

phys =
Gravity: [0 0 -9.8000]
UpdateRate: 100
TimeStep: 0.0100
SimulationStatus: 0
DisableBodies: 0
PreconditioningIterations: 0
InnerIterations: 50
Relaxation: 1.3000
ErrorToleranceRMS: 0
ContactWidth: 1.0000e-03
MaxCorrectingVelocity: 100
ConstantForceMixing: 0
ErrorReductionParameter: 0.2000
MaxContacts: 20

Gazebo使用SI单位。

为了探索,更改仿真的重力,使得与z轴方向相反。此外,减少数值。重置仿真的物理特性以作用于所有对象。

phys.Gravity = [0 0 0.01]; % Units are m/s^2
setPhysics(gazebo,phys);
resetSim(gazebo);

暂停、继续和重启仿真

用户可观察到TurtleBot(以及世界中的一切)缓慢加速上升离开地面。使用“theExampleHelperGazeboCommunicator”暂停仿真。

pauseSim(gazebo);

当用户暂停的时候,场景看起来类似下图。

请输入图片描述

恢复仿真,观察所有物体又开始向上移动。

resumeSim(gazebo);

重置仿真。

resetSim(gazebo);

TurtleBot又继续上浮。

“resetSim”函数并没有重置物理特性到原始值,使用“setPhysics”手动重置物理特性。

四分之一速度运行仿真

本小节描述如何以慢速度运行Gazebo仿真器,使用慢速度在仔细检查突发或短暂的物理交互或者行为是非常有益的。通过更改物理过程的更新速度使得仿真运行在减缓的速度,用户可以看到运行在1/4速度的情况。

pauseSim(gazebo);
phys = readPhysics(gazebo);
phys.UpdateRate = phys.UpdateRate/4;
setPhysics(gazebo,phys);

设置重力位正常数值。

phys.Gravity = [0 0 -9.8]; % Set gravity back to normal value (m/s^2)
setPhysics(gazebo,phys);
resumeSim(gazebo);

当用户恢复仿真,TurtleBot回落到地上,但以慢动作。

转换更新速率到正常值,使得仿真运行在真实的时间。

phys.UpdateRate = phys.UpdateRate*4; % Set update rate back to normal value
setPhysics(gazebo,phys);

获取Gazebo世界的模型列表

“ExampleHelperGazeboCommunicator”类允许用户发现世界中的所有模型:

models = getSpawnedModels(gazebo)

模型列表输出看起来类似如下:

models =
'ground_plane'
'mobile_base'

从模型获取链接

使用“ExampleHelperGazeboSpawnedModel”类为模型创建对象,获取有关模型(此处,Kobuki TurtleBot)的指定信息。

kobuki = ExampleHelperGazeboSpawnedModel('mobile_base',gazebo)
[kobukiLinks, kobukiJoints] = getComponents(kobuki)

该指令的输出类似如下:

kobuki =
ExampleHelperGazeboSpawnedModel with properties:
Name: 'mobile_base'
Links: {3x1 cell}
Joints: {2x1 cell}
kobukiLinks =
'base_footprint'
'wheel_left_link'
'wheel_right_link'
kobukiJoints =
'wheel_left_joint'
'wheel_right_joint'

读取模型的位姿和方向属性

对TurtleBot使用“ExampleHelperGazeboSpawnedModel”对象,通过使用“getState”函数获得位置、方向和速度。

[position, orientation, velocity] = getState(kobuki)

状态指令的输出类似如下:

position =
0.0011 0.0095 -0.0011
orientation =
-0.6015 -0.4581 0.0028
velocity =
Linear: [1.0093e-05 3.8059e-05 -9.3143e-06]
Angular: [-3.7350e-04 -2.4515e-04 -4.5738e-05]

从Gazebo在线模型数据库获取模型列表

使用“exampleHelperGazeboListModels”函数显示所有可用的Gazebo模型,以从Gazebo网站下载。

builtInModels = exampleHelperGazeboListModels()

下面是用户见到的项目的一部分列表,即使用户的虚拟机没有连接到互联网,如果宿主计算机连接互联网那么还是可以看到该列表。注意,在这种情况下,用户不能产生这些模型。

builtInModels =
'ball_bearing'
'beer'
'bookshelf'
'bowl'
'breakable_test'
'brick_box_3x1x3'
'cabinet'
'cafe'
'cafe_table'
'camera'
'cart_front_steer'
'cart_rigid_suspension'
'cart_soft_suspension'
'cessna'
'cinder_block'
'cinder_block_2'
'cinder_block_wide'
'coke_can'
'construction_barrel'
'construction_cone'
'cordless_drill'
'create'
'cricket_ball'
'cube_20k'
'door_handle'
'double_pendulum_with_base'
'drc_practice_2x4'
'drc_practice_2x6'
'drc_practice_4x4x20'
'drc_practice_4x4x40'

关闭

当用户完成工作后,清楚工作空间中的发布器、订阅器和其它有关ROS的对象是个良好的习惯。

clear

建议当完成ROS网络有关的工作之后,使用“rosshutdown”指令关闭全局节点和断开与Gazebo的连接。

rosshutdown

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ros与matlab语言入门教程