Skip to content

Commit 1133ee0

Browse files
Michael CarrollAddisu Z. Taddese
andauthored
Add API to set next sensor update time (#196)
Signed-off-by: Michael Carroll <michael@openrobotics.org> Co-authored-by: Addisu Z. Taddese <addisu@openrobotics.org>
1 parent b1f78a5 commit 1133ee0

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

include/ignition/sensors/Sensor.hh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ namespace ignition
9999
/// \brief Return the next time the sensor will generate data
100100
public: std::chrono::steady_clock::duration NextDataUpdateTime() const;
101101

102+
/// \brief Manually set the next time the sensor will generate data
103+
/// Useful for accomodating jumps backwards in time as well
104+
/// as specifying updates for non-uniformly updating sensors
105+
/// \param[in] _time The next update time
106+
public: void SetNextDataUpdateTime(
107+
const std::chrono::steady_clock::duration &_time);
108+
102109
/// \brief Update the sensor.
103110
///
104111
/// This is called by the manager, and is responsible for determining

src/Sensor.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,14 @@ bool Sensor::Update(const std::chrono::steady_clock::duration &_now,
433433
// Update the time the plugin should be loaded
434434
auto delta = std::chrono::duration_cast< std::chrono::milliseconds>
435435
(std::chrono::duration< double >(1.0 / this->dataPtr->updateRate));
436+
436437
this->dataPtr->nextUpdateTime += delta;
438+
439+
// Catch up to "now", if necessary.
440+
while (this->dataPtr->nextUpdateTime <= _now)
441+
{
442+
this->dataPtr->nextUpdateTime += delta;
443+
}
437444
}
438445

439446
return result;
@@ -445,6 +452,13 @@ std::chrono::steady_clock::duration Sensor::NextDataUpdateTime() const
445452
return this->dataPtr->nextUpdateTime;
446453
}
447454

455+
//////////////////////////////////////////////////
456+
void Sensor::SetNextDataUpdateTime(
457+
const std::chrono::steady_clock::duration &_time)
458+
{
459+
this->dataPtr->nextUpdateTime = _time;
460+
}
461+
448462
/////////////////////////////////////////////////
449463
void Sensor::AddSequence(ignition::msgs::Header *_msg,
450464
const std::string &_seqKey)

src/Sensor_TEST.cc

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,70 @@ TEST(Sensor_TEST, SetRateZeroService)
342342
EXPECT_FLOAT_EQ(20.0, sensor.UpdateRate());
343343
}
344344

345+
//////////////////////////////////////////////////
346+
TEST_F(SensorUpdate, NextDataUpdateTime)
347+
{
348+
// Create sensor.
349+
sdf::Sensor sdfSensor;
350+
sdfSensor.SetName(kSensorName);
351+
sdfSensor.SetTopic(kSensorTopic);
352+
sdfSensor.SetUpdateRate(1);
353+
354+
std::unique_ptr<Sensor> sensor = std::make_unique<TestSensor>();
355+
sensor->Load(sdfSensor);
356+
357+
{
358+
std::chrono::steady_clock::duration now = std::chrono::seconds(0);
359+
std::chrono::steady_clock::duration next = std::chrono::seconds(1);
360+
EXPECT_TRUE(sensor->Update(now, false));
361+
EXPECT_EQ(next.count(), sensor->NextDataUpdateTime().count());
362+
}
363+
364+
{
365+
std::chrono::steady_clock::duration now = std::chrono::seconds(1);
366+
std::chrono::steady_clock::duration next = std::chrono::seconds(2);
367+
EXPECT_TRUE(sensor->Update(now, false));
368+
EXPECT_EQ(next.count(), sensor->NextDataUpdateTime().count());
369+
}
370+
371+
{
372+
// set the next data update time into the future, so we no longer
373+
// expect an update to happen
374+
std::chrono::steady_clock::duration now = std::chrono::seconds(2);
375+
std::chrono::steady_clock::duration next = std::chrono::seconds(5);
376+
sensor->SetNextDataUpdateTime(next);
377+
EXPECT_FALSE(sensor->Update(now, false));
378+
EXPECT_EQ(next.count(), sensor->NextDataUpdateTime().count());
379+
}
380+
381+
{
382+
// Force has no impact on the next update time
383+
std::chrono::steady_clock::duration now = std::chrono::seconds(3);
384+
std::chrono::steady_clock::duration next = std::chrono::seconds(5);
385+
EXPECT_TRUE(sensor->Update(now, true));
386+
EXPECT_EQ(next.count(), sensor->NextDataUpdateTime().count());
387+
}
388+
389+
{
390+
// When that time point is reached, the update happens
391+
std::chrono::steady_clock::duration now = std::chrono::seconds(5);
392+
std::chrono::steady_clock::duration next = std::chrono::seconds(6);
393+
EXPECT_TRUE(sensor->Update(now, false));
394+
EXPECT_EQ(next.count(), sensor->NextDataUpdateTime().count());
395+
}
396+
397+
{
398+
// Jump backwards in time
399+
std::chrono::steady_clock::duration now = std::chrono::seconds(5);
400+
std::chrono::steady_clock::duration next = std::chrono::seconds(1);
401+
sensor->SetNextDataUpdateTime(next);
402+
EXPECT_TRUE(sensor->Update(now, false));
403+
404+
// The next update should be the first dt past the current time
405+
std::chrono::steady_clock::duration newNext = std::chrono::seconds(6);
406+
EXPECT_EQ(newNext.count(), sensor->NextDataUpdateTime().count());
407+
}
408+
}
345409

346410
//////////////////////////////////////////////////
347411
int main(int argc, char **argv)

0 commit comments

Comments
 (0)