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

Prevent crash when objects move to invalid poses (backport #706) #707

Merged
merged 1 commit into from
Dec 19, 2024
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
1 change: 1 addition & 0 deletions dartsim/src/Base.hh
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ struct ModelInfo
std::vector<std::shared_ptr<LinkInfo>> links {};
std::vector<std::shared_ptr<JointInfo>> joints {};
std::vector<std::size_t> nestedModels = {};
std::optional<Eigen::VectorXd> lastGoodPositions = {};
};

struct ShapeInfo
Expand Down
33 changes: 33 additions & 0 deletions dartsim/src/SimulationFeatures.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*
*/

#include <Eigen/Geometry>
#include <limits>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
Expand All @@ -30,6 +32,7 @@
#include <dart/constraint/ContactSurface.hpp>
#endif

#include <gz/common/Console.hh>
#include <gz/common/Profiler.hh>

#include <gz/math/Pose3.hh>
Expand Down Expand Up @@ -106,6 +109,36 @@ void SimulationFeatures::WorldForwardStep(

// TODO(MXG): Parse input
world->step();

for (auto &[ignore, modelInfo] : this->models.idToObject)
{
const Eigen::VectorXd &positions = modelInfo->model->getPositions();
if (positions.hasNaN())
{
std::stringstream ss;
ss << "Some links in model '" << modelInfo->localName
<< "' have invalid poses. ";
if (modelInfo->lastGoodPositions)
{
ss << "Resetting to last known poses.";
modelInfo->model->setPositions(*modelInfo->lastGoodPositions);
}
else
{
ss << "Resetting to zero poses.";
modelInfo->model->resetPositions();
}
gzerr << ss.str()
<< " Also resetting velocities and accelerations to zero."
<< std::endl;
modelInfo->model->resetVelocities();
modelInfo->model->resetAccelerations();
}
else
{
modelInfo->lastGoodPositions = positions;
}
}
this->WriteRequiredData(_h);
this->Write(_h.Get<ChangedWorldPoses>());
// TODO(MXG): Fill in state
Expand Down
Loading