Skip to content
This repository was archived by the owner on Feb 3, 2025. It is now read-only.

Commit

Permalink
Merged in issue_2441 (pull request #2933)
Browse files Browse the repository at this point in the history
Backport pull request #2890 to gazebo7 (issue #2441)

Approved-by: Steven Peters <scpeters@osrfoundation.org>
Approved-by: Ian Chen <ichen@osrfoundation.org>
  • Loading branch information
chapulina committed Apr 10, 2018
2 parents b6d86c8 + 1500468 commit 2152095
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 15 deletions.
45 changes: 33 additions & 12 deletions gazebo/physics/World.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ World::World(const std::string &_name)
this->dataPtr->sdf.reset(new sdf::Element);
sdf::initFile("world.sdf", this->dataPtr->sdf);

this->dataPtr->initialSdf.reset(new sdf::Element);
sdf::initFile("world.sdf", this->dataPtr->initialSdf);

// Keep this in the constructor for performance.
// sdf::initFile causes disk access.
this->dataPtr->factorySDF.reset(new sdf::SDF);
Expand Down Expand Up @@ -176,8 +173,6 @@ void World::Load(sdf::ElementPtr _sdf)
this->dataPtr->loaded = false;
this->dataPtr->sdf = _sdf;

this->dataPtr->initialSdf->Copy(_sdf);

if (this->dataPtr->sdf->Get<std::string>("name").empty())
gzwarn << "create_world(world_name =["
<< this->dataPtr->name << "]) overwrites sdf world name\n!";
Expand Down Expand Up @@ -843,6 +838,10 @@ void World::Fini()
}
this->dataPtr->prevStates[0].SetWorld(WorldPtr());
this->dataPtr->prevStates[1].SetWorld(WorldPtr());
this->dataPtr->prevUnfilteredState.SetWorld(WorldPtr());
this->dataPtr->logPlayState.SetWorld(WorldPtr());
this->dataPtr->states[0].clear();
this->dataPtr->states[1].clear();

this->dataPtr->presetManager.reset();
this->dataPtr->userCmdManager.reset();
Expand Down Expand Up @@ -1005,6 +1004,18 @@ ModelPtr World::LoadModel(sdf::ElementPtr _sdf , BasePtr _parent)

if (_sdf->GetName() == "model")
{
std::string modelName = _sdf->Get<std::string>("name");
for (auto const m : this->dataPtr->models)
{
if (m->GetName() == modelName)
{
gzwarn << "Model with name [" << modelName << "] already exists. "
<< "Not inserting model. This warning can be ignored in certain "
<< "situations such as rewind during log playback.\n";
return model;
}
}

model = this->dataPtr->physicsEngine->CreateModel(_parent);
model->SetWorld(shared_from_this());
model->Load(_sdf);
Expand Down Expand Up @@ -2028,8 +2039,11 @@ void World::ProcessFactoryMsgs()
boost::mutex::scoped_lock lock(this->dataPtr->factoryDeleteMutex);

ModelPtr model = this->LoadModel(elem, this->dataPtr->rootElement);
model->Init();
model->LoadPlugins();
if (model != nullptr)
{
model->Init();
model->LoadPlugins();
}
}
catch(...)
{
Expand Down Expand Up @@ -2152,9 +2166,12 @@ void World::SetState(const WorldState &_state)
boost::mutex::scoped_lock lock(this->dataPtr->factoryDeleteMutex);

ModelPtr model = this->LoadModel(elem, this->dataPtr->rootElement);
model->Init();
if (!util::LogPlay::Instance()->IsOpen())
model->LoadPlugins();
if (model != nullptr)
{
model->Init();
if (!util::LogPlay::Instance()->IsOpen())
model->LoadPlugins();
}
}
catch(...)
{
Expand Down Expand Up @@ -2285,10 +2302,11 @@ bool World::OnLog(std::ostringstream &_stream)
// Save the entire state when its the first call to OnLog.
if (util::LogRecord::Instance()->FirstUpdate())
{
this->dataPtr->sdf->Update();
_stream << "<sdf version ='";
_stream << SDF_VERSION;
_stream << "'>\n";
_stream << this->dataPtr->initialSdf->ToString("");
_stream << this->dataPtr->sdf->ToString("");
_stream << "</sdf>\n";
}
else if (this->dataPtr->states[bufferIndex].size() >= 1)
Expand Down Expand Up @@ -2549,6 +2567,9 @@ void World::LogWorker()

GZ_ASSERT(self, "Self pointer to World is invalid");

// Init the prevUnfilteredState
this->dataPtr->prevUnfilteredState.Load(self);

while (!this->dataPtr->stop)
{
// get unfiltered world state
Expand All @@ -2562,7 +2583,7 @@ void World::LogWorker()
std::vector<std::string> insertions;
std::vector<std::string> deletions;
bool insertDelete = false;
if (this->dataPtr->logLastStateTime != common::Time::Zero)

{
WorldState unfilteredDiffState = unfilteredState -
this->dataPtr->prevUnfilteredState;
Expand Down
3 changes: 0 additions & 3 deletions gazebo/physics/WorldPrivate.hh
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,6 @@ namespace gazebo
/// \brief The world's current SDF description.
public: sdf::ElementPtr sdf;

/// \brief The world description when it was first loaded.
public: sdf::ElementPtr initialSdf;

/// \brief All the plugins.
public: std::vector<WorldPluginPtr> plugins;

Expand Down
2 changes: 2 additions & 0 deletions gazebo/physics/WorldState.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ void WorldState::Load(const WorldPtr _world)
this->simTime = _world->GetSimTime();
this->realTime = _world->GetRealTime();
this->iterations = _world->GetIterations();
this->insertions.clear();
this->deletions.clear();

std::string filter = worldStateFilter;
std::list<std::string> mainParts, parts;
Expand Down
110 changes: 110 additions & 0 deletions test/regression/2297_log_insertions_paused.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2018 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 <unistd.h>
#include <gazebo/util/LogRecord.hh>
#include <gazebo/util/LogPlay.hh>
#include <gazebo/test/ServerFixture.hh>

using namespace gazebo;

class Issue2297_Test : public ServerFixture
{
};

/////////////////////////////////////////////////
TEST_F(Issue2297_Test, ModelInsertRecord)
{
// Create a temporary directory
char dirTemplate[] ="/tmp/gazeboXXXXXX";
std::string tmpDir = mkdtemp(dirTemplate);

// Initialize log recording
util::LogRecord *recorder = util::LogRecord::Instance();
ASSERT_TRUE(recorder != nullptr);
recorder->Init("test");

// Start gazebo paused.
this->Load("worlds/empty.world", true);

// Get a pointer to the world
physics::WorldPtr world = physics::get_world("default");
ASSERT_TRUE(world != nullptr);

recorder->Start("txt", tmpDir);
std::string filename = recorder->Filename();

std::string modelString =
"<sdf version='1.6'>"
" <model name='box'>"
" <pose>0 0 10 0 0 0</pose>"
" <link name='link'>"
" <collision name='collision'>"
" <geometry><box><size>1 1 1</size></box></geometry>"
" </collision>"
" <visual name='visual'>"
" <geometry><box><size>1 1 1</size></box></geometry>"
" </visual>"
" </link>"
" </model>"
"</sdf>";

world->InsertModelString(modelString);

// Step the world enough iterations to guarantee that the <insertions> is
// recorded in the log file.
world->Step(200);

// Stop recording
recorder->Stop();
recorder->Fini();

// Open the log file
util::LogPlay *player = util::LogPlay::Instance();
player->Open(filename);

// The first step should be the initial world configuration
std::string data;
player->Step(data);
EXPECT_NE(data.find("<world name='default'>"), std::string::npos);
EXPECT_EQ(data.find("<insertions>"), std::string::npos);

int count = 0;
// The insertions should come after the first step
while (player->Step(data))
{
size_t boxPos = data.find("<insertions><model name='box'>");
while (boxPos != std::string::npos)
{
count++;
boxPos = data.find("<insertions><model name='box'>", boxPos + 1);
}
}
EXPECT_EQ(1, count) << "There were multiple insertions\n";

// Cleanup the directory
remove(filename.c_str());
rmdir(tmpDir.c_str());
}

/////////////////////////////////////////////////
// Main
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
114 changes: 114 additions & 0 deletions test/regression/2428_log_insertions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (C) 2018 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 <unistd.h>
#include <gazebo/util/LogRecord.hh>
#include <gazebo/util/LogPlay.hh>
#include <gazebo/test/ServerFixture.hh>

using namespace gazebo;

class Issue2428_Test : public ServerFixture
{
};

/////////////////////////////////////////////////
// This test checks that the following steps work:
//
// 1. run Gazebo
// 2. Insert some models, at least one of them should move
// 3. Record a log
// 4. Stop record
// 5. The log file should have the inserted model inside the
// <world>...</world> block.
TEST_F(Issue2428_Test, InsertionBeforeRecording)
{
// Create a temporary directory
char dirTemplate[] ="/tmp/gazeboXXXXXX";
std::string tmpDir = mkdtemp(dirTemplate);

util::LogRecord *recorder = util::LogRecord::Instance();
recorder->Init("test");

// Playback the state log
this->Load("worlds/empty.world", true);

// Get a pointer to the world
physics::WorldPtr world = physics::get_world("default");
ASSERT_TRUE(world != nullptr);

// take some steps before inserting model to avoid issue #2297
world->Step(100);

std::string modelString =
"<sdf version='1.6'>"
" <model name='box'>"
" <pose>0 0 10 0 0 0</pose>"
" <link name='link'>"
" <collision name='collision'>"
" <geometry><box><size>1 1 1</size></box></geometry>"
" </collision>"
" <visual name='visual'>"
" <geometry><box><size>1 1 1</size></box></geometry>"
" </visual>"
" </link>"
" </model>"
"</sdf>";

// Insert a model before recording starts
world->InsertModelString(modelString);
world->Step(100);

recorder->Start("txt", tmpDir);

// Step the world enough iterations to guarantee that the <insertions> is
// recorded in the log file.
world->Step(200);

std::string filename = recorder->Filename();

// Stop recording
recorder->Stop();
recorder->Fini();

// Open the log file
util::LogPlay *player = util::LogPlay::Instance();
player->Open(filename);

std::string data;
player->Step(data);
size_t worldStart = data.find("<world name='default'>");
size_t boxPos = data.find("<model name='box'>");
size_t worldEnd = data.find("</world>");

// The box model should exist in the initial world.
EXPECT_LT(worldStart, boxPos);
EXPECT_GT(worldEnd, boxPos);
EXPECT_NE(boxPos, std::string::npos);

// Cleanup the directory
remove(filename.c_str());
rmdir(tmpDir.c_str());
}

/////////////////////////////////////////////////
// Main
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
2 changes: 2 additions & 0 deletions test/regression/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ set(tests
1569_hydra_crash.cc
1694_world_accel.cc
1782_unadvertise.cc
2297_log_insertions_paused.cc
2428_log_insertions.cc
)
gz_build_tests(${tests} EXTRA_LIBS gazebo_test_fixture)

Expand Down

0 comments on commit 2152095

Please sign in to comment.