8#include "../../../utilities/Utilities.hpp"
12const uint8_t kGyroAddress = 105;
13const auto kMeasurementInterval = 100;
14const float kGyroSensitivity = 0.07F;
15const int kGyroThreshold = 12;
23 , kSamplingInterval{ samplingInterval }
31 static constexpr auto kFullScaleDegrees = 360;
32 auto normalizedReading =
static_cast<int>(mAngularDisplacement) % kFullScaleDegrees;
34 return normalizedReading < 0 ? normalizedReading + kFullScaleDegrees : normalizedReading;
40 unsigned long interval = currentTime - mPreviousSample;
41 if (interval <= kSamplingInterval)
46 int drift = kOffset - getAngularVelocity();
50 float gyroRate =
static_cast<float>(drift) * kGyroSensitivity;
51 static constexpr auto kGyroscopeReadingScale = 1000.0F;
52 mAngularDisplacement += gyroRate / (kGyroscopeReadingScale /
static_cast<float>(interval));
54 mPreviousSample = currentTime;
66 static constexpr uint8_t kControlRegister1 = 0x20;
67 writeL3G4200DRegister(kControlRegister1, 0b00001100);
69 static constexpr uint8_t kControlRegister2 = 0x21;
70 writeL3G4200DRegister(kControlRegister2, 0b00000000);
74 static constexpr uint8_t kControlRegister3 = 0x22;
75 writeL3G4200DRegister(kControlRegister3, 0b00001000);
77 static constexpr uint8_t kControlRegister4 = 0x23;
78 writeL3G4200DRegister(kControlRegister4, 0b00110000);
80 static constexpr uint8_t kControlRegister5 = 0x24;
81 writeL3G4200DRegister(kControlRegister5, 0b00000000);
88 if (measurements <= 0)
94 for (
auto i = 0; i < measurements; i++)
96 sum += getAngularVelocity();
100 return static_cast<int>(sum / measurements);
103int GY50::getAngularVelocity()
107 static const uint8_t zAxisFirstByteRegister = 0x2D;
108 static const uint8_t zAxisSecondByteRegister = 0x2C;
110 auto firstByte = readL3G4200DRegister(zAxisFirstByteRegister);
111 auto secondByte = readL3G4200DRegister(zAxisSecondByteRegister);
113 static constexpr auto kBitsInByte = 8;
115 return static_cast<int16_t
>((firstByte << kBitsInByte) | secondByte);
118int GY50::readL3G4200DRegister(uint8_t registerAddress)
128void GY50::writeL3G4200DRegister(uint8_t registerAddress, uint8_t value)
int getOffset(int measurements=smartcarlib::constants::gy50::kDefaultCalibrationMeasurements)
Get the sensor's offset which is the value the sensor returns when still.
GY50(Runtime &runtime, int offset, unsigned long samplingInterval=smartcarlib::constants::gy50::kDefaultSamplingInterval)
Constructs a GY50 gyroscope.
int getHeading() override
Returns the current heading of the vehicle.
void update() override
Updates the sensor's readings.
An interface to serve as a wrapper for the native runtime environment, greatly inspired by the Arduin...
virtual size_t i2cWrite(uint8_t value)=0
Send the specified byte via i2c, equivalent of Wire.write in Arduino.
virtual void delayMillis(unsigned long milliseconds)=0
Block the execution for the specified number of milliseconds, equivalent of delay in Arduino.
virtual void i2cBeginTransmission(uint8_t address)=0
Initiate a transmission to the specified I2C slave device, equivalent of Wire.beginTransmission in Ar...
virtual uint8_t i2cEndTransmission()=0
Ends a transmission to an I2C device equivalent of Wire.endTransmission in Arduino.
virtual int i2cAvailable()=0
Gets the number of bytes available to be retrieved, equivalent of Wire.availableO in Arduino.
virtual int i2cRead()=0
Reads a byte from I2C bus, equivalent of Wire.read in Arduino.
virtual void i2cInit()=0
Initialize I2C bus as master, equivalent of Wire.begin in Arduino.
virtual uint8_t i2cRequestFrom(uint8_t address, uint8_t numberOfBytes)=0
Request a number of bytes from the specified I2C slave, equivalent of Wire.requestFrom in Arduino.
virtual unsigned long currentTimeMillis()=0
Gets the amount of milliseconds since the microcontroller started running, equivalent of millis in Ar...
constexpr AnyNumber getAbsolute(AnyNumber number)
Gets the absolute of the supplied number.