Commit 316620fd 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 90efec1b
This diff is collapsed.
......@@ -57,8 +57,6 @@ public:
void run();
void shutdown();
const QString& getName();
/**
* @brief Load joystick settings
*/
......@@ -69,19 +67,19 @@ public:
*/
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
......@@ -89,21 +87,6 @@ public:
return yawAxis;
}
int getMappingAutoButton() const
{
return autoButtonMapping;
}
int getMappingManualButton() const
{
return manualButtonMapping;
}
int getMappingStabilizeButton() const
{
return stabilizeButtonMapping;
}
int getJoystickNumButtons() const
{
return joystickButtons;
......@@ -119,6 +102,11 @@ public:
return joystickID;
}
const QString& getName() const
{
return joystickName;
}
int getNumJoysticks() const
{
return joysticksFound;
......@@ -129,33 +117,31 @@ public:
return QString(SDL_JoystickName(id));
}
float getCurrentValueForAxis(int axisID);
const double sdlJoystickMin;
const double sdlJoystickMax;
protected:
int defaultIndex;
double calibrationPositive[10];
double calibrationNegative[10];
SDL_Joystick* joystick;
UASInterface* uas;
QList<int> uasButtonList;
bool done;
QMutex m_doneMutex;
// Axis 3 is thrust (CALIBRATION!)
int thrustAxis;
int xAxis;
int yAxis;
// Store the mapping between axis numbers and the roll/pitch/yaw/throttle configuration.
int rollAxis;
int pitchAxis;
int yawAxis;
int autoButtonMapping;
int manualButtonMapping;
int stabilizeButtonMapping;
SDL_Event event;
int throttleAxis;
// Cache information on the joystick instead of polling the SDL everytime.
QString joystickName;
int joystickAxes;
int joystickButtons;
int joystickID;
int joysticksFound;
quint16 buttonState; ///< Track the state of the buttons so we can trigger on Up and Down events
void init();
......@@ -175,32 +161,11 @@ signals:
void joystickChanged(double roll, double pitch, double yaw, double thrust, int xHat, int yHat, int buttons);
/**
* @brief Thrust lever of the joystick has changed
*
* @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
* @brief Emit a new value for an axis
*
* @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.
......@@ -236,19 +201,15 @@ public slots:
void setActiveUAS(UASInterface* uas);
/** @brief Switch to a new joystick by ID number. */
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)
......@@ -256,19 +217,9 @@ public slots:
yawAxis = mapping;
}
void setMappingAutoButton(int mapping)
{
autoButtonMapping = mapping;
}
void setMappingManualButton(int mapping)
{
manualButtonMapping = mapping;
}
void setMappingStabilizeButton(int mapping)
void setMappingThrottleAxis(int mapping)
{
stabilizeButtonMapping = mapping;
throttleAxis = mapping;
}
};
......
......@@ -4,10 +4,12 @@
JoystickAxis::JoystickAxis(int id, QWidget *parent) :
QWidget(parent),
id(id),
ui(new Ui::JoystickAxis)
{
ui->setupUi(this);
ui->label->setText(QString::number(id));
connect(ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(mappingComboBoxChanged(int)));
}
JoystickAxis::~JoystickAxis()
......@@ -15,7 +17,12 @@ JoystickAxis::~JoystickAxis()
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:
explicit JoystickAxis(int id, QWidget *parent = 0);
~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:
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:
int id; ///< The ID for this axis. Corresponds to the IDs used by JoystickInput.
Ui::JoystickAxis *ui;
private slots:
/** @brief Handle changes to the mapping dropdown bar. */
void mappingComboBoxChanged(int newMapping);
};
#endif // JOYSTICKAXIS_H
......@@ -61,6 +61,26 @@
<string>--</string>
</property>
</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>
</item>
<item>
......@@ -80,6 +100,9 @@
<height>100</height>
</size>
</property>
<property name="minimum">
<number>-100</number>
</property>
<property name="value">
<number>0</number>
</property>
......
......@@ -11,9 +11,11 @@ class JoystickButton;
class JoystickButton : public QWidget
{
Q_OBJECT
public:
explicit JoystickButton(int id, QWidget *parent = 0);
virtual ~JoystickButton();
private:
int id;
Ui::JoystickButton *m_ui;
......
......@@ -48,26 +48,6 @@
<string>--</string>
</property>
</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>
</item>
<item>
......
......@@ -21,17 +21,10 @@ JoystickWidget::JoystickWidget(JoystickInput* joystick, QWidget *parent) :
// Initialize the UI based on the current joystick
initUI();
// Watch for 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)));
// Watch for button and hat input events from the joystick.
connect(this->joystick, SIGNAL(buttonPressed(int)), this, SLOT(joystickButtonPressed(int)));
connect(this->joystick, SIGNAL(buttonReleased(int)), this, SLOT(joystickButtonReleased(int)));
// 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)));
connect(this->joystick, SIGNAL(axisValueChanged(int,float)), this, SLOT(updateAxisValue(int,float)));
// Update the UI if the joystick changes.
connect(m_ui->joystickNameComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(updateUIForJoystick(int)));
......@@ -92,15 +85,6 @@ JoystickWidget::~JoystickWidget()
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)
{
switch (e->type()) {
......@@ -143,49 +127,46 @@ void JoystickWidget::updateUIForJoystick(int id)
for (int i = 0; i < joystick->getJoystickNumAxes(); i++)
{
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.
m_ui->axesLayout->insertWidget(i, axis);
axes.append(axis);
}
}
void JoystickWidget::setX(float x)
{
if (axes.size() > 0)
{
axes.at(0)->setValue(x * 100);
}
}
void JoystickWidget::setY(float y)
void JoystickWidget::updateAxisValue(int axis, float value)
{
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)
{
axes.at(2)->setValue(z * 100);
}
updateStatus(tr("Hat position: x: %1, y: %2").arg(x).arg(y));
}
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)
{
QString colorStyle = QString("QLabel { background-color: %1;}").arg(buttonLabelColor.name());
......@@ -199,4 +180,5 @@ void JoystickWidget::joystickButtonReleased(int key)
void JoystickWidget::updateStatus(const QString& status)
{
m_ui->statusLabel->setText(status);
}
......@@ -33,6 +33,7 @@ This file is part of the PIXHAWK project
#include <QtGui/QDialog>
#include <QLabel>
#include <QMap>
#include "JoystickInput.h"
#include "MainWindow.h"
#include "JoystickAxis.h"
......@@ -52,26 +53,7 @@ public:
virtual ~JoystickWidget();
public slots:
/**
* @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 updateAxisValue(int axis, float value);
void setHat(float x, float y);
/** @brief Trigger a UI change based on a button being pressed */
void joystickButtonPressed(int key);
......@@ -98,6 +80,8 @@ protected:
protected slots:
/** @brief Update the UI for a new joystick based on SDL ID. */
void updateUIForJoystick(int id);
/** @brief Change the stored mapping for a given axis. */
void setMappingAxis(int axisID, int newMapping);
private:
Ui::JoystickWidget *m_ui;
......
......@@ -78,86 +78,97 @@
<property name="frameShadow">
<enum>QFrame::Sunken</enum>
</property>
<layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0">
<layout class="QVBoxLayout" name="verticalLayout_2" stretch="0,0">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<item>
<widget class="QGroupBox" name="buttonBox">
<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="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="title">
<string>Buttons</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="flat">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="buttonLayout" stretch="">
<property name="spacing">
<number>1</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<property name="leftMargin">
<number>3</number>
</property>
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>3</number>
</property>
</layout>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QGroupBox" name="buttonBox">
<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="maximumSize">
<size>
<width>16777215</width>
<height>16777215</height>
</size>
</property>
<property name="title">
<string>Buttons</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<property name="flat">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="buttonLayout">
<property name="spacing">
<number>1</number>
</property>
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
<property name="leftMargin">
<number>3</number>
</property>
<property name="topMargin">
<number>3</number>
</property>
<property name="rightMargin">
<number>3</number>
</property>
<property name="bottomMargin">
<number>3</number>
</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>
<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>
<widget class="QLabel" name="statusLabel">
<property name="text">
<string/>
</property>
<layout class="QHBoxLayout" name="axesLayout" stretch="">
<property name="sizeConstraint">
<enum>QLayout::SetMinimumSize</enum>
</property>
</layout>
</widget>
</item>
</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