From 860e47b6ace973054b8db84859d0c1e40b4ac4ea Mon Sep 17 00:00:00 2001 From: Bryant Date: Mon, 17 Jun 2013 13:30:24 -0700 Subject: [PATCH] Restored range limiting to the throttle channel. Now if the UAS can't reverse, the throttle can be set to only use the positive range of the axis (useful with auto-centering control sticks) or to use the full range (for sticks that hold position). --- src/input/JoystickInput.cc | 32 +++++++++++++++++++++++++++----- src/input/JoystickInput.h | 8 ++++++++ src/ui/JoystickAxis.cc | 25 +++++++++++++++++++++---- src/ui/JoystickAxis.h | 6 +++++- src/ui/JoystickAxis.ui | 16 ++++++++++++++++ src/ui/JoystickWidget.cc | 1 + 6 files changed, 78 insertions(+), 10 deletions(-) diff --git a/src/input/JoystickInput.cc b/src/input/JoystickInput.cc index f854b8671..81b929572 100644 --- a/src/input/JoystickInput.cc +++ b/src/input/JoystickInput.cc @@ -37,7 +37,8 @@ JoystickInput::JoystickInput() : throttleAxis(-1), joystickName(""), joystickID(-1), - joystickNumButtons(0) + joystickNumButtons(0), + throttleAxisLimited(false) { loadSettings(); @@ -191,6 +192,7 @@ void JoystickInput::run() { // First emit the uncalibrated values for each axis based on their ID. // This is generally not used for controlling a vehicle, but a UI representation, so it being slightly off is fine. + // Here we map the joystick axis value into the initial range of [0:1]. float axisValue = SDL_JoystickGetAxis(joystick, i); if (joystickAxesInverted[i]) { @@ -202,12 +204,20 @@ void JoystickInput::run() } axisValue = 1.0f - axisValue; - // Only map the throttle into [0:1] if the UAS can reverse. + // For non-throttle axes or if the UAS can reverse, go ahead and convert this into the range [-1:1]. if (uasCanReverse || throttleAxis != i) { axisValue = axisValue * 2.0f - 1.0f; - } else { - int a = 8; + } + // Otherwise if this vehicle can only go forward, but the axis is limited to only the positive range, + // scale this so the negative values are ignored for this axis and it's clamped to [0:1]. + else if (throttleAxis == i && joystickAxesLimited[i]) + { + axisValue = axisValue * 2.0f - 1.0f; + if (axisValue < 0.0f) + { + axisValue = 0.0f; + } } // Bound rounding errors @@ -217,6 +227,7 @@ void JoystickInput::run() { joystickAxes[i] = axisValue; emit axisValueChanged(i, axisValue); + qDebug() << "Axis " << i << ": " << axisValue; } } @@ -272,8 +283,8 @@ void JoystickInput::setActiveJoystick(int id) joystick = NULL; joystickID = -1; } -} + joystickID = id; joystick = SDL_JoystickOpen(joystickID); if (joystick && SDL_JoystickOpened(joystickID)) { @@ -286,6 +297,7 @@ void JoystickInput::setActiveJoystick(int id) // Update cached joystick values joystickAxes.clear(); joystickAxesInverted.clear(); + joystickAxesLimited.clear(); for (int i = 0; i < joystickNumAxes; i++) { int axisValue = SDL_JoystickGetAxis(joystick, i); @@ -293,6 +305,7 @@ void JoystickInput::setActiveJoystick(int id) emit axisValueChanged(i, axisValue); joystickAxesInverted.append(false); + joystickAxesLimited.append(false); } joystickButtons = 0; for (int i = 0; i < joystickNumButtons; i++) @@ -345,6 +358,7 @@ void JoystickInput::setAxisMapping(int axis, JOYSTICK_INPUT_MAPPING newMapping) if (throttleAxis == axis) { throttleAxis = -1; + joystickAxesLimited[axis] = false; } break; } @@ -358,6 +372,14 @@ void JoystickInput::setAxisInversion(int axis, bool inverted) } } +void JoystickInput::setAxisRangeLimit(int axis, bool limitRange) +{ + if (axis < joystickAxesLimited.size()) + { + joystickAxesLimited[axis] = limitRange; + } +} + float JoystickInput::getCurrentValueForAxis(int axis) { if (axis < joystickAxes.size()) diff --git a/src/input/JoystickInput.h b/src/input/JoystickInput.h index 9a0dc7d28..e12f0a6b9 100644 --- a/src/input/JoystickInput.h +++ b/src/input/JoystickInput.h @@ -159,6 +159,8 @@ protected: QList joystickAxes; ///< The values of every axes during the last sample. QList joystickAxesInverted; ///< Whether each axis should be used inverted from what was reported. + QList joystickAxesLimited; ///< Whether each axis should be limited to only the positive range. + bool throttleAxisLimited; ///< Indicates if the throttle channel should be limited to mapping from a 0:1 range instead of -1:1. Useful for controlling throttle with an axis that autocenters. 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. @@ -233,6 +235,12 @@ public slots: * @param inverted True indicates inverted from normal. Varies by controller. */ void setAxisInversion(int axis, bool inverted); + /** + * @brief Specify that an axis should only transmit the positive values. Useful for controlling throttle from auto-centering axes. + * @param axis Which axis has its range limited. + * @param limitRange If true only the positive half of this axis will be read. + */ + void setAxisRangeLimit(int axis, bool limitRange); }; #endif // _JOYSTICKINPUT_H_ diff --git a/src/ui/JoystickAxis.cc b/src/ui/JoystickAxis.cc index 526d3f130..d90aa2699 100644 --- a/src/ui/JoystickAxis.cc +++ b/src/ui/JoystickAxis.cc @@ -1,10 +1,7 @@ #include "JoystickAxis.h" #include "JoystickInput.h" #include "ui_JoystickAxis.h" -<<<<<<< HEAD #include "UASManager.h" -======= ->>>>>>> 64d0741ee82db3d5dac2108f3f6d03c6fa66e5c6 #include JoystickAxis::JoystickAxis(int id, QWidget *parent) : @@ -13,9 +10,11 @@ JoystickAxis::JoystickAxis(int id, QWidget *parent) : ui(new Ui::JoystickAxis) { ui->setupUi(this); + mappingComboBoxChanged(JoystickInput::JOYSTICK_INPUT_MAPPING_NONE); ui->label->setText(QString::number(id)); connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(mappingComboBoxChanged(int))); connect(ui->invertedCheckBox, SIGNAL(clicked(bool)), this, SLOT(inversionCheckBoxChanged(bool))); + connect(ui->rangeCheckBox, SIGNAL(clicked(bool)), this, SLOT(rangeCheckBoxChanged(bool))); } JoystickAxis::~JoystickAxis() @@ -30,6 +29,14 @@ void JoystickAxis::setValue(float value) void JoystickAxis::mappingComboBoxChanged(int newMapping) { + if (newMapping == JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE) + { + ui->rangeCheckBox->show(); + } + else + { + ui->rangeCheckBox->hide(); + } emit mappingChanged(id, (JoystickInput::JOYSTICK_INPUT_MAPPING)newMapping); this->setActiveUAS(UASManager::instance()->getActiveUAS()); } @@ -39,14 +46,24 @@ void JoystickAxis::inversionCheckBoxChanged(bool inverted) emit inversionChanged(id, inverted); } +void JoystickAxis::rangeCheckBoxChanged(bool limited) +{ + emit rangeChanged(id, limited); +} + void JoystickAxis::setActiveUAS(UASInterface* uas) { - if (uas && !uas->systemCanReverse() && ui->comboBox->currentIndex() == JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE) + // Set the throttle display to only positive if: + // * This is the throttle axis AND + // * The current UAS can't reverse OR there is no current UAS + if (((uas && !uas->systemCanReverse()) || !uas) && ui->comboBox->currentIndex() == JoystickInput::JOYSTICK_INPUT_MAPPING_THROTTLE) { ui->progressBar->setRange(0, 100); + ui->rangeCheckBox->show(); } else { ui->progressBar->setRange(-100, 100); + ui->rangeCheckBox->hide(); } } diff --git a/src/ui/JoystickAxis.h b/src/ui/JoystickAxis.h index a24e42fe4..28a1dc54b 100644 --- a/src/ui/JoystickAxis.h +++ b/src/ui/JoystickAxis.h @@ -20,7 +20,9 @@ signals: /** @brief Signal a change in this axis' yaw/pitch/roll mapping */ void mappingChanged(int id, JoystickInput::JOYSTICK_INPUT_MAPPING newMapping); /** @brief Signal a change in this axis' inversion status */ - void inversionChanged(int id, bool); + void inversionChanged(int id, bool inversion); + /** @brief Signal a change in this axis' range setting. If limited is true then only the positive values should be read from this axis. */ + void rangeChanged(int id, bool limited); public slots: /** @brief Update the displayed value of the included progressbar. @@ -39,6 +41,8 @@ private slots: void mappingComboBoxChanged(int newMapping); /** @brief Emit signal when the inversion checkbox is changed. */ void inversionCheckBoxChanged(bool inverted); + /** @brief Emit signal when the range checkbox is changed. */ + void rangeCheckBoxChanged(bool inverted); }; #endif // JOYSTICKAXIS_H diff --git a/src/ui/JoystickAxis.ui b/src/ui/JoystickAxis.ui index 0299b07ce..212133c50 100644 --- a/src/ui/JoystickAxis.ui +++ b/src/ui/JoystickAxis.ui @@ -59,6 +59,9 @@ 0 + + Specify what property of the UAS this axis should command + -- @@ -86,6 +89,13 @@ + + + + Half range + + + @@ -103,6 +113,9 @@ 100 + + Only use the positive values from this axis for control + -100 @@ -140,6 +153,9 @@ 25 + + Reverse the values for this axis + Inverted diff --git a/src/ui/JoystickWidget.cc b/src/ui/JoystickWidget.cc index 3e0dfe7bb..9681611d6 100644 --- a/src/ui/JoystickWidget.cc +++ b/src/ui/JoystickWidget.cc @@ -144,6 +144,7 @@ void JoystickWidget::updateUIForJoystick(int id) axis->setValue(joystick->getCurrentValueForAxis(i)); connect(axis, SIGNAL(mappingChanged(int,JoystickInput::JOYSTICK_INPUT_MAPPING)), this->joystick, SLOT(setAxisMapping(int,JoystickInput::JOYSTICK_INPUT_MAPPING))); connect(axis, SIGNAL(inversionChanged(int,bool)), this->joystick, SLOT(setAxisInversion(int,bool))); + connect(axis, SIGNAL(rangeChanged(int,bool)), this->joystick, SLOT(setAxisRangeLimit(int,bool))); connect(UASManager::instance(), SIGNAL(activeUASSet(UASInterface*)), axis, SLOT(setActiveUAS(UASInterface*))); m_ui->axesLayout->addWidget(axis); axes.append(axis); -- 2.22.0