trikRuntime
gyroSensor.h
Go to the documentation of this file.
1 /* Copyright 2016 Anastasiya Kornilova.
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 #pragma once
16 
17 #include <QtCore/QScopedPointer>
18 #include <QtCore/QThread>
19 #include <QQuaternion>
20 #include <QTimer>
21 #include <QReadWriteLock>
22 #include <cmath>
23 #include <trikKernel/timeVal.h>
24 
25 #include "gyroSensorInterface.h"
26 #include "deviceState.h"
27 
28 namespace trikKernel {
29 class Configurer;
30 }
31 
32 namespace trikHal {
33 class HardwareAbstractionInterface;
34 }
35 
36 namespace trikControl {
37 
38 class VectorSensorWorker;
39 
42 {
43  Q_OBJECT
44 
45 public:
49  GyroSensor(const QString &deviceName, const trikKernel::Configurer &configurer
50  , const trikHal::HardwareAbstractionInterface &hardwareAbstraction, VectorSensorInterface *accelerometer);
51 
52  ~GyroSensor() override;
53 
54  Status status() const override;
55 
56 public slots:
57  QVector<int> read() const override;
58 
59  QVector<int> readRawData() const override;
60 
61  void calibrate(int msec) override;
62 
63  QVector<int> getCalibrationValues() override;
64 
65  void setCalibrationValues(const QVector<int> &values) override;
66 
67  bool isCalibrated() const override;
68 
69 private slots:
72  void countTilt(const QVector<int> &gyroData, trikKernel::TimeVal t);
73 
75  void countCalibrationParameters();
76 
77  void sumAccelerometer(const QVector<int> &accelerometerData, const trikKernel::TimeVal & t);
78 
80  void sumGyroscope(const QVector<int> &gyroData, const trikKernel::TimeVal &);
81 
82  QVector3D getEulerAngles(const QQuaternion &q);
83 
84 private:
86  DeviceState mState;
87 
88  VectorSensorWorker *mVectorSensorWorker; // Has ownership
89  QThread mWorkerThread;
90 
91  QTimer mCalibrationTimer;
92  bool mIsCalibrated;
93 
95  QQuaternion mQ;
96 
98  QVector<int> mBias;
99 
100  QVector<int> mCalibrationValues;
101 
103  QVector<int> mGyroSum;
104 
106  int mGyroCounter;
107 
112  QVector<int> mResult;
113 
115  QVector<int> mRawData;
116 
118  trikKernel::TimeVal mLastUpdate;
119 
120  VectorSensorInterface *mAccelerometer; // Has no ownership
121 
122  QVector<int> mAccelerometerVector;
123  QVector<int> mAccelerometerSum;
124  int mAccelerometerCounter;
125 
126  bool mAxesSwapped;
127 
128  mutable QReadWriteLock mResultLock;
129 };
130 
131 }
Definition: analogSensor.h:23
Generic configuration helper, parses configuration XML files and presents configuration as a set of a...
Definition: configurer.h:28
Helper class to track device state (off, starting, ready, stopping, fail).
Definition: deviceState.h:26
Sensor that returns a vector.
Definition: vectorSensorInterface.h:29
Structure of a time value in a convenient format.
Definition: timeVal.h:20
Handles events from sensor, intended to work in separate thread.
Definition: vectorSensorWorker.h:35
Status
Device status.
Definition: deviceInterface.h:33
Definition: trikCommunicator.h:27
Sensor that returns a vector.
Definition: gyroSensor.h:41
Class that returns raw and processed data from the gyroscope.
Definition: gyroSensorInterface.h:27
Definition: brickFactory.h:21
Hardware abstraction, provides devices that are used to communicate with robot hardware or emulate it...
Definition: hardwareAbstractionInterface.h:29