gazebosim / gz-sensors

Provides numerous sensor models designed to generate realistic data from simulation environments.
https://gazebosim.org
Apache License 2.0
63 stars 58 forks source link

Don't generate sensor data if no one is listening #147

Open chapulina opened 3 years ago

chapulina commented 3 years ago

Desired behavior

The most expensive action that rendering sensors take is generating data. So that should only be done when necessary, that is, when someone is listening to that sensor data.

It looks to me that the usual use case if to render the scene even if there are no listeners:

https://github.com/ignitionrobotics/ign-sensors/blob/3e486b404ff29b0a9360be9ce9308edc8cd576d9/src/RgbdCameraSensor.cc#L454-L458

However, Ignition Gazebo uses manual updates, so the scene is updated once for all sensors. Manual update is turned on, and rendering happens here. There's a check to see if it's time to update any sensors (according to their update rate), but I didn't see any checks for whether the sensor actually has subscribers. (did I miss anything?)

Implementation suggestion

My suggestion would be to:

  1. When not in manual mode, don't render sensors unless there are listeners
  2. For manual mode, add API that allows applications like Gazebo check if a sensor currently has listeners or not, and use that on the Sensors system to skip unnecessary renders

I'm focusing on rendering sensors, but this could be beneficial for other sensors as well.

Alternatives considered

Instead of offering an API so that Gazebo can handle this, ign-sensors could handle it internally?

iche033 commented 3 years ago

it may be ok to just skip rendering if there are no listeners regardless of which mode it is in. We did that for CameraSensor in https://github.com/ignitionrobotics/ign-sensors/pull/37 so we maybe able to do the same for other rendering sensors.