Permalink
Browse files

AR.Drone somewhat working in the UI

  • Loading branch information...
bmcdorman committed Mar 20, 2013
1 parent b76ecb1 commit 7e043156f2f03b42dfe2e9dd174992d96160eece
@@ -9,12 +9,18 @@
class CameraInputManager : public QObject, public Singleton<CameraInputManager>
{
Q_OBJECT
-
public:
+ enum Source {
+ Nil = 0,
+ UsbCamera,
+ ARDroneFront,
+ ARDroneBottom
+ };
+
CameraInputManager();
- void setInputProvider(Camera::InputProvider *inputProvider);
- Camera::InputProvider *inputProvider() const;
+ void setSource(const Source source);
+ Source source() const;
void setFrameRate(int frameRate);
int frameRate() const;
@@ -36,11 +42,16 @@ public slots:
void updateCamera();
private:
+ void setInputProvider(Camera::InputProvider *const inputProvider);
+ Camera::InputProvider *inputProvider() const;
+
Camera::InputProvider *m_inputProvider;
+ Source m_source;
QTimer *m_timer;
cv::Mat m_image;
int m_frameRate;
int m_refs;
+ bool m_reentryBarrier;
};
class CameraInputAdapter : public Camera::InputProvider
@@ -1,18 +1,25 @@
#ifndef _CAMERAINPUTSELECTORWIDGET_H_
#define _CAMERAINPUTSELECTORWIDGET_H_
-#include "StandardWidge.h"
+#include "StandardWidget.h"
-class CameraInputSelectorWidget
+namespace Ui
{
+ class CameraInputSelectorWidget;
+}
+
+class CameraInputSelectorWidget : public StandardWidget
+{
+Q_OBJECT
public:
CameraInputSelectorWidget(Device *device, QWidget *parent = 0);
~CameraInputSelectorWidget();
-
-
private slots:
+ void sourceIndexChanged(const int index);
+private:
+ Ui::CameraInputSelectorWidget *ui;
};
#endif
View
@@ -25,11 +25,13 @@ Q_OBJECT
public slots:
void updateImage();
void currentIndexChanged(const int &index);
+ void selectSource();
private:
Ui::CameraWidget *ui;
Camera::Device *m_device;
ChannelConfigurationsModel *m_model;
+ bool m_reentryBarrier;
};
#endif
@@ -0,0 +1,16 @@
+#ifndef _CONNECTIONEDARDRONEINPUTPROVIDER_H_
+#define _CONNECTIONEDARDRONEINPUTPROVIDER_H_
+
+#include <kovan/ardrone.hpp>
+
+namespace Camera
+{
+ class ConnectionedARDroneInputProvider : public ARDroneInputProvider
+ {
+ public:
+ virtual bool open(const int number);
+ virtual bool close();
+ };
+}
+
+#endif
View
@@ -5,9 +5,9 @@
class CreateWidget : public StandardWidget
{
- Q_OBJECT
+Q_OBJECT
public:
- CreateWidget();
+ CreateWidget(Device *device, QWidget *parent = 0);
~CreateWidget();
private slots:
View
@@ -16,6 +16,7 @@ Q_PROPERTY(bool invalid READ invalid WRITE setInvalid)
void setInvalid(bool invalid);
const bool& invalid() const;
+public slots:
void updateImage(cv::Mat image);
signals:
@@ -31,6 +32,7 @@ Q_PROPERTY(bool invalid READ invalid WRITE setInvalid)
bool m_invalid;
cv::Mat m_image;
+ unsigned char *m_data;
QImage m_resizedImage;
};
@@ -19,7 +19,7 @@ AccessoriesWidget::~AccessoriesWidget()
void AccessoriesWidget::create()
{
- RootController::ref().presentWidget(new CreateWidget());
+ RootController::ref().presentWidget(new CreateWidget(device()));
}
void AccessoriesWidget::ardrone()
View
@@ -1,31 +1,46 @@
#include "CameraInputManager.h"
+#include "ConnectionedARDroneInputProvider.h"
#include <QDebug>
CameraInputManager::CameraInputManager()
- : m_timer(new QTimer(this)),
+ : m_inputProvider(new Camera::UsbInputProvider()),
+ m_source(CameraInputManager::UsbCamera),
+ m_timer(new QTimer(this)),
m_frameRate(1),
- m_refs(0)
+ m_refs(0),
+ m_reentryBarrier(false)
{
m_image = cv::Mat();
connect(m_timer, SIGNAL(timeout()), SLOT(updateCamera()));
}
-void CameraInputManager::setInputProvider(Camera::InputProvider *inputProvider)
+void CameraInputManager::setSource(const CameraInputManager::Source source)
{
- if(m_inputProvider) m_inputProvider->close();
- delete m_inputProvider;
- m_inputProvider = inputProvider;
+ m_source = source;
+
+ Camera::InputProvider *inputProvider = 0;
+ switch(m_source) {
+ case CameraInputManager::UsbCamera:
+ inputProvider = new Camera::UsbInputProvider;
+ break;
+ case CameraInputManager::ARDroneFront:
+ inputProvider = new Camera::ConnectionedARDroneInputProvider;
+ break;
+ default: break;
+ }
+
+ setInputProvider(inputProvider);
}
-Camera::InputProvider *CameraInputManager::inputProvider() const
+CameraInputManager::Source CameraInputManager::source() const
{
- return m_inputProvider;
+ return m_source;
}
void CameraInputManager::setFrameRate(int frameRate)
{
m_frameRate = frameRate;
- m_timer->start(1000/frameRate);
+ m_timer->start(1000.0 / m_frameRate);
}
int CameraInputManager::frameRate() const
@@ -82,26 +97,52 @@ cv::Mat CameraInputManager::image() const
void CameraInputManager::updateCamera()
{
- if(!m_refs) return;
+ if(m_reentryBarrier) return;
+ m_reentryBarrier = true;
+
+ if(!m_refs || !m_inputProvider) {
+ setFrameRate(1);
+ m_reentryBarrier = false;
+ return;
+ }
if(!m_inputProvider->isOpen()) {
if(!m_inputProvider->open(0)) {
+ qDebug() << "Failed to open input provider... for now.";
setFrameRate(1);
+ m_reentryBarrier = false;
return;
}
- //m_inputProvider->setWidth(320);
- //m_inputProvider->setHeight(240);
+ // m_inputProvider->setWidth(320);
+ // m_inputProvider->setHeight(240);
setFrameRate(20);
}
if(!m_inputProvider->next(m_image)) {
qWarning() << "Camera update failed";
- //ui->camera->setInvalid(true);
+ // ui->camera->setInvalid(true);
m_inputProvider->close();
setFrameRate(1);
m_image = cv::Mat();
}
emit frameAvailable(m_image);
+ m_reentryBarrier = false;
+}
+
+void CameraInputManager::setInputProvider(Camera::InputProvider *inputProvider)
+{
+ const bool wasOpened = m_inputProvider ? m_inputProvider->isOpen() : false;
+ if(m_inputProvider) m_inputProvider->close();
+ delete m_inputProvider;
+
+ m_inputProvider = inputProvider;
+
+ if(m_inputProvider && wasOpened) m_inputProvider->open(0);
+}
+
+Camera::InputProvider *CameraInputManager::inputProvider() const
+{
+ return m_inputProvider;
}
CameraInputAdapter::CameraInputAdapter(CameraInputManager *manager)
@@ -0,0 +1,32 @@
+#include "CameraInputSelectorWidget.h"
+#include "ui_CameraInputSelectorWidget.h"
+
+#include "CameraInputManager.h"
+
+CameraInputSelectorWidget::CameraInputSelectorWidget(Device *device, QWidget *parent)
+ : StandardWidget(device, parent),
+ ui(new Ui::CameraInputSelectorWidget)
+{
+ ui->setupUi(this);
+ performStandardSetup(tr("Camera Input Selector"), false);
+
+ CameraInputManager::ref().retain();
+ connect(ui->source, SIGNAL(currentIndexChanged(int)), SLOT(sourceIndexChanged(int)));
+ ui->camera->setInvalid(false);
+ ui->camera->connect(&CameraInputManager::ref(), SIGNAL(frameAvailable(cv::Mat)),
+ SLOT(updateImage(cv::Mat)));
+ ui->source->setCurrentIndex((int)CameraInputManager::ref().source() - 1);
+}
+
+CameraInputSelectorWidget::~CameraInputSelectorWidget()
+{
+ CameraInputManager::ref().release();
+ delete ui;
+}
+
+void CameraInputSelectorWidget::sourceIndexChanged(const int index)
+{
+ // TODO: This is pretty bad...
+ CameraInputManager::Source source = (CameraInputManager::Source)(index + 1);
+ CameraInputManager::ref().setSource(source);
+}
View
@@ -2,10 +2,14 @@
#include "ui_CameraWidget.h"
#include "Device.h"
#include "ChannelConfigurationsModel.h"
+#include "CameraInputSelectorWidget.h"
+#include "RootController.h"
#include "CameraInputManager.h"
+#include "MenuBar.h"
#include <QDebug>
#include <kovan/camera.hpp>
+#include <opencv2/highgui/highgui.hpp>
CameraWidget::CameraWidget(Device *device, QWidget *parent)
: StandardWidget(device, parent),
@@ -15,41 +19,40 @@ CameraWidget::CameraWidget(Device *device, QWidget *parent)
{
ui->setupUi(this);
performStandardSetup(tr("Camera"), false);
+ QAction *selectSource = menuBar()->addAction(tr("Source..."));
+ connect(selectSource, SIGNAL(activated()), SLOT(selectSource()));
ui->config->setModel(m_model);
- qDebug() << m_model->rootPath();
ui->config->setRootModelIndex(m_model->index(m_model->rootPath()));
ui->config->setCurrentIndex(m_model->defaultConfiguration().row());
connect(ui->config, SIGNAL(currentIndexChanged(int)), SLOT(currentIndexChanged(int)));
- CameraInputManager::ref().setInputProvider(new Camera::UsbInputProvider);
- connect(&CameraInputManager::ref(), SIGNAL(frameAvailable(cv::Mat)), SLOT(updateImage()));
m_device->open();
- updateImage();
+ connect(&CameraInputManager::ref(), SIGNAL(frameAvailable(cv::Mat)), SLOT(updateImage()));
}
CameraWidget::~CameraWidget()
{
- qWarning() << "Destroyed!";
m_device->close();
delete ui;
delete m_device;
}
void CameraWidget::updateImage()
{
+ if(!isVisible()) return;
if(!m_device->update()) {
qWarning() << "camera update failed";
- ui->camera->setInvalid(true);
return;
- } else ui->camera->setInvalid(false);
+ }
+ cv::Mat image = m_device->rawImage();
int h = 0;
const static int hStep = 137; // Golden angle
Camera::ChannelPtrVector::const_iterator it = m_device->channels().begin();
- cv::Mat image = m_device->rawImage();
+ // cv::imshow("Device Image", image);
for(; it != m_device->channels().end(); ++it, h += hStep) {
const QColor rectColor = QColor::fromHsv(h % 360, 255, 255);
const Camera::ObjectVector *objs = (*it)->objects();
@@ -62,7 +65,6 @@ void CameraWidget::updateImage()
}
}
- if(image.empty()) qWarning() << "EMPTY!";
ui->camera->updateImage(image);
}
@@ -74,3 +76,8 @@ void CameraWidget::currentIndexChanged(const int &index)
m_device->setConfig(conf ? *conf : Config());
delete conf;
}
+
+void CameraWidget::selectSource()
+{
+ RootController::ref().presentWidget(new CameraInputSelectorWidget(device()));
+}
@@ -0,0 +1,17 @@
+#include "ConnectionedARDroneInputProvider.h"
+
+using namespace Camera;
+
+bool ConnectionedARDroneInputProvider::open(const int number)
+{
+ if(!ARDrone::instance()->connect()) return false;
+ bool ret = ARDroneInputProvider::open(number);
+ return ret;
+}
+
+bool ConnectionedARDroneInputProvider::close()
+{
+ bool ret = ARDroneInputProvider::close();
+ if(ret) ARDrone::instance()->disconnect();
+ return ret;
+}
Oops, something went wrong.

0 comments on commit 7e04315

Please sign in to comment.