diff --git a/src/cmd/cmdsim.rb.in b/src/cmd/cmdsim.rb.in index d137647a17..8955bb8bf4 100755 --- a/src/cmd/cmdsim.rb.in +++ b/src/cmd/cmdsim.rb.in @@ -104,6 +104,9 @@ COMMANDS = { 'sim' => " --log-compress When recording, compress final log files. \n"\ " Only valid if recording is enabled. \n"\ "\n"\ + " --seed [arg] Pass a custom seed value to the random \n"\ + " number generator. \n"\ + "\n"\ " --playback [arg] Use logging system to play back states. \n"\ " Argument is path to recorded states. \n"\ "\n"\ @@ -224,7 +227,8 @@ class Cmd 'render_engine_gui' => '', 'render_engine_server' => '', 'wait_gui' => 1, - 'headless-rendering' => 0 + 'headless-rendering' => 0, + 'seed' => 0 } usage = COMMANDS[args[0]] @@ -317,6 +321,9 @@ class Cmd opts.on('--version') do options['version'] = '1' end + opts.on('--seed [arg]', Integer) do |i| + options['seed'] = i + end end # opt_parser do @@ -455,7 +462,7 @@ Please use [GZ_SIM_RESOURCE_PATH] instead." const char *, int, int, const char *, int, int, int, const char *, const char *, const char *, const char *, const char *, - const char *, int, int)' + const char *, int, int, int)' # Import the runGui function Importer.extern 'int runGui(const char *, const char *, int, const char *)' @@ -490,7 +497,8 @@ See https://github.com/gazebosim/gz-sim/issues/44 for more info." options['render_engine_server'], options['render_engine_gui'], options['file'], options['record-topics'].join(':'), options['wait_gui'], - options['headless-rendering']) + options['headless-rendering'], options['seed']) + end guiPid = Process.fork do @@ -527,8 +535,10 @@ See https://github.com/gazebosim/gz-sim/issues/44 for more info." options['playback'], options['physics_engine'], options['render_engine_server'], options['render_engine_gui'], options['file'], options['record-topics'].join(':'), - options['wait_gui'], options['headless-rendering']) - # Otherwise run the gui + options['wait_gui'], + options['headless-rendering'], options['seed']) + # Otherwise run the gui + else options['gui'] if plugin.end_with? ".dylib" puts "`gz sim` currently only works with the -s argument on macOS. diff --git a/src/gz.cc b/src/gz.cc index 7f5f045e02..6eb0bff2c5 100644 --- a/src/gz.cc +++ b/src/gz.cc @@ -136,7 +136,7 @@ extern "C" int runServer(const char *_sdfString, const char *_playback, const char *_physicsEngine, const char *_renderEngineServer, const char *_renderEngineGui, const char *_file, const char *_recordTopics, int _waitGui, - int _headless) + int _headless, int _seed) { std::string startingWorldPath{""}; gz::sim::ServerConfig serverConfig; @@ -396,6 +396,12 @@ extern "C" int runServer(const char *_sdfString, serverConfig.SetRenderEngineGui(_renderEngineGui); } + if (_seed != 0) + { + serverConfig.SetSeed(_seed); + gzmsg << "Setting seed value: " << _seed << "\n"; + } + // Create the Gazebo server gz::sim::Server server(serverConfig); diff --git a/src/gz.hh b/src/gz.hh index 2d958a7408..0bb1565974 100644 --- a/src/gz.hh +++ b/src/gz.hh @@ -57,6 +57,7 @@ extern "C" const char *worldInstallDir(); /// it receives a world path from GUI. /// null to record the default topics. /// \param[in] _headless True if server rendering should run headless +/// \param[in] _seed --seed value to be used for random number generator. /// \return 0 if successful, 1 if not. extern "C" int runServer(const char *_sdfString, int _iterations, int _run, float _hz, int _levels, @@ -65,7 +66,7 @@ extern "C" int runServer(const char *_sdfString, int _logCompress, const char *_playback, const char *_physicsEngine, const char *_renderEngineServer, const char *_renderEngineGui, const char *_file, - const char *_recordTopics, int _waitGui, int _headless); + const char *_recordTopics, int _waitGui, int _headless, int _seed); /// \brief External hook to run simulation GUI. /// \param[in] _guiConfig Path to Gazebo GUI configuration file. diff --git a/src/gz_TEST.cc b/src/gz_TEST.cc index 633b837ad8..87aad0b8c7 100644 --- a/src/gz_TEST.cc +++ b/src/gz_TEST.cc @@ -105,6 +105,17 @@ TEST(CmdLine, GZ_UTILS_TEST_DISABLED_ON_WIN32(CachedFuelWorld)) << output; } +///////////////////////////////////////////////// +TEST(CmdLine, GZ_UTILS_TEST_DISABLED_ON_WIN32(RandomSeedValue)) +{ + std::string cmd = kGzCommand + " -r -v 4 --seed 5 --iterations 5"; + std::cout << "Running command [" << cmd << "]" << std::endl; + + std::string output = customExecStr(cmd); + EXPECT_NE(output.find("Setting seed value"), std::string::npos) + << output; +} + ///////////////////////////////////////////////// TEST(CmdLine, GZ_UTILS_TEST_DISABLED_ON_WIN32(SimServer)) {