Skip to content

Commit

Permalink
gui: added working levels
Browse files Browse the repository at this point in the history
  • Loading branch information
bmanga committed Apr 15, 2019
1 parent 2ac729e commit ee76b5a
Show file tree
Hide file tree
Showing 12 changed files with 141 additions and 154 deletions.
2 changes: 0 additions & 2 deletions tools/QtGAITGui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ find_package(Qt5Charts CONFIG REQUIRED)
add_executable(qt-gait-gui
main.cpp
WindowBase.hpp
Training_mode.cpp
Training_mode.hpp
CircleWidget.cpp
CircleWidget.hpp
progressbar.cpp
progressbar.hpp
intro_window.cpp
intro_window.hpp
Level_weight_shifting.cpp
Level_weight_shifting.hpp
MainWindow.cpp
MainWindow.hpp
Expand Down
6 changes: 3 additions & 3 deletions tools/QtGAITGui/CircleWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ float CircleWidget::getDiameter()
return this->diameter;
}

void CircleWidget::setTarget(float lower, float upper)
void CircleWidget::setTarget(Target t)
{
m_targetLow = lower;
m_targetHigh = upper;
m_targetLow = t.low;
m_targetHigh = t.high;
}

void CircleWidget::checkStatus()
Expand Down
7 changes: 5 additions & 2 deletions tools/QtGAITGui/CircleWidget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include <QWidget>
#include "MainWindow.hpp"
#include "TargetBase.h"
#include "common_types.h"

class CircleWidget : public QWidget {
Expand All @@ -28,8 +29,10 @@ class CircleWidget : public QWidget {
public slots:
void setDiameter(float d);
float getDiameter();
void setTarget(float lower, float upper); // NEW
void checkStatus(); // NEW

void setTarget(Target t);
void checkStatus();

void onNewFSRData(fsr_data p) { setDiameter(p.toe + p.heel); }

private:
Expand Down
3 changes: 2 additions & 1 deletion tools/QtGAITGui/FinalWindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
#define FinalWindow_hpp
#include <stdio.h>
#include <QLabel>
#include "WindowBase.hpp"

class FinalWindow : public QWidget {
class FinalWindow : public WindowBase {
public:
FinalWindow();
~FinalWindow();
Expand Down
103 changes: 0 additions & 103 deletions tools/QtGAITGui/Level_weight_shifting.cpp

This file was deleted.

120 changes: 101 additions & 19 deletions tools/QtGAITGui/Level_weight_shifting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,129 @@
#define Level_weight_shifting_hpp

#include <stdio.h>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
#include <QObject>
#include <QTimer>
#include <chrono>
#include "TargetBase.h"

class ProgressBar;

struct Target {
float low;
float high;
};
template <class>
class TrainingMode;

template <class TargetT>
class LevelWS : public QObject {
Q_OBJECT
public:
LevelWS(ProgressBar *left, ProgressBar *right);
LevelWS(TrainingMode<TargetT> *parent,
TargetT *left,
TargetT *right,
QLabel *label)
: m_parent(parent), m_leftBar(left), m_rightBar(right), m_label(label)
{
connect(left, &TargetT::onTarget, this, &LevelWS::onLeftFSRChanged);
connect(right, &TargetT::onTarget, this, &LevelWS::onRightFSRChanged);

m_timer.setSingleShot(true);

connect(&m_timer, &QTimer::timeout, this, &LevelWS::levelComplete);
}

void setLvlRequirements(Target left,
Target right,
std::chrono::seconds timeRequirement);
void loadJsonFile(QString file);
std::chrono::seconds timeRequirement)
{
m_leftBar->setTarget(left);
m_rightBar->setTarget(right);
m_timeRequirement = timeRequirement;
}

void loadJsonFile(QString file)
{
QFile dataFile(file);

if (!dataFile.open(QFile::ReadOnly)) {
QMessageBox().critical(0, "File Error", "Data file does not exist");
return;
}

auto jsonDoc = QJsonDocument::fromJson(dataFile.readAll());

levelsRoot = jsonDoc.object();
}

public slots:
void onLeftFSRChanged(bool onTarget);
void onRightFSRChanged(bool onTarget);
void onLeftFSRChanged(bool onTarget)
{
m_leftOnTgt = onTarget;
checkTgtStatus();
}

void start();
void onRightFSRChanged(bool onTarget)
{
m_rightOnTgt = onTarget;
checkTgtStatus();
}

void runLevel();
void levelComplete()
{
auto levelIdx = levelsRoot.keys().indexOf(currentLevel);
if (levelIdx == levelsRoot.keys().size() - 1) {
// This was the last level
m_parent->emitWindowDone();
return;
}
currentLevel = levelsRoot.keys()[levelIdx + 1];
runLevel();
}

private:
void levelComplete();
void start()
{
currentLevel = levelsRoot.keys()[0];

runLevel();
}

void runLevel()
{
m_label->setText(currentLevel);
auto levelData = levelsRoot[currentLevel].toObject();

long duration = levelData["Time"].toDouble();

auto leftArray = levelData["Left"].toArray();
auto rightArray = levelData["Right"].toArray();

setLvlRequirements({leftArray[0].toDouble(), leftArray[1].toDouble()},
{rightArray[0].toDouble(), rightArray[1].toDouble()},
std::chrono::seconds(duration));

m_timer.start(m_timeRequirement);
}

private:
void checkTgtStatus();
void checkTgtStatus()
{
if (m_rightOnTgt && m_leftOnTgt) {
m_timer.start(m_timeRequirement);
}
else {
m_timer.stop();
}
}

private:
QJsonObject levelsRoot;
QString currentLevel;
QTimer m_timer;

ProgressBar *m_leftBar;
ProgressBar *m_rightBar;
TargetT *m_leftBar;
TargetT *m_rightBar;

QLabel *m_label;

TrainingMode<TargetT> *m_parent;

Target m_tgtLeft;
Target m_tgtRight;
Expand Down
4 changes: 3 additions & 1 deletion tools/QtGAITGui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ void MainWindow::onWindowDone(WindowKind wk, int extra)
this);
;
}

break;
case WindowKind::Training:
nextWindow = new FinalWindow();
break;
}
connect(nextWindow, &WindowBase::windowDone, this, &MainWindow::onWindowDone);
Expand Down
8 changes: 8 additions & 0 deletions tools/QtGAITGui/TargetBase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef RTEP_TEAM18_TARGETBASE_H
#define RTEP_TEAM18_TARGETBASE_H

struct Target {
float low, high;
};

#endif // RTEP_TEAM18_TARGETBASE_H
15 changes: 0 additions & 15 deletions tools/QtGAITGui/Training_mode.cpp

This file was deleted.

12 changes: 11 additions & 1 deletion tools/QtGAITGui/Training_mode.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdio.h>
#include <CircleWidget.hpp>
#include <QGridLayout>
#include <QJsonArray>
#include <QLabel>
#include <QLineEdit>
#include <QPainter>
Expand All @@ -13,6 +14,7 @@
#include <QVBoxLayout>
#include <QtWidgets/QMainWindow>
#include <chrono>
#include "Level_weight_shifting.hpp"
#include "WindowBase.hpp"
#include "progressbar.hpp"
#include "telemetry/client.h"
Expand All @@ -27,7 +29,8 @@ class TrainingMode : public WindowBase {
: widget_r(new WidgetT()),
widget_l(new WidgetT()),
level_label(new QLabel()),
instruction_label(new QLabel(instructions))
instruction_label(new QLabel(instructions)),
level_manager(this, widget_l, widget_r, level_label)

{
QPalette pal = palette();
Expand Down Expand Up @@ -73,8 +76,13 @@ class TrainingMode : public WindowBase {
widget_l, &WidgetT::onNewFSRData);
connect(static_cast<MainWindow *>(parent), &MainWindow::newFSRDataR,
widget_r, &WidgetT::onNewFSRData);

level_manager.loadJsonFile("levels.json");
level_manager.start();
}

void emitWindowDone() { emit windowDone(WindowKind::Training, 0); }

private:
WidgetT *widget_r;
WidgetT *widget_l;
Expand All @@ -84,6 +92,8 @@ class TrainingMode : public WindowBase {

QLabel *level_label;
QLabel *instruction_label;

LevelWS<WidgetT> level_manager;
};

#endif /* Training_mode_hpp */
Loading

0 comments on commit ee76b5a

Please sign in to comment.