Skip to content

Commit e0fbb95

Browse files
Fixed gyroscope and, hopefully, accelerometer
1 parent 7962990 commit e0fbb95

File tree

7 files changed

+129
-25
lines changed

7 files changed

+129
-25
lines changed

global.pri

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ CONFIG(debug, debug | release) {
4040

4141
DESTDIR = $$PWD/bin/$$CONFIGURATION
4242

43-
TARGET = $$basename($$_PRO_FILE_)$$CONFIGURATION_SUFFIX
43+
PROJECT_BASENAME = $$basename(_PRO_FILE_)
44+
PROJECT_NAME = $$section(PROJECT_BASENAME, ".", 0, 0)
45+
46+
TARGET = $$PROJECT_NAME$$CONFIGURATION_SUFFIX
4447

4548
equals(TEMPLATE, app) {
4649
!macx {
@@ -54,10 +57,8 @@ MOC_DIR = .build/$$CONFIGURATION/.moc
5457
RCC_DIR = .build/$$CONFIGURATION/.rcc
5558
UI_DIR = .build/$$CONFIGURATION/.ui
5659

57-
PROJECT_BASENAME = $$basename(_PRO_FILE_)
58-
5960
INCLUDEPATH += $$_PRO_FILE_PWD_ \
60-
$$_PRO_FILE_PWD_/include/$$section(PROJECT_BASENAME, ".", 0, 0) \
61+
$$_PRO_FILE_PWD_/include/$$PROJECT_NAME \
6162

6263
unix {
6364
target.path = $$[INSTALL_ROOT]/

trikControl/include/trikControl/sensor3d.h

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
#pragma once
1616

1717
#include <QtCore/QObject>
18-
#include <QtCore/QSocketNotifier>
19-
#include <QtCore/QSharedPointer>
18+
#include <QtCore/QScopedPointer>
19+
#include <QtCore/QThread>
2020
#include <QtCore/QVector>
2121

2222
#include "declSpec.h"
2323

2424
namespace trikControl {
2525

26+
class Sensor3dWorker;
27+
2628
/// Sensor that returns 3d vector.
2729
class TRIKCONTROL_EXPORT Sensor3d : public QObject
2830
{
@@ -35,20 +37,15 @@ class TRIKCONTROL_EXPORT Sensor3d : public QObject
3537
/// @param deviceFile - device file for this sensor.
3638
Sensor3d(int min, int max, QString const &deviceFile);
3739

40+
~Sensor3d();
41+
3842
public slots:
3943
/// Returns current raw reading of a sensor in a form of vector with 3 coordinates.
40-
QVector<int> const &read() const;
41-
42-
private slots:
43-
/// Updates current reading when new value is ready.
44-
void readFile();
44+
QVector<int> read() const;
4545

4646
private:
47-
QSharedPointer<QSocketNotifier> mSocketNotifier;
48-
QVector<int> mReading;
49-
int mDeviceFileDescriptor;
50-
int mMax;
51-
int mMin;
47+
QScopedPointer<Sensor3dWorker> mSensor3dWorker;
48+
QThread mWorkerThread;
5249
};
5350

5451
}
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright 2013 Matvey Bryksin, Yurii Litvinov
1+
/* Copyright 2014 CyberTech Labs Ltd.
22
*
33
* Licensed under the Apache License, Version 2.0 (the "License");
44
* you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
* See the License for the specific language governing permissions and
1313
* limitations under the License. */
1414

15-
#include "sensor3d.h"
15+
#include "src/sensor3dWorker.h"
1616

1717
#include <QtCore/QDebug>
1818

@@ -28,7 +28,7 @@
2828

2929
using namespace trikControl;
3030

31-
Sensor3d::Sensor3d(int min, int max, const QString &controlFile)
31+
Sensor3dWorker::Sensor3dWorker(int min, int max, const QString &controlFile)
3232
: mDeviceFileDescriptor(0)
3333
, mMax(max)
3434
, mMin(min)
@@ -41,15 +41,15 @@ Sensor3d::Sensor3d(int min, int max, const QString &controlFile)
4141
return;
4242
}
4343

44-
mSocketNotifier = QSharedPointer<QSocketNotifier>(
44+
mSocketNotifier.reset(
4545
new QSocketNotifier(mDeviceFileDescriptor, QSocketNotifier::Read, this)
4646
);
4747

4848
connect(mSocketNotifier.data(), SIGNAL(activated(int)), this, SLOT(readFile()));
4949
mSocketNotifier->setEnabled(true);
5050
}
5151

52-
void Sensor3d::readFile()
52+
void Sensor3dWorker::readFile()
5353
{
5454
struct input_event event;
5555
int size = 0;
@@ -63,13 +63,19 @@ void Sensor3d::readFile()
6363
case EV_ABS:
6464
switch (event.code) {
6565
case ABS_X:
66+
mLock.lockForWrite();
6667
mReading[0] = event.value;
68+
mLock.unlock();
6769
break;
6870
case ABS_Y:
71+
mLock.lockForWrite();
6972
mReading[1] = event.value;
73+
mLock.unlock();
7074
break;
7175
case ABS_Z:
76+
mLock.lockForWrite();
7277
mReading[2] = event.value;
78+
mLock.unlock();
7379
break;
7480
}
7581
break;
@@ -78,15 +84,17 @@ void Sensor3d::readFile()
7884
}
7985
}
8086

81-
8287
if (0 <= size && size < static_cast<int>(sizeof(event))) {
8388
qDebug() << "incomplete data read";
8489
}
8590

8691
mSocketNotifier->setEnabled(true);
8792
}
8893

89-
QVector<int> const &Sensor3d::read() const
94+
QVector<int> const &Sensor3dWorker::read() const
9095
{
91-
return mReading;
96+
mLock.lockForRead();
97+
QVector<int> const result = mReading;
98+
mLock.unlock();
99+
return result;
92100
}

trikControl/src/sensor3d.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* Copyright 2013-2014 Matvey Bryksin, Yurii Litvinov, CyberTech Labs Ltd.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License. */
14+
15+
#include "sensor3d.h"
16+
17+
#include "src/sensor3dWorker.h"
18+
19+
using namespace trikControl;
20+
21+
Sensor3d::Sensor3d(int min, int max, const QString &controlFile)
22+
: mSensor3dWorker(new Sensor3dWorker(min, max, controlFile))
23+
{
24+
mSensor3dWorker->moveToThread(&mWorkerThread);
25+
mWorkerThread.start();
26+
}
27+
28+
29+
Sensor3d::~Sensor3d()
30+
{
31+
mWorkerThread.quit();
32+
mWorkerThread.wait();
33+
}
34+
35+
QVector<int> Sensor3d::read() const
36+
{
37+
return mSensor3dWorker->read();
38+
}

trikControl/src/sensor3dWorker.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#pragma once
2+
3+
/* Copyright 2014 CyberTech Labs Ltd.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License. */
16+
17+
#include <QtCore/QObject>
18+
#include <QtCore/QSocketNotifier>
19+
#include <QtCore/QScopedPointer>
20+
#include <QtCore/QVector>
21+
#include <QtCore/QReadWriteLock>
22+
23+
namespace trikControl {
24+
25+
/// Handles events from sensor, intended to work in separate thread
26+
class Sensor3dWorker : public QObject
27+
{
28+
Q_OBJECT
29+
30+
public:
31+
/// Constructor.
32+
/// @param min - minimal actual (physical) value returned by sensor. Used to normalize returned values.
33+
/// @param max - maximal actual (physical) value returned by sensor. Used to normalize returned values.
34+
/// @param deviceFile - device file for this sensor.
35+
Sensor3dWorker(int min, int max, QString const &deviceFile);
36+
37+
public slots:
38+
/// Returns current raw reading of a sensor in a form of vector with 3 coordinates.
39+
QVector<int> const &read() const;
40+
41+
private slots:
42+
/// Updates current reading when new value is ready.
43+
void readFile();
44+
45+
private:
46+
QScopedPointer<QSocketNotifier> mSocketNotifier;
47+
QVector<int> mReading;
48+
int mDeviceFileDescriptor;
49+
int mMax;
50+
int mMin;
51+
QReadWriteLock mLock;
52+
};
53+
54+
55+
}

trikControl/trikControl.pro

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ HEADERS += \
4545
$$PWD/src/i2cCommunicator.h \
4646
$$PWD/src/keysWorker.h \
4747
$$PWD/src/powerMotor.h \
48+
$$PWD/src/sensor3dWorker.h \
4849
$$PWD/src/servoMotor.h \
4950
$$PWD/src/tcpConnector.h \
5051

@@ -66,12 +67,13 @@ SOURCES += \
6667
$$PWD/src/led.cpp \
6768
$$PWD/src/powerMotor.cpp \
6869
$$PWD/src/pwmCapture.cpp \
70+
$$PWD/src/sensor3d.cpp \
6971
$$PWD/src/servoMotor.cpp \
7072
$$PWD/src/tcpConnector.cpp \
7173
$$PWD/src/$$PLATFORM/cameraLineDetectorSensorWorker.cpp \
7274
$$PWD/src/$$PLATFORM/i2cCommunicator.cpp \
7375
$$PWD/src/$$PLATFORM/keysWorker.cpp \
74-
$$PWD/src/$$PLATFORM/sensor3d.cpp \
76+
$$PWD/src/$$PLATFORM/sensor3dWorker.cpp \
7577

7678
OTHER_FILES += \
7779
config.xml \

trikScriptRunner/src/scriptEngineWorker.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <QtCore/QDebug>
1818
#include <QtCore/QFile>
19+
#include <QtCore/QVector>
1920

2021
#include <trikKernel/fileUtils.h>
2122
#include <trikKernel/debug.h>
@@ -45,6 +46,7 @@ Q_DECLARE_METATYPE(Motor*)
4546
Q_DECLARE_METATYPE(Sensor*)
4647
Q_DECLARE_METATYPE(Sensor3d*)
4748
Q_DECLARE_METATYPE(CameraLineDetectorSensor*)
49+
Q_DECLARE_METATYPE(QVector<int>)
4850

4951
ScriptEngineWorker::ScriptEngineWorker(QString const &configFilePath, QString const &startDirPath)
5052
: mEngine(NULL)
@@ -124,6 +126,7 @@ void ScriptEngineWorker::resetScriptEngine()
124126
qScriptRegisterMetaType(mEngine, sensorToScriptValue, sensorFromScriptValue);
125127
qScriptRegisterMetaType(mEngine, sensor3dToScriptValue, sensor3dFromScriptValue);
126128
qScriptRegisterMetaType(mEngine, cameraLineDetectorSensorToScriptValue, cameraLineDetectorSensorFromScriptValue);
129+
qScriptRegisterSequenceMetaType<QVector<int> >(mEngine);
127130

128131
QScriptValue brickProxy = mEngine->newQObject(mBrick.data());
129132
mEngine->globalObject().setProperty("brick", brickProxy);

0 commit comments

Comments
 (0)