Skip to content

Commit

Permalink
Apply initial sim time also after a reset.
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Pecka <[email protected]>
  • Loading branch information
peci1 committed Aug 22, 2023
1 parent af7d327 commit 97df15c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 3 deletions.
6 changes: 4 additions & 2 deletions gazebo/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,10 @@ bool Server::ParseArgs(int _argc, char **_argv)
{
try
{
physics::get_world()->SetSimTime(
common::Time(this->dataPtr->vm["initial_sim_time"].as<double>()));
common::Time initialSimTime {
this->dataPtr->vm["initial_sim_time"].as<double>()};
physics::get_world()->SetSimTime(initialSimTime);
physics::get_world()->SetInitialSimTime(initialSimTime);
gzmsg << "Setting initial sim time to [" <<
physics::get_world()->SimTime() << "]\n" << std::endl;
}
Expand Down
8 changes: 7 additions & 1 deletion gazebo/physics/World.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@ Light_V World::Lights() const
//////////////////////////////////////////////////
void World::ResetTime()
{
this->dataPtr->simTime = common::Time(0);
this->dataPtr->simTime = common::Time(this->dataPtr->initialSimTime);
this->dataPtr->pauseTime = common::Time(0);
this->dataPtr->startTime = common::Time::GetWallTime();
this->dataPtr->realTimeOffset = common::Time(0);
Expand Down Expand Up @@ -1528,6 +1528,12 @@ void World::SetSimTime(const common::Time &_t)
this->dataPtr->simTime = _t;
}

//////////////////////////////////////////////////
void World::SetInitialSimTime(const common::Time &_t)
{
this->dataPtr->initialSimTime = _t;
}

//////////////////////////////////////////////////
gazebo::common::Time World::PauseTime() const
{
Expand Down
4 changes: 4 additions & 0 deletions gazebo/physics/World.hh
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ namespace gazebo
/// \return The real time.
public: common::Time RealTime() const;

/// \brief Set the initial sim time.
/// \param[in] _t The new simulation time
public: void SetInitialSimTime(const common::Time &_t);

/// \brief Returns the state of the simulation true if paused.
/// \return True if paused.
public: bool IsPaused() const;
Expand Down
3 changes: 3 additions & 0 deletions gazebo/physics/WorldPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ namespace gazebo
/// \brief Clock time when simulation was started.
public: common::Time startTime;

/// \brief Initial simulation time.
public: common::Time initialSimTime;

/// \brief True if simulation is paused.
public: bool pause;

Expand Down
10 changes: 10 additions & 0 deletions test/integration/world_with_initial_sim_time_from_cli.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,18 @@ TEST_F(WorldWithInitialSimTimeFromCliTest, CheckInitialSimTime)

// check that the simulation time is the same as the initial sim time
EXPECT_DOUBLE_EQ(this->world->SimTime().Double(), initialSimTime);

// check that after a step, the simulation time advances
this->world->Step(2);
EXPECT_GT(this->world->SimTime().Double() - initialSimTime, 1e-4);

// check that the simulation time is again the same as the initial sim time
// after a reset
this->world->Reset();
EXPECT_DOUBLE_EQ(this->world->SimTime().Double(), initialSimTime);
}


/////////////////////////////////////////////////
int main(int argc, char **argv)
{
Expand Down

0 comments on commit 97df15c

Please sign in to comment.