Skip to content

Commit

Permalink
Enable metrics from sdf.
Browse files Browse the repository at this point in the history
Signed-off-by: Franco Cipollone <[email protected]>
  • Loading branch information
francocipollone committed Aug 19, 2021
1 parent 3b81fdb commit 6aef657
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/ignition/sensors/Sensor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,14 @@ namespace ignition
/// \return True if a valid topic was set.
public: bool SetTopic(const std::string &_topic);

/// \brief Get flag state for enabling performance metrics publication.
/// \return True if performance metrics are enabled, false otherwise.
public: bool EnableMetrics() const;

/// \brief Set flag to enable publishing performance metrics
/// \param[in] _enableMetrics True to enable.
public: void SetEnableMetrics(bool _enableMetrics);

/// \brief Get parent link of the sensor.
/// \return Parent link of sensor.
public: std::string Parent() const;
Expand Down
23 changes: 22 additions & 1 deletion src/Sensor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class ignition::sensors::SensorPrivate
/// \brief Pose of the sensor
public: ignition::math::Pose3d pose;

/// \brief Flag to enable publishing performance metrics.
public: bool enableMetrics{false};

/// \brief How many times the sensor will generate data per second
public: double updateRate = 0.0;

Expand Down Expand Up @@ -127,6 +130,8 @@ bool SensorPrivate::PopulateFromSDF(const sdf::Sensor &_sdf)
}

this->updateRate = _sdf.UpdateRate();

this->enableMetrics = _sdf.EnableMetrics();
return true;
}

Expand Down Expand Up @@ -213,6 +218,19 @@ bool SensorPrivate::SetTopic(const std::string &_topic)
return true;
}

//////////////////////////////////////////////////
bool Sensor::EnableMetrics() const
{
return this->dataPtr->enableMetrics;
}


//////////////////////////////////////////////////
void Sensor::SetEnableMetrics(bool _enableMetrics)
{
this->dataPtr->enableMetrics = _enableMetrics;
}

//////////////////////////////////////////////////
void Sensor::PublishMetrics(const std::chrono::duration<double> &_now)
{
Expand Down Expand Up @@ -334,7 +352,10 @@ bool Sensor::Update(const ignition::common::Time &_now,
result = this->Update(_now);

// Publish metrics
this->PublishMetrics(std::chrono::duration<double>(_now.Double()));
if (this->EnableMetrics())
{
this->PublishMetrics(std::chrono::duration<double>(_now.Double()));
}

if (!_force && this->dataPtr->updateRate > 0.0)
{
Expand Down
4 changes: 4 additions & 0 deletions src/Sensor_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ class SensorUpdate : public ::testing::Test
protected: const std::string kSensorName{"sensor_test"};
// Sensor topic.
protected: const std::string kSensorTopic{"/sensor_test"};
// Enable metrics flag.
protected: const bool kEnableMetrics{true};
// Topic where performance metrics are published.
protected: const std::string kPerformanceMetricTopic{
kSensorTopic + "/performance_metrics"};
Expand All @@ -180,9 +182,11 @@ TEST_F(SensorUpdate, Update)
sdf::Sensor sdfSensor;
sdfSensor.SetName(kSensorName);
sdfSensor.SetTopic(kSensorTopic);
sdfSensor.SetEnableMetrics(kEnableMetrics);
std::unique_ptr<Sensor> sensor = std::make_unique<TestSensor>();
sensor->Load(sdfSensor);
ASSERT_EQ(kSensorTopic, sensor->Topic());
ASSERT_EQ(kEnableMetrics, sensor->EnableMetrics());

// Call Update() method kNumberOfMessages times.
// For each call there is also a call to Sensor::PublishMetrics() that
Expand Down

0 comments on commit 6aef657

Please sign in to comment.