Skip to content

Commit 8227fa8

Browse files
Maybe fixes accelerometer hangings, some other minor fixes
1 parent 7de47d6 commit 8227fa8

File tree

12 files changed

+79
-4
lines changed

12 files changed

+79
-4
lines changed

trikControl/src/deviceState.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,8 @@ void DeviceState::resetFailure()
133133

134134
mLock.unlock();
135135
}
136+
137+
QString DeviceState::deviceName() const
138+
{
139+
return mDeviceName;
140+
}

trikControl/src/deviceState.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class DeviceState
5959
/// Clears "failed" state and returns state to "off"
6060
void resetFailure();
6161

62+
/// Returns name of the device for debug purposes.
63+
QString deviceName() const;
64+
6265
private:
6366
/// Current state of a device.
6467
DeviceInterface::Status mStatus = DeviceInterface::Status::off;

trikControl/src/vectorSensor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ VectorSensor::VectorSensor(const QString &deviceName, const trikKernel::Configur
4747
VectorSensor::~VectorSensor()
4848
{
4949
if (mWorkerThread.isRunning()) {
50+
QMetaObject::invokeMethod(mVectorSensorWorker.data(), "deinitialize");
5051
mWorkerThread.quit();
5152
mWorkerThread.wait();
5253
}

trikControl/src/vectorSensorWorker.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,40 @@
1616

1717
#include <QsLog.h>
1818

19+
static const int maxEventDelay = 1000;
20+
1921
using namespace trikControl;
2022

2123
VectorSensorWorker::VectorSensorWorker(const QString &eventFile, DeviceState &state
2224
, const trikHal::HardwareAbstractionInterface &hardwareAbstraction)
2325
: mEventFile(hardwareAbstraction.createEventFile(eventFile))
2426
, mState(state)
2527
{
28+
mState.start();
29+
2630
mReading << 0 << 0 << 0;
2731
mReadingUnsynced = mReading;
2832

29-
mState.start();
33+
mLastEventTimer.setInterval(maxEventDelay);
34+
mLastEventTimer.setSingleShot(false);
3035

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

39+
connect(&mLastEventTimer, SIGNAL(timeout()), this, SLOT(onSensorHanged()));
40+
3441
mEventFile->open();
42+
43+
if (mEventFile->isOpened()) {
44+
mLastEventTimer.start();
45+
}
3546
}
3647

3748
void VectorSensorWorker::onNewEvent(trikHal::EventFileInterface::EventType eventType, int code, int value
3849
, const trikKernel::TimeVal &eventTime)
3950
{
51+
mLastEventTimer.start();
52+
4053
switch (eventType) {
4154
case trikHal::EventFileInterface::EventType::evAbsX:
4255
mReadingUnsynced[0] = value;
@@ -52,7 +65,7 @@ void VectorSensorWorker::onNewEvent(trikHal::EventFileInterface::EventType event
5265
emit newData(mReading, eventTime);
5366
break;
5467
default:
55-
QLOG_ERROR() << "Unknown event type in vector sensor event file"<< mEventFile->fileName() << " :"
68+
QLOG_ERROR() << "Unknown event type in vector sensor event file" << mEventFile->fileName() << " :"
5669
<< static_cast<int>(eventType) << code << value;
5770
}
5871
}
@@ -66,3 +79,24 @@ QVector<int> VectorSensorWorker::read()
6679
return {};
6780
}
6881
}
82+
83+
void VectorSensorWorker::deinitialize()
84+
{
85+
mLastEventTimer.stop();
86+
}
87+
88+
void VectorSensorWorker::onSensorHanged()
89+
{
90+
QLOG_WARN() << "Sensor" << mState.deviceName() << "hanged, reopening device file...";
91+
mLastEventTimer.stop();
92+
93+
mEventFile->close();
94+
mEventFile->open();
95+
96+
if (!mEventFile->isOpened()) {
97+
mState.fail();
98+
} else {
99+
QLOG_INFO() << "Sensor" << mState.deviceName() << ", device file reopened.";
100+
mLastEventTimer.start();
101+
}
102+
}

trikControl/src/vectorSensorWorker.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <QtCore/QObject>
1818
#include <QtCore/QScopedPointer>
1919
#include <QtCore/QVector>
20+
#include <QtCore/QTimer>
2021
#include <QtCore/QReadWriteLock>
2122

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

53+
/// Shuts down sensor.
54+
void deinitialize();
55+
5256
private slots:
5357
/// Updates current reading when new value is ready in event file.
5458
void onNewEvent(trikHal::EventFileInterface::EventType eventType, int code, int value
5559
, const trikKernel::TimeVal &eventTime);
5660

61+
/// Called when there are no events from event file for too long (1 second hardcoded). Attempts to reopen
62+
/// event file.
63+
void onSensorHanged();
64+
5765
private:
5866
QScopedPointer<trikHal::EventFileInterface> mEventFile;
5967

@@ -64,6 +72,9 @@ private slots:
6472

6573
/// Device state, shared between worker and proxy.
6674
DeviceState &mState;
75+
76+
/// Timer that reopens event file when there are no events for too long (1 second hardcoded).
77+
QTimer mLastEventTimer;
6778
};
6879

6980
}

trikGui/controller.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ Controller::Controller(const QString &configPath)
7979
mTelemetry->startServer(telemetryPort);
8080

8181
mAutoRunner.reset(new AutoRunner(*this));
82+
83+
mBrick->led()->green();
8284
}
8385

8486
Controller::~Controller()
8587
{
88+
mBrick->led()->orange();
8689
}
8790

8891
void Controller::runFile(const QString &filePath)

trikHal/include/trikHal/eventFileInterface.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class EventFileInterface : public QObject
5858
/// Returns name of a file.
5959
virtual QString fileName() const = 0;
6060

61+
/// Returns true if a file is opened.
62+
virtual bool isOpened() const = 0;
63+
6164
signals:
6265
/// Emitted when there is new event in an event file.
6366
/// @param eventType - type of an event (unknown, if such event is not listed in EventType enum).

trikHal/src/stub/stubEventFile.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,7 @@ QString StubEventFile::fileName() const
4444
return mFileName;
4545
}
4646

47+
bool StubEventFile::isOpened() const
48+
{
49+
return true;
50+
}

trikHal/src/stub/stubEventFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class StubEventFile : public EventFileInterface
3333
bool close() override;
3434
void cancelWaiting() override;
3535
QString fileName() const override;
36+
bool isOpened() const override;
3637

3738
private:
3839
QString mFileName;

trikHal/src/trik/trikEventFile.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,8 @@ void TrikEventFile::readFile()
161161

162162
mSocketNotifier->setEnabled(true);
163163
}
164+
165+
bool TrikEventFile::isOpened() const
166+
{
167+
return mEventFileDescriptor == -1;
168+
}

0 commit comments

Comments
 (0)