Skip to content

Commit

Permalink
Maybe fixes accelerometer hangings, some other minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yurii-litvinov committed Mar 10, 2016
1 parent 7de47d6 commit 8227fa8
Show file tree
Hide file tree
Showing 12 changed files with 79 additions and 4 deletions.
5 changes: 5 additions & 0 deletions trikControl/src/deviceState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,8 @@ void DeviceState::resetFailure()

mLock.unlock();
}

QString DeviceState::deviceName() const
{
return mDeviceName;
}
3 changes: 3 additions & 0 deletions trikControl/src/deviceState.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class DeviceState
/// Clears "failed" state and returns state to "off"
void resetFailure();

/// Returns name of the device for debug purposes.
QString deviceName() const;

private:
/// Current state of a device.
DeviceInterface::Status mStatus = DeviceInterface::Status::off;
Expand Down
1 change: 1 addition & 0 deletions trikControl/src/vectorSensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ VectorSensor::VectorSensor(const QString &deviceName, const trikKernel::Configur
VectorSensor::~VectorSensor()
{
if (mWorkerThread.isRunning()) {
QMetaObject::invokeMethod(mVectorSensorWorker.data(), "deinitialize");
mWorkerThread.quit();
mWorkerThread.wait();
}
Expand Down
38 changes: 36 additions & 2 deletions trikControl/src/vectorSensorWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,40 @@

#include <QsLog.h>

static const int maxEventDelay = 1000;

using namespace trikControl;

VectorSensorWorker::VectorSensorWorker(const QString &eventFile, DeviceState &state
, const trikHal::HardwareAbstractionInterface &hardwareAbstraction)
: mEventFile(hardwareAbstraction.createEventFile(eventFile))
, mState(state)
{
mState.start();

mReading << 0 << 0 << 0;
mReadingUnsynced = mReading;

mState.start();
mLastEventTimer.setInterval(maxEventDelay);
mLastEventTimer.setSingleShot(false);

connect(mEventFile.data(), SIGNAL(newEvent(trikHal::EventFileInterface::EventType, int, int, trikKernel::TimeVal))
, this, SLOT(onNewEvent(trikHal::EventFileInterface::EventType, int, int, trikKernel::TimeVal)));

connect(&mLastEventTimer, SIGNAL(timeout()), this, SLOT(onSensorHanged()));

mEventFile->open();

if (mEventFile->isOpened()) {
mLastEventTimer.start();
}
}

void VectorSensorWorker::onNewEvent(trikHal::EventFileInterface::EventType eventType, int code, int value
, const trikKernel::TimeVal &eventTime)
{
mLastEventTimer.start();

switch (eventType) {
case trikHal::EventFileInterface::EventType::evAbsX:
mReadingUnsynced[0] = value;
Expand All @@ -52,7 +65,7 @@ void VectorSensorWorker::onNewEvent(trikHal::EventFileInterface::EventType event
emit newData(mReading, eventTime);
break;
default:
QLOG_ERROR() << "Unknown event type in vector sensor event file"<< mEventFile->fileName() << " :"
QLOG_ERROR() << "Unknown event type in vector sensor event file" << mEventFile->fileName() << " :"
<< static_cast<int>(eventType) << code << value;
}
}
Expand All @@ -66,3 +79,24 @@ QVector<int> VectorSensorWorker::read()
return {};
}
}

void VectorSensorWorker::deinitialize()
{
mLastEventTimer.stop();
}

void VectorSensorWorker::onSensorHanged()
{
QLOG_WARN() << "Sensor" << mState.deviceName() << "hanged, reopening device file...";
mLastEventTimer.stop();

mEventFile->close();
mEventFile->open();

if (!mEventFile->isOpened()) {
mState.fail();
} else {
QLOG_INFO() << "Sensor" << mState.deviceName() << ", device file reopened.";
mLastEventTimer.start();
}
}
11 changes: 11 additions & 0 deletions trikControl/src/vectorSensorWorker.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <QtCore/QObject>
#include <QtCore/QScopedPointer>
#include <QtCore/QVector>
#include <QtCore/QTimer>
#include <QtCore/QReadWriteLock>

#include <trikHal/hardwareAbstractionInterface.h>
Expand Down Expand Up @@ -49,11 +50,18 @@ public slots:
/// Returns current raw reading of a sensor.
QVector<int> read();

/// Shuts down sensor.
void deinitialize();

private slots:
/// Updates current reading when new value is ready in event file.
void onNewEvent(trikHal::EventFileInterface::EventType eventType, int code, int value
, const trikKernel::TimeVal &eventTime);

/// Called when there are no events from event file for too long (1 second hardcoded). Attempts to reopen
/// event file.
void onSensorHanged();

private:
QScopedPointer<trikHal::EventFileInterface> mEventFile;

Expand All @@ -64,6 +72,9 @@ private slots:

/// Device state, shared between worker and proxy.
DeviceState &mState;

/// Timer that reopens event file when there are no events for too long (1 second hardcoded).
QTimer mLastEventTimer;
};

}
3 changes: 3 additions & 0 deletions trikGui/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ Controller::Controller(const QString &configPath)
mTelemetry->startServer(telemetryPort);

mAutoRunner.reset(new AutoRunner(*this));

mBrick->led()->green();
}

Controller::~Controller()
{
mBrick->led()->orange();
}

void Controller::runFile(const QString &filePath)
Expand Down
3 changes: 3 additions & 0 deletions trikHal/include/trikHal/eventFileInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class EventFileInterface : public QObject
/// Returns name of a file.
virtual QString fileName() const = 0;

/// Returns true if a file is opened.
virtual bool isOpened() const = 0;

signals:
/// Emitted when there is new event in an event file.
/// @param eventType - type of an event (unknown, if such event is not listed in EventType enum).
Expand Down
4 changes: 4 additions & 0 deletions trikHal/src/stub/stubEventFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ QString StubEventFile::fileName() const
return mFileName;
}

bool StubEventFile::isOpened() const
{
return true;
}
1 change: 1 addition & 0 deletions trikHal/src/stub/stubEventFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class StubEventFile : public EventFileInterface
bool close() override;
void cancelWaiting() override;
QString fileName() const override;
bool isOpened() const override;

private:
QString mFileName;
Expand Down
5 changes: 5 additions & 0 deletions trikHal/src/trik/trikEventFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,8 @@ void TrikEventFile::readFile()

mSocketNotifier->setEnabled(true);
}

bool TrikEventFile::isOpened() const
{
return mEventFileDescriptor == -1;
}
1 change: 1 addition & 0 deletions trikHal/src/trik/trikEventFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class TrikEventFile : public EventFileInterface
bool close() override;
void cancelWaiting() override;
QString fileName() const override;
bool isOpened() const override;

private slots:
/// Tries to open event file and if opened successfully stops waiting event loop.
Expand Down
8 changes: 6 additions & 2 deletions trikNetwork/src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ void Connection::send(const QByteArray &data)
return;
}

QLOG_INFO() << "Sending:" << data << " to" << peerAddress() << ":" << peerPort();
if (data != "keepalive") {
QLOG_INFO() << "Sending:" << data << " to" << peerAddress() << ":" << peerPort();
}

if (mUseHeartbeat) {
/// Reset keepalive timer to avoid spamming with keepalive packets.
Expand Down Expand Up @@ -135,7 +137,9 @@ void Connection::onReadyRead()
const QByteArray &data = mSocket->readAll();
mBuffer.append(data);

QLOG_INFO() << "Received from" << peerAddress() << ":" << peerPort() << ":" << mBuffer;
if (mBuffer != "9:keepalive") {
QLOG_INFO() << "Received from" << peerAddress() << ":" << peerPort() << ":" << mBuffer;
}

processBuffer();
}
Expand Down

0 comments on commit 8227fa8

Please sign in to comment.