From e0bb0b9c78d9854aac6203e12512f03d6b920f06 Mon Sep 17 00:00:00 2001 From: C-Glick <colton.glick@gmail.com> Date: Mon, 21 Mar 2022 23:37:32 -0500 Subject: [PATCH] Added joystick scaling to gamepad config tab --- .../gui/MicroCART/gamepadmonitor.cpp | 49 ++- groundStation/gui/MicroCART/gamepadmonitor.h | 16 + groundStation/gui/MicroCART/mainwindow.cpp | 71 ++++- groundStation/gui/MicroCART/mainwindow.h | 8 + groundStation/gui/MicroCART/mainwindow.ui | 295 ++++++++++++++++-- 5 files changed, 405 insertions(+), 34 deletions(-) diff --git a/groundStation/gui/MicroCART/gamepadmonitor.cpp b/groundStation/gui/MicroCART/gamepadmonitor.cpp index 78156dcd6..486e2d929 100644 --- a/groundStation/gui/MicroCART/gamepadmonitor.cpp +++ b/groundStation/gui/MicroCART/gamepadmonitor.cpp @@ -5,10 +5,10 @@ GamepadMonitor::GamepadMonitor(QObject *parent) : QObject(parent) { m_gamepadManager = QGamepadManager::instance(); - rollScale = 90; //-20 to 20 deg/s - pitchScale = -90; - yawScale = 40; - thrustScale = 30000; //0 to 60,000 thrust + rollScale = DEFAULT_ROLL_SCALE; + pitchScale = DEFAULT_PITCH_SCALE; + yawScale = DEFAULT_YAW_SCALE; + thrustScale = DEFAULT_THRUST_SCALE; connect(m_gamepadManager, SIGNAL(gamepadConnected(int)), this, SLOT(gamepadConnectedHandler(int))); connect(m_gamepadManager, SIGNAL(gamepadDisconnected(int)), this, SLOT(gamepadDisconnectedHandler(int))); @@ -41,6 +41,12 @@ bool GamepadMonitor::isGamepadConnected(){ void GamepadMonitor::resetConfig(){ qInfo() << "gamepad config reset"; + + rollScale = DEFAULT_ROLL_SCALE; + pitchScale = DEFAULT_PITCH_SCALE; + yawScale = DEFAULT_YAW_SCALE; + thrustScale = DEFAULT_THRUST_SCALE; + m_gamepadManager->resetConfiguration(m_gamepad->deviceId()); } @@ -73,7 +79,40 @@ float GamepadMonitor::getPitch(){ } float GamepadMonitor::getThrust(){ - return (float) (m_gamepad->axisLeftY() + 1) * thrustScale; + //first scale range (-1, 1) to (-scale/2 , scale/2) + //then shift range to (0, scale) + //multiplying by range first allows for inverting input + return (float) (m_gamepad->axisLeftY() * thrustScale/2 ) + (std::abs(thrustScale)/2) ; +} + +void GamepadMonitor::setRollScale(float scale){ + rollScale = scale; +} + +void GamepadMonitor::setYawScale(float scale){ + yawScale = scale; +} + +void GamepadMonitor::setPitchScale(float scale){ + pitchScale = scale; +} + +void GamepadMonitor::setThrustScale(float scale){ + thrustScale = scale; +} + + +float GamepadMonitor::getRollScale(){ + return rollScale; +} +float GamepadMonitor::getPitchScale(){ + return pitchScale; +} +float GamepadMonitor::getYawScale(){ + return yawScale; +} +float GamepadMonitor::getThrustScale(){ + return thrustScale; } GamepadMonitor::~GamepadMonitor(){ diff --git a/groundStation/gui/MicroCART/gamepadmonitor.h b/groundStation/gui/MicroCART/gamepadmonitor.h index 5b3ef05cd..dfb25f101 100644 --- a/groundStation/gui/MicroCART/gamepadmonitor.h +++ b/groundStation/gui/MicroCART/gamepadmonitor.h @@ -5,6 +5,12 @@ #include <QtGamepad/QGamepad> +#define DEFAULT_ROLL_SCALE 30 //deg +#define DEFAULT_PITCH_SCALE 30 //deg +#define DEFAULT_YAW_SCALE 45 //deg / s +#define DEFAULT_THRUST_SCALE 60000 + + class GamepadMonitor : public QObject { Q_OBJECT @@ -21,6 +27,16 @@ public: void configureAxis(QString axisName); void resetConfig(); + void setRollScale(float scale); + void setPitchScale(float scale); + void setYawScale(float scale); + void setThrustScale(float scale); + + float getRollScale(); + float getPitchScale(); + float getYawScale(); + float getThrustScale(); + private: QGamepad *m_gamepad; diff --git a/groundStation/gui/MicroCART/mainwindow.cpp b/groundStation/gui/MicroCART/mainwindow.cpp index 773e7291f..7835d8577 100644 --- a/groundStation/gui/MicroCART/mainwindow.cpp +++ b/groundStation/gui/MicroCART/mainwindow.cpp @@ -198,6 +198,13 @@ MainWindow::MainWindow(QWidget *parent) : connect(pollGamepadTimer, SIGNAL(timeout()), this, SLOT(updateGamepad())); pollGamepadTimer->start(50); + //gamepad configure tab setup + ui->gamepadRollScale->setValue((int)gamepadMonitor->getRollScale()); + ui->gamepadPitchScale->setValue((int)gamepadMonitor->getPitchScale()); + ui->gamepadYawScale->setValue((int)gamepadMonitor->getYawScale()); + ui->gamepadThrustScale->setValue((int)gamepadMonitor->getThrustScale()); + + /* Populate scripts list */ QDir scriptsDir("scripts/"); @@ -800,12 +807,16 @@ void MainWindow::on_tActual_sliderMoved(int position) /******** Gamepad handlers ********/ void MainWindow::onGamepadDisconnect(){ + crazyflieWorker->setCurrAttRateSetpoint(0,0,0,0); + //navigation tab ui->pitchSetpointBox->setText("0"); ui->rollSetpointBox->setText("0"); ui->yawSetpointBox->setText("0"); ui->tActual->setValue(0); + ui->rbManualSetpoint->setChecked(true); + //gamepad tab ui->noGamepadWarning->show(); ui->rbGamepadControl->setEnabled(false); ui->pb_configPitch->setEnabled(false); @@ -813,17 +824,42 @@ void MainWindow::onGamepadDisconnect(){ ui->pb_configYaw->setEnabled(false); ui->pb_configThrust->setEnabled(false); ui->pb_resetConfig->setEnabled(false); - ui->rbManualSetpoint->setChecked(true); + ui->gamepadRollScale->setEnabled(false); + ui->rollScaleLabel->setEnabled(false); + ui->gamepadPitchScale->setEnabled(false); + ui->pitchScaleLabel->setEnabled(false); + ui->gamepadYawScale->setEnabled(false); + ui->yawScaleLabel->setEnabled(false); + ui->gamepadThrustScale->setEnabled(false); + ui->thrustScaleLabel->setEnabled(false); + ui->thrustScaleNote->setEnabled(false); + + + } void MainWindow::onGamepadConnect(){ - ui->noGamepadWarning->hide(); + + //navigation tab ui->rbGamepadControl->setEnabled(true); + + //gamepad tab + ui->noGamepadWarning->hide(); ui->pb_configPitch->setEnabled(true); ui->pb_configRoll->setEnabled(true); ui->pb_configYaw->setEnabled(true); ui->pb_configThrust->setEnabled(true); ui->pb_resetConfig->setEnabled(true); + + ui->gamepadRollScale->setEnabled(true); + ui->rollScaleLabel->setEnabled(true); + ui->gamepadPitchScale->setEnabled(true); + ui->pitchScaleLabel->setEnabled(true); + ui->gamepadYawScale->setEnabled(true); + ui->yawScaleLabel->setEnabled(true); + ui->gamepadThrustScale->setEnabled(true); + ui->thrustScaleLabel->setEnabled(true); + ui->thrustScaleNote->setEnabled(true); } void MainWindow::updateGamepad(){ @@ -855,6 +891,10 @@ void MainWindow::updateGamepad(){ void MainWindow::on_pb_resetConfig_clicked() { gamepadMonitor->resetConfig(); + ui->gamepadRollScale->setValue((int)gamepadMonitor->getRollScale()); + ui->gamepadPitchScale->setValue((int)gamepadMonitor->getPitchScale()); + ui->gamepadYawScale->setValue((int)gamepadMonitor->getYawScale()); + ui->gamepadThrustScale->setValue((int)gamepadMonitor->getThrustScale()); } void MainWindow::on_pb_configRoll_clicked() @@ -926,3 +966,30 @@ void MainWindow::trigger_send_setpoint(){ } } } + +void MainWindow::on_gamepadYawScale_valueChanged(int arg1) +{ + gamepadMonitor->setYawScale(arg1); + ui->yawVizBar->setMinimum(std::abs(arg1) * -1); + ui->yawVizBar->setMaximum(std::abs(arg1)); +} + +void MainWindow::on_gamepadThrustScale_valueChanged(int arg1) +{ + gamepadMonitor->setThrustScale(arg1); + //Dont need to change the scale of the thrust viz bar because it is always interpreted as 0 to 60,000 +} + +void MainWindow::on_gamepadRollScale_valueChanged(int arg1) +{ + gamepadMonitor->setRollScale(arg1); + ui->rollVizBar->setMinimum(std::abs(arg1) * -1); + ui->rollVizBar->setMaximum(std::abs(arg1)); +} + +void MainWindow::on_gamepadPitchScale_valueChanged(int arg1) +{ + gamepadMonitor->setPitchScale(arg1); + ui->pitchVizBar->setMinimum(std::abs(arg1) * -1); + ui->pitchVizBar->setMaximum(std::abs(arg1)); +} diff --git a/groundStation/gui/MicroCART/mainwindow.h b/groundStation/gui/MicroCART/mainwindow.h index 1a706bab0..becd6e3f5 100644 --- a/groundStation/gui/MicroCART/mainwindow.h +++ b/groundStation/gui/MicroCART/mainwindow.h @@ -138,6 +138,14 @@ private slots: void trigger_send_setpoint(); + void on_gamepadYawScale_valueChanged(int arg1); + + void on_gamepadThrustScale_valueChanged(int arg1); + + void on_gamepadRollScale_valueChanged(int arg1); + + void on_gamepadPitchScale_valueChanged(int arg1); + private: Ui::MainWindow *ui; QStandardItemModel * setpointList; diff --git a/groundStation/gui/MicroCART/mainwindow.ui b/groundStation/gui/MicroCART/mainwindow.ui index b360c4e73..c202a2c5c 100644 --- a/groundStation/gui/MicroCART/mainwindow.ui +++ b/groundStation/gui/MicroCART/mainwindow.ui @@ -33,7 +33,7 @@ </size> </property> <property name="currentIndex"> - <number>4</number> + <number>2</number> </property> <widget class="QWidget" name="backend"> <attribute name="title"> @@ -355,7 +355,7 @@ </rect> </property> <property name="text"> - <string>Configure Roll</string> + <string>Bind Roll</string> </property> </widget> <widget class="QSlider" name="rollVizBar"> @@ -371,10 +371,10 @@ </rect> </property> <property name="minimum"> - <number>-20</number> + <number>-30</number> </property> <property name="maximum"> - <number>20</number> + <number>30</number> </property> <property name="value"> <number>0</number> @@ -406,7 +406,7 @@ </rect> </property> <property name="text"> - <string>Configure Yaw</string> + <string>Bind Yaw</string> </property> </widget> <widget class="QSlider" name="yawVizBar"> @@ -422,10 +422,10 @@ </rect> </property> <property name="minimum"> - <number>-30</number> + <number>-45</number> </property> <property name="maximum"> - <number>30</number> + <number>45</number> </property> <property name="value"> <number>0</number> @@ -433,6 +433,9 @@ <property name="orientation"> <enum>Qt::Horizontal</enum> </property> + <property name="tickPosition"> + <enum>QSlider::NoTicks</enum> + </property> </widget> <widget class="QSlider" name="thrustVizBar"> <property name="enabled"> @@ -440,7 +443,7 @@ </property> <property name="geometry"> <rect> - <x>220</x> + <x>223</x> <y>190</y> <width>16</width> <height>160</height> @@ -462,17 +465,17 @@ </property> <property name="geometry"> <rect> - <x>780</x> + <x>783</x> <y>180</y> <width>16</width> <height>160</height> </rect> </property> <property name="minimum"> - <number>-20</number> + <number>-30</number> </property> <property name="maximum"> - <number>20</number> + <number>30</number> </property> <property name="value"> <number>0</number> @@ -484,41 +487,43 @@ <widget class="QPushButton" name="pb_configThrust"> <property name="geometry"> <rect> - <x>250</x> - <y>260</y> + <x>246</x> + <y>280</y> <width>141</width> <height>25</height> </rect> </property> <property name="text"> - <string>Configure Thrust</string> + <string>Bind Thrust</string> </property> </widget> <widget class="QPushButton" name="pb_configPitch"> <property name="geometry"> <rect> <x>810</x> - <y>250</y> + <y>270</y> <width>141</width> <height>25</height> </rect> </property> <property name="text"> - <string>Configure Pitch</string> + <string>Bind Pitch</string> </property> </widget> <widget class="QLabel" name="noGamepadWarning"> <property name="geometry"> <rect> - <x>360</x> + <x>381</x> <y>10</y> - <width>381</width> + <width>311</width> <height>61</height> </rect> </property> <property name="font"> <font> <pointsize>20</pointsize> + <underline>true</underline> + <kerning>true</kerning> </font> </property> <property name="text"> @@ -531,25 +536,261 @@ <set>Qt::AlignCenter</set> </property> </widget> - <widget class="QLabel" name="label_6"> + <widget class="QLabel" name="yawScaleLabel"> <property name="geometry"> <rect> - <x>450</x> - <y>130</y> - <width>181</width> - <height>91</height> + <x>162</x> + <y>113</y> + <width>211</width> + <height>20</height> </rect> </property> <property name="text"> - <string>Select the input to bind then move the joystick that will control that input</string> + <string>Yaw Scale: Deg / s</string> </property> - <property name="alignment"> - <set>Qt::AlignCenter</set> + </widget> + <widget class="QSpinBox" name="gamepadYawScale"> + <property name="geometry"> + <rect> + <x>240</x> + <y>110</y> + <width>51</width> + <height>26</height> + </rect> + </property> + <property name="minimum"> + <number>-999</number> </property> - <property name="wordWrap"> + <property name="maximum"> + <number>999</number> + </property> + <property name="value"> + <number>45</number> + </property> + </widget> + <widget class="QWidget" name="horizontalLayoutWidget"> + <property name="geometry"> + <rect> + <x>160</x> + <y>390</y> + <width>791</width> + <height>151</height> + </rect> + </property> + <layout class="QHBoxLayout" name="horizontalLayout"> + <item> + <widget class="QLabel" name="label_6"> + <property name="text"> + <string>Select the input to bind then move the joystick that will control that input to its maximum values.</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="wordWrap"> + <bool>true</bool> + </property> + </widget> + </item> + <item> + <widget class="Line" name="line_2"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QLabel" name="label_7"> + <property name="text"> + <string>The scale of each input controls the maximum values sent to the Crazyflie with max joystick deflection. </string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="wordWrap"> + <bool>true</bool> + </property> + </widget> + </item> + <item> + <widget class="Line" name="line_3"> + <property name="orientation"> + <enum>Qt::Vertical</enum> + </property> + </widget> + </item> + <item> + <spacer name="horizontalSpacer_3"> + <property name="orientation"> + <enum>Qt::Horizontal</enum> + </property> + <property name="sizeHint" stdset="0"> + <size> + <width>40</width> + <height>20</height> + </size> + </property> + </spacer> + </item> + <item> + <widget class="QLabel" name="label_8"> + <property name="text"> + <string>Negative scale values can be used to invert a joystick if needed.</string> + </property> + <property name="alignment"> + <set>Qt::AlignCenter</set> + </property> + <property name="wordWrap"> + <bool>true</bool> + </property> + </widget> + </item> + </layout> + </widget> + <widget class="QSpinBox" name="gamepadThrustScale"> + <property name="geometry"> + <rect> + <x>342</x> + <y>228</y> + <width>71</width> + <height>26</height> + </rect> + </property> + <property name="minimum"> + <number>-60000</number> + </property> + <property name="maximum"> + <number>60000</number> + </property> + <property name="value"> + <number>60000</number> + </property> + </widget> + <widget class="QLabel" name="thrustScaleLabel"> + <property name="geometry"> + <rect> + <x>247</x> + <y>230</y> + <width>191</width> + <height>20</height> + </rect> + </property> + <property name="text"> + <string>Thrust Scale: </string> + </property> + </widget> + <widget class="QLabel" name="thrustScaleNote"> + <property name="geometry"> + <rect> + <x>248</x> + <y>256</y> + <width>221</width> + <height>17</height> + </rect> + </property> + <property name="text"> + <string>Note: max thrust is 60,000</string> + </property> + </widget> + <widget class="QSpinBox" name="gamepadRollScale"> + <property name="geometry"> + <rect> + <x>798</x> + <y>100</y> + <width>51</width> + <height>26</height> + </rect> + </property> + <property name="minimum"> + <number>-999</number> + </property> + <property name="maximum"> + <number>999</number> + </property> + <property name="value"> + <number>30</number> + </property> + </widget> + <widget class="QLabel" name="rollScaleLabel"> + <property name="enabled"> <bool>true</bool> </property> + <property name="geometry"> + <rect> + <x>720</x> + <y>103</y> + <width>211</width> + <height>20</height> + </rect> + </property> + <property name="text"> + <string>Roll Scale: Deg</string> + </property> + </widget> + <widget class="QSpinBox" name="gamepadPitchScale"> + <property name="geometry"> + <rect> + <x>895</x> + <y>239</y> + <width>51</width> + <height>26</height> + </rect> + </property> + <property name="minimum"> + <number>-999</number> + </property> + <property name="maximum"> + <number>999</number> + </property> + <property name="value"> + <number>30</number> + </property> + </widget> + <widget class="QLabel" name="pitchScaleLabel"> + <property name="geometry"> + <rect> + <x>812</x> + <y>242</y> + <width>211</width> + <height>20</height> + </rect> + </property> + <property name="text"> + <string>Pitch Scale: Deg</string> + </property> </widget> + <zorder>pitchScaleLabel</zorder> + <zorder>rollScaleLabel</zorder> + <zorder>thrustScaleLabel</zorder> + <zorder>yawScaleLabel</zorder> + <zorder>pb_configRoll</zorder> + <zorder>rollVizBar</zorder> + <zorder>pb_resetConfig</zorder> + <zorder>pb_configYaw</zorder> + <zorder>yawVizBar</zorder> + <zorder>thrustVizBar</zorder> + <zorder>pitchVizBar</zorder> + <zorder>pb_configThrust</zorder> + <zorder>pb_configPitch</zorder> + <zorder>noGamepadWarning</zorder> + <zorder>gamepadYawScale</zorder> + <zorder>horizontalLayoutWidget</zorder> + <zorder>gamepadThrustScale</zorder> + <zorder>thrustScaleNote</zorder> + <zorder>gamepadRollScale</zorder> + <zorder>gamepadPitchScale</zorder> </widget> <widget class="QWidget" name="plots"> <attribute name="title"> -- GitLab