Ask Your Question

How to put a sensor/camera in a .model code? (For Beginner)

asked 2013-03-25 12:21:16 -0500

Marlon Rocha gravatar image

updated 2013-03-25 12:31:11 -0500

Hi again, I have another question today, I need to know how to put a sensor in my .model, to know the distance of something in front of it. I think I need to implement it in a node (using c++ in my case), but I'm lost and I searched a lot but no results.

Thanks :D

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2013-03-25 17:29:47 -0500

updated 2013-03-25 17:35:10 -0500

If this is for Fuerte then you will be using Gazebo 1.0. In your SDF file, add a sensor element which is a child of your link element. An example is below that includes two camera sensors in the link called base_link

<link name = "base_link" >
   <visual name="base_link_visual">
    <mesh filename="Base Assembly.stl" scale="1 1 1"/>
            <origin pose="0 0 0 0 0 0"/>
            <material script="Gazebo/Grey"/>
    <collision name="base_link_collision">
            <box size="1.528 0.491 .825"/>
        <origin pose="0 0 0 0 0 0"/>
    <inertial mass ="750.004">
        <inertia ixx="67.5" ixy="0.022" iyy="67" ixz="-0.363" iyz="-0.0352" izz="25"/>
        <origin pose=" 0.262 -0.005 0.053 0 0 0" />
    <sensor name='cam_sensor2' type='camera' always_on='1' update_rate='15'>
          <origin pose='-1.25 0.4 0.5 0 0.7 1.0'/>
            <horizontal_fov angle='1.57'/>
            <image width='320' height='240' format='R8G8B8'/>
            <clip near='0.5' far='5'/>
          <plugin name='CameraPublisher' filename=''/>
    <sensor name='cam_sensor1' type='camera' always_on='1' update_rate='15'>
          <origin pose='1.25 0.4 0.5 0 0.7 2.0'/>
            <horizontal_fov angle='1.57'/>
            <image width='320' height='240' format='R8G8B8'/>
            <clip near='0.5' far='5'/>
          <plugin name='CameraPublisher' filename=''/>

to position your cameras use the <origin> element of the sensor. this position is the difference compared to the link's origin.

The CameraPublisher is a plugin that I copied probably from the Gazebo website. You shouldn't need to change it as the configuring of the camera (size, framerate etc) occurs in the SDF file. The CameraPublisher plugin's code is here:


  #include "gazebo.hh"
    #include "plugins/CameraPlugin.hh"
    #include "common/common.h"
    #include "transport/transport.h"
    #include <stdio.h>
    #include "ros/ros.h"
    #include "sensor_msgs/Image.h"
    #include "image_transport/image_transport.h"

    namespace gazebo
      class CameraPublisher : public CameraPlugin
        public: CameraPublisher() : CameraPlugin(),saveCount(0) 

            std::string name = "CameraPublisher";
            int argc = 0;
            ros::init(argc, NULL, name);


        public: ~CameraPublisher()
            delete this->node;

        public: void Load(sensors::SensorPtr _parent, sdf::ElementPtr _sdf)

          // Don't forget to load the camera plugin
            //this->model = _parent;            

            //ROS Nodehandle

            //current_time = this->model->GetWorld()->GetSimTime();

            this->node = new ros::NodeHandle("~");

            //this->it = this->node;
            ros::NodeHandle node_temp = *this->node;
            image_transport::ImageTransport trans(node_temp);
            char topicname[12];
            char fullname[50];

            snprintf(fullname, 30, "%s", this->parentSensor->GetCamera()->GetName().c_str()); 

            strncpy(&topicname[0], &fullname[9], 11);
            topicname[11] ='\0';
            //ROS Publisher
            //this->pub = this->node->advertise<sensor_msgs::Image>(topicname, 1000);
            this->pub = trans.advertise(topicname, 1);


        // Update the controller
        public: void OnNewFrame(const unsigned char *_image, 
            unsigned int _width, unsigned int _height, unsigned int _depth, 
            const std::string &_format)

                sensor_msgs::Image msg;
                //msg.header.stamp = this->model->GetWorld()->GetSimTime();

                msg.is_bigendian = 0;

                if(this->pub.getNumSubscribers() >0)
                    //for (int i =0; i<(msg.height*msg ...
edit flag offensive delete link more


Thanks a lot, I'm studying this code right now, but I'm in trouble with the SDF... Like I need to create it, or the ROS have a default?

Marlon Rocha gravatar image Marlon Rocha  ( 2013-04-01 01:17:36 -0500 )edit

SDF is the description format for models in Gazebo. Its based on xml and is relatively equivalent to the URDF used in ROS, though the way SDF treats joint placement is different to URDF. Check tutorials for Gazebo on how to use SDF 1.0. NB it is different to later versions of SDF.

PeterMilani gravatar image PeterMilani  ( 2013-04-01 02:37:51 -0500 )edit

I am having trouble with the imagetransport.h, can't find here :(

Marlon Rocha gravatar image Marlon Rocha  ( 2013-04-01 16:07:13 -0500 )edit

Problem solved, now I can't create the .bin with the CameraPublisher.cpp to use with the gazebo

Marlon Rocha gravatar image Marlon Rocha  ( 2013-04-01 16:14:34 -0500 )edit

CameraPublisher should be contained in a library .so file called if you've updated your Cmakelists.txt file as shown

PeterMilani gravatar image PeterMilani  ( 2013-04-01 18:44:19 -0500 )edit

I found the, now, what I have to do? Thanks man :D

Marlon Rocha gravatar image Marlon Rocha  ( 2013-04-02 03:08:45 -0500 )edit

Follow this tutorial. Disregard the part about running the gazebo server from the build directory, start gazebo the way you've been doing

PeterMilani gravatar image PeterMilani  ( 2013-04-02 11:55:42 -0500 )edit

Hmmm ok, I need to know how to put my .model and the plugins in the same project using ROS... Can you help? thanks man

Marlon Rocha gravatar image Marlon Rocha  ( 2013-04-03 15:33:54 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-03-25 12:21:16 -0500

Seen: 1,193 times

Last updated: Mar 25 '13