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

Set initial sim time from the command-line #3294

Merged
merged 7 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 19 additions & 0 deletions gazebo/Server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ bool Server::ParseArgs(int _argc, char **_argv)
"Absolute path in which to store state data")
("record_period", po::value<double>()->default_value(-1),
"Recording period (seconds).")
("initial_sim_time", po::value<double>(),
"Initial simulation time (seconds).")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the order of these arguments determines the formatting of the help string, so I would move this out of the middle of the record_* arguments to be next to iters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5cc4db3.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks; I pushed it one more line in 7f872b4

("record_filter", po::value<std::string>()->default_value(""),
"Recording filter (supports wildcard and regular expression).")
("record_resources", "Recording with model meshes and materials.")
Expand Down Expand Up @@ -406,6 +408,23 @@ bool Server::ParseArgs(int _argc, char **_argv)
}
}

if (this->dataPtr->vm.count("initial_sim_time"))
{
try
{
physics::get_world()->SetSimTime(
common::Time(this->dataPtr->vm["initial_sim_time"].as<double>()));
gzmsg << "Setting initial sim time to [" <<
physics::get_world()->SimTime() << "]\n" << std::endl;
}
catch(...)
{
gzerr << "Unable to cast initial_sim_time[" <<
this->dataPtr->vm["initial_sim_time"].as<double>() << "] "
<< "to double for setting initial sim time\n" << std::endl;
}
}

this->ProcessParams();

return true;
Expand Down
1 change: 1 addition & 0 deletions test/integration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ set(tests
world_entity_below_point.cc
world_playback.cc
world_population.cc
world_with_initial_sim_time_from_cli.cc
worlds_installed.cc
)

Expand Down
65 changes: 65 additions & 0 deletions test/integration/world_with_initial_sim_time_from_cli.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2023 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <string>
#include "gazebo/common/Time.hh"
#include "gazebo/test/ServerFixture.hh"

using namespace gazebo;

const double initialSimTime = 1675117690.123456;

/// \brief Helper class that initializes each test.
class WorldWithInitialSimTimeFromCliTest : public ServerFixture
{
/// \brief Class constructor.
public: WorldWithInitialSimTimeFromCliTest()
{
// Start the server with a log file and paused.
scpeters marked this conversation as resolved.
Show resolved Hide resolved
this->LoadArgs("-u --initial_sim_time " + std::to_string(initialSimTime));
this->world = physics::get_world("default");

// Prepare the transport.
this->node = transport::NodePtr(new transport::Node());
this->node->Init();

std::this_thread::sleep_for(std::chrono::milliseconds(300));
scpeters marked this conversation as resolved.
Show resolved Hide resolved
}

/// \brief Gazebo transport node.
public: transport::NodePtr node;

/// \brief Pointer to the world.
public: physics::WorldPtr world;
};

/////////////////////////////////////////////////
/// \brief Check that the initial simulation time is set correctly.
TEST_F(WorldWithInitialSimTimeFromCliTest, CheckInitialSimTime)
{
ASSERT_TRUE(this->world != NULL);
EXPECT_TRUE(this->world->IsPaused());

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

/////////////////////////////////////////////////
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}