diff --git a/src/plugins/teleop/CMakeLists.txt b/src/plugins/teleop/CMakeLists.txt index 7186223a4..c0c136a25 100644 --- a/src/plugins/teleop/CMakeLists.txt +++ b/src/plugins/teleop/CMakeLists.txt @@ -5,4 +5,4 @@ ign_gui_add_plugin(Teleop Teleop.hh TEST_SOURCES # todo # Teleop_TEST.cc -) \ No newline at end of file +) diff --git a/src/plugins/teleop/Teleop.cc b/src/plugins/teleop/Teleop.cc index c8beffc03..39905ce23 100644 --- a/src/plugins/teleop/Teleop.cc +++ b/src/plugins/teleop/Teleop.cc @@ -31,9 +31,6 @@ #include "Teleop.hh" -/// \brief Publisher -ignition::transport::Node::Publisher cmdVelPub; - namespace ignition { namespace gui @@ -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"; }; @@ -56,9 +53,6 @@ using namespace gui; ///////////////////////////////////////////////// Teleop::Teleop(): Plugin(), dataPtr(new TeleopPrivate) { - // Advertise publisher node - cmdVelPub = this->dataPtr->node.Advertise - (this->dataPtr->topic); } ///////////////////////////////////////////////// @@ -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); } ///////////////////////////////////////////////// @@ -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, diff --git a/src/plugins/teleop/Teleop.hh b/src/plugins/teleop/Teleop.hh index ec15d3ddf..3041148d6 100644 --- a/src/plugins/teleop/Teleop.hh +++ b/src/plugins/teleop/Teleop.hh @@ -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 dataPtr; + + /// \brief Publisher + ignition::transport::Node::Publisher cmdVelPub; + + float linearVel; + float angularVel; }; } } diff --git a/src/plugins/teleop/Teleop.qml b/src/plugins/teleop/Teleop.qml index bddd776f3..a359e6118 100644 --- a/src/plugins/teleop/Teleop.qml +++ b/src/plugins/teleop/Teleop.qml @@ -23,7 +23,7 @@ import QtQuick.Layouts 1.3 Rectangle { Layout.minimumWidth: 250 - Layout.minimumHeight: 200 + Layout.minimumHeight: 400 anchors.fill: parent Label { @@ -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 @@ -66,7 +114,7 @@ Rectangle { Layout.row: 0 Layout.column: 1 onClicked: { - Teleop.OnForwardButton() + Teleop.OnDirectionButton(1,0) } Material.background: Material.primary style: ButtonStyle { @@ -89,7 +137,7 @@ Rectangle { Layout.row: 1 Layout.column: 0 onClicked: { - Teleop.OnLeftButton() + Teleop.OnDirectionButton(0, 1) } Material.background: Material.primary style: ButtonStyle { @@ -112,7 +160,7 @@ Rectangle { Layout.row: 1 Layout.column: 2 onClicked: { - Teleop.OnRightButton() + Teleop.OnDirectionButton(0,-1) } Material.background: Material.primary style: ButtonStyle { @@ -135,7 +183,7 @@ Rectangle { Layout.row: 2 Layout.column: 1 onClicked: { - Teleop.OnBackwardButton() + Teleop.OnDirectionButton(-1, 0) } Material.background: Material.primary style: ButtonStyle { @@ -151,4 +199,5 @@ Rectangle { } } } + }