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

Apply initial sim time also after a reset. #3340

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions gazebo/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ bool Server::ParseArgs(int _argc, char **_argv)
("record_resources", "Recording with model meshes and materials.")
("seed", po::value<double>(), "Start with a given random number seed.")
("initial_sim_time", po::value<double>(),
"Initial simulation time (seconds).")
"Initial simulation time (seconds). This time is also used after reset.")
("iters", po::value<unsigned int>(), "Number of iterations to simulate.")
("minimal_comms", "Reduce the TCP/IP traffic output by gzserver")
("server-plugin,s", po::value<std::vector<std::string> >(),
Expand Down 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
Loading