Skip to content
Snippets Groups Projects
Unverified Commit 0f797e30 authored by Jake Drahos's avatar Jake Drahos
Browse files

Implemented VRPN

parent c34860ec
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,8 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets ...@@ -11,6 +11,8 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MicroCART TARGET = MicroCART
TEMPLATE = app TEMPLATE = app
LIBS += ../../frontend.a
INCLUDEPATH += ../../src/frontend/
SOURCES += main.cpp\ SOURCES += main.cpp\
mainwindow.cpp \ mainwindow.cpp \
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 3.2.2, 2017-03-05T17:14:24. --> <!-- Written by QtCreator 3.2.2, 2017-03-06T11:03:28. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
</data> </data>
<data> <data>
<variable>ProjectExplorer.Project.ActiveTarget</variable> <variable>ProjectExplorer.Project.ActiveTarget</variable>
<value type="int">-1</value> <value type="int">0</value>
</data> </data>
<data> <data>
<variable>ProjectExplorer.Project.EditorSettings</variable> <variable>ProjectExplorer.Project.EditorSettings</variable>
......
...@@ -10,7 +10,9 @@ ...@@ -10,7 +10,9 @@
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent), QMainWindow(parent),
ui(new Ui::MainWindow) ui(new Ui::MainWindow),
backendPid(0),
backendPipe(-1)
{ {
ui->setupUi(this); ui->setupUi(this);
...@@ -27,8 +29,10 @@ MainWindow::MainWindow(QWidget *parent) : ...@@ -27,8 +29,10 @@ MainWindow::MainWindow(QWidget *parent) :
/* Connect tracker worker */ /* Connect tracker worker */
connect(trackerWorker, SIGNAL (finished(float, float, float, float, float, float)), connect(trackerWorker, SIGNAL (finished(float, float, float, float, float, float)),
this, SLOT (updateTracker(float, float, float, float, float, float))); this, SLOT (updateTracker(float, float, float, float, float, float)));
connect(findChild<QPushButton *>("pbStart"), SIGNAL (clicked()), trackerWorker, SLOT (connectBackend()));
connect(findChild<QPushButton *>("pbConnect"), SIGNAL (clicked()), trackerWorker, SLOT (connectBackend())); /* Connect and disconnect from backend when signals emitted */
connect(this, SIGNAL (connectWorkers()), trackerWorker, SLOT (connectBackend()));
connect(this, SIGNAL (disconnectWorkers()), trackerWorker, SLOT (disconnectBackend()));
/* Create other workers and add them to the worker thread, then connect them */ /* Create other workers and add them to the worker thread, then connect them */
...@@ -41,6 +45,9 @@ MainWindow::MainWindow(QWidget *parent) : ...@@ -41,6 +45,9 @@ MainWindow::MainWindow(QWidget *parent) :
/* Start the things */ /* Start the things */
trackerTimer->start(300); trackerTimer->start(300);
workerThread->start(); workerThread->start();
/* Create a timer to poll stdout and populate virtual console */
QTimer * consoleTimer = new QTimer(this);
} }
MainWindow::~MainWindow() MainWindow::~MainWindow()
...@@ -48,6 +55,21 @@ MainWindow::~MainWindow() ...@@ -48,6 +55,21 @@ MainWindow::~MainWindow()
delete ui; delete ui;
} }
void MainWindow::updateConsole()
{
if (backendPipe != -1) {
char buf[256];
size_t len = 0;
len = readBackend(backendPipe, buf, len);
printf("%d\n", len);
if (len > 0) {
printf("From pipe: %s", buf);
QLineEdit * con = findChild<QLineEdit *>("vConsole");
con->setText(con->text().append(buf));
}
}
}
void MainWindow::updateTracker(float x, float y, float z, float p, float r, float yaw) void MainWindow::updateTracker(float x, float y, float z, float p, float r, float yaw)
{ {
findChild<QLineEdit *>("xLineEdit")->setText(QString::number(x)); findChild<QLineEdit *>("xLineEdit")->setText(QString::number(x));
...@@ -60,9 +82,8 @@ void MainWindow::updateTracker(float x, float y, float z, float p, float r, floa ...@@ -60,9 +82,8 @@ void MainWindow::updateTracker(float x, float y, float z, float p, float r, floa
void MainWindow::on_pbStart_clicked() void MainWindow::on_pbStart_clicked()
{ {
this->backendPid = startBackend(findChild<QLineEdit *>("backendPath")->text().toStdString().c_str()); this->backendPid = startBackend(findChild<QLineEdit *>("backendPath")->text().toStdString().c_str(), &backendPipe);
findChild<QPushButton *>("pbStart")->setEnabled(false); findChild<QPushButton *>("pbStart")->setEnabled(false);
findChild<QPushButton *>("pbConnect")->setEnabled(false);
findChild<QPushButton *>("pbStop")->setEnabled(true); findChild<QPushButton *>("pbStop")->setEnabled(true);
backendState = 1; backendState = 1;
} }
...@@ -72,12 +93,18 @@ void MainWindow::on_pbConnect_clicked() ...@@ -72,12 +93,18 @@ void MainWindow::on_pbConnect_clicked()
findChild<QPushButton *>("pbStart")->setEnabled(false); findChild<QPushButton *>("pbStart")->setEnabled(false);
findChild<QPushButton *>("pbConnect")->setEnabled(false); findChild<QPushButton *>("pbConnect")->setEnabled(false);
findChild<QPushButton *>("pbStop")->setEnabled(true); findChild<QPushButton *>("pbStop")->setEnabled(true);
emit(connectWorkers());
backendState = 1; backendState = 1;
} }
void MainWindow::on_pbStop_clicked() void MainWindow::on_pbStop_clicked()
{ {
stopBackend(backendPid); emit(disconnectWorkers());
if (backendPid) {
stopBackend(backendPid, backendPipe);
backendPipe = -1;
backendPid = 0;
}
findChild<QPushButton *>("pbStart")->setEnabled(true); findChild<QPushButton *>("pbStart")->setEnabled(true);
findChild<QPushButton *>("pbConnect")->setEnabled(true); findChild<QPushButton *>("pbConnect")->setEnabled(true);
findChild<QPushButton *>("pbStop")->setEnabled(false); findChild<QPushButton *>("pbStop")->setEnabled(false);
......
...@@ -15,6 +15,10 @@ public: ...@@ -15,6 +15,10 @@ public:
explicit MainWindow(QWidget *parent = 0); explicit MainWindow(QWidget *parent = 0);
~MainWindow(); ~MainWindow();
signals:
void connectWorkers();
void disconnectWorkers();
private slots: private slots:
void on_pbStart_clicked(); void on_pbStart_clicked();
...@@ -26,9 +30,12 @@ private slots: ...@@ -26,9 +30,12 @@ private slots:
void updateTracker(float x, float y, float z, float p, float r, float yaw); void updateTracker(float x, float y, float z, float p, float r, float yaw);
void updateConsole();
private: private:
Ui::MainWindow *ui; Ui::MainWindow *ui;
pid_t backendPid; pid_t backendPid;
int backendPipe;
int backendState; int backendState;
}; };
......
...@@ -6,8 +6,8 @@ ...@@ -6,8 +6,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>569</width> <width>652</width>
<height>427</height> <height>454</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
...@@ -19,8 +19,8 @@ ...@@ -19,8 +19,8 @@
<rect> <rect>
<x>10</x> <x>10</x>
<y>10</y> <y>10</y>
<width>531</width> <width>461</width>
<height>281</height> <height>341</height>
</rect> </rect>
</property> </property>
<property name="sizePolicy"> <property name="sizePolicy">
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
</sizepolicy> </sizepolicy>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>1</number> <number>0</number>
</property> </property>
<widget class="QWidget" name="backend"> <widget class="QWidget" name="backend">
<attribute name="title"> <attribute name="title">
...@@ -42,7 +42,7 @@ ...@@ -42,7 +42,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>451</width> <width>451</width>
<height>231</height> <height>296</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout"> <layout class="QVBoxLayout" name="verticalLayout">
...@@ -105,7 +105,7 @@ ...@@ -105,7 +105,7 @@
</spacer> </spacer>
</item> </item>
<item> <item>
<widget class="QTextEdit" name="textEdit"> <widget class="QTextEdit" name="vConsole">
<property name="enabled"> <property name="enabled">
<bool>true</bool> <bool>true</bool>
</property> </property>
...@@ -233,7 +233,7 @@ ...@@ -233,7 +233,7 @@
<property name="geometry"> <property name="geometry">
<rect> <rect>
<x>10</x> <x>10</x>
<y>290</y> <y>350</y>
<width>88</width> <width>88</width>
<height>34</height> <height>34</height>
</rect> </rect>
...@@ -248,7 +248,7 @@ ...@@ -248,7 +248,7 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>569</width> <width>652</width>
<height>30</height> <height>30</height>
</rect> </rect>
</property> </property>
......
#include "trackerworker.h" #include "trackerworker.h"
#include <QThread> #include <QThread>
#include "frontend_common.h"
#include "frontend_tracker.h"
TrackerWorker::TrackerWorker() : TrackerWorker::TrackerWorker() :
QObject(), run(true) QObject(), conn(NULL)
{ {
} }
TrackerWorker::~TrackerWorker() TrackerWorker::~TrackerWorker()
{ {
disconnectBackend();
} }
void TrackerWorker::connectBackend() void TrackerWorker::connectBackend()
{ {
conn = ucart_backendConnect();
} }
void TrackerWorker::disconnectBackend() void TrackerWorker::disconnectBackend()
{ {
if (conn) {
ucart_backendDisconnect(conn);
conn = NULL;
}
} }
void TrackerWorker::process() void TrackerWorker::process()
{ {
/* Fake data. SAD! */ if (conn) {
static float x = 1.0f, y = 2.0f, z = 3.0f, p = 4.0f, r = 5.0f, yaw = 6.0f; struct frontend_tracker_data td;
x += 1.0; frontend_track(conn, &td);
y += 2.0;
z += 3.0;
p += 4.0;
r += 5.0;
yaw += 6.0;
emit finished(x, y, z, p, r, yaw); emit finished(td.height, td.lateral, td.longitudinal, td.pitch, td.roll, td.yaw);
}
} }
...@@ -3,6 +3,9 @@ ...@@ -3,6 +3,9 @@
#include <QObject> #include <QObject>
#include "frontend_common.h"
#include "frontend_tracker.h"
class TrackerWorker : public QObject class TrackerWorker : public QObject
{ {
Q_OBJECT Q_OBJECT
...@@ -19,7 +22,7 @@ public slots: ...@@ -19,7 +22,7 @@ public slots:
void disconnectBackend(); void disconnectBackend();
private: private:
bool run; struct backend_conn * conn;
}; };
#endif // TRACKERWORKER_H #endif // TRACKERWORKER_H
#include "wrappers.h" #include "wrappers.h"
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <err.h>
#include <stdlib.h> #include <stdlib.h>
int stopBackend(int pid)
size_t readBackend(int fd, char * buf, size_t maxlen)
{ {
int len = read(fd, buf, maxlen);
if (len < 0) {
len = 0;
}
return len;
}
int stopBackend(int pid, int pipefd)
{
close(pipefd);
kill(pid, SIGTERM); kill(pid, SIGTERM);
int status; int status;
wait(&status); wait(&status);
return status; return status;
} }
int startBackend(const char * backend) int startBackend(const char * backend, int * pipefd)
{ {
/* Create a pipe */
int pipe_fds[2];
if (pipe(pipe_fds)) {
warn("Failed to open pipe, cannot start backend!");
return -1;
}
int pid = fork(); int pid = fork();
if (!pid) { if (!pid) {
execl(backend, "backEnd", NULL); /* Child closes read end of pipe */
close(pipe_fds[0]);
write(pipe_fds[1], "From child", 11);
/* dup write end of pipe to stdout (FD 1), closing old stdout */
//dup2(pipe_fds[1], 1);
//dup2(pipe_fds[1], 2);
write(2, "Child stderr\n", 13);
printf("Child stdout\n");
/* Don't need whatever FD is in pipe_fds[1] anymore, since it is stdout now */
close(pipe_fds[1]);
execl(backend, "BackEnd", NULL);
exit(0); exit(0);
} }
/* Return the read end of the pipe in pipefd pointer */
*pipefd = pipe_fds[0];
/* Parent closes write end of the pipe */
close(pipe_fds[1]);
return pid; return pid;
} }
...@@ -6,8 +6,11 @@ extern "C" ...@@ -6,8 +6,11 @@ extern "C"
{ {
#endif #endif
int startBackend(const char * backend); #include <stdio.h>
int stopBackend(int pid);
int startBackend(const char * backend, int * pipefd);
int stopBackend(int pid, int pipefd);
size_t readBackend(int fd, char * buf, size_t maxlen);
#ifdef __cplusplus #ifdef __cplusplus
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment