Skip to content

Commit

Permalink
Added functionality to the buttons.
Browse files Browse the repository at this point in the history
Signed-off-by: LolaSegura <lsegura@ekumenlabs.com>
  • Loading branch information
LolaSegura committed Jun 11, 2021
1 parent 9c88a4e commit 31c4a4b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/plugins/teleop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ ign_gui_add_plugin(Teleop
Teleop.hh
TEST_SOURCES # todo
# Teleop_TEST.cc
)
)
34 changes: 22 additions & 12 deletions src/plugins/teleop/Teleop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@

#include "Teleop.hh"

/// \brief Publisher
ignition::transport::Node::Publisher cmdVelPub;

namespace ignition
{
namespace gui
Expand All @@ -43,7 +40,7 @@ namespace gui
/// \brief Node for communication
public: ignition::transport::Node node;

/// \brief Topic
/// \brief Topic. Set '/cmd_vel' as default.
public: std::string topic = "/cmd_vel";

};
Expand All @@ -56,9 +53,6 @@ using namespace gui;
/////////////////////////////////////////////////
Teleop::Teleop(): Plugin(), dataPtr(new TeleopPrivate)
{
// Advertise publisher node
cmdVelPub = this->dataPtr->node.Advertise<ignition::msgs::Twist>
(this->dataPtr->topic);
}

/////////////////////////////////////////////////
Expand All @@ -77,15 +71,16 @@ void Teleop::LoadConfig(const tinyxml2::XMLElement *)
}

/////////////////////////////////////////////////
void Teleop::OnForwardButton()
void Teleop::OnDirectionButton(int _linearDirection, int _angularDirection)
{
ignmsg << "[OnForwardButton]: Forward pressed " << std::endl;
ignition::msgs::Twist cmdVelMsg;
cmdVelMsg.mutable_linear()->set_x(1.0);
cmdVelMsg.mutable_angular()->set_z(1.0);

cmdVelPub.Publish(cmdVelMsg);
cmdVelMsg.mutable_linear()->set_x(
_linearDirection*this->linearVel);
cmdVelMsg.mutable_angular()->set_z(
_angularDirection*this->angularVel);

cmdVelPub.Publish(cmdVelMsg);
}

/////////////////////////////////////////////////
Expand All @@ -97,6 +92,21 @@ void Teleop::OnTopicSelection(const QString& _topic)
(this->dataPtr->topic);
}

/////////////////////////////////////////////////
void Teleop::OnLinearVelSelection(const QString& _velocity)
{
this->linearVel = _velocity.toDouble();
ignmsg << "[OnlinearVelSelection]: linear velocity: "
<< linearVel << std::endl;
}

/////////////////////////////////////////////////
void Teleop::OnAngularVelSelection(const QString& _velocity)
{
this->angularVel = _velocity.toDouble();
ignmsg << "[OnlinearVelSelection]: angular velocity: "
<< angularVel << std::endl;
}

// Register this plugin
IGNITION_ADD_PLUGIN(ignition::gui::Teleop,
Expand Down
26 changes: 24 additions & 2 deletions src/plugins/teleop/Teleop.hh
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,36 @@ namespace gui
// Documentation inherited
public: virtual void LoadConfig(const tinyxml2::XMLElement *) override;

//
public slots: void OnForwardButton();
/// \brief Callback in Qt thread when the direction of the movement changes.
/// \param[in] _linearDirection variable to indicate if the robot its going
/// forward or backward.
/// \param[in] _angularDirection variable to indicate if the robot its
/// turning left or right.
public slots: void OnDirectionButton(
int _linearDirection, int _angularDirection);

/// \brief Callback in Qt thread when the topic changes.
/// \param[in] _topic variable to indicate the topic in which to
/// publish the Twist commands.
public slots: void OnTopicSelection(const QString& _topic);

/// \brief Callback in Qt thread when the linear velocity changes.
/// \param[in] _velocity variable to indicate the linear velocity.
public slots: void OnLinearVelSelection(const QString& _velocity);

/// \brief Callback in Qt thread when the linear velocity changes.
/// \param[in] _velocity variable to indicate the angular velocity.
public slots: void OnAngularVelSelection(const QString& _velocity);

/// \internal
/// \brief Pointer to private data.
private: std::unique_ptr<TeleopPrivate> dataPtr;

/// \brief Publisher
ignition::transport::Node::Publisher cmdVelPub;

float linearVel;
float angularVel;
};
}
}
Expand Down
61 changes: 55 additions & 6 deletions src/plugins/teleop/Teleop.qml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import QtQuick.Layouts 1.3

Rectangle {
Layout.minimumWidth: 250
Layout.minimumHeight: 200
Layout.minimumHeight: 400
anchors.fill: parent

Label {
Expand All @@ -50,9 +50,57 @@ Rectangle {
}
}

Label {
id: linearVelLabel
text: "Linear:"
anchors.top: topicField.bottom
anchors.topMargin: 10
anchors.left: parent.left
anchors.leftMargin: 5
}
TextField {
id: linearVelField
anchors.top: topicField.bottom
anchors.topMargin: 5
anchors.left: linearVelLabel.right//ambiar a linear label
anchors.leftMargin: 5
anchors.right: buttonsGrid.right
Layout.fillWidth: true
text: "0.0"

placeholderText: qsTr("Linear velocity...")
onTextChanged: {
Teleop.OnLinearVelSelection(text)
}
}

Label {
id: angularVelLabel
text: "Angular:"
anchors.top: linearVelField.bottom
anchors.topMargin: 10
anchors.left: parent.left
anchors.leftMargin: 5
}
TextField {
id: angularVelField
anchors.top: linearVelField.bottom
anchors.topMargin: 5
anchors.left: angularVelLabel.right
anchors.leftMargin: 5
anchors.right: buttonsGrid.right
Layout.fillWidth: true
text: "0.0"

placeholderText: qsTr("Angular velocity...")
onTextChanged: {
Teleop.OnAngularVelSelection(text)
}
}

GridLayout {
id: buttonsGrid
anchors.top: topicField.bottom
anchors.top: angularVelField.bottom
anchors.topMargin: 15
anchors.left: parent.left
anchors.leftMargin: 5
Expand All @@ -66,7 +114,7 @@ Rectangle {
Layout.row: 0
Layout.column: 1
onClicked: {
Teleop.OnForwardButton()
Teleop.OnDirectionButton(1,0)
}
Material.background: Material.primary
style: ButtonStyle {
Expand All @@ -89,7 +137,7 @@ Rectangle {
Layout.row: 1
Layout.column: 0
onClicked: {
Teleop.OnLeftButton()
Teleop.OnDirectionButton(0, 1)
}
Material.background: Material.primary
style: ButtonStyle {
Expand All @@ -112,7 +160,7 @@ Rectangle {
Layout.row: 1
Layout.column: 2
onClicked: {
Teleop.OnRightButton()
Teleop.OnDirectionButton(0,-1)
}
Material.background: Material.primary
style: ButtonStyle {
Expand All @@ -135,7 +183,7 @@ Rectangle {
Layout.row: 2
Layout.column: 1
onClicked: {
Teleop.OnBackwardButton()
Teleop.OnDirectionButton(-1, 0)
}
Material.background: Material.primary
style: ButtonStyle {
Expand All @@ -151,4 +199,5 @@ Rectangle {
}
}
}

}

0 comments on commit 31c4a4b

Please sign in to comment.