From 0f797e305d9fb7f08afdecbde669f22464213ce7 Mon Sep 17 00:00:00 2001 From: Jake Drahos <j@kedrahos.com> Date: Tue, 21 Mar 2017 21:13:10 -0500 Subject: [PATCH] Implemented VRPN --- groundStation/gui/MicroCART/MicroCART.pro | 2 + .../gui/MicroCART/MicroCART.pro.user | 4 +- groundStation/gui/MicroCART/mainwindow.cpp | 39 ++++++++++++--- groundStation/gui/MicroCART/mainwindow.h | 7 +++ groundStation/gui/MicroCART/mainwindow.ui | 18 +++---- groundStation/gui/MicroCART/trackerworker.cpp | 24 +++++---- groundStation/gui/MicroCART/trackerworker.h | 5 +- groundStation/gui/MicroCART/wrappers.c | 50 +++++++++++++++++-- groundStation/gui/MicroCART/wrappers.h | 7 ++- 9 files changed, 123 insertions(+), 33 deletions(-) diff --git a/groundStation/gui/MicroCART/MicroCART.pro b/groundStation/gui/MicroCART/MicroCART.pro index 7bcc5c374..ba492f24e 100644 --- a/groundStation/gui/MicroCART/MicroCART.pro +++ b/groundStation/gui/MicroCART/MicroCART.pro @@ -11,6 +11,8 @@ greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = MicroCART TEMPLATE = app +LIBS += ../../frontend.a +INCLUDEPATH += ../../src/frontend/ SOURCES += main.cpp\ mainwindow.cpp \ diff --git a/groundStation/gui/MicroCART/MicroCART.pro.user b/groundStation/gui/MicroCART/MicroCART.pro.user index d8c1922dd..64f95e242 100644 --- a/groundStation/gui/MicroCART/MicroCART.pro.user +++ b/groundStation/gui/MicroCART/MicroCART.pro.user @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <!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> <data> <variable>EnvironmentId</variable> @@ -8,7 +8,7 @@ </data> <data> <variable>ProjectExplorer.Project.ActiveTarget</variable> - <value type="int">-1</value> + <value type="int">0</value> </data> <data> <variable>ProjectExplorer.Project.EditorSettings</variable> diff --git a/groundStation/gui/MicroCART/mainwindow.cpp b/groundStation/gui/MicroCART/mainwindow.cpp index bb6976bd5..8df17de0b 100644 --- a/groundStation/gui/MicroCART/mainwindow.cpp +++ b/groundStation/gui/MicroCART/mainwindow.cpp @@ -10,7 +10,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), - ui(new Ui::MainWindow) + ui(new Ui::MainWindow), + backendPid(0), + backendPipe(-1) { ui->setupUi(this); @@ -27,8 +29,10 @@ MainWindow::MainWindow(QWidget *parent) : /* Connect tracker worker */ connect(trackerWorker, SIGNAL (finished(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 */ @@ -41,6 +45,9 @@ MainWindow::MainWindow(QWidget *parent) : /* Start the things */ trackerTimer->start(300); workerThread->start(); + + /* Create a timer to poll stdout and populate virtual console */ + QTimer * consoleTimer = new QTimer(this); } MainWindow::~MainWindow() @@ -48,6 +55,21 @@ MainWindow::~MainWindow() 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) { 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 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 *>("pbConnect")->setEnabled(false); findChild<QPushButton *>("pbStop")->setEnabled(true); backendState = 1; } @@ -72,12 +93,18 @@ void MainWindow::on_pbConnect_clicked() findChild<QPushButton *>("pbStart")->setEnabled(false); findChild<QPushButton *>("pbConnect")->setEnabled(false); findChild<QPushButton *>("pbStop")->setEnabled(true); + emit(connectWorkers()); backendState = 1; } 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 *>("pbConnect")->setEnabled(true); findChild<QPushButton *>("pbStop")->setEnabled(false); diff --git a/groundStation/gui/MicroCART/mainwindow.h b/groundStation/gui/MicroCART/mainwindow.h index f54a88e37..042e5f73c 100644 --- a/groundStation/gui/MicroCART/mainwindow.h +++ b/groundStation/gui/MicroCART/mainwindow.h @@ -15,6 +15,10 @@ public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); +signals: + void connectWorkers(); + void disconnectWorkers(); + private slots: void on_pbStart_clicked(); @@ -26,9 +30,12 @@ private slots: void updateTracker(float x, float y, float z, float p, float r, float yaw); + void updateConsole(); + private: Ui::MainWindow *ui; pid_t backendPid; + int backendPipe; int backendState; }; diff --git a/groundStation/gui/MicroCART/mainwindow.ui b/groundStation/gui/MicroCART/mainwindow.ui index f2e2ab683..5fc49d1e9 100644 --- a/groundStation/gui/MicroCART/mainwindow.ui +++ b/groundStation/gui/MicroCART/mainwindow.ui @@ -6,8 +6,8 @@ <rect> <x>0</x> <y>0</y> - <width>569</width> - <height>427</height> + <width>652</width> + <height>454</height> </rect> </property> <property name="windowTitle"> @@ -19,8 +19,8 @@ <rect> <x>10</x> <y>10</y> - <width>531</width> - <height>281</height> + <width>461</width> + <height>341</height> </rect> </property> <property name="sizePolicy"> @@ -30,7 +30,7 @@ </sizepolicy> </property> <property name="currentIndex"> - <number>1</number> + <number>0</number> </property> <widget class="QWidget" name="backend"> <attribute name="title"> @@ -42,7 +42,7 @@ <x>0</x> <y>0</y> <width>451</width> - <height>231</height> + <height>296</height> </rect> </property> <layout class="QVBoxLayout" name="verticalLayout"> @@ -105,7 +105,7 @@ </spacer> </item> <item> - <widget class="QTextEdit" name="textEdit"> + <widget class="QTextEdit" name="vConsole"> <property name="enabled"> <bool>true</bool> </property> @@ -233,7 +233,7 @@ <property name="geometry"> <rect> <x>10</x> - <y>290</y> + <y>350</y> <width>88</width> <height>34</height> </rect> @@ -248,7 +248,7 @@ <rect> <x>0</x> <y>0</y> - <width>569</width> + <width>652</width> <height>30</height> </rect> </property> diff --git a/groundStation/gui/MicroCART/trackerworker.cpp b/groundStation/gui/MicroCART/trackerworker.cpp index 46bb0ab51..f5d3cda28 100644 --- a/groundStation/gui/MicroCART/trackerworker.cpp +++ b/groundStation/gui/MicroCART/trackerworker.cpp @@ -1,33 +1,37 @@ #include "trackerworker.h" #include <QThread> +#include "frontend_common.h" +#include "frontend_tracker.h" TrackerWorker::TrackerWorker() : - QObject(), run(true) + QObject(), conn(NULL) { } TrackerWorker::~TrackerWorker() { + disconnectBackend(); } void TrackerWorker::connectBackend() { + conn = ucart_backendConnect(); } void TrackerWorker::disconnectBackend() { + if (conn) { + ucart_backendDisconnect(conn); + conn = NULL; + } } void TrackerWorker::process() { - /* Fake data. SAD! */ - static float x = 1.0f, y = 2.0f, z = 3.0f, p = 4.0f, r = 5.0f, yaw = 6.0f; - x += 1.0; - y += 2.0; - z += 3.0; - p += 4.0; - r += 5.0; - yaw += 6.0; + if (conn) { + struct frontend_tracker_data td; + frontend_track(conn, &td); - emit finished(x, y, z, p, r, yaw); + emit finished(td.height, td.lateral, td.longitudinal, td.pitch, td.roll, td.yaw); + } } diff --git a/groundStation/gui/MicroCART/trackerworker.h b/groundStation/gui/MicroCART/trackerworker.h index dd72c9edf..b263d6078 100644 --- a/groundStation/gui/MicroCART/trackerworker.h +++ b/groundStation/gui/MicroCART/trackerworker.h @@ -3,6 +3,9 @@ #include <QObject> +#include "frontend_common.h" +#include "frontend_tracker.h" + class TrackerWorker : public QObject { Q_OBJECT @@ -19,7 +22,7 @@ public slots: void disconnectBackend(); private: - bool run; + struct backend_conn * conn; }; #endif // TRACKERWORKER_H diff --git a/groundStation/gui/MicroCART/wrappers.c b/groundStation/gui/MicroCART/wrappers.c index f5e598c68..45fdf0b28 100644 --- a/groundStation/gui/MicroCART/wrappers.c +++ b/groundStation/gui/MicroCART/wrappers.c @@ -1,24 +1,68 @@ #include "wrappers.h" #include <sys/types.h> #include <sys/wait.h> +#include <sys/socket.h> #include <unistd.h> #include <signal.h> +#include <err.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); int status; wait(&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(); 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); } + + /* 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; } diff --git a/groundStation/gui/MicroCART/wrappers.h b/groundStation/gui/MicroCART/wrappers.h index eea3e1b19..6a29ecf90 100644 --- a/groundStation/gui/MicroCART/wrappers.h +++ b/groundStation/gui/MicroCART/wrappers.h @@ -6,8 +6,11 @@ extern "C" { #endif -int startBackend(const char * backend); -int stopBackend(int pid); +#include <stdio.h> + +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 } -- GitLab