主要内容

Perform Co-Simulation between Simulink and Gazebo

此示例显示了如何在Simulink™和Gazebo之间设置同步模拟以发送命令并从凉亭接收数据。金宝app

设置凉亭仿真环境

For this example, use your own Linux environment with Gazebo or download the providedVirtual Machine with ROS and Gazebo。在虚拟机(VM)中,所需的凉亭插件位于/home/user/src/GazeboPlugin.

For more information on the Linux VM and the requirements for setting up your own Linux environment with Gazebo, see凉亭模拟环境要求和局限性

如果使用自己的Linux环境,请按照手动安装凉亭插件。Othwerise,去启动凉亭仿真环境

手动安装凉亭插件

获取插件源代码作为ZIP软件包。此功能创建一个名为的文件夹GazeboPlugin在您当前的工作目录中,并将其压缩为凉亭

packageGazeboPlugin

Copy the凉亭to your Linux machine that meets the following requirement:

Unzip the package on your Linux platform, for this example we unpack to/home/user/src/GazeboPlugin

在终端com运行这些命令pile the plugin.

CD/Home/user/src/gazeboplugin

If the建造文件夹已经存在,将其删除。

rm -r build

安装插件。

mkdir构建CD构建Cmake ..

插件位置是/home/user/src/gazeboplugin/export/lib/libgazebocosimplugin.so

Remove the generated plugin from host computer.

ifexist('GazeboPlugin','dir')rmdir('GazeboPlugin','s');endifexist('GazeboPlugin.zip','file') 删除('GazeboPlugin.zip');end

启动凉亭仿真环境

在VM或您自己的Linux操作系统中打开终端,运行以下命令以启动凉亭模拟器。

cd/home/user/src/gazeboplugin/offortexportSVGA_VGPU10=0凉亭。。/world/multiSensorPluginTest.world--verbose

These commands launch a Gazebo simulator with:

  • Two laser range finders:hokuyo0hokuyo1

  • Two RGB cameras:camera0camera1

  • Two depth cameras:depth_camera0depth_camera1

  • 两个IMU传感器:imu0imu1

  • Unit box model:unit_box

ThemultiSensorPluginTest.world位于/home/user/src/GazeboPlugin/world文件夹。这个世界文件包括使用以下行中的Simulink共同模拟的凉亭插件。金宝appxml身体:

"GazeboPlugin"文件名="lib/libGazeboCoSimPlugin.so">  14581  

The filename field must be pointing to the location of the compiled Gazebo plugin. This path can be relative to the location Gazebo itself is launched, or you could add it to the Gazebo plugin search path by running:

exportGAZEBO_PLUGIN_PATH = $ {GAZEBO_PLUGIN_PATH}:/HOME/user/user/src/src/gazeboplugin/export

Configure Gazebo Co-Simulation

Open theperformCoSimulationWithGazebomodel, which demonstrates how to receive sensor data from these simulated sensors and how to actuate the unit box model from Simulink.

open_system("performCoSimulationWithGazebo")

在模拟模型之前,请使用凉亭共模拟凉亭起搏器block:

hilite_system('performCoSimulationWithGazebo/Gazebo Pacer')

Open the block and click the配置凉亭网络和仿真设置关联。

open_system('performCoSimulationWithGazebo/Gazebo Pacer')

在里面Network Address下拉,选择风俗。Enter the IP address of your Linux machine. The defaultPortfor Gazebo is14581。放Response timeout到10秒。

点击Testbutton to test the connection to the running Gazebo simulator.

Get Sensor Data

采用the凉亭阅读block to obtain data on specific topics from three sensors:

  • IMU,/凉亭/默认/imu0/link/imu/imu

  • Lidar Scan,/gazebo/default/hokuyo0/link/laser/scan

  • RGB camera,/gazebo/default/camera0/link/camera/image

Display the IMU readings and visualize the Lidar Scan and RGB Image using MATLAB® function blocks.

Actuate Gazebo Model

采用the凉亭应用命令块以施加恒定力z-direction to the unit box that results in an acceleration of 1 m / s 2 。Create a blankapplylinkwrenchmessage using凉亭空白消息。指定消息的元素将力应用于unit_box/linkentity using theBus Assignment堵塞。采用凉亭阅读输出盒子的地面真相姿势。盒子在1第二周期内的位移应接近0.5米。

Perform Co-Simulation

To start co-simulation,clickRun。You can also step the simulation using向前一步退后is not supported during co-simulation.

在运行模拟时,请注意凉亭模拟器和Simulink时间已同步。金宝app

This model visualizes the Gazebo sensor data using MATLAB function block and MATLAB plotting functionalities. Here is a snapshot of the image data obtained from Gazebo camera:

这是激光扫描图像的快照:

单位箱块位置的时间情节z-direction can be viewed using数据检查员。The block tracks a parabolic shape due to the constant acceleration over time.

The position of the unit box at the end of simulation is 1.001, leading to a 0.5001 displacement, which is slightly different from the expected value of 0.5. This is due to the error of the Gazebo physics engine. Make the max step size in the Gazebo physics engine smaller to reduce this error.

Time Synchronization

在共模拟过程中,您可以随时使用Simulink和凉亭模拟器使用金宝app暂停

Note:Gazebo pauses one time step ahead of the simulation.

这是由于以下共拟合时间序列:

传感器数据和驱动命令在正确的时间步骤交换。执行选择先踩凉亭,然后使用Simulink。金宝app模拟执行仍在t+1,S金宝appimulink只是停留在上一步,直到您恢复模型。

Next Steps