Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update sensors with pending trigger immediately in Sensors system #2408

Merged
merged 1 commit into from
May 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/systems/sensors/Sensors.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#include <gz/common/Profiler.hh>
#include <gz/plugin/Register.hh>
Expand All @@ -41,6 +40,7 @@
#include <gz/sensors/RgbdCameraSensor.hh>
#include <gz/sensors/ThermalCameraSensor.hh>
#include <gz/sensors/SegmentationCameraSensor.hh>
#include <gz/sensors/Sensor.hh>
#include <gz/sensors/WideAngleCameraSensor.hh>
#include <gz/sensors/Manager.hh>

Expand Down Expand Up @@ -228,6 +228,9 @@
/// \brief Check if any of the sensors have connections
public: bool SensorsHaveConnections();

/// \brief Returns all sensors that have a pending trigger
public: std::unordered_set<sensors::SensorId> SensorsWithPendingTrigger();

/// \brief Use to optionally set the background color.
public: std::optional<math::Color> backgroundColor;

Expand Down Expand Up @@ -745,11 +748,15 @@
this->dataPtr->sensorsToUpdate, _info.simTime);
}

std::unordered_set<sensors::SensorId> sensorsWithPendingTriggers =
this->dataPtr->SensorsWithPendingTrigger();

// notify the render thread if updates are available
if (hasRenderConnections ||
this->dataPtr->nextUpdateTime <= _info.simTime ||
this->dataPtr->renderUtil.PendingSensors() > 0 ||
this->dataPtr->forceUpdate)
this->dataPtr->forceUpdate ||
!sensorsWithPendingTriggers.empty())
{
if (this->dataPtr->disableOnDrainedBattery)
this->dataPtr->UpdateBatteryState(_ecm);
Expand All @@ -769,6 +776,9 @@
std::unique_lock<std::mutex> lockSensors(this->dataPtr->sensorsMutex);
this->dataPtr->activeSensors =
std::move(this->dataPtr->sensorsToUpdate);
// Add all sensors that have pending triggers.
this->dataPtr->activeSensors.insert(sensorsWithPendingTriggers.begin(),
sensorsWithPendingTriggers.end());
}

this->dataPtr->nextUpdateTime = this->dataPtr->NextUpdateTime(
Expand Down Expand Up @@ -1075,6 +1085,27 @@
return false;
}

//////////////////////////////////////////////////
std::unordered_set<sensors::SensorId>
SensorsPrivate::SensorsWithPendingTrigger()
{
std::unordered_set<sensors::SensorId> sensorsWithPendingTrigger;
for (auto id : this->sensorIds)
{
sensors::Sensor *s = this->sensorManager.Sensor(id);
if (nullptr == s)
{
continue;

Check warning on line 1098 in src/systems/sensors/Sensors.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/sensors/Sensors.cc#L1098

Added line #L1098 was not covered by tests
}

if (s->HasPendingTrigger())
{
sensorsWithPendingTrigger.insert(id);

Check warning on line 1103 in src/systems/sensors/Sensors.cc

View check run for this annotation

Codecov / codecov/patch

src/systems/sensors/Sensors.cc#L1103

Added line #L1103 was not covered by tests
}
}
return sensorsWithPendingTrigger;
}

GZ_ADD_PLUGIN(Sensors, System,
Sensors::ISystemConfigure,
Sensors::ISystemReset,
Expand Down
Loading