Gazebo | Ignition | Community
Ask Your Question
0

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">
      <gravity>true</gravity>
      <inertial>...</inertial>

      <sensor name="camera" type="camera">
        <pose>0 0 0 0 0 0</pose>
        <camera>
          <horizontal_fov>2.0</horizontal_fov>
          <image>
            <width>640</width>
            <height>480</height>
          </image>
          <clip>
            <near>1.0</near>
            <far>15000</far>
          </clip>
        </camera>
        <always_on>1</always_on>
        <update_rate>10</update_rate>
        <visualize>1</visualize>
      </sensor>

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

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

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"));
if(base){
    boost::dynamic_pointer_cast<gazebo::sensors::CameraSensor>(base)->SetPose(pose);
}

Interestingly base always ends up being a nullptr.

When I call

_model->GetLink()->GetChildCount()

I get the children count of 1.

If I call

_model->GetLink()->GetChild(0)->GetName()

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
1

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

chapulina gravatar image

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

Comments

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

Question Tools

1 follower

Stats

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

Seen: 642 times

Last updated: Feb 13 '17