Skip to content

Commit

Permalink
VisualizeForces: use msgs::EntityWrenchMap message.
Browse files Browse the repository at this point in the history
- Use new message type msgs::EntityWrenchMap.
- Disable  components::EntityWrench and components::EntityWrenches.
- Enable components::EntityWrenchMap.

Signed-off-by: Rhys Mainwaring <[email protected]>
  • Loading branch information
srmainwaring committed Mar 23, 2023
1 parent 53210d2 commit fd90729
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 2 deletions.
9 changes: 9 additions & 0 deletions include/gz/sim/components/EntityWrench.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <unordered_map>

#include <gz/msgs/entity_wrench.pb.h>
#include <gz/msgs/entity_wrench_map.pb.h>
#include <gz/sim/components/Component.hh>
#include <gz/sim/components/Factory.hh>
#include <gz/sim/components/Serialization.hh>
Expand Down Expand Up @@ -105,6 +106,14 @@ namespace components
GZ_SIM_REGISTER_COMPONENT("gz_sim_components.EntityWrench",
EntityWrench)

/// \brief A component type that contains an map of wrenches
/// for an entity. The wrenches are represented by gz::msgs::EntityWrenchMap.
using EntityWrenchMap =
Component<msgs::EntityWrenchMap, class EntityWrenchMapTag,
serializers::MsgSerializer>;
GZ_SIM_REGISTER_COMPONENT("gz_sim_components.EntityWrenchMap",
EntityWrenchMap)

/// \brief A component type that contains an unordered map of wrenches
/// for an entity. The wrenches are represented by gz::msgs::EntityWrench.
/// The key is a string label.
Expand Down
70 changes: 69 additions & 1 deletion src/Link.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <gz/msgs/Utility.hh>
#include <gz/msgs/entity_wrench.pb.h>
#include <gz/msgs/entity_wrench_map.pb.h>

#include "gz/sim/components/AngularAcceleration.hh"
#include "gz/sim/components/AngularVelocity.hh"
Expand Down Expand Up @@ -438,7 +439,7 @@ void Link::AddWorldWrench(EntityComponentManager &_ecm,
msgs::Convert(linkWrenchComp->Data().torque()) + _torque);
}

#if 1
#if 0
// Publish components::EntityWrenches
if (this->dataPtr->visualizationLabel.has_value())
{
Expand Down Expand Up @@ -513,6 +514,73 @@ void Link::AddWorldWrench(EntityComponentManager &_ecm,
#endif

#if 1
// Publish components::EntityWrenchMap
if (this->dataPtr->visualizationLabel.has_value())
{
auto& label = this->dataPtr->visualizationLabel.value();

// Enable required components.
enableComponent<components::WorldPose>(_ecm, this->dataPtr->id, true);
enableComponent<components::EntityWrenchMap>(_ecm, this->dataPtr->id, true);

auto entityWrenchMapComp =
_ecm.Component<components::EntityWrenchMap>(this->dataPtr->id);
if (!entityWrenchMapComp)
{
static bool informed{false};
if (!informed)
{
gzerr << "Failed to retrieve EntityWrenchMap component for link ["
<< this->dataPtr->id << "] from [" << label << "]\n";
}
return;
}

// Populate data
msgs::EntityWrench msg;

// Set label
{
auto data = msg.mutable_header()->add_data();
data->set_key("label");
data->add_value(label);
}

// Set name
{
auto data = msg.mutable_header()->add_data();
data->set_key("name");
if (this->Name(_ecm).has_value())
{
data->add_value(this->Name(_ecm).value());
}
}

// Set entity
msg.mutable_entity()->set_id(this->Entity());

// Set wrench
msgs::Set(msg.mutable_wrench()->mutable_force(), _force);
msgs::Set(msg.mutable_wrench()->mutable_torque(), _torque);

// Update map with wrench
auto& data = entityWrenchMapComp->Data();
(*data.mutable_wrenches())[label] = msg;

_ecm.SetChanged(this->dataPtr->id, components::EntityWrenchMap::typeId,
ComponentState::PeriodicChange);

// {
// gzdbg << "Publishing entity wrench map for link ["
// << this->dataPtr->id << "]\n"
// << "Size: " << entityWrenchMapComp->Data().wrenches().size() << "\n"
// << "Label: " << label << "\n"
// << entityWrenchMapComp->Data().DebugString() << "\n";
// }
}
#endif

#if 0
/// \todo(srmainwaring) - for debugging - publish components::EntityWrenches
if (this->dataPtr->visualizationLabel.has_value())
{
Expand Down
36 changes: 35 additions & 1 deletion src/gui/plugins/visualize_forces/VisualizeForces.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <gz/math/Vector3.hh>

#include <gz/msgs/entity_wrench.pb.h>
#include <gz/msgs/wrench.pb.h>
#include <gz/msgs/entity_wrench_map.pb.h>

#include <gz/plugin/Register.hh>

Expand Down Expand Up @@ -165,6 +165,40 @@ void VisualizeForcesPrivate::RetrieveWrenchesFromEcm(
#endif

#if 1
{
// Read components::EntityWrenchMap
_ecm.Each<components::WorldPose, components::EntityWrenchMap>(
[&](const Entity &_entity,
components::WorldPose *_worldPose,
components::EntityWrenchMap *_entityWrenchMap) -> bool
{
// Push all the wrenches for this entity to the queue.
for (auto& [key, value] : _entityWrenchMap->Data().wrenches())
{
this->queue.push({_worldPose->Data(), value});
}

// debugging
// {
// std::stringstream ss;
// ss << "Received EntityWrenchMap for entity ["
// << _entity << "]\n"
// << "WorldPose [" << _worldPose->Data().Pos() << "]\n"
// << "Size " << _entityWrenchMap->Data().wrenches().size() << "\n";
// for (auto& [key, value] : _entityWrenchMap->Data().wrenches())
// {
// ss << "Key: " << key << "\n"
// << "Msg: " << value.DebugString() << "\n";
// }
// gzdbg << ss.str();
// }

return true;
});
}
#endif

#if 0
{
/// \todo(srmainwaring) - for debugging - read components::EntityWrench
_ecm.Each<components::WorldPose, components::EntityWrench>(
Expand Down

0 comments on commit fd90729

Please sign in to comment.