Tutorials/1.9/ROS Motor and Sensor Plugins

Gazebo plugins give your URDF models greater functionality and can tie in ROS messages and service calls for sensor output and motor input. In this tutorial we explain both how to setup preexisting plugins and how to create your own custom plugins that can work with ROS.


 * 1) Prerequisites

Make sure you have the RRBot setup as described in the previous tutorial on URDFs.


 * 1) Adding Plugins

Plugins can be added to any of the main elements of a URDF - a  ,  , or   depending on what the scope and purpose of the plugin is. To accomplish adding a plugin to a particular element in your URDF, you must wrap your   tag within a   element.


 * 1) Adding a plugin to the element

The following is an example of a plugin for a   element in a URDF:

 ... plugin parameters ...

In the above example the plugin was added to the   element because, similar to other  </tt> elements and properties, if no reference="x"</tt> is specified it is assumes the reference is the entire  </tt>. In SDF terminology, it assumes the reference is the  </tt>.

 SDF Note: 

''Delving a little deeper in the conversion process, your URDF is converted to a SDF before being parsed by Gazebo. Any elements inside the  </tt> tags which are not in the element table described in the previous tutorial on URDFs are directly inserted into the  </tt> tag of the generated SDF. As an example, this feature can be used to introduce model specific plugins. The following is the converted SDF from the above URDF example:''

<model name="your_robot_model"> <plugin name="differential_drive_controller" filename="libdiffdrive_plugin.so"> ... plugin parameters ...

Refer to the SDF documentation for more information on how this feature can be used.


 * 1) Adding a plugin to the element

Similar to  </tt> elements for  </tt>, you can add a  </tt> element to a link by passing a reference="your_link_name"</tt> value.

<gazebo reference="your_link_name"> <plugin name="your_link_laser_controller" filename="libgazebo_ros_laser.so"> ... plugin parameters ...


 * 1) Adding a plugin to the element

This is accomplished in the same way as a  </tt> except the reference name is a joint name.


 * 1) Plugins available in gazebo_plugins

The following sections document all of the plugins available in the gazebo_plugins</tt>. We suggest you review them in order because more detail is covered in the first couple of plugins and you can learn some of the concepts from the various plugins' documentation.

The names of each section is derived from the plugin class name. For example, "Block Laser" is from the GazeboRosBlockLaser</tt> class and can be found in the file gazebo_plugins/src/gazebo_ros_block_laser.cpp</tt>.

If there are some sections blank, it means that this author got tired of documenting every plugin and you should fill in the area with your experience should you have knowledge and examples of how to use the particular plugin.


 * 1) Camera

Description: provides ROS interface for simulating cameras such as wge100_camera by publishing the CameraInfo and Image ROS messages as described in sensor_msgs.


 * 1) RRBot Example

In this section, we will review a simple RGB camera attached to the end of the RRBot pendulum arm. You can look inside rrbot.xacro</tt> to follow the explanation. The first elements of this block are an extra link and joint added to the URDF file that represents the camera. We are just using a simple red box to represent the camera, though typically you could use a mesh file for a better representation.

<joint name="camera_joint" type="fixed"> <origin xyz="${camera_link} 0 ${height3 - axel_offset*2}" rpy="0 0 0"/> <child link="camera_link"/>

<link name="camera_link"> <origin xyz="0 0 0" rpy="0 0 0"/> <box size="${camera_link} ${camera_link} ${camera_link}"/>

<origin xyz="0 0 0" rpy="0 0 0"/> <box size="${camera_link} ${camera_link} ${camera_link}"/>

<mass value="1e-5" /> <origin xyz="0 0 0" rpy="0 0 0"/> <inertia ixx="1e-6" ixy="0" ixz="0" iyy="1e-6" iyz="0" izz="1e-6" />

A Xacro property is also defined: <xacro:property name="camera_link" value="0.05" />

You should be able to launch the RRBot and see a red box attached to the end of the arm.

Next we will review the Gazebo plugin that gives us the camera functionality and publishes the image to a ROS message. In the RRBot we have been following the convention of putting Gazebo elements in the rrbot.gazebo</tt> file:

<gazebo reference="camera_link"> <sensor type="camera" name="camera1"> <update_rate>30.0</update_rate> <horizontal_fov>1.3962634</horizontal_fov> 800          800           R8G8B8 0.02          300           gaussian 0.0          0.007       <plugin name="camera_controller" filename="libgazebo_ros_camera.so"> <alwaysOn>true</alwaysOn> <updateRate>0.0</updateRate> <cameraName>rrbot/camera1</cameraName> <imageTopicName>image_raw</imageTopicName> <cameraInfoTopicName>camera_info</cameraInfoTopicName> <frameName>camera_link</frameName> <hackBaseline>0.07</hackBaseline> <distortionK1>0.0</distortionK1> <distortionK2>0.0</distortionK2> <distortionK3>0.0</distortionK3> <distortionT1>0.0</distortionT1> <distortionT2>0.0</distortionT2>

Let's discuss some of the properties of this plugin...

<gazebo reference="camera_link">

The link name "camera_link" must match the name of the link we added to the Xacro URDF.

<sensor type="camera" name="camera1">

The sensor name "camera1" must be unique from all other sensor names. The name is not used many places except for within Gazebo plugins you can access

<update_rate>30.0</update_rate>

Number of times per second a new camera image is taken within Gazebo. This is the maximum update rate the sensor will attempt during simulation but it could fall behind this target rate if the physics simulation runs faster than the sensor generation can keep up.

<horizontal_fov>1.3962634</horizontal_fov> 800          800           R8G8B8 0.02          300

Fill in these values to match the manufacturer's specs on your physical camera hardware. One thing to note is that the pixels are assumed to be square.

Additionally, the near and far clips are simulation-specific parameters that give an upper and lower bound to the distance in which the cameras can see objects in the simulation. This is specified in the camera's optometry frame.

<plugin name="camera_controller" filename="libgazebo_ros_camera.so">

This is where the actual gazebo_ros/gazebo_ros_camera.cpp</tt> file is linked to, as a shared object.

<cameraName>rrbot/camera1</cameraName> <imageTopicName>image_raw</imageTopicName> <cameraInfoTopicName>camera_info</cameraInfoTopicName>

Here we define the rostopic the camera will be publishing to, for both the image topic and the camera info topic. For RRBot, you should subscribe to:

/rrbot/camera1/image_raw /rrbot/camera1/camera_info

<frameName>camera_link</frameName>

The coordinate frame the image is published under in the tf tree.


 * 1) Running the RRBot Example

After you have saved both <tt>rrbot.xacro</tt> and <tt>rrbot.gazebo</tt>, you should be able to launch both Rviz and Gazebo in separate terminals:

roslaunch rrbot_gazebo rrbot_world.launch roslaunch rrbot_description rrbot_rviz.launch

In Rviz, add a Camera display and under Image Topic set it to <tt>/rrbot/camera1/image_raw</tt>.

You should see a camera view of your Gazebo environment. In the following two pictures, a soda can was added to the environment for better visuals.

The coke can added:



The corresponding camera view after the pendulum has fallen:




 * 1) Multicamera

Description: synchronizes multiple camera's shutters such that they publish their images together. Typically used for stereo cameras, uses a very similar interface as the plain <tt>Camera</tt> plugin

Note: currently only supports stereo cameras. See Github issue.


 * 1) Atlas Code Example

In this code example there is both a left and right camera:

<gazebo reference="left_camera_frame"> <sensor type="multicamera" name="stereo_camera"> <update_rate>30.0</update_rate> <horizontal_fov>1.3962634</horizontal_fov> 800          800           R8G8B8 0.02          300           gaussian 0.0          0.007         0 -0.07 0 0 0 0         <horizontal_fov>1.3962634</horizontal_fov> 800          800           R8G8B8 0.02          300           gaussian 0.0          0.007       <plugin name="stereo_camera_controller" filename="libgazebo_ros_multicamera.so"> <alwaysOn>true</alwaysOn> <updateRate>0.0</updateRate> <cameraName>multisense_sl/camera</cameraName> <imageTopicName>image_raw</imageTopicName> <cameraInfoTopicName>camera_info</cameraInfoTopicName> <frameName>left_camera_optical_frame</frameName> <hackBaseline>0.07</hackBaseline> <distortionK1>0.0</distortionK1> <distortionK2>0.0</distortionK2> <distortionK3>0.0</distortionK3> <distortionT1>0.0</distortionT1> <distortionT2>0.0</distortionT2>


 * 1) Depth Camera

Description: simulates a sensor like a Kinect, which is duplicated in the Kinect plugin. Will probably be merged in the future.


 * 1) Openni Kinect

Description: Simulates an Xbox-Kinect, publishes the same topics as the corresponding ROS drivers for the Xbox kinect as documented in the Fuerte documentation here.

<plugin name="${link_name}_controller" filename="libgazebo_ros_openni_kinect.so"> 0.2    <alwaysOn>true</alwaysOn> <updateRate>1.0</updateRate> <cameraName>${camera_name}_ir</cameraName> <imageTopicName>/${camera_name}/depth/image_raw</imageTopicName> <cameraInfoTopicName>/${camera_name}/depth/camera_info</cameraInfoTopicName> <depthImageTopicName>/${camera_name}/depth/image_raw</depthImageTopicName> <depthImageInfoTopicName>/${camera_name}/depth/camera_info</depthImageInfoTopicName> <pointCloudTopicName>/${camera_name}/depth/points</pointCloudTopicName> <frameName>${frame_name}</frameName> <pointCloudCutoff>0.5</pointCloudCutoff> <distortionK1>0.00000001</distortionK1> <distortionK2>0.00000001</distortionK2> <distortionK3>0.00000001</distortionK3> <distortionT1>0.00000001</distortionT1> <distortionT2>0.00000001</distortionT2> <CxPrime>0</CxPrime> <Cx>0</Cx> <Cy>0</Cy> <focalLength>0</focalLength> <hackBaseline>0</hackBaseline>


 * 1) GPU Laser

Description: simulates laser range sensor by broadcasting LaserScan message as described in sensor_msgs. See Hokuyo Laser Scanners Reference.


 * 1) RRBot Example

See the RRBot Example for adding a Camera to RRBot before reviewing this example. Similar to adding a camera, we will add a new link and joint to the Xacro URDF of the RRBot. This time, instead of using just a rectangle for the visual model, we'll use a mesh:

<joint name="hokuyo_joint" type="fixed"> <origin xyz="0 0 ${height3 - axel_offset/2}" rpy="0 0 0"/> <child link="hokuyo_link"/>

<link name="hokuyo_link"> <origin xyz="0 0 0" rpy="0 0 0"/> <box size="0.1 0.1 0.1"/>

<origin xyz="0 0 0" rpy="0 0 0"/> <mesh filename="package://rrbot_description/meshes/hokuyo.dae"/>

<mass value="1e-5" /> <origin xyz="0 0 0" rpy="0 0 0"/> <inertia ixx="1e-6" ixy="0" ixz="0" iyy="1e-6" iyz="0" izz="1e-6" />

Now we'll add the plugin information to <tt>rrbot.gazebo</tt>, again as we did for the camera example:

<gazebo reference="hokuyo_link"> <sensor type="gpu_ray" name="head_hokuyo_sensor"> 0 0 0 0 0 0      false <update_rate>40</update_rate> 720            1             <min_angle>-1.570796</min_angle> <max_angle>1.570796</max_angle> 0.10          30.0           0.01           gaussian 0.0          0.01       <plugin name="gazebo_ros_head_hokuyo_controller" filename="libgazebo_ros_gpu_laser.so"> <topicName>/rrbot/laser/scan</topicName> <frameName>hokuyo_link</frameName>

Most of the properties are self-explanatory, but we'll review some below:

false

When true, a semi-translucent laser ray is visualized within the scanning zone of the gpu laser. This can be an informative visualization, or an nuisance.

More documentation on the <tt> </tt> and <tt> </tt> elements can be found in the SDF Documentation.

<topicName>/rrbot/laser/scan</topicName> <frameName>hokuyo_link</frameName>

Set these to the ROS topic name you would like to publish the laser scans to, and the transform frame you would like TF to use.


 * 1) Running the RRBot Example

After you have saved both <tt>rrbot.xacro</tt> and <tt>rrbot.gazebo</tt>, you should be able to launch both Rviz and Gazebo in separate terminals:

roslaunch rrbot_gazebo rrbot.launch roslaunch rrbot_description rrbot_rviz.launch

In Rviz, add a LaserScan display and under Topic set it to <tt>/rrbot/camera1/image_raw</tt>.

You should see a faint laser scan line in your Gazebo environment. While the pendulum is swinging, you should also see the laser scan swing. If the scan is too faint, you can up the size of the laser scan in the properties of the LaserScan display in Rviz. A size of 1m is very easy to see. In the following two pictures, a house and construction barrel was added to the environment for better visuals.

View from Gazebo:



The corresponding laser view from Rviz:




 * 1) Laser

Description: the non-GPU version of <tt>GPU Laser</tt>, but essentially uses the same code. See GPU Laser for documentation.

To run with RRBot, open <tt>rrbot.gazebo</tt> and change the following two lines.

replace <sensor type="gpu_ray" name="head_hokuyo_sensor">

with <sensor type="ray" name="head_hokuyo_sensor">

and replace <plugin name="gazebo_ros_head_hokuyo_controller" filename="libgazebo_ros_gpu_laser.so">

with <plugin name="gazebo_ros_head_hokuyo_controller" filename="libgazebo_ros_laser.so">

save, then launch the same launch files as for GPU Laser.


 * 1) Block Laser

Description: provides grid style laser range scanner simulation (e.g. Velodyne).


 * 1) F3D (Force Feedback Ground Truth)

Description: broadcasts external forces on a body in simulation over WrenchStamped message as described in geometry_msgs.


 * 1) Force

Description: ROS interface for applying Wrench (geometry_msgs) on a body in simulation.


 * 1) IMU

Description: simulates imu_node


 * 1) Joint Pose Trajectory

Description: listens to a joint_trajectory_action and plays back the set of joint positions. Sets the set of joints to exact positions without regards to simulated physics and forces.


 * 1) P3D (3D Position Interface for Ground Truth)

Description: broadcasts the inertial pose of any body in simulation via Odometry message as described in nav_msgs via ROS topic.


 * 1) Projector

Description: projects a static texture from a source outwards, such as used with the PR2's original head camera sensor. See API documentation for more information.


 * 1) Prosilica Camera

Description: Simulates interfaces exposed by a ROS Prosilica Camera. Here's an example URDF Xacro macro.


 * 1) Bumper

Description: provides contact feedback via ContactsState message.

<plugin name="${name}_gazebo_ros_bumper_controller" filename="libgazebo_ros_bumper.so"> <alwaysOn>true</alwaysOn> <updateRate>${update_rate}</updateRate> <bumperTopicName>${name}_bumper</bumperTopicName> <frameName>world</frameName>


 * 1) Differential Drive

Description model plugin that provides a basic controller for differential drive robots in Gazebo. You need a well defined differential drive robot to use this plugin.

<plugin name="differential_drive_controller" filename="libgazebo_ros_diff_drive.so"> <alwaysOn>true</alwaysOn> <updateRate>${update_rate}</updateRate> <leftJoint>base_link_right_wheel_joint</leftJoint> <rightJoint>base_link_left_wheel_joint</rightJoint> <wheelSeparation>0.5380</wheelSeparation> <wheelDiameter>0.2410</wheelDiameter> 20    <commandTopic>cmd_vel</commandTopic> <odometryTopic>odom</odometryTopic> <odometryFrame>odom</odometryFrame> <robotBaseFrame>base_footprint</robotBaseFrame>


 * 1) Skid Steering Drive

Description model plugin that provides a basic controller for skid steering drive robots in Gazebo (Pioneer 3AT for instance).

<plugin name="skid_steer_drive_controller" filename="libgazebo_ros_skid_steer_drive.so"> <updateRate>100.0</updateRate> <robotNamespace>/</robotNamespace> <leftFrontJoint>front_left_wheel_joint</leftFrontJoint> <rightFrontJoint>front_right_wheel_joint</rightFrontJoint> <leftRearJoint>back_left_wheel_joint</leftRearJoint> <rightRearJoint>back_right_wheel_joint</rightRearJoint> <wheelSeparation>0.4</wheelSeparation> <wheelDiameter>0.215</wheelDiameter> <robotBaseFrame>base_link</robotBaseFrame> 20    <topicName>cmd_vel</topicName> <broadcastTF>false</broadcastTF>


 * 1) Video Plugin

Description visual plugin that displays a ROS image stream on an OGRE Texture inside gazebo. This plugin does not modify the texture of one of the existing link surfaces, but creates a new texture on top of it. The texture will be created on the XY plane, visible from the +Z side. The plugin requires a pixel size while constructing the texture, and will resize incoming ROS image messages to match if they are a different size.

<gazebo reference="display_screen_link"> <plugin name="display_video_controller" filename="libgazebo_ros_video.so"> <topicName>image</topicName> 120      160


 * 1) Planar Move Plugin

Description model plugin that allows arbitrary objects (for instance cubes, spheres and cylinders) to be moved along a horizontal plane using a geometry_msgs/Twist message. The plugin works by imparting a linear velocity (XY) and an angular velocity (Z) to the object every cycle.

Here is a full URDF example that demonstrates how to control a floating box inside gazebo using this plugin, using different visual and collision elements. Note: The object needs to have sufficient inertia to prevent undesirable motions - which can occur as a reaction to the supplied velocity. You can try increasing inertia until the object moves as desired. It is also good to have the center of mass close to the ground.

<robot name="test_model">

<link name="base_footprint"> <origin xyz="0 0 0" rpy="0 0 0" /> <box size="0.001 0.001 0.001" />

<joint name="base_link_joint" type="fixed"> <origin xyz="0.0 0 1.25" rpy="0 0 0" /> <parent link="base_footprint"/> <child link="base_link" />

<link name="base_link"> <origin xyz="0 0 -1.25" /> <inertia ixx="50.0" ixy="0.0" ixz="0.0" iyy="50.0" iyz="0.0" izz="50.0" /> <box size="0.5 0.5 1.0" /> <origin xyz="0 0 -1.0" /> <cylinder length="0.5" radius="0.25" />

<plugin name="object_controller" filename="libgazebo_ros_planar_move.so"> <commandTopic>cmd_vel</commandTopic> <odometryTopic>odom</odometryTopic> <odometryFrame>odom</odometryFrame> <odometryRate>20.0</odometryRate> <robotBaseFrame>base_footprint</robotBaseFrame>


 * 1) Template

Description: an example c++ plugin template for anyone who wants to write their own plugin.


 * 1) Next Steps

Next we will analyze the <tt>ros_control</tt> packages integrated with Gazebo for tight controller/actuator/simulator integration Actuators, controllers, and ros_control.