8 #include "../../../utilities/Utilities.hpp"
12 const uint8_t kGyroAddress = 105;
13 const auto kMeasurementInterval = 100;
14 const float kGyroSensitivity = 0.07F;
15 const int kGyroThreshold = 12;
23 , kSamplingInterval{ samplingInterval }
25 , mPreviousSample{ 0 }
27 , mAngularDisplacement{ 0 }
34 static constexpr
auto kFullScaleDegrees = 360;
35 auto normalizedReading =
static_cast<int>(mAngularDisplacement) % kFullScaleDegrees;
37 return normalizedReading < 0 ? normalizedReading + kFullScaleDegrees : normalizedReading;
43 unsigned long interval = currentTime - mPreviousSample;
44 if (interval <= kSamplingInterval)
49 int drift = kOffset - getAngularVelocity();
53 float gyroRate =
static_cast<float>(drift) * kGyroSensitivity;
54 static constexpr
auto kGyroscopeReadingScale = 1000.0F;
55 mAngularDisplacement += gyroRate / (kGyroscopeReadingScale /
static_cast<float>(interval));
57 mPreviousSample = currentTime;
69 static constexpr uint8_t kControlRegister1 = 0x20;
70 writeL3G4200DRegister(kControlRegister1, 0b00001100);
72 static constexpr uint8_t kControlRegister2 = 0x21;
73 writeL3G4200DRegister(kControlRegister2, 0b00000000);
77 static constexpr uint8_t kControlRegister3 = 0x22;
78 writeL3G4200DRegister(kControlRegister3, 0b00001000);
80 static constexpr uint8_t kControlRegister4 = 0x23;
81 writeL3G4200DRegister(kControlRegister4, 0b00110000);
83 static constexpr uint8_t kControlRegister5 = 0x24;
84 writeL3G4200DRegister(kControlRegister5, 0b00000000);
91 if (measurements <= 0)
97 for (
auto i = 0; i < measurements; i++)
99 sum += getAngularVelocity();
103 return static_cast<int>(sum / measurements);
106 int GY50::getAngularVelocity()
110 static const uint8_t zAxisFirstByteRegister = 0x2D;
111 static const uint8_t zAxisSecondByteRegister = 0x2C;
113 auto firstByte = readL3G4200DRegister(zAxisFirstByteRegister);
114 auto secondByte = readL3G4200DRegister(zAxisSecondByteRegister);
116 static constexpr
auto kBitsInByte = 8;
118 return static_cast<int16_t
>((firstByte << kBitsInByte) | secondByte);
121 int GY50::readL3G4200DRegister(uint8_t registerAddress)
131 void GY50::writeL3G4200DRegister(uint8_t registerAddress, uint8_t value)