Skip to content

Commit

Permalink
Configurable stiffnesses in MouseDrag (#2057)
Browse files Browse the repository at this point in the history
Makes the rotation and translation stiffnesses in the MouseDrag plugin configurable.

This can be done either through the <rotation_stiffness> and <position_stiffness> elements on the SDF file or through fields on the interface.
---------

Signed-off-by: Henrique-BO <[email protected]>
  • Loading branch information
Henrique-BO authored Aug 21, 2023
1 parent 900a6ea commit 3432dcb
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 9 deletions.
46 changes: 43 additions & 3 deletions src/gui/plugins/mouse_drag/MouseDrag.cc
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ namespace sim
public: MouseDragMode mode = MouseDragMode::NONE;

/// \brief Link to which the wrenches are applied
public: Entity linkId;
public: Entity linkId{kNullEntity};

/// \brief Plane of force application
public: math::Planed plane;
Expand Down Expand Up @@ -237,7 +237,7 @@ MouseDrag::MouseDrag()
MouseDrag::~MouseDrag() = default;

/////////////////////////////////////////////////
void MouseDrag::LoadConfig(const tinyxml2::XMLElement */*_pluginElem*/)
void MouseDrag::LoadConfig(const tinyxml2::XMLElement *_pluginElem)
{
if (this->title.empty())
this->title = "Mouse drag";
Expand All @@ -249,7 +249,7 @@ void MouseDrag::LoadConfig(const tinyxml2::XMLElement */*_pluginElem*/)
this->dataPtr->worldName = worldNames[0].toStdString();
const auto topic = transport::TopicUtils::AsValidTopic(
"/world/" + this->dataPtr->worldName + "/wrench");
if (topic == "")
if (topic.empty())
{
gzerr << "Unable to create publisher" << std::endl;
return;
Expand All @@ -264,6 +264,22 @@ void MouseDrag::LoadConfig(const tinyxml2::XMLElement */*_pluginElem*/)
return;
}

// Read configuration
if (_pluginElem)
{
if (auto elem = _pluginElem->FirstChildElement("rotation_stiffness"))
{
elem->QueryDoubleText(&this->dataPtr->rotStiffness);
emit this->RotStiffnessChanged();
}

if (auto elem = _pluginElem->FirstChildElement("position_stiffness"))
{
elem->QueryDoubleText(&this->dataPtr->posStiffness);
emit this->PosStiffnessChanged();
}
}

gz::gui::App()->findChild<gz::gui::MainWindow *>
()->installEventFilter(this);
}
Expand Down Expand Up @@ -520,6 +536,30 @@ void MouseDrag::OnSwitchCOM(const bool _checked)
this->dataPtr->applyCOM = _checked;
}

/////////////////////////////////////////////////
double MouseDrag::RotStiffness() const
{
return this->dataPtr->rotStiffness;
}

/////////////////////////////////////////////////
void MouseDrag::SetRotStiffness(double _rotStiffness)
{
this->dataPtr->rotStiffness = _rotStiffness;
}

/////////////////////////////////////////////////
double MouseDrag::PosStiffness() const
{
return this->dataPtr->posStiffness;
}

/////////////////////////////////////////////////
void MouseDrag::SetPosStiffness(double _posStiffness)
{
this->dataPtr->posStiffness = _posStiffness;
}

/////////////////////////////////////////////////
void MouseDragPrivate::OnRender()
{
Expand Down
42 changes: 41 additions & 1 deletion src/gui/plugins/mouse_drag/MouseDrag.hh
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,29 @@ namespace sim
/// Automatically loads the ApplyLinkWrench system.
///
/// ## Configuration
/// This plugin doesn't accept any custom configuration.
///
/// * \<rotation_stiffness\> : Stiffness of rotation mode, defaults to 100
/// * \<position_stiffness\> : Stiffness of translation mode, defaults to 100
class MouseDrag : public gz::sim::GuiSystem
{
Q_OBJECT

/// \brief Stiffness of rotation mode
Q_PROPERTY(
double rotStiffness
READ RotStiffness
WRITE SetRotStiffness
NOTIFY RotStiffnessChanged
)

/// \brief Stiffness of translation mode
Q_PROPERTY(
double posStiffness
READ PosStiffness
WRITE SetPosStiffness
NOTIFY PosStiffnessChanged
)

/// \brief Constructor
public: MouseDrag();

Expand All @@ -57,6 +75,28 @@ namespace sim
/// \param[in] _checked True if force should be applied to center of mass
public slots: void OnSwitchCOM(const bool _checked);

/// \brief Get the rotational stiffness
/// \return The rotational stiffness
public: Q_INVOKABLE double RotStiffness() const;

/// \brief Notify that the rotational stiffness changed
signals: void RotStiffnessChanged();

/// \brief Set the rotational stiffness
/// \param[in] _rotStiffness The new rotational stiffness
public: Q_INVOKABLE void SetRotStiffness(double _rotStiffness);

/// \brief Get the translational stiffness
/// \return The translational stiffness
public: Q_INVOKABLE double PosStiffness() const;

/// \brief Notify that the translational stiffness changed
signals: void PosStiffnessChanged();

/// \brief Set the translational stiffness
/// \param[in] _posStiffness The new translational stiffness
public: Q_INVOKABLE void SetPosStiffness(double _posStiffness);

/// \internal
/// \brief Pointer to private data.
private: std::unique_ptr<MouseDragPrivate> dataPtr;
Expand Down
55 changes: 50 additions & 5 deletions src/gui/plugins/mouse_drag/MouseDrag.qml
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,68 @@ GridLayout {
columns: 8
columnSpacing: 10
Layout.minimumWidth: 350
Layout.minimumHeight: 200
Layout.minimumHeight: 300
anchors.fill: parent
anchors.leftMargin: 10
anchors.rightMargin: 10

// Maximum value for GzSpinBoxes
property int maxValue: 1000000

// Precision of the GzSpinBoxes in decimal places
property int decimalPlaces: 1

// Step size of the GzSpinBoxes
property double step: 10.0

Text {
Layout.columnSpan: 8
id: rotationText
color: "dimgrey"
text: qsTr("Ctrl+Left-Click: rotation")
text: qsTr("Rotation (Ctrl+Left-Click)")
}

Label {
Layout.columnSpan: 2
horizontalAlignment: Text.AlignRight
id: rotStiffText
text: qsTr("Rotation stiffness")
}

GzSpinBox {
Layout.columnSpan: 6
Layout.fillWidth: true
id: rotStiffness
maximumValue: maxValue
minimumValue: 0
value: MouseDrag.rotStiffness
decimals: decimalPlaces
stepSize: step
onValueChanged: MouseDrag.rotStiffness = rotStiffness.value
}

Text {
Layout.columnSpan: 8
id: translationText
color: "dimgrey"
text: qsTr("Ctrl+Right-Click: translation")
text: qsTr("Translation (Ctrl+Right-Click)")
}

Label {
Layout.columnSpan: 2
horizontalAlignment: Text.AlignRight
id: posStiffText
text: qsTr("Position stiffness")
}

GzSpinBox {
Layout.columnSpan: 6
Layout.fillWidth: true
id: posStiffness
maximumValue: maxValue
minimumValue: 0
value: MouseDrag.posStiffness
decimals: decimalPlaces
stepSize: step
onValueChanged: MouseDrag.posStiffness = posStiffness.value
}

Switch {
Expand Down

0 comments on commit 3432dcb

Please sign in to comment.