From 8e533fd32b87414742f67c2d0441e24c9430539b Mon Sep 17 00:00:00 2001 From: Bryant Date: Fri, 21 Jun 2013 14:51:52 -0700 Subject: [PATCH] Vastly improved the joystick documentation, removed some redundant/unnecessary code, and fixed a couple of small bugs. This code should be ready for live testing. --- src/input/JoystickInput.cc | 4 +++- src/input/JoystickInput.h | 21 ++++++++++++++++++--- src/uas/UAS.cc | 23 +---------------------- src/uas/UAS.h | 2 -- src/uas/UASInterface.h | 4 +++- src/ui/JoystickAxis.h | 27 +++++++++++++++++++++++++++ src/ui/JoystickButton.h | 27 +++++++++++++++++++++++++++ src/ui/JoystickWidget.cc | 6 +++--- src/ui/JoystickWidget.h | 8 ++++---- 9 files changed, 86 insertions(+), 36 deletions(-) diff --git a/src/input/JoystickInput.cc b/src/input/JoystickInput.cc index 4903ae814..71ee4cbae 100644 --- a/src/input/JoystickInput.cc +++ b/src/input/JoystickInput.cc @@ -505,7 +505,6 @@ void JoystickInput::setActiveJoystick(int id) joystick = SDL_JoystickOpen(joystickID); if (joystick && SDL_JoystickOpened(joystickID)) { - SDL_JoystickUpdate(); // Update joystick configuration. joystickName = QString(SDL_JoystickName(joystickID)); joystickNumButtons = SDL_JoystickNumButtons(joystick); @@ -532,6 +531,9 @@ void JoystickInput::setActiveJoystick(int id) joystickNumAxes = 0; } + // Specify that a new joystick has been selected, so that any UI elements can update. + emit newJoystickSelected(); + // And then trigger an update of this new UI. emit joystickSettingsChanged(); } diff --git a/src/input/JoystickInput.h b/src/input/JoystickInput.h index 97c4263e2..66070990d 100644 --- a/src/input/JoystickInput.h +++ b/src/input/JoystickInput.h @@ -23,11 +23,20 @@ This file is part of the PIXHAWK project /** * @file - * @brief Definition of joystick interface + * @brief Definition of joystick interface * - * @author Lorenz Meier - * @author Andreas Romer + * This class defines a new thread to operate the reading of any joystick/controllers + * via the Simple Directmedia Library (libsdl.org). This relies on polling of the SDL, + * which is not their recommended method, instead they suggest use event checking. That + * does not seem to support switching joysticks after their internal event loop has started, + * so it was abandoned. * + * All joystick-related functionality is done in this class, though the JoystickWidget provides + * a UI around modifying its settings. Additionally controller buttons can be mapped to + * actions defined by any UASInterface object through the `UASInterface::getActions()` function. + * + * @author Lorenz Meier + * @author Andreas Romer */ #ifndef _JOYSTICKINPUT_H_ @@ -198,6 +207,9 @@ protected: quint16 joystickButtons; ///< The state of every button. Bitfield supporting 16 buttons with 1s indicating that the button is down. int xHat, yHat; ///< The horizontal/vertical hat directions. Values are -1, 0, 1, with (-1,-1) indicating bottom-left. + /** + * @brief Called before main run() event loop starts. Waits for joysticks to be connected. + */ void init(); signals: @@ -265,6 +277,9 @@ signals: /** @brief Signals that new joystick-specific settings were changed. Useful for triggering updates that at dependent on the current joystick. */ void joystickSettingsChanged(); + /** @brief The JoystickInput has switched to a different joystick. UI should be adjusted accordingly. */ + void newJoystickSelected(); + public slots: /** @brief Enable or disable emitting the high-level control signals from the joystick. */ void setEnabled(bool enable); diff --git a/src/uas/UAS.cc b/src/uas/UAS.cc index fef8bc4eb..5f925072e 100644 --- a/src/uas/UAS.cc +++ b/src/uas/UAS.cc @@ -284,7 +284,7 @@ int UAS::getUASID() const void UAS::triggerAction(int action) { - if (action > 0 && action < actions.size()) + if (action >= 0 && action < actions.size()) { qDebug() << "Triggering action: '" << actions[action]->text() << "'"; actions[action]->trigger(); @@ -2789,27 +2789,6 @@ int UAS::getSystemType() return this->type; } -/** -* @param buttonIndex -*/ -void UAS::receiveButton(int buttonIndex) -{ - switch (buttonIndex) - { - case 0: - - break; - case 1: - - break; - default: - - break; - } - // qDebug() << __FILE__ << __LINE__ << ": Received button clicked signal (button # is: " << buttonIndex << "), UNIMPLEMENTED IN MAVLINK!"; - -} - /** * Halt the uas. */ diff --git a/src/uas/UAS.h b/src/uas/UAS.h index 85b8b250d..a55d501e3 100644 --- a/src/uas/UAS.h +++ b/src/uas/UAS.h @@ -799,8 +799,6 @@ public slots: /** @brief Set the values for the manual control of the vehicle */ void setManualControlCommands(double roll, double pitch, double yaw, double thrust, int xHat, int yHat, int buttons); - /** @brief Receive a button pressed event from an input device, e.g. joystick */ - void receiveButton(int buttonIndex); /** @brief Set the values for the 6dof manual control of the vehicle */ void setManual6DOFControlCommands(double x, double y, double z, double roll, double pitch, double yaw); diff --git a/src/uas/UASInterface.h b/src/uas/UASInterface.h index eb90539a9..3e416ce57 100644 --- a/src/uas/UASInterface.h +++ b/src/uas/UASInterface.h @@ -261,7 +261,9 @@ public: } /** @brief Returns a list of actions/commands that this vehicle can perform. - * Used for creating UI elements for built-in functionality for this vehicle. + * Used for creating UI elements for built-in functionality for this vehicle. + * Actions should be mappings to `void f(void);` functions that simply issue + * a command to the vehicle. */ virtual QList getActions() const = 0; diff --git a/src/ui/JoystickAxis.h b/src/ui/JoystickAxis.h index 3fddd57d5..6bb94b349 100644 --- a/src/ui/JoystickAxis.h +++ b/src/ui/JoystickAxis.h @@ -1,3 +1,30 @@ +/*===================================================================== + +QGroundControl Open Source Ground Control Station + +This file is part of the QGROUNDCONTROL project + + QGROUNDCONTROL is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + QGROUNDCONTROL is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QGROUNDCONTROL. If not, see . + +======================================================================*/ + +/** + * @file + * This class defines a UI element to represent a single controller axis. + * It is used by the JoystickWidget to simplify some of the logic in that class. + */ + #ifndef JOYSTICKAXIS_H #define JOYSTICKAXIS_H diff --git a/src/ui/JoystickButton.h b/src/ui/JoystickButton.h index 6af5fe3a2..559f49201 100644 --- a/src/ui/JoystickButton.h +++ b/src/ui/JoystickButton.h @@ -1,3 +1,30 @@ +/*===================================================================== + +QGroundControl Open Source Ground Control Station + +This file is part of the QGROUNDCONTROL project + + QGROUNDCONTROL is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + QGROUNDCONTROL is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with QGROUNDCONTROL. If not, see . + +======================================================================*/ + +/** + * @file + * This class defines a UI element to represent a single controller axis. + * It is used by the JoystickWidget to simplify some of the logic in that class. + */ + #ifndef JOYSTICKBUTTON_H #define JOYSTICKBUTTON_H diff --git a/src/ui/JoystickWidget.cc b/src/ui/JoystickWidget.cc index d68f644bc..918ef537d 100644 --- a/src/ui/JoystickWidget.cc +++ b/src/ui/JoystickWidget.cc @@ -30,8 +30,10 @@ JoystickWidget::JoystickWidget(JoystickInput* joystick, QWidget *parent) : // Also watch for when new settings were loaded for the current joystick to do a mass UI refresh. connect(this->joystick, SIGNAL(joystickSettingsChanged()), this, SLOT(updateUI())); - // If the selected joystick is changed, update the joystick. + // If the selected joystick is changed, update the JoystickInput. connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this->joystick, SLOT(setActiveJoystick(int))); + // Also wait for the JoystickInput to switch, then update our UI. + connect(this->joystick, SIGNAL(newJoystickSelected()), this, SLOT(createUIForJoystick())); // Initialize the UI to the current JoystickInput state. Also make sure to listen for future changes // so that the UI can be updated. @@ -124,8 +126,6 @@ void JoystickWidget::updateUI() for (int i = 0; i < axes.size(); i++) { JoystickAxis* axis = axes[i]; - float value = joystick->getCurrentValueForAxis(i); - axis->setValue(value); JoystickInput::JOYSTICK_INPUT_MAPPING mapping = JoystickInput::JOYSTICK_INPUT_MAPPING_NONE; if (i == rollAxis) { diff --git a/src/ui/JoystickWidget.h b/src/ui/JoystickWidget.h index 578701fc9..c1eb1d51a 100644 --- a/src/ui/JoystickWidget.h +++ b/src/ui/JoystickWidget.h @@ -23,7 +23,7 @@ This file is part of the PIXHAWK project /** * @file - * @brief Definition of joystick widget + * @brief Definition of joystick widget. Provides a UI for configuring the joystick settings. * @author Lorenz Meier * */ @@ -76,8 +76,6 @@ public slots: void updateUI(); protected: - /** @brief Update the proper number of buttons for the current joystick. */ - void updateButtons(); /** @brief UI change event */ virtual void changeEvent(QEvent *e); JoystickInput* joystick; ///< Reference to the joystick @@ -90,7 +88,9 @@ protected: private: Ui::JoystickWidget *m_ui; - /** @brief Initialize all dynamic UI elements (button list, joystick names, etc.) */ + /** @brief Initialize all dynamic UI elements (button list, joystick names, etc.). + * Only done once at startup. + */ void initUI(); }; -- 2.22.0