-
Notifications
You must be signed in to change notification settings - Fork 342
Allow to enable/disable static and gravity #3007
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
base: gz-sim8
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,10 @@ namespace components | |
using GravityEnabled = Component<bool, class GravityEnabledTag>; | ||
GZ_SIM_REGISTER_COMPONENT( | ||
"gz_sim_components.GravityEnabled", GravityEnabled) | ||
|
||
/// \brief Store the gravity acceleration. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Update indentation. Also, is the comment correct? I'd expect something about store whether we want to enable gravity or not. |
||
using GravityEnabledCmd = Component<bool, class GravityEnabledCmdTag>; | ||
GZ_SIM_REGISTER_COMPONENT("gz_sim_components.GravityEnabledCmd", GravityEnabledCmd) | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
*/ | ||
|
||
#include "gz/sim/components/CanonicalLink.hh" | ||
#include "gz/sim/components/Gravity.hh" | ||
#include "gz/sim/components/Joint.hh" | ||
#include "gz/sim/components/Link.hh" | ||
#include "gz/sim/components/Model.hh" | ||
|
@@ -219,6 +220,44 @@ void Model::SetWorldPoseCmd(EntityComponentManager &_ecm, | |
} | ||
} | ||
|
||
void Model::SetStaticStateCmd(EntityComponentManager &_ecm, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Add |
||
bool _state) | ||
{ | ||
std::cout << "SetStaticState " << std::endl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove? |
||
auto staticComp = _ecm.Component<components::StaticStateCmd>( | ||
this->dataPtr->id); | ||
if (!staticComp) | ||
{ | ||
_ecm.CreateComponent(this->dataPtr->id, components::StaticStateCmd(_state)); | ||
} | ||
else | ||
{ | ||
staticComp->SetData(_state, | ||
[](const bool &, const bool &){return false;}); | ||
_ecm.SetChanged(this->dataPtr->id, | ||
components::StaticStateCmd::typeId, ComponentState::OneTimeChange); | ||
} | ||
} | ||
|
||
void Model::SetGravityEnabledCmd(EntityComponentManager &_ecm, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Add |
||
bool _enabled) | ||
{ | ||
std::cout << "SetGravityEnabledCmd " << std::endl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove? |
||
auto staticComp = _ecm.Component<components::GravityEnabledCmd>( | ||
this->dataPtr->id); | ||
if (!staticComp) | ||
{ | ||
_ecm.CreateComponent(this->dataPtr->id, components::GravityEnabledCmd(_enabled)); | ||
} | ||
else | ||
{ | ||
staticComp->SetData(_enabled, | ||
[](const bool &, const bool &){return false;}); | ||
_ecm.SetChanged(this->dataPtr->id, | ||
components::GravityEnabledCmd::typeId, ComponentState::OneTimeChange); | ||
} | ||
} | ||
|
||
////////////////////////////////////////////////// | ||
Entity Model::CanonicalLink(const EntityComponentManager &_ecm) const | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -380,6 +380,12 @@ class gz::sim::systems::PhysicsPrivate | |
/// deleted the following iteration. | ||
public: std::unordered_set<Entity> worldPoseCmdsToRemove; | ||
|
||
/// \brief Entities whose static commands have been processed and should be | ||
/// deleted the following iteration. | ||
public: std::unordered_set<Entity> staticStateCmdsToRemove; | ||
|
||
public: std::unordered_set<Entity> gravityEnabledCmdsToRemove; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add doxygen. |
||
|
||
/// \brief IDs of the ContactSurfaceHandler callbacks registered for worlds | ||
public: std::unordered_map<Entity, std::string> worldContactCallbackIDs; | ||
|
||
|
@@ -571,6 +577,19 @@ class gz::sim::systems::PhysicsPrivate | |
physics::GetModelBoundingBox>{}; | ||
|
||
////////////////////////////////////////////////// | ||
// static | ||
/// \brief Feature list for model static state. | ||
public: struct StaticStateFeatureList : physics::FeatureList< | ||
MinimumFeatureList, | ||
physics::SetFreeGroupStaticState>{}; | ||
|
||
////////////////////////////////////////////////// | ||
// enabled gravity | ||
/// \brief Feature list for model static state. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update comment. |
||
public: struct GravityEnabledFeatureList : physics::FeatureList< | ||
MinimumFeatureList, | ||
physics::SetFreeGroupGravityEnabled>{}; | ||
|
||
// Link Bounding box | ||
/// \brief Feature list for model bounding box. | ||
public: struct LinkBoundingBoxFeatureList : physics::FeatureList< | ||
|
@@ -754,7 +773,9 @@ class gz::sim::systems::PhysicsPrivate | |
public: using EntityFreeGroupMap = EntityFeatureMap3d< | ||
physics::FreeGroup, | ||
MinimumFeatureList, | ||
WorldVelocityCommandFeatureList | ||
WorldVelocityCommandFeatureList, | ||
StaticStateFeatureList, | ||
GravityEnabledFeatureList | ||
>; | ||
|
||
/// \brief A map between collision entity ids in the ECM to FreeGroup Entities | ||
|
@@ -2101,6 +2122,30 @@ void PhysicsPrivate::RemovePhysicsEntities(const EntityComponentManager &_ecm) | |
void PhysicsPrivate::UpdatePhysics(EntityComponentManager &_ecm) | ||
{ | ||
GZ_PROFILE("PhysicsPrivate::UpdatePhysics"); | ||
|
||
// _ecm.Each<components::Model, components::Name, components::Static>( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this block? |
||
// [&](const Entity & _entity, | ||
// const components::Model * _model, | ||
// const components::Name * _name, | ||
// const components::Static *_static) -> bool | ||
// { | ||
// bool is_static = this->staticEntities.find(_entity) != | ||
// this->staticEntities.end(); | ||
// if (is_static != _static->Data()) { | ||
// if (is_static) { | ||
// this->staticEntities.erase(_entity); | ||
// gzwarn << "UpdatePhysics static erase" << std::endl; | ||
// } else { | ||
// this->staticEntities.insert(_entity); | ||
// gzwarn << "UpdatePhysics static insert " << std::endl; | ||
// for (auto & lol : this->staticEntities) { | ||
// gzwarn << "lol " << lol << " " << _name->Data() << std::endl; | ||
// } | ||
// } | ||
// } | ||
// return true; | ||
// }); | ||
|
||
// Battery state | ||
_ecm.Each<components::BatterySoC>( | ||
[&](const Entity & _entity, const components::BatterySoC *_bat) | ||
|
@@ -2411,6 +2456,141 @@ void PhysicsPrivate::UpdatePhysics(EntityComponentManager &_ecm) | |
return true; | ||
}); | ||
|
||
// update Static State | ||
auto olderStaticStateCmdsToRemove = std::move(this->staticStateCmdsToRemove); | ||
this->staticStateCmdsToRemove.clear(); | ||
|
||
_ecm.Each<components::Model, | ||
components::StaticStateCmd, | ||
components::Name>( | ||
[&](const Entity &_entity, const components::Model *, | ||
const components::StaticStateCmd *_staticSateCmd, | ||
const components::Name *_name)->bool | ||
{ | ||
this->staticStateCmdsToRemove.insert(_entity); | ||
|
||
auto modelPtrPhys = this->entityModelMap.Get(_entity); | ||
if (nullptr == modelPtrPhys) | ||
return true; | ||
|
||
auto freeGroup = modelPtrPhys->FindFreeGroup(); | ||
if (!freeGroup) | ||
return true; | ||
|
||
this->entityFreeGroupMap.AddEntity(_entity, freeGroup); | ||
|
||
auto ssModel = | ||
this->entityFreeGroupMap.EntityCast<StaticStateFeatureList>(_entity); | ||
|
||
if (!ssModel) | ||
{ | ||
static bool informed{false}; | ||
if (!informed) | ||
{ | ||
gzdbg << "Attempting to set a static state, but the physics " | ||
<< "engine doesn't support feature " | ||
<< "[SetStaticState]. static state won't be populated." | ||
<< " " << _name->Data() | ||
<< std::endl; | ||
informed = true; | ||
} | ||
|
||
// Break Each call since no Static state'es can be processed | ||
return false; | ||
} | ||
gzwarn << "_staticSateCmd->Data() " << _staticSateCmd->Data() << std::endl; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this actually a warning or should we use |
||
|
||
bool is_static = this->staticEntities.find(_entity) != | ||
this->staticEntities.end(); | ||
if (is_static != _staticSateCmd->Data()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: Move |
||
if (is_static) { | ||
this->staticEntities.erase(_entity); | ||
gzwarn << "UpdatePhysics static erase " << _name->Data() << std::endl; | ||
} else { | ||
this->staticEntities.insert(_entity); | ||
gzwarn << "UpdatePhysics static insert " << _name->Data() << std::endl; | ||
// for (auto & lol : this->staticEntities) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove commented code? |
||
// gzwarn << "lol " << lol << " " << _name->Data() << std::endl; | ||
// } | ||
} | ||
} | ||
|
||
// auto modelPtrPhys = this->entityModelMap.Get(_entity); | ||
// if (nullptr == modelPtrPhys) | ||
// return true; | ||
|
||
// // TODO(addisu) Store the free group instead of searching for it at | ||
// // every iteration | ||
// auto freeGroup = modelPtrPhys->FindFreeGroup(); | ||
// if (!freeGroup) | ||
// return true; | ||
|
||
ssModel->SetStaticState(_staticSateCmd->Data()); | ||
return true; | ||
}); | ||
|
||
// Remove world commands from previous iteration. We let them rotate one | ||
// iteration so other systems have a chance to react to them too. | ||
for (const Entity &entity : olderStaticStateCmdsToRemove) | ||
{ | ||
_ecm.RemoveComponent<components::StaticStateCmd>(entity); | ||
} | ||
|
||
// update Gravity enabled | ||
auto olderGravityEnabledCmdsToRemove = std::move(this->gravityEnabledCmdsToRemove); | ||
this->gravityEnabledCmdsToRemove.clear(); | ||
|
||
_ecm.Each<components::Model, | ||
components::GravityEnabledCmd, | ||
components::Name>( | ||
[&](const Entity &_entity, const components::Model *, | ||
const components::GravityEnabledCmd *_gravityEnabledCmd, | ||
const components::Name *_name)->bool | ||
{ | ||
this->gravityEnabledCmdsToRemove.insert(_entity); | ||
|
||
auto modelPtrPhys = this->entityModelMap.Get(_entity); | ||
if (nullptr == modelPtrPhys) | ||
return true; | ||
|
||
auto freeGroup = modelPtrPhys->FindFreeGroup(); | ||
if (!freeGroup) | ||
return true; | ||
|
||
this->entityFreeGroupMap.AddEntity(_entity, freeGroup); | ||
|
||
auto ssModel = | ||
this->entityFreeGroupMap.EntityCast<GravityEnabledFeatureList>(_entity); | ||
|
||
if (!ssModel) | ||
{ | ||
static bool informed{false}; | ||
if (!informed) | ||
{ | ||
gzdbg << "Attempting to set a static state, but the physics " | ||
<< "engine doesn't support feature " | ||
<< "[SetGravityEnabled]. static state won't be populated." | ||
<< " " << _name->Data() | ||
<< std::endl; | ||
informed = true; | ||
} | ||
|
||
// Break Each call since no Static state'es can be processed | ||
return false; | ||
} | ||
gzwarn << "_gravityEnabledCmd->Data() " << _gravityEnabledCmd->Data() << std::endl; | ||
|
||
ssModel->SetGravityEnabled(_gravityEnabledCmd->Data()); | ||
return true; | ||
}); | ||
|
||
// Remove world commands from previous iteration. We let them rotate one | ||
// iteration so other systems have a chance to react to them too. | ||
for (const Entity &entity : olderGravityEnabledCmdsToRemove) | ||
{ | ||
_ecm.RemoveComponent<components::GravityEnabledCmd>(entity); | ||
} | ||
|
||
// Update model pose | ||
auto olderWorldPoseCmdsToRemove = std::move(this->worldPoseCmdsToRemove); | ||
this->worldPoseCmdsToRemove.clear(); | ||
|
@@ -2743,6 +2923,7 @@ void PhysicsPrivate::ResetPhysics(EntityComponentManager &_ecm) | |
this->canonicalLinkModelTracker = CanonicalLinkModelTracker(); | ||
this->modelWorldPoses.clear(); | ||
this->worldPoseCmdsToRemove.clear(); | ||
this->staticStateCmdsToRemove.clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we also clear |
||
|
||
this->RemovePhysicsEntities(_ecm); | ||
this->CreatePhysicsEntities(_ecm, false); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace
_gravity
with_enabled
?