Commit ff2c011d authored by Bryant's avatar Bryant

Refactored much of the joystick interface with the largest changes being the...

Refactored much of the joystick interface with the largest changes being the removal of dead/unused code including broken event-based SDL reading code. Additionally the mapping code should now work, though it hasn't been tested with controller switching or a real UAS. Finally the existing settings code has been commented-out as it is not compatible with the current state of this code and needs to be able to support different joysticks based on name.
parent 295b8a25
This diff is collapsed.
...@@ -57,8 +57,6 @@ public: ...@@ -57,8 +57,6 @@ public:
void run(); void run();
void shutdown(); void shutdown();
const QString& getName();
/** /**
* @brief Load joystick settings * @brief Load joystick settings
*/ */
...@@ -69,19 +67,19 @@ public: ...@@ -69,19 +67,19 @@ public:
*/ */
void storeSettings(); void storeSettings();
int getMappingThrustAxis() const int getMappingThrottleAxis() const
{ {
return thrustAxis; return throttleAxis;
} }
int getMappingXAxis() const int getMappingRollAxis() const
{ {
return xAxis; return rollAxis;
} }
int getMappingYAxis() const int getMappingPitchAxis() const
{ {
return yAxis; return pitchAxis;
} }
int getMappingYawAxis() const int getMappingYawAxis() const
...@@ -89,21 +87,6 @@ public: ...@@ -89,21 +87,6 @@ public:
return yawAxis; return yawAxis;
} }
int getMappingAutoButton() const
{
return autoButtonMapping;
}
int getMappingManualButton() const
{
return manualButtonMapping;
}
int getMappingStabilizeButton() const
{
return stabilizeButtonMapping;
}
int getJoystickNumButtons() const int getJoystickNumButtons() const
{ {
return joystickButtons; return joystickButtons;
...@@ -119,6 +102,11 @@ public: ...@@ -119,6 +102,11 @@ public:
return joystickID; return joystickID;
} }
const QString& getName() const
{
return joystickName;
}
int getNumJoysticks() const int getNumJoysticks() const
{ {
return joysticksFound; return joysticksFound;
...@@ -129,33 +117,31 @@ public: ...@@ -129,33 +117,31 @@ public:
return QString(SDL_JoystickName(id)); return QString(SDL_JoystickName(id));
} }
float getCurrentValueForAxis(int axisID);
const double sdlJoystickMin; const double sdlJoystickMin;
const double sdlJoystickMax; const double sdlJoystickMax;
protected: protected:
int defaultIndex;
double calibrationPositive[10]; double calibrationPositive[10];
double calibrationNegative[10]; double calibrationNegative[10];
SDL_Joystick* joystick; SDL_Joystick* joystick;
UASInterface* uas; UASInterface* uas;
QList<int> uasButtonList;
bool done; bool done;
QMutex m_doneMutex;
// Axis 3 is thrust (CALIBRATION!) // Store the mapping between axis numbers and the roll/pitch/yaw/throttle configuration.
int thrustAxis; int rollAxis;
int xAxis; int pitchAxis;
int yAxis;
int yawAxis; int yawAxis;
int autoButtonMapping; int throttleAxis;
int manualButtonMapping;
int stabilizeButtonMapping; // Cache information on the joystick instead of polling the SDL everytime.
SDL_Event event;
QString joystickName; QString joystickName;
int joystickAxes; int joystickAxes;
int joystickButtons; int joystickButtons;
int joystickID; int joystickID;
int joysticksFound; int joysticksFound;
quint16 buttonState; ///< Track the state of the buttons so we can trigger on Up and Down events quint16 buttonState; ///< Track the state of the buttons so we can trigger on Up and Down events
void init(); void init();
...@@ -175,32 +161,11 @@ signals: ...@@ -175,32 +161,11 @@ signals:
void joystickChanged(double roll, double pitch, double yaw, double thrust, int xHat, int yHat, int buttons); void joystickChanged(double roll, double pitch, double yaw, double thrust, int xHat, int yHat, int buttons);
/** /**
* @brief Thrust lever of the joystick has changed * @brief Emit a new value for an axis
*
* @param thrust Thrust, 0%: 0, 100%: 1.0
*/
void thrustChanged(int thrust);
/**
* @brief X-Axis / forward-backward axis has changed
*
* @param x forward / pitch / x axis, front: +1.0, center: 0.0, back: -1.0
*/
void xChanged(int x);
/**
* @brief Y-Axis / left-right axis has changed
*
* @param y left / roll / y axis, left: -1.0, middle: 0.0, right: +1.0
*/
void yChanged(int y);
/**
* @brief Yaw / left-right turn has changed
* *
* @param yaw turn axis, left-turn: -1.0, middle: 0.0, right-turn: +1.0 * @param value Value of the axis, between -1.0 and 1.0.
*/ */
void yawChanged(int yaw); void axisValueChanged(int axis, float value);
/** /**
* @brief Joystick button has changed state from unpressed to pressed. * @brief Joystick button has changed state from unpressed to pressed.
...@@ -236,19 +201,15 @@ public slots: ...@@ -236,19 +201,15 @@ public slots:
void setActiveUAS(UASInterface* uas); void setActiveUAS(UASInterface* uas);
/** @brief Switch to a new joystick by ID number. */ /** @brief Switch to a new joystick by ID number. */
void setActiveJoystick(int id); void setActiveJoystick(int id);
void setMappingThrustAxis(int mapping)
{
thrustAxis = mapping;
}
void setMappingXAxis(int mapping) void setMappingRollAxis(int mapping)
{ {
xAxis = mapping; rollAxis = mapping;
} }
void setMappingYAxis(int mapping) void setMappingPitchAxis(int mapping)
{ {
yAxis = mapping; pitchAxis = mapping;
} }
void setMappingYawAxis(int mapping) void setMappingYawAxis(int mapping)
...@@ -256,19 +217,9 @@ public slots: ...@@ -256,19 +217,9 @@ public slots:
yawAxis = mapping; yawAxis = mapping;
} }
void setMappingAutoButton(int mapping) void setMappingThrottleAxis(int mapping)
{
autoButtonMapping = mapping;
}
void setMappingManualButton(int mapping)
{
manualButtonMapping = mapping;
}
void setMappingStabilizeButton(int mapping)
{ {
stabilizeButtonMapping = mapping; throttleAxis = mapping;
} }
}; };
......
...@@ -4,10 +4,12 @@ ...@@ -4,10 +4,12 @@
JoystickAxis::JoystickAxis(int id, QWidget *parent) : JoystickAxis::JoystickAxis(int id, QWidget *parent) :
QWidget(parent), QWidget(parent),
id(id),
ui(new Ui::JoystickAxis) ui(new Ui::JoystickAxis)
{ {
ui->setupUi(this); ui->setupUi(this);
ui->label->setText(QString::number(id)); ui->label->setText(QString::number(id));
connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(mappingComboBoxChanged(int)));
} }
JoystickAxis::~JoystickAxis() JoystickAxis::~JoystickAxis()
...@@ -15,7 +17,12 @@ JoystickAxis::~JoystickAxis() ...@@ -15,7 +17,12 @@ JoystickAxis::~JoystickAxis()
delete ui; delete ui;
} }
void JoystickAxis::setValue(int value) void JoystickAxis::setValue(float value)
{ {
ui->progressBar->setValue(value); ui->progressBar->setValue(100.0f * value);
}
void JoystickAxis::mappingComboBoxChanged(int newMapping)
{
emit mappingChanged(id, newMapping);
} }
...@@ -15,11 +15,36 @@ public: ...@@ -15,11 +15,36 @@ public:
explicit JoystickAxis(int id, QWidget *parent = 0); explicit JoystickAxis(int id, QWidget *parent = 0);
~JoystickAxis(); ~JoystickAxis();
/**
* @brief The JOYSTICK_MAPPING enum storing the values for each item in the mapping combobox.
* This should match the order of items in the mapping combobox in JoystickAxis.ui.
*/
enum JOYSTICK_AXIS_MAPPING
{
JOYSTICK_AXIS_MAPPING_NONE = 0,
JOYSTICK_AXIS_MAPPING_YAW = 1,
JOYSTICK_AXIS_MAPPING_PITCH = 2,
JOYSTICK_AXIS_MAPPING_ROLL = 3,
JOYSTICK_AXIS_MAPPING_THROTTLE = 4
};
signals:
/** @brief Signal a change in this axis' yaw/pitch/roll mapping */
void mappingChanged(int id, int newMapping);
public slots: public slots:
void setValue(int value); /** @brief Update the displayed value of the included progressbar.
* @param value A value between -1.0 and 1.0.
*/
void setValue(float value);
private: private:
int id; ///< The ID for this axis. Corresponds to the IDs used by JoystickInput.
Ui::JoystickAxis *ui; Ui::JoystickAxis *ui;
private slots:
/** @brief Handle changes to the mapping dropdown bar. */
void mappingComboBoxChanged(int newMapping);
}; };
#endif // JOYSTICKAXIS_H #endif // JOYSTICKAXIS_H
...@@ -61,6 +61,26 @@ ...@@ -61,6 +61,26 @@
<string>--</string> <string>--</string>
</property> </property>
</item> </item>
<item>
<property name="text">
<string>Yaw</string>
</property>
</item>
<item>
<property name="text">
<string>Pitch</string>
</property>
</item>
<item>
<property name="text">
<string>Roll</string>
</property>
</item>
<item>
<property name="text">
<string>Throttle</string>
</property>
</item>
</widget> </widget>
</item> </item>
<item> <item>
...@@ -80,6 +100,9 @@ ...@@ -80,6 +100,9 @@
<height>100</height> <height>100</height>
</size> </size>
</property> </property>
<property name="minimum">
<number>-100</number>
</property>
<property name="value"> <property name="value">
<number>0</number> <number>0</number>
</property> </property>
......
...@@ -11,9 +11,11 @@ class JoystickButton; ...@@ -11,9 +11,11 @@ class JoystickButton;
class JoystickButton : public QWidget class JoystickButton : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit JoystickButton(int id, QWidget *parent = 0); explicit JoystickButton(int id, QWidget *parent = 0);
virtual ~JoystickButton(); virtual ~JoystickButton();
private: private:
int id; int id;
Ui::JoystickButton *m_ui; Ui::JoystickButton *m_ui;
......
...@@ -48,26 +48,6 @@ ...@@ -48,26 +48,6 @@
<string>--</string> <string>--</string>
</property> </property>
</item> </item>
<item>
<property name="text">
<string>Yaw</string>
</property>
</item>
<item>
<property name="text">
<string>Pitch</string>
</property>
</item>
<item>
<property name="text">
<string>Roll</string>
</property>
</item>
<item>
<property name="text">
<string>Throttle</string>
</property>
</item>
</widget> </widget>
</item> </item>
<item> <item>
......
...@@ -21,17 +21,10 @@ JoystickWidget::JoystickWidget(JoystickInput* joystick, QWidget *parent) : ...@@ -21,17 +21,10 @@ JoystickWidget::JoystickWidget(JoystickInput* joystick, QWidget *parent) :
// Initialize the UI based on the current joystick // Initialize the UI based on the current joystick
initUI(); initUI();
// Watch for input events from the joystick // Watch for button and hat input events from the joystick.
connect(this->joystick, SIGNAL(joystickChanged(double,double,double,double,int,int,int)), this, SLOT(updateJoystick(double,double,double,double,int,int)));
connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(joystickButtonPressed(int))); connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(joystickButtonPressed(int)));
connect(this->joystick, SIGNAL(buttonReleased(int)), this, SLOT(joystickButtonReleased(int))); connect(this->joystick, SIGNAL(buttonReleased(int)), this, SLOT(joystickButtonReleased(int)));
connect(this->joystick, SIGNAL(axisValueChanged(int,float)), this, SLOT(updateAxisValue(int,float)));
// Watch for changes to the button/axis mappings
// connect(m_ui->rollMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingXAxis(int)));
// connect(m_ui->pitchMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingYAxis(int)));
// connect(m_ui->yawMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingYawAxis(int)));
// connect(m_ui->throttleMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingThrustAxis(int)));
// connect(m_ui->autoMapSpinBox, SIGNAL(valueChanged(int)), this->joystick, SLOT(setMappingAutoButton(int)));
// Update the UI if the joystick changes. // Update the UI if the joystick changes.
connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUIForJoystick(int))); connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUIForJoystick(int)));
...@@ -92,15 +85,6 @@ JoystickWidget::~JoystickWidget() ...@@ -92,15 +85,6 @@ JoystickWidget::~JoystickWidget()
delete m_ui; delete m_ui;
} }
void JoystickWidget::updateJoystick(double roll, double pitch, double yaw, double thrust, int xHat, int yHat)
{
setX(roll);
setY(pitch);
setZ(yaw);
setThrottle(thrust);
setHat(xHat, yHat);
}
void JoystickWidget::changeEvent(QEvent *e) void JoystickWidget::changeEvent(QEvent *e)
{ {
switch (e->type()) { switch (e->type()) {
...@@ -143,49 +127,46 @@ void JoystickWidget::updateUIForJoystick(int id) ...@@ -143,49 +127,46 @@ void JoystickWidget::updateUIForJoystick(int id)
for (int i = 0; i < joystick->getJoystickNumAxes(); i++) for (int i = 0; i < joystick->getJoystickNumAxes(); i++)
{ {
JoystickAxis* axis = new JoystickAxis(i, m_ui->axesBox); JoystickAxis* axis = new JoystickAxis(i, m_ui->axesBox);
axis->setValue(joystick->getCurrentValueForAxis(i));
connect(axis, SIGNAL(mappingChanged(int,int)), this, SLOT(setMappingAxis(int,int)));
// And make sure we insert BEFORE the vertical spacer. // And make sure we insert BEFORE the vertical spacer.
m_ui->axesLayout->insertWidget(i, axis); m_ui->axesLayout->insertWidget(i, axis);
axes.append(axis); axes.append(axis);
} }
} }
void JoystickWidget::setX(float x) void JoystickWidget::updateAxisValue(int axis, float value)
{
if (axes.size() > 0)
{
axes.at(0)->setValue(x * 100);
}
}
void JoystickWidget::setY(float y)
{ {
if (axes.size() > 1) if (axis < axes.size())
{ {
axes.at(1)->setValue(y * 100); axes.at(axis)->setValue(value);
} }
} }
void JoystickWidget::setZ(float z) void JoystickWidget::setHat(float x, float y)
{ {
if (axes.size() > 2) updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
{
axes.at(2)->setValue(z * 100);
}
} }
void JoystickWidget::setThrottle(float t) void JoystickWidget::setMappingAxis(int axisID, int newMapping)
{ {
if (axes.size() > 3) switch (newMapping)
{ {
axes.at(3)->setValue(t * 100); case JoystickAxis::JOYSTICK_AXIS_MAPPING_ROLL:
joystick->setMappingRollAxis(axisID);
break;
case JoystickAxis::JOYSTICK_AXIS_MAPPING_PITCH:
joystick->setMappingPitchAxis(axisID);
break;
case JoystickAxis::JOYSTICK_AXIS_MAPPING_YAW:
joystick->setMappingYawAxis(axisID);
break;
case JoystickAxis::JOYSTICK_AXIS_MAPPING_THROTTLE:
joystick->setMappingThrottleAxis(axisID);
break;
} }
} }
void JoystickWidget::setHat(float x, float y)
{
updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
}
void JoystickWidget::joystickButtonPressed(int key) void JoystickWidget::joystickButtonPressed(int key)
{ {
QString colorStyle = QString("QLabel { background-color: %1;}").arg(buttonLabelColor.name()); QString colorStyle = QString("QLabel { background-color: %1;}").arg(buttonLabelColor.name());
...@@ -199,4 +180,5 @@ void JoystickWidget::joystickButtonReleased(int key) ...@@ -199,4 +180,5 @@ void JoystickWidget::joystickButtonReleased(int key)
void JoystickWidget::updateStatus(const QString& status) void JoystickWidget::updateStatus(const QString& status)
{ {
m_ui->statusLabel->setText(status);
} }
...@@ -33,6 +33,7 @@ This file is part of the PIXHAWK project ...@@ -33,6 +33,7 @@ This file is part of the PIXHAWK project
#include <QtGui/QDialog> #include <QtGui/QDialog>
#include <QLabel> #include <QLabel>
#include <QMap>
#include "JoystickInput.h" #include "JoystickInput.h"
#include "MainWindow.h" #include "MainWindow.h"
#include "JoystickAxis.h" #include "JoystickAxis.h"
...@@ -52,26 +53,7 @@ public: ...@@ -52,26 +53,7 @@ public:
virtual ~JoystickWidget(); virtual ~JoystickWidget();
public slots: public slots:
/** void updateAxisValue(int axis, float value);
* @brief Receive raw joystick values
*
* @param roll forward / pitch / x axis, front: 32'767, center: 0, back: -32'768
* @param pitch left / roll / y axis, left: -32'768, middle: 0, right: 32'767
* @param yaw turn axis, left-turn: -32'768, centered: 0, right-turn: 32'767
* @param thrust Thrust, 0%: 0, 100%: 65535
* @param xHat hat vector in forward-backward direction, +1 forward, 0 center, -1 backward
* @param yHat hat vector in left-right direction, -1 left, 0 center, +1 right
*/
void updateJoystick(double roll, double pitch, double yaw, double thrust, int xHat, int yHat);
/** @brief Back/forth movement */
void setX(float x);
/** @brief Left/right movement */
void setY(float y);
/** @brief Wrist rotation */
void setZ(float z);
/** @brief Throttle lever */
void setThrottle(float thrust);
/** @brief Hat switch position */
void setHat(float x, float y); void setHat(float x, float y);
/** @brief Trigger a UI change based on a button being pressed */ /** @brief Trigger a UI change based on a button being pressed */
void joystickButtonPressed(int key); void joystickButtonPressed(int key);
...@@ -98,6 +80,8 @@ protected: ...@@ -98,6 +80,8 @@ protected:
protected slots: protected slots:
/** @brief Update the UI for a new joystick based on SDL ID. */ /** @brief Update the UI for a new joystick based on SDL ID. */
void updateUIForJoystick(int id); void updateUIForJoystick(int id);
/** @brief Change the stored mapping for a given axis. */
void setMappingAxis(int axisID, int newMapping);
private: private:
Ui::JoystickWidget *m_ui; Ui::JoystickWidget *m_ui;
......
...@@ -78,86 +78,97 @@ ...@@ -78,86 +78,97 @@
<property name="frameShadow"> <property name="frameShadow">
<enum>QFrame::Sunken</enum> <enum>QFrame::Sunken</enum>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0"> <layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,0">
<property name="sizeConstraint"> <property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum> <enum>QLayout::SetMinimumSize</enum>
</property> </property>
<item> <item>
<widget class="QGroupBox" name="buttonBox"> <layout class="QHBoxLayout" name="horizontalLayout">
<property name="sizePolicy"> <item>
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding"> <widget class="QGroupBox" name="buttonBox">
<horstretch>0</horstretch> <property name="sizePolicy">
<verstretch>0</verstretch> <sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
</sizepolicy> <horstretch>0</horstretch>
</property> <verstretch>0</verstretch>
<property name="minimumSize"> </sizepolicy>
<size> </property>
<width>100</width> <property name="minimumSize">
<height>0</height> <size>
</size> <width>100</width>
</property> <height>0</height>
<property name="maximumSize"> </size>
<size> </property>
<width>16777215</width> <property name="maximumSize">
<height>16777215</height> <size>
</size> <width>16777215</width>
</property> <height>16777215</height>
<property name="title"> </size>
<string>Buttons</string> </property>
</property> <property name="title">
<property name="alignment"> <string>Buttons</string>
<set>Qt::AlignCenter</set> </property>
</property> <property name="alignment">
<property name="flat"> <set>Qt::AlignCenter</set>
<bool>false</bool> </property>
</property> <property name="flat">
<layout class="QVBoxLayout" name="buttonLayout" stretch=""> <bool>false</bool>
<property name="spacing"> </property>
<number>1</number> <layout class="QVBoxLayout" name="buttonLayout">
</property> <property name="spacing">
<property name="sizeConstraint"> <number>1</number>
<enum>QLayout::SetMinimumSize</enum> </property>
</property> <property name="sizeConstraint">
<property name="leftMargin"> <enum>QLayout::SetMinimumSize</enum>
<number>3</number> </property>
</property> <property name="leftMargin">
<property name="topMargin"> <number>3</number>
<number>3</number> </property>
</property> <property name="topMargin">
<property name="rightMargin"> <number>3</number>
<number>3</number> </property>
</property> <property name="rightMargin">
<property name="bottomMargin"> <number>3</number>
<number>3</number> </property>
</property> <property name="bottomMargin">
</layout> <number>3</number>
</widget> </property>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="axesBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="title">
<string>Axes</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<layout class="QHBoxLayout" name="axesLayout">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
</layout>
</widget>
</item>
</layout>
</item> </item>
<item> <item>
<widget class="QGroupBox" name="axesBox"> <widget class="QLabel" name="statusLabel">
<property name="sizePolicy"> <property name="text">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding"> <string/>
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>100</width>
<height>0</height>
</size>
</property>
<property name="title">
<string>Axes</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property> </property>
<layout class="QHBoxLayout" name="axesLayout" stretch="">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
</layout>
</widget> </widget>
</item> </item>
</layout> </layout>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment