Home | Tutorials | Wiki | Issues
Ask Your Question

How does getChild() and getChildCount() work?

asked 2017-02-13 07:21:22 -0500

lysgaard gravatar image

I am making a plugin for this model:

<?xml version="1.0"?> 
<sdf version="1.4">
  <model name="my_model">

    <link name="link">

      <sensor name="camera" type="camera">
        <pose>0 0 0 0 0 0</pose>

      <collision name="collision">...</collision>
      <visual name="visual">...</visual>

    <plugin name="myplugin" filename="libmyplugin.so"/>

In the plugin I want to manipulate the pose of the camera. The code I use to do this is:

gazebo::physics::BasePtr base = _model->GetLink()->GetChild(std::string("camera"));

Interestingly base always ends up being a nullptr.

When I call


I get the children count of 1.

If I call


I get "collision". So it seems like the only children to the <link> is the <collision>. This seems quite unlogical for me and I am stuck. I also tried adding another collision tag to the model SDF, GetChildCount then increases to 2, and both collisions can be listed. Is there something about the semantics of GetChild() that I do not understand? I thought I could manipulate the camera using it?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-02-13 11:55:15 -0500

updated 2017-02-13 11:56:52 -0500

The gazebo::physics library deals with physics entities, such as models, links and collisions. Therefore, when you get a child entity, you get one of these physics objects.

Sensors are handled by a separate library, gazebo::sensors. I can see how that can be confusing, but even though the camera sensor is a child of the link in SDF, it isn't a child for the physics library. The maximum the physics library can give you is the name and number of sensors, but no pointers. Take a look at these functions:

To get a pointer to the camera sensor, you'll have to use the sensor library like this for example:

auto camera = sensors::SensorManager::Instance()->GetSensor("my_model::link::camera");
edit flag offensive delete link more


Thanks for the good answer. Might I suggest that the `GetChild()` and `GetChildCount()` documentation is updated to include a note about it's limitations, and maybe even a link to other possibly wanted functions like `GetSensor()`?

lysgaard gravatar imagelysgaard ( 2017-02-14 03:13:27 -0500 )edit
chapulina gravatar imagechapulina ( 2017-02-14 09:41:32 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-02-13 07:21:22 -0500

Seen: 29 times

Last updated: Feb 13