main content

perform co-凯发k8网页登录

this example shows how to set up a synchronized simulation between simulink™ and gazebo to send commands and receive data from gazebo.

setup gazebo simulation environment

for this example, use your own linux environment with gazebo or download the provided . in the virtual machine (vm), the required gazebo plugin is located in /home/user/src/gazeboplugin.

for more information on the linux vm and the requirements for setting up your own linux environment with gazebo, see .

in the vm settings disable accelerate 3d graphics. in vm settings, vm > settings > hardware > display, disable accelerate 3d graphics.

if using your own linux environment, follow the steps in . otherwise, go to .

install gazebo plugin manually

obtain the plugin source code as a zip package. this function creates a folder called gazeboplugin in your current working directory and compresses it as gazeboplugin.zip.

packagegazeboplugin

copy the gazeboplugin.zip 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.

run these commands in the terminal to compile the plugin.

cd /home/user/src/gazeboplugin

if the build folder already exists, remove it.

rm -r build

install the plugin.

mkdir build
cd build
cmake ..
make

the plugin location is /home/user/src/gazeboplugin/export/lib/libgazebocosimplugin.so.

remove the generated plugin from host computer.

if exist('gazeboplugin', 'dir')
    rmdir('gazeboplugin', 's');
end
if exist('gazeboplugin.zip', 'file')
    delete('gazeboplugin.zip');
end

launch gazebo simulation environment

open a terminal in the vm or your own linux operating system, run the following commands to launch the gazebo simulator.

cd /home/user/src/gazeboplugin/export
export svga_vgpu10=0
gazebo ../world/multisensorplugintest.world --verbose

these commands launch a gazebo simulator with:

  • two laser range finders: hokuyo0 and hokuyo1

  • two rgb cameras: camera0 and camera1

  • two depth cameras: depth_camera0 and depth_camera1

  • two imu sensors: imu0 and imu1

  • unit box model: unit_box

the multisensorplugintest.world is located in /home/user/src/gazeboplugin/world folder. this world file includes the gazebo plugin for co-simulation with simulink using the following lines in its .xml body:

"gazeboplugin" filename="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:

export gazebo_plugin_path=${gazebo_plugin_path}:/home/user/src/gazeboplugin/export

configure gazebo co-simulation

open the performcosimulationwithgazebo model, which demonstrates how to receive sensor data from these simulated sensors and how to actuate the unit box model from simulink.

open_system("performcosimulationwithgazebo")

before simulating the model, configure gazebo co-simulation using gazebo pacer block:

hilite_system('performcosimulationwithgazebo/gazebo pacer')

open the block and click the configure gazebo network and simulation settings link.

open_system('performcosimulationwithgazebo/gazebo pacer')

in the network address drop down, select custom. enter the ip address of your linux machine. the default port for gazebo is 14581. set response timeout to 10 seconds.

click the test button to test the connection to the running gazebo simulator.

get sensor data

use the gazebo read block to obtain data on specific topics from three sensors:

  • imu, /gazebo/default/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

use the gazebo apply command block to apply a constant force in the z-direction to the unit box that results in an acceleration of 1 m/s2. create a blank applylinkwrench message using gazebo blank message. specify elements of the message to apply the force to the unit_box/link entity using the bus assignment block. use gazebo read to output the ground truth pose of the box. the displacement of the box over a 1 second period should be close to 0.5 meters.

perform co-simulation

to start co-simulation,click run. you can also step the simulation using step forward. step back is not supported during co-simulation.

while the simulation is running, notice that gazebo simulator and simulink time are synchronized.

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:

here is a snapshot of the lidar scan image:

the time-plot of the position of the unit box block in z-direction can be viewed using data inspector. 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

during co-simulation, you can pause simulink and the gazebo simulator at any time using pause

note: gazebo pauses one time step ahead of the simulation.

this is due to the following co-simulation time sequence:

sensor data and actuation commands are exchanged at the correct time step. the execution chooses to step gazebo first, then simulink. the simulation execution is still on the t 1, simulink just stays on the previous step time until you resume the model.

next steps

网站地图