-
Notifications
You must be signed in to change notification settings - Fork 277
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
Apply Force and Torque GUI plugin #2014
Merged
Merged
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
89c5b9c
Create ApplyForceTorque GUI
Henrique-BO 15746ef
Apply forces using the apply_link_wrench system
Henrique-BO 992a6f5
Add the ability to select the link of wrench application
Henrique-BO 2c69d18
Clean up ApplyForceTorque and improve debug messages
Henrique-BO 5d63bdf
Merge branch 'gazebosim:gz-sim7' into apply_force_torque
Henrique-BO 5af8dfc
Merge branch 'gazebosim:gz-sim7' into apply_force_torque
Henrique-BO 2364fc9
Fix codecheck
Henrique-BO 17693a0
Merge branch 'gazebosim:gz-sim7' into apply_force_torque
Henrique-BO 6fbbfc0
Remove force offset from interface and store only the selected entity
Henrique-BO 814d501
Merge branch 'gazebosim:gz-sim7' into apply_force_torque
Henrique-BO 7450833
Merge branch 'gazebosim:gz-sim7' into apply_force_torque
Henrique-BO ca1c904
Apply force to COM and minor changes
Henrique-BO de6000b
Merge branch 'gazebosim:gz-sim7' into apply_force_torque
Henrique-BO 4243567
Wrench specified in link-fixed frame
Henrique-BO 12f6fbe
Merge branch 'gazebosim:gz-sim7' into apply_force_torque
Henrique-BO 3f08e6d
Merge branch 'gazebosim:gz-sim7' into apply_force_torque
Henrique-BO 3911d73
Automatically load ApplyLinkWrench and minor fixes
Henrique-BO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,318 @@ | ||||||
/* | ||||||
* Copyright (C) 2023 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 <mutex> | ||||||
#include <string> | ||||||
|
||||||
#include <gz/gui/Application.hh> | ||||||
#include <gz/gui/Helpers.hh> | ||||||
#include <gz/gui/MainWindow.hh> | ||||||
#include <gz/math/Quaternion.hh> | ||||||
#include <gz/math/Vector3.hh> | ||||||
#include <gz/msgs/entity_wrench.pb.h> | ||||||
#include <gz/msgs/Utility.hh> | ||||||
#include <gz/plugin/Register.hh> | ||||||
#include <gz/sim/EntityComponentManager.hh> | ||||||
#include <gz/sim/Link.hh> | ||||||
#include <gz/sim/Model.hh> | ||||||
#include <gz/sim/Util.hh> | ||||||
#include <gz/sim/components/Inertial.hh> | ||||||
#include <gz/sim/gui/GuiEvents.hh> | ||||||
#include <gz/transport/Node.hh> | ||||||
|
||||||
quarkytale marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
#include "ApplyForceTorque.hh" | ||||||
|
||||||
namespace gz | ||||||
{ | ||||||
namespace sim | ||||||
{ | ||||||
class ApplyForceTorquePrivate | ||||||
{ | ||||||
/// \brief Publish wrench messages | ||||||
public: void PublishWrench(bool _applyForce, bool _applyTorque); | ||||||
|
||||||
/// \brief Transport node | ||||||
public: transport::Node node; | ||||||
|
||||||
/// \brief Publisher for EntityWrench messages | ||||||
public: transport::Node::Publisher pub; | ||||||
|
||||||
/// \brief To synchronize member access | ||||||
public: std::mutex mutex; | ||||||
azeey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
/// \brief Name of the selected model | ||||||
public: QString modelName; | ||||||
|
||||||
/// \brief List of the name of the links in the selected model | ||||||
public: QStringList linkNameList; | ||||||
|
||||||
/// \brief Index of selected link in list | ||||||
public: int linkIndex{-1}; | ||||||
|
||||||
/// \brief Entity of the currently selected Link | ||||||
public: std::optional<Entity> selectedEntity; | ||||||
|
||||||
/// \brief True if a new entity was selected | ||||||
public: bool changedEntity{false}; | ||||||
|
||||||
/// \brief True if a new link was selected from the dropdown | ||||||
public: bool changedIndex{false}; | ||||||
|
||||||
/// \brief Force to be applied in link-fixed frame'' | ||||||
public: math::Vector3d force{0.0, 0.0, 0.0}; | ||||||
|
||||||
/// \brief Torque to be applied in link-fixed frame | ||||||
public: math::Vector3d torque{0.0, 0.0, 0.0}; | ||||||
|
||||||
/// \brief Offset from the link origin to the center of mass in world coords | ||||||
public: math::Vector3d inertialPos; | ||||||
|
||||||
/// \brief Orientation of the link-fixed frame | ||||||
public: math::Quaterniond linkRot; | ||||||
}; | ||||||
} | ||||||
} | ||||||
|
||||||
using namespace gz; | ||||||
using namespace sim; | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
ApplyForceTorque::ApplyForceTorque() | ||||||
: GuiSystem(), dataPtr(std::make_unique<ApplyForceTorquePrivate>()) | ||||||
{ | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
ApplyForceTorque::~ApplyForceTorque() = default; | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
void ApplyForceTorque::LoadConfig(const tinyxml2::XMLElement */*_pluginElem*/) | ||||||
{ | ||||||
if (this->title.empty()) | ||||||
this->title = "Apply force and torque"; | ||||||
|
||||||
// Create wrench publisher | ||||||
auto worldNames = gz::gui::worldNames(); | ||||||
if (!worldNames.empty()) | ||||||
{ | ||||||
auto topic = transport::TopicUtils::AsValidTopic( | ||||||
"/world/" + worldNames[0].toStdString() + "/wrench"); | ||||||
if (topic == "") | ||||||
{ | ||||||
gzerr << "Unable to create publisher" << std::endl; | ||||||
return; | ||||||
} | ||||||
this->dataPtr->pub = | ||||||
this->dataPtr->node.Advertise<msgs::EntityWrench>(topic); | ||||||
gzdbg << "Created publisher to " << topic << std::endl; | ||||||
} | ||||||
|
||||||
gz::gui::App()->findChild<gz::gui::MainWindow *> | ||||||
()->installEventFilter(this); | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
bool ApplyForceTorque::eventFilter(QObject *_obj, QEvent *_event) | ||||||
{ | ||||||
std::lock_guard<std::mutex> lock(this->dataPtr->mutex); | ||||||
|
||||||
if (_event->type() == | ||||||
gz::sim::gui::events::EntitiesSelected::kType) | ||||||
{ | ||||||
gz::sim::gui::events::EntitiesSelected *_e = | ||||||
static_cast<gz::sim::gui::events::EntitiesSelected*>(_event); | ||||||
this->dataPtr->selectedEntity = _e->Data().front(); | ||||||
this->dataPtr->changedEntity = true; | ||||||
} | ||||||
else if (_event->type() == | ||||||
gz::sim::gui::events::DeselectAllEntities::kType) | ||||||
{ | ||||||
this->dataPtr->selectedEntity.reset(); | ||||||
this->dataPtr->changedEntity = true; | ||||||
} | ||||||
|
||||||
return QObject::eventFilter(_obj, _event); | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
void ApplyForceTorque::Update(const UpdateInfo &/*_info*/, | ||||||
EntityComponentManager &_ecm) | ||||||
{ | ||||||
std::lock_guard<std::mutex> lock(this->dataPtr->mutex); | ||||||
|
||||||
if (this->dataPtr->changedEntity) | ||||||
{ | ||||||
this->dataPtr->changedEntity = false; | ||||||
|
||||||
this->dataPtr->modelName = ""; | ||||||
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.
Suggested change
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. I don't think this works, since this is an assignment, not an initialization. I get a compilation error if I try this. |
||||||
this->dataPtr->linkNameList.clear(); | ||||||
this->dataPtr->linkIndex = -1; | ||||||
|
||||||
if (this->dataPtr->selectedEntity.has_value()) | ||||||
{ | ||||||
Model parentModel(*this->dataPtr->selectedEntity); | ||||||
Link selectedLink(*this->dataPtr->selectedEntity); | ||||||
if (parentModel.Valid(_ecm)) | ||||||
{ | ||||||
selectedLink = Link(parentModel.CanonicalLink(_ecm)); | ||||||
} | ||||||
else if (selectedLink.Valid(_ecm)) | ||||||
{ | ||||||
parentModel = *selectedLink.ParentModel(_ecm); | ||||||
} | ||||||
else | ||||||
{ | ||||||
this->dataPtr->selectedEntity.reset(); | ||||||
return; | ||||||
} | ||||||
|
||||||
this->dataPtr->modelName = QString::fromStdString( | ||||||
parentModel.Name(_ecm)); | ||||||
this->dataPtr->selectedEntity = selectedLink.Entity(); | ||||||
|
||||||
// Put all of the model's links into the list | ||||||
auto links = parentModel.Links(_ecm); | ||||||
unsigned int i{0}; | ||||||
while (i < links.size()) | ||||||
{ | ||||||
Link link(links[i]); | ||||||
this->dataPtr->linkNameList.push_back( | ||||||
QString::fromStdString(*link.Name(_ecm))); | ||||||
if (link.Entity() == this->dataPtr->selectedEntity) | ||||||
{ | ||||||
this->dataPtr->linkIndex = i; | ||||||
} | ||||||
++i; | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
if (this->dataPtr->changedIndex) | ||||||
{ | ||||||
this->dataPtr->changedIndex = false; | ||||||
|
||||||
if (this->dataPtr->selectedEntity.has_value()) | ||||||
{ | ||||||
auto parentModel = Link(*this->dataPtr->selectedEntity).ParentModel(_ecm); | ||||||
std::string linkName = | ||||||
this->dataPtr->linkNameList[this->dataPtr->linkIndex].toStdString(); | ||||||
this->dataPtr->selectedEntity = parentModel->LinkByName(_ecm, linkName); | ||||||
} | ||||||
} | ||||||
|
||||||
// Get the position of the center of mass and link orientation | ||||||
if (this->dataPtr->selectedEntity.has_value()) | ||||||
{ | ||||||
auto linkWorldPose = worldPose(*this->dataPtr->selectedEntity, _ecm); | ||||||
auto inertial = _ecm.Component<components::Inertial>( | ||||||
*this->dataPtr->selectedEntity); | ||||||
if (inertial) | ||||||
{ | ||||||
this->dataPtr->inertialPos = | ||||||
linkWorldPose.Rot().RotateVector(inertial->Data().Pose().Pos()); | ||||||
this->dataPtr->linkRot = linkWorldPose.Rot(); | ||||||
} | ||||||
} | ||||||
|
||||||
emit this->ModelNameChanged(); | ||||||
emit this->LinkNameListChanged(); | ||||||
emit this->LinkIndexChanged(); | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
QString ApplyForceTorque::ModelName() const | ||||||
{ | ||||||
return this->dataPtr->modelName; | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
QStringList ApplyForceTorque::LinkNameList() const | ||||||
{ | ||||||
return this->dataPtr->linkNameList; | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
int ApplyForceTorque::LinkIndex() const | ||||||
{ | ||||||
return this->dataPtr->linkIndex; | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
void ApplyForceTorque::SetLinkIndex(int _linkIndex) | ||||||
{ | ||||||
this->dataPtr->linkIndex = _linkIndex; | ||||||
this->dataPtr->changedIndex = true; | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
void ApplyForceTorque::UpdateForce(double _x, double _y, double _z) | ||||||
{ | ||||||
this->dataPtr->force = {_x, _y, _z}; | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
void ApplyForceTorque::UpdateTorque(double _x, double _y, double _z) | ||||||
{ | ||||||
this->dataPtr->torque = {_x, _y, _z}; | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
void ApplyForceTorque::ApplyForce() | ||||||
{ | ||||||
this->dataPtr->PublishWrench(true, false); | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
void ApplyForceTorque::ApplyTorque() | ||||||
{ | ||||||
this->dataPtr->PublishWrench(false, true); | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
void ApplyForceTorque::ApplyAll() | ||||||
{ | ||||||
this->dataPtr->PublishWrench(true, true); | ||||||
} | ||||||
|
||||||
///////////////////////////////////////////////// | ||||||
void ApplyForceTorquePrivate::PublishWrench(bool _applyForce, bool _applyTorque) | ||||||
{ | ||||||
std::lock_guard<std::mutex> lock(this->mutex); | ||||||
|
||||||
if (!this->selectedEntity.has_value()) | ||||||
{ | ||||||
gzdbg << "No link selected" << std::endl; | ||||||
return; | ||||||
} | ||||||
|
||||||
// Force and torque in world coordinates | ||||||
math::Vector3d forceToApply = this->linkRot.RotateVector( | ||||||
_applyForce ? this->force : math::Vector3d::Zero); | ||||||
math::Vector3d torqueToApply = this->linkRot.RotateVector( | ||||||
_applyTorque ? this->torque : math::Vector3d::Zero) + | ||||||
this->inertialPos.Cross(forceToApply); | ||||||
|
||||||
msgs::EntityWrench msg; | ||||||
msg.mutable_entity()->set_id(*this->selectedEntity); | ||||||
msgs::Set(msg.mutable_wrench()->mutable_force(), forceToApply); | ||||||
msgs::Set(msg.mutable_wrench()->mutable_torque(), torqueToApply); | ||||||
|
||||||
this->pub.Publish(msg); | ||||||
} | ||||||
|
||||||
// Register this plugin | ||||||
GZ_ADD_PLUGIN(ApplyForceTorque, gz::gui::Plugin); | ||||||
azeey marked this conversation as resolved.
Show resolved
Hide resolved
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
alphabetize headers