From 20d1e51925eca5b7e2116c22dc358af621f2e5f4 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Thu, 26 Jan 2023 19:03:31 +0000 Subject: [PATCH 01/17] Step up --- lib/sensors/temperature.cpp | 55 +++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 lib/sensors/temperature.cpp diff --git a/lib/sensors/temperature.cpp b/lib/sensors/temperature.cpp new file mode 100644 index 00000000..88a83278 --- /dev/null +++ b/lib/sensors/temperature.cpp @@ -0,0 +1,55 @@ +#include "temperature.hpp" + +namespace hyped::sensors { + +Temperature::Temperature(hyped::core::ILogger &log, io::I2c &i2c, const std::uint8_t channel) + : log_(log), + i2c_(i2c), + channel_(channel) +{ +} + + +std::optional Temperature::read() +{ + const auto status_check_result = i2c_.readByte(kTemperature, kStatus); + if (!status_check_result) { + log_.log(hyped::core::LogLevel::kFatal, "Temperature could not read status"); + return std::nullopt; + } + if (status_check_result.value() == 0) { + log_.log(hyped::core::LogLevel::kFatal, "Temperature sensor is not ready to be read from"); + return std::nullopt; + } + const auto temperature_high_byte = i2c_.readByte(kTemperature, kDataTH); + if (!temperature_high_byte) { + log_.log(hyped::core::LogLevel::kFatal, "Temperature high could not be read"); + return std::nullopt; + } + const auto temperature_low_byte = i2c_.readByte(kTemperature, kDataTL); + if (!temperature_low_byte) { + log_.log(hyped::core::LogLevel::kFatal, "Temperature low could not be read"); + return std::nullopt; + } + const auto temperature = ((temperature_high_byte.value() << 8) | temperature_low_byte.value()); + // Scaling temperature as per the datasheet + return temperature * 0.01; +} + +core::Result Temperature::configure() +{ + const core::Result write_result + = i2c_.writeByteToRegister(kTemperature, kCtrl, kConfigurationSetting); + if (write_result == hyped::core::Result::kFailure) { + log_.log(hyped::core::LogLevel::kFatal, "Temperature configure not implemented"); + return core::Result::kFailure; + } + return core::Result::kSuccess; +} + +std::uint8_t Temperature::getChannel() +{ + return channel_; +} + +} // namespace hyped::sensors \ No newline at end of file From 895097ca0d1722eddbe0ccf35a1e2a9223e78326 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Wed, 1 Feb 2023 17:36:25 +0000 Subject: [PATCH 02/17] This is the first version of the Gyroscope sensor, the conversions of the read still need to be done --- lib/sensors/temperature.cpp | 55 ------------------------------------- 1 file changed, 55 deletions(-) delete mode 100644 lib/sensors/temperature.cpp diff --git a/lib/sensors/temperature.cpp b/lib/sensors/temperature.cpp deleted file mode 100644 index 88a83278..00000000 --- a/lib/sensors/temperature.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include "temperature.hpp" - -namespace hyped::sensors { - -Temperature::Temperature(hyped::core::ILogger &log, io::I2c &i2c, const std::uint8_t channel) - : log_(log), - i2c_(i2c), - channel_(channel) -{ -} - - -std::optional Temperature::read() -{ - const auto status_check_result = i2c_.readByte(kTemperature, kStatus); - if (!status_check_result) { - log_.log(hyped::core::LogLevel::kFatal, "Temperature could not read status"); - return std::nullopt; - } - if (status_check_result.value() == 0) { - log_.log(hyped::core::LogLevel::kFatal, "Temperature sensor is not ready to be read from"); - return std::nullopt; - } - const auto temperature_high_byte = i2c_.readByte(kTemperature, kDataTH); - if (!temperature_high_byte) { - log_.log(hyped::core::LogLevel::kFatal, "Temperature high could not be read"); - return std::nullopt; - } - const auto temperature_low_byte = i2c_.readByte(kTemperature, kDataTL); - if (!temperature_low_byte) { - log_.log(hyped::core::LogLevel::kFatal, "Temperature low could not be read"); - return std::nullopt; - } - const auto temperature = ((temperature_high_byte.value() << 8) | temperature_low_byte.value()); - // Scaling temperature as per the datasheet - return temperature * 0.01; -} - -core::Result Temperature::configure() -{ - const core::Result write_result - = i2c_.writeByteToRegister(kTemperature, kCtrl, kConfigurationSetting); - if (write_result == hyped::core::Result::kFailure) { - log_.log(hyped::core::LogLevel::kFatal, "Temperature configure not implemented"); - return core::Result::kFailure; - } - return core::Result::kSuccess; -} - -std::uint8_t Temperature::getChannel() -{ - return channel_; -} - -} // namespace hyped::sensors \ No newline at end of file From 3cb7643046a26131ebaa8a2ba87fb1e2c7fc9cc1 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Wed, 1 Feb 2023 17:37:44 +0000 Subject: [PATCH 03/17] First version of the Gyroscope code, need to add the conversion of the axis --- lib/sensors/gyroscope.cpp | 87 +++++++++++++++++++++++++++++++++++++++ lib/sensors/gyroscope.hpp | 44 ++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 lib/sensors/gyroscope.cpp create mode 100644 lib/sensors/gyroscope.hpp diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp new file mode 100644 index 00000000..f938d7ee --- /dev/null +++ b/lib/sensors/gyroscope.cpp @@ -0,0 +1,87 @@ +#include "gyroscope.hpp" + +namespace hyped::sensors { + +Gyroscope::Gyroscope(hyped::core::ILogger &log, io::I2c &i2c, const std::uint8_t channel) + : log_(log), + i2c_(i2c), + channel_(channel) +{ +} + +std::optional Gyroscope::read() +{ + const auto gyroscope_x_high_byte = i2c_.readByte(kGyroscope, kDataXH); + if (!gyroscope_x_high_byte) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope X-axis high could not be read"); + return std::nullopt; + } + const auto gyroscope_x_low_byte = i2c_.readByte(kGyroscope, kDataXL); + if (!gyroscope_x_low_byte) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope X-axis low could not be read"); + return std::nullopt; + } + const auto gyroscope_y_high_byte = i2c_.readByte(kGyroscope, kDataYH); + if (!gyroscope_y_high_byte) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope Y-axis high could not be read"); + return std::nullopt; + } + const auto gyroscope_y_low_byte = i2c_.readByte(kGyroscope, kDataYL); + if (!gyroscope_y_low_byte) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope Y-axis low could not be read"); + return std::nullopt; + } + const auto gyroscope_z_high_byte = i2c_.readByte(kGyroscope, kDataZH); + if (!gyroscope_z_high_byte) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope Z-axis high could not be read"); + return std::nullopt; + } + const auto gyroscope_z_low_byte = i2c_.readByte(kGyroscope, kDataZL); + if (!gyroscope_z_low_byte) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope Z-axis low could not be read"); + return std::nullopt; + } + + const auto x_axis = ((gyroscope_x_high_byte.value() << 8) | gyroscope_x_low_byte.value()); + const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); + const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); + + // TODO the conversions of the 3 axis + return 0; +} + +core::Result Gyroscope::configure() +{ + const core::Result write_result1 + = i2c_.writeByteToRegister(kGyroscope, kCtrl1, kConfigurationSetting1); + if (write_result1 == hyped::core::Result::kFailure) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope control 1 configure not implemented"); + return core::Result::kFailure; + } + const core::Result write_result2 + = i2c_.writeByteToRegister(kGyroscope, kCtrl2, kConfigurationSetting2); + if (write_result2 == hyped::core::Result::kFailure) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope control 2 configure not implemented"); + return core::Result::kFailure; + } + const core::Result write_result3 + = i2c_.writeByteToRegister(kGyroscope, kCtrl3, kConfigurationSetting3); + if (write_result3 == hyped::core::Result::kFailure) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope control 3 configure not implemented"); + return core::Result::kFailure; + } + const core::Result write_result5 + = i2c_.writeByteToRegister(kGyroscope, kCtrl5, kConfigurationSetting5); + if (write_result5 == hyped::core::Result::kFailure) { + log_.log(hyped::core::LogLevel::kFatal, "Gyroscope control 5 configure not implemented"); + return core::Result::kFailure; + } + return core::Result::kSuccess; +} + +std::uint8_t Gyroscope::getChannel() +{ + return channel_; +} + +} // namespace hyped::sensors \ No newline at end of file diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp new file mode 100644 index 00000000..96d0511b --- /dev/null +++ b/lib/sensors/gyroscope.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include "i2c_sensors.hpp" + +#include +#include + +#include +#include + +namespace hyped::sensors { + +static constexpr std::uint8_t kGyroscope = 0x69; +static constexpr std::uint8_t kDataXH = 0x29; +static constexpr std::uint8_t kDataXL = 0x28; +static constexpr std::uint8_t kDataYH = 0x2B; +static constexpr std::uint8_t kDataYL = 0x2A; +static constexpr std::uint8_t kDataZH = 0x2D; +static constexpr std::uint8_t kDataZL = 0x2C; +static constexpr std::uint8_t kCtrl1 = 0x20; +static constexpr std::uint8_t kCtrl2 = 0x21; +static constexpr std::uint8_t kCtrl3 = 0x22; +static constexpr std::uint8_t kCtrl5 = 0x24; +static constexpr std::uint8_t kConfigurationSetting1 = 0xff; +static constexpr std::uint8_t kConfigurationSetting2 = 0x20; +static constexpr std::uint8_t kConfigurationSetting3 = 0xff; +static constexpr std::uint8_t kConfigurationSetting5 = 0x40; + +class Gyroscope : public II2cMuxSensor { + public: + Gyroscope(hyped::core::ILogger &log, io::I2c &i2c, const std::uint8_t channel); + ~Gyroscope(); + + core::Result configure(); + std::optional read(); + std::uint8_t getChannel(); + + private: + hyped::core::ILogger &log_; + hyped::io::I2c &i2c_; + const std::uint8_t channel_; +}; + +} // namespace hyped::sensors \ No newline at end of file From 5f7fbc9937e349d079ff4cbe089ccb7cf26c9731 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Thu, 2 Feb 2023 22:30:10 +0000 Subject: [PATCH 04/17] The changes for the logger and the naming convention for kData have been done --- lib/sensors/gyroscope.cpp | 64 ++++++++++++++++++++++++++------------- lib/sensors/gyroscope.hpp | 18 +++++------ 2 files changed, 52 insertions(+), 30 deletions(-) diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index f938d7ee..eba0ff30 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -2,43 +2,55 @@ namespace hyped::sensors { -Gyroscope::Gyroscope(hyped::core::ILogger &log, io::I2c &i2c, const std::uint8_t channel) - : log_(log), +Gyroscope::Gyroscope(hyped::core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel) + : logger_(logger), i2c_(i2c), channel_(channel) { } - +// Todo add a parameter that would return only the axis required std::optional Gyroscope::read() { - const auto gyroscope_x_high_byte = i2c_.readByte(kGyroscope, kDataXH); + const auto gyroscope_x_high_byte = i2c_.readByte(kGyroscope, kDataXHigh); if (!gyroscope_x_high_byte) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope X-axis high could not be read"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope X-axis high at channel %d", + channel_); return std::nullopt; } - const auto gyroscope_x_low_byte = i2c_.readByte(kGyroscope, kDataXL); + const auto gyroscope_x_low_byte = i2c_.readByte(kGyroscope, kDataXLow); if (!gyroscope_x_low_byte) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope X-axis low could not be read"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope X-axis low at channel %d", + channel_); return std::nullopt; } - const auto gyroscope_y_high_byte = i2c_.readByte(kGyroscope, kDataYH); + const auto gyroscope_y_high_byte = i2c_.readByte(kGyroscope, kDataYHigh); if (!gyroscope_y_high_byte) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope Y-axis high could not be read"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope Y-axis high at channel %d", + channel_); return std::nullopt; } - const auto gyroscope_y_low_byte = i2c_.readByte(kGyroscope, kDataYL); + const auto gyroscope_y_low_byte = i2c_.readByte(kGyroscope, kDataYLow); if (!gyroscope_y_low_byte) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope Y-axis low could not be read"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope Y-axis low at channel %d", + channel_); return std::nullopt; } - const auto gyroscope_z_high_byte = i2c_.readByte(kGyroscope, kDataZH); + const auto gyroscope_z_high_byte = i2c_.readByte(kGyroscope, kDataZHigh); if (!gyroscope_z_high_byte) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope Z-axis high could not be read"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope Z-axis high at channel %d", + channel_); return std::nullopt; } - const auto gyroscope_z_low_byte = i2c_.readByte(kGyroscope, kDataZL); + const auto gyroscope_z_low_byte = i2c_.readByte(kGyroscope, kDataZLow); if (!gyroscope_z_low_byte) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope Z-axis low could not be read"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope Z-axis low at channel %d", + channel_); return std::nullopt; } @@ -46,8 +58,10 @@ std::optional Gyroscope::read() const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); - // TODO the conversions of the 3 axis - return 0; + logger_.log( + hyped::core::LogLevel::kDebug, "Successfully read from gyroscope at channel %d", channel_); + + return x_axis; } core::Result Gyroscope::configure() @@ -55,25 +69,33 @@ core::Result Gyroscope::configure() const core::Result write_result1 = i2c_.writeByteToRegister(kGyroscope, kCtrl1, kConfigurationSetting1); if (write_result1 == hyped::core::Result::kFailure) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope control 1 configure not implemented"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, configure gyroscope control 1 at channel %d", + channel_); return core::Result::kFailure; } const core::Result write_result2 = i2c_.writeByteToRegister(kGyroscope, kCtrl2, kConfigurationSetting2); if (write_result2 == hyped::core::Result::kFailure) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope control 2 configure not implemented"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, configure gyroscope control 2 at channel %d", + channel_); return core::Result::kFailure; } const core::Result write_result3 = i2c_.writeByteToRegister(kGyroscope, kCtrl3, kConfigurationSetting3); if (write_result3 == hyped::core::Result::kFailure) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope control 3 configure not implemented"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, configure gyroscope control 3 at channel %d", + channel_); return core::Result::kFailure; } const core::Result write_result5 = i2c_.writeByteToRegister(kGyroscope, kCtrl5, kConfigurationSetting5); if (write_result5 == hyped::core::Result::kFailure) { - log_.log(hyped::core::LogLevel::kFatal, "Gyroscope control 5 configure not implemented"); + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, configure gyroscope control 5 at channel %d", + channel_); return core::Result::kFailure; } return core::Result::kSuccess; diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index 96d0511b..e5c39fec 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -11,12 +11,12 @@ namespace hyped::sensors { static constexpr std::uint8_t kGyroscope = 0x69; -static constexpr std::uint8_t kDataXH = 0x29; -static constexpr std::uint8_t kDataXL = 0x28; -static constexpr std::uint8_t kDataYH = 0x2B; -static constexpr std::uint8_t kDataYL = 0x2A; -static constexpr std::uint8_t kDataZH = 0x2D; -static constexpr std::uint8_t kDataZL = 0x2C; +static constexpr std::uint8_t kDataXHigh = 0x29; +static constexpr std::uint8_t kDataXLow = 0x28; +static constexpr std::uint8_t kDataYHigh = 0x2B; +static constexpr std::uint8_t kDataYLow = 0x2A; +static constexpr std::uint8_t kDataZHigh = 0x2D; +static constexpr std::uint8_t kDataZLow = 0x2C; static constexpr std::uint8_t kCtrl1 = 0x20; static constexpr std::uint8_t kCtrl2 = 0x21; static constexpr std::uint8_t kCtrl3 = 0x22; @@ -28,7 +28,7 @@ static constexpr std::uint8_t kConfigurationSetting5 = 0x40; class Gyroscope : public II2cMuxSensor { public: - Gyroscope(hyped::core::ILogger &log, io::I2c &i2c, const std::uint8_t channel); + Gyroscope(hyped::core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel); ~Gyroscope(); core::Result configure(); @@ -36,8 +36,8 @@ class Gyroscope : public II2cMuxSensor { std::uint8_t getChannel(); private: - hyped::core::ILogger &log_; - hyped::io::I2c &i2c_; + hyped::core::ILogger &logger_; + hyped::io::II2c &i2c_; const std::uint8_t channel_; }; From c246addda9aea73d6572eda7c01cb186a5389b5e Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 13:49:34 +0000 Subject: [PATCH 05/17] not fully done trying to solve error --- lib/core/types.hpp | 3 +- lib/sensors/gyroscope.cpp | 138 +++++++++++++++++++++++++++----------- lib/sensors/gyroscope.hpp | 13 ++-- 3 files changed, 109 insertions(+), 45 deletions(-) diff --git a/lib/core/types.hpp b/lib/core/types.hpp index f0ec335a..c9fb4a68 100644 --- a/lib/core/types.hpp +++ b/lib/core/types.hpp @@ -9,6 +9,7 @@ static constexpr float kEpsilon = 0.0001; enum class DigitalSignal { kLow = 0, kHigh }; enum class Result { kSuccess = 0, kFailure }; +enum class GyroscopeAxis {kX,kY,kZ}; using Float = float; @@ -40,4 +41,4 @@ using ImuData = std::array; using EncoderData = std::array; using KeyenceData = std::array; -} // namespace hyped::core +} // namespace hyped::core \ No newline at end of file diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index eba0ff30..ac248455 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -2,66 +2,124 @@ namespace hyped::sensors { -Gyroscope::Gyroscope(hyped::core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel) +Gyroscope::Gyroscope(core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel) : logger_(logger), i2c_(i2c), channel_(channel) { } + +Gyroscope::~Gyroscope() +{ +} // Todo add a parameter that would return only the axis required -std::optional Gyroscope::read() +std::optional Gyroscope::read(core::GyroscopeAxis axis) { - const auto gyroscope_x_high_byte = i2c_.readByte(kGyroscope, kDataXHigh); - if (!gyroscope_x_high_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope X-axis high at channel %d", + if (axis == core::GyroscopeAxis::kX) { + const auto gyroscope_x_high_byte = i2c_.readByte(kGyroscope, kDataXHigh); + if (!gyroscope_x_high_byte) { + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope X-axis high at channel %d", + channel_); + return std::nullopt; + } + const auto gyroscope_x_low_byte = i2c_.readByte(kGyroscope, kDataXLow); + if (!gyroscope_x_low_byte) { + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope X-axis low at channel %d", + channel_); + return std::nullopt; + } + const auto x_axis = ((gyroscope_x_high_byte.value() << 8) | gyroscope_x_low_byte.value()); + logger_.log(hyped::core::LogLevel::kDebug, + "Successfully read x-axis from gyroscope at channel %d", channel_); - return std::nullopt; - } - const auto gyroscope_x_low_byte = i2c_.readByte(kGyroscope, kDataXLow); - if (!gyroscope_x_low_byte) { + return x_axis; + } else if (axis == core::GyroscopeAxis::kY) { + const auto gyroscope_y_high_byte = i2c_.readByte(kGyroscope, kDataYHigh); + if (!gyroscope_y_high_byte) { + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope Y-axis high at channel %d", + channel_); + return std::nullopt; + } + const auto gyroscope_y_low_byte = i2c_.readByte(kGyroscope, kDataYLow); + if (!gyroscope_y_low_byte) { + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope Y-axis low at channel %d", + channel_); + return std::nullopt; + } + const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); + logger_.log(hyped::core::LogLevel::kDebug, + "Successfully read y-axis from gyroscope at channel %d", + channel_); + return y_axis; + } else if (axis == core::GyroscopeAxis::kZ) { + const auto gyroscope_z_high_byte = i2c_.readByte(kGyroscope, kDataZHigh); + if (!gyroscope_z_high_byte) { + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope Z-axis high at channel %d", + channel_); + return std::nullopt; + } + const auto gyroscope_z_low_byte = i2c_.readByte(kGyroscope, kDataZLow); + if (!gyroscope_z_low_byte) { + logger_.log(hyped::core::LogLevel::kFatal, + "Failed to, read gyroscope Z-axis low at channel %d", + channel_); + return std::nullopt; + } + const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); + logger_.log(hyped::core::LogLevel::kDebug, + "Successfully read z-axis from gyroscope at channel %d", + channel_); + return z_axis; + } else { logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope X-axis low at channel %d", + "Failed to, read gyroscope at channel %d, axis parameters are invalid types", channel_); return std::nullopt; } - const auto gyroscope_y_high_byte = i2c_.readByte(kGyroscope, kDataYHigh); - if (!gyroscope_y_high_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope Y-axis high at channel %d", - channel_); +} + +std::optional Gyroscope::create(core::ILogger &logger, + io::II2c &i2c, + const std::uint8_t channel) +{ + const core::Result write_result1 + = i2c.writeByteToRegister(kGyroscope, kCtrl1, kConfigurationSetting1); + if (write_result1 == hyped::core::Result::kFailure) { + logger.log(hyped::core::LogLevel::kFatal, + "Failed to, configure gyroscope control 1 at channel %d", + channel); return std::nullopt; } - const auto gyroscope_y_low_byte = i2c_.readByte(kGyroscope, kDataYLow); - if (!gyroscope_y_low_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope Y-axis low at channel %d", - channel_); + const core::Result write_result2 + = i2c.writeByteToRegister(kGyroscope, kCtrl2, kConfigurationSetting2); + if (write_result2 == hyped::core::Result::kFailure) { + logger.log(hyped::core::LogLevel::kFatal, + "Failed to, configure gyroscope control 2 at channel %d", + channel); return std::nullopt; } - const auto gyroscope_z_high_byte = i2c_.readByte(kGyroscope, kDataZHigh); - if (!gyroscope_z_high_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope Z-axis high at channel %d", - channel_); + const core::Result write_result3 + = i2c.writeByteToRegister(kGyroscope, kCtrl3, kConfigurationSetting3); + if (write_result3 == hyped::core::Result::kFailure) { + logger.log(hyped::core::LogLevel::kFatal, + "Failed to, configure gyroscope control 3 at channel %d", + channel); return std::nullopt; } - const auto gyroscope_z_low_byte = i2c_.readByte(kGyroscope, kDataZLow); - if (!gyroscope_z_low_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope Z-axis low at channel %d", - channel_); + const core::Result write_result5 + = i2c.writeByteToRegister(kGyroscope, kCtrl5, kConfigurationSetting5); + if (write_result5 == hyped::core::Result::kFailure) { + logger.log(hyped::core::LogLevel::kFatal, + "Failed to, configure gyroscope control 5 at channel %d", + channel); return std::nullopt; } - - const auto x_axis = ((gyroscope_x_high_byte.value() << 8) | gyroscope_x_low_byte.value()); - const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); - const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); - - logger_.log( - hyped::core::LogLevel::kDebug, "Successfully read from gyroscope at channel %d", channel_); - - return x_axis; + return Gyroscope(logger, i2c, channel); } core::Result Gyroscope::configure() diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index e5c39fec..a0a8f15d 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -25,19 +25,24 @@ static constexpr std::uint8_t kConfigurationSetting1 = 0xff; static constexpr std::uint8_t kConfigurationSetting2 = 0x20; static constexpr std::uint8_t kConfigurationSetting3 = 0xff; static constexpr std::uint8_t kConfigurationSetting5 = 0x40; +static constexpr std::uint8_t kStatus = 0x27; class Gyroscope : public II2cMuxSensor { public: - Gyroscope(hyped::core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel); + static std::optional create(core::ILogger &logger, + io::II2c &i2c, + const std::uint8_t channel); ~Gyroscope(); core::Result configure(); - std::optional read(); + std::optional read(core::GyroscopeAxis axis); std::uint8_t getChannel(); private: - hyped::core::ILogger &logger_; - hyped::io::II2c &i2c_; + Gyroscope(core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel); + + core::ILogger &logger_; + io::II2c &i2c_; const std::uint8_t channel_; }; From 99e49275a4e8d23abbb5eeab41f958c74af7671e Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 13:57:37 +0000 Subject: [PATCH 06/17] Added the required parameters for read and the new create funtion --- lib/sensors/gyroscope.cpp | 2 +- lib/sensors/gyroscope.hpp | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index ac248455..e31f331e 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -12,7 +12,7 @@ Gyroscope::Gyroscope(core::ILogger &logger, io::II2c &i2c, const std::uint8_t ch Gyroscope::~Gyroscope() { } -// Todo add a parameter that would return only the axis required + std::optional Gyroscope::read(core::GyroscopeAxis axis) { if (axis == core::GyroscopeAxis::kX) { diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index a0a8f15d..536c9117 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -25,9 +25,8 @@ static constexpr std::uint8_t kConfigurationSetting1 = 0xff; static constexpr std::uint8_t kConfigurationSetting2 = 0x20; static constexpr std::uint8_t kConfigurationSetting3 = 0xff; static constexpr std::uint8_t kConfigurationSetting5 = 0x40; -static constexpr std::uint8_t kStatus = 0x27; -class Gyroscope : public II2cMuxSensor { +class Gyroscope{ public: static std::optional create(core::ILogger &logger, io::II2c &i2c, From 52441634e8ed6a1a5f065cf8943ae1fa1e082be6 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 13:58:52 +0000 Subject: [PATCH 07/17] Added the required parameters for read and the new create funtion --- lib/sensors/gyroscope.cpp | 37 ------------------------------------- lib/sensors/gyroscope.hpp | 1 - 2 files changed, 38 deletions(-) diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index e31f331e..43dd934f 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -122,43 +122,6 @@ std::optional Gyroscope::create(core::ILogger &logger, return Gyroscope(logger, i2c, channel); } -core::Result Gyroscope::configure() -{ - const core::Result write_result1 - = i2c_.writeByteToRegister(kGyroscope, kCtrl1, kConfigurationSetting1); - if (write_result1 == hyped::core::Result::kFailure) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, configure gyroscope control 1 at channel %d", - channel_); - return core::Result::kFailure; - } - const core::Result write_result2 - = i2c_.writeByteToRegister(kGyroscope, kCtrl2, kConfigurationSetting2); - if (write_result2 == hyped::core::Result::kFailure) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, configure gyroscope control 2 at channel %d", - channel_); - return core::Result::kFailure; - } - const core::Result write_result3 - = i2c_.writeByteToRegister(kGyroscope, kCtrl3, kConfigurationSetting3); - if (write_result3 == hyped::core::Result::kFailure) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, configure gyroscope control 3 at channel %d", - channel_); - return core::Result::kFailure; - } - const core::Result write_result5 - = i2c_.writeByteToRegister(kGyroscope, kCtrl5, kConfigurationSetting5); - if (write_result5 == hyped::core::Result::kFailure) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, configure gyroscope control 5 at channel %d", - channel_); - return core::Result::kFailure; - } - return core::Result::kSuccess; -} - std::uint8_t Gyroscope::getChannel() { return channel_; diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index 536c9117..d270628b 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -33,7 +33,6 @@ class Gyroscope{ const std::uint8_t channel); ~Gyroscope(); - core::Result configure(); std::optional read(core::GyroscopeAxis axis); std::uint8_t getChannel(); From 1ebe8d9dc8a9a7cf02e93c3740c2c914b9505d08 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 14:02:01 +0000 Subject: [PATCH 08/17] Added the required parameters for read and the new create function with a change of parameters --- lib/sensors/gyroscope.cpp | 20 ++++++++++---------- lib/sensors/gyroscope.hpp | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index 43dd934f..19e5512c 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -16,14 +16,14 @@ Gyroscope::~Gyroscope() std::optional Gyroscope::read(core::GyroscopeAxis axis) { if (axis == core::GyroscopeAxis::kX) { - const auto gyroscope_x_high_byte = i2c_.readByte(kGyroscope, kDataXHigh); + const auto gyroscope_x_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXHigh); if (!gyroscope_x_high_byte) { logger_.log(hyped::core::LogLevel::kFatal, "Failed to, read gyroscope X-axis high at channel %d", channel_); return std::nullopt; } - const auto gyroscope_x_low_byte = i2c_.readByte(kGyroscope, kDataXLow); + const auto gyroscope_x_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXLow); if (!gyroscope_x_low_byte) { logger_.log(hyped::core::LogLevel::kFatal, "Failed to, read gyroscope X-axis low at channel %d", @@ -36,14 +36,14 @@ std::optional Gyroscope::read(core::GyroscopeAxis axis) channel_); return x_axis; } else if (axis == core::GyroscopeAxis::kY) { - const auto gyroscope_y_high_byte = i2c_.readByte(kGyroscope, kDataYHigh); + const auto gyroscope_y_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYHigh); if (!gyroscope_y_high_byte) { logger_.log(hyped::core::LogLevel::kFatal, "Failed to, read gyroscope Y-axis high at channel %d", channel_); return std::nullopt; } - const auto gyroscope_y_low_byte = i2c_.readByte(kGyroscope, kDataYLow); + const auto gyroscope_y_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYLow); if (!gyroscope_y_low_byte) { logger_.log(hyped::core::LogLevel::kFatal, "Failed to, read gyroscope Y-axis low at channel %d", @@ -56,14 +56,14 @@ std::optional Gyroscope::read(core::GyroscopeAxis axis) channel_); return y_axis; } else if (axis == core::GyroscopeAxis::kZ) { - const auto gyroscope_z_high_byte = i2c_.readByte(kGyroscope, kDataZHigh); + const auto gyroscope_z_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZHigh); if (!gyroscope_z_high_byte) { logger_.log(hyped::core::LogLevel::kFatal, "Failed to, read gyroscope Z-axis high at channel %d", channel_); return std::nullopt; } - const auto gyroscope_z_low_byte = i2c_.readByte(kGyroscope, kDataZLow); + const auto gyroscope_z_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZLow); if (!gyroscope_z_low_byte) { logger_.log(hyped::core::LogLevel::kFatal, "Failed to, read gyroscope Z-axis low at channel %d", @@ -88,7 +88,7 @@ std::optional Gyroscope::create(core::ILogger &logger, const std::uint8_t channel) { const core::Result write_result1 - = i2c.writeByteToRegister(kGyroscope, kCtrl1, kConfigurationSetting1); + = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl1, kConfigurationSetting1); if (write_result1 == hyped::core::Result::kFailure) { logger.log(hyped::core::LogLevel::kFatal, "Failed to, configure gyroscope control 1 at channel %d", @@ -96,7 +96,7 @@ std::optional Gyroscope::create(core::ILogger &logger, return std::nullopt; } const core::Result write_result2 - = i2c.writeByteToRegister(kGyroscope, kCtrl2, kConfigurationSetting2); + = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl2, kConfigurationSetting2); if (write_result2 == hyped::core::Result::kFailure) { logger.log(hyped::core::LogLevel::kFatal, "Failed to, configure gyroscope control 2 at channel %d", @@ -104,7 +104,7 @@ std::optional Gyroscope::create(core::ILogger &logger, return std::nullopt; } const core::Result write_result3 - = i2c.writeByteToRegister(kGyroscope, kCtrl3, kConfigurationSetting3); + = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl3, kConfigurationSetting3); if (write_result3 == hyped::core::Result::kFailure) { logger.log(hyped::core::LogLevel::kFatal, "Failed to, configure gyroscope control 3 at channel %d", @@ -112,7 +112,7 @@ std::optional Gyroscope::create(core::ILogger &logger, return std::nullopt; } const core::Result write_result5 - = i2c.writeByteToRegister(kGyroscope, kCtrl5, kConfigurationSetting5); + = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl5, kConfigurationSetting5); if (write_result5 == hyped::core::Result::kFailure) { logger.log(hyped::core::LogLevel::kFatal, "Failed to, configure gyroscope control 5 at channel %d", diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index d270628b..0ba37112 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -10,7 +10,7 @@ namespace hyped::sensors { -static constexpr std::uint8_t kGyroscope = 0x69; +static constexpr std::uint8_t kDefaultGyroscopeAddress = 0x69; static constexpr std::uint8_t kDataXHigh = 0x29; static constexpr std::uint8_t kDataXLow = 0x28; static constexpr std::uint8_t kDataYHigh = 0x2B; From f3c35e855b06ce55b8f6c798da1720dbecde0777 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 14:06:46 +0000 Subject: [PATCH 09/17] removing redundant hyped:: --- lib/sensors/gyroscope.cpp | 75 ++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 44 deletions(-) diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index 19e5512c..df57d62d 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -18,65 +18,56 @@ std::optional Gyroscope::read(core::GyroscopeAxis axis) if (axis == core::GyroscopeAxis::kX) { const auto gyroscope_x_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXHigh); if (!gyroscope_x_high_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope X-axis high at channel %d", - channel_); + logger_.log( + core::LogLevel::kFatal, "Failed to, read gyroscope X-axis high at channel %d", channel_); return std::nullopt; } const auto gyroscope_x_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXLow); if (!gyroscope_x_low_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope X-axis low at channel %d", - channel_); + logger_.log( + core::LogLevel::kFatal, "Failed to, read gyroscope X-axis low at channel %d", channel_); return std::nullopt; } const auto x_axis = ((gyroscope_x_high_byte.value() << 8) | gyroscope_x_low_byte.value()); - logger_.log(hyped::core::LogLevel::kDebug, - "Successfully read x-axis from gyroscope at channel %d", - channel_); + logger_.log( + core::LogLevel::kDebug, "Successfully read x-axis from gyroscope at channel %d", channel_); return x_axis; } else if (axis == core::GyroscopeAxis::kY) { const auto gyroscope_y_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYHigh); if (!gyroscope_y_high_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope Y-axis high at channel %d", - channel_); + logger_.log( + core::LogLevel::kFatal, "Failed to, read gyroscope Y-axis high at channel %d", channel_); return std::nullopt; } const auto gyroscope_y_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYLow); if (!gyroscope_y_low_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope Y-axis low at channel %d", - channel_); + logger_.log( + core::LogLevel::kFatal, "Failed to, read gyroscope Y-axis low at channel %d", channel_); return std::nullopt; } const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); - logger_.log(hyped::core::LogLevel::kDebug, - "Successfully read y-axis from gyroscope at channel %d", - channel_); + logger_.log( + core::LogLevel::kDebug, "Successfully read y-axis from gyroscope at channel %d", channel_); return y_axis; } else if (axis == core::GyroscopeAxis::kZ) { const auto gyroscope_z_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZHigh); if (!gyroscope_z_high_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope Z-axis high at channel %d", - channel_); + logger_.log( + core::LogLevel::kFatal, "Failed to, read gyroscope Z-axis high at channel %d", channel_); return std::nullopt; } const auto gyroscope_z_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZLow); if (!gyroscope_z_low_byte) { - logger_.log(hyped::core::LogLevel::kFatal, - "Failed to, read gyroscope Z-axis low at channel %d", - channel_); + logger_.log( + core::LogLevel::kFatal, "Failed to, read gyroscope Z-axis low at channel %d", channel_); return std::nullopt; } const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); - logger_.log(hyped::core::LogLevel::kDebug, - "Successfully read z-axis from gyroscope at channel %d", - channel_); + logger_.log( + core::LogLevel::kDebug, "Successfully read z-axis from gyroscope at channel %d", channel_); return z_axis; } else { - logger_.log(hyped::core::LogLevel::kFatal, + logger_.log(core::LogLevel::kFatal, "Failed to, read gyroscope at channel %d, axis parameters are invalid types", channel_); return std::nullopt; @@ -89,34 +80,30 @@ std::optional Gyroscope::create(core::ILogger &logger, { const core::Result write_result1 = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl1, kConfigurationSetting1); - if (write_result1 == hyped::core::Result::kFailure) { - logger.log(hyped::core::LogLevel::kFatal, - "Failed to, configure gyroscope control 1 at channel %d", - channel); + if (write_result1 == core::Result::kFailure) { + logger.log( + core::LogLevel::kFatal, "Failed to, configure gyroscope control 1 at channel %d", channel); return std::nullopt; } const core::Result write_result2 = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl2, kConfigurationSetting2); - if (write_result2 == hyped::core::Result::kFailure) { - logger.log(hyped::core::LogLevel::kFatal, - "Failed to, configure gyroscope control 2 at channel %d", - channel); + if (write_result2 == core::Result::kFailure) { + logger.log( + core::LogLevel::kFatal, "Failed to, configure gyroscope control 2 at channel %d", channel); return std::nullopt; } const core::Result write_result3 = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl3, kConfigurationSetting3); - if (write_result3 == hyped::core::Result::kFailure) { - logger.log(hyped::core::LogLevel::kFatal, - "Failed to, configure gyroscope control 3 at channel %d", - channel); + if (write_result3 == core::Result::kFailure) { + logger.log( + core::LogLevel::kFatal, "Failed to, configure gyroscope control 3 at channel %d", channel); return std::nullopt; } const core::Result write_result5 = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl5, kConfigurationSetting5); - if (write_result5 == hyped::core::Result::kFailure) { - logger.log(hyped::core::LogLevel::kFatal, - "Failed to, configure gyroscope control 5 at channel %d", - channel); + if (write_result5 == core::Result::kFailure) { + logger.log( + core::LogLevel::kFatal, "Failed to, configure gyroscope control 5 at channel %d", channel); return std::nullopt; } return Gyroscope(logger, i2c, channel); From 568db5f8193980378419fbee140eb32937f60a65 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 14:09:07 +0000 Subject: [PATCH 10/17] Added the required parameters for read and the new create function with a change of parameters --- lib/sensors/gyroscope.hpp | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index 0ba37112..79a0f5a1 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -10,23 +10,23 @@ namespace hyped::sensors { -static constexpr std::uint8_t kDefaultGyroscopeAddress = 0x69; -static constexpr std::uint8_t kDataXHigh = 0x29; -static constexpr std::uint8_t kDataXLow = 0x28; -static constexpr std::uint8_t kDataYHigh = 0x2B; -static constexpr std::uint8_t kDataYLow = 0x2A; -static constexpr std::uint8_t kDataZHigh = 0x2D; -static constexpr std::uint8_t kDataZLow = 0x2C; -static constexpr std::uint8_t kCtrl1 = 0x20; -static constexpr std::uint8_t kCtrl2 = 0x21; -static constexpr std::uint8_t kCtrl3 = 0x22; -static constexpr std::uint8_t kCtrl5 = 0x24; -static constexpr std::uint8_t kConfigurationSetting1 = 0xff; -static constexpr std::uint8_t kConfigurationSetting2 = 0x20; -static constexpr std::uint8_t kConfigurationSetting3 = 0xff; -static constexpr std::uint8_t kConfigurationSetting5 = 0x40; - -class Gyroscope{ +static constexpr std::uint8_t kDefaultGyroscopeAddress = 0x69; +static constexpr std::uint8_t kDataXHigh = 0x29; +static constexpr std::uint8_t kDataXLow = 0x28; +static constexpr std::uint8_t kDataYHigh = 0x2B; +static constexpr std::uint8_t kDataYLow = 0x2A; +static constexpr std::uint8_t kDataZHigh = 0x2D; +static constexpr std::uint8_t kDataZLow = 0x2C; +static constexpr std::uint8_t kCtrl1 = 0x20; +static constexpr std::uint8_t kCtrl2 = 0x21; +static constexpr std::uint8_t kCtrl3 = 0x22; +static constexpr std::uint8_t kCtrl5 = 0x24; +static constexpr std::uint8_t kConfigurationSetting1 = 0xff; +static constexpr std::uint8_t kConfigurationSetting2 = 0x20; +static constexpr std::uint8_t kConfigurationSetting3 = 0xff; +static constexpr std::uint8_t kConfigurationSetting5 = 0x40; + +class Gyroscope { public: static std::optional create(core::ILogger &logger, io::II2c &i2c, From c163bc6c08641a83e384cf563dd9d94c50c7c4dd Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 14:12:20 +0000 Subject: [PATCH 11/17] Added the required parameters for read and the new create function with a change of parameters --- lib/core/types.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/core/types.hpp b/lib/core/types.hpp index c9fb4a68..05a9ce89 100644 --- a/lib/core/types.hpp +++ b/lib/core/types.hpp @@ -9,7 +9,7 @@ static constexpr float kEpsilon = 0.0001; enum class DigitalSignal { kLow = 0, kHigh }; enum class Result { kSuccess = 0, kFailure }; -enum class GyroscopeAxis {kX,kY,kZ}; +enum class GyroscopeAxis { kX, kY, kZ }; using Float = float; From b2a0e6ec7960636feefd2025cfbe9b1b786bdecf Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 15:39:06 +0000 Subject: [PATCH 12/17] Changes to formating --- lib/core/types.hpp | 2 +- lib/sensors/gyroscope.cpp | 58 +++++++++++++++++++++------------------ lib/sensors/gyroscope.hpp | 5 ++-- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/lib/core/types.hpp b/lib/core/types.hpp index 05a9ce89..59325a39 100644 --- a/lib/core/types.hpp +++ b/lib/core/types.hpp @@ -9,7 +9,7 @@ static constexpr float kEpsilon = 0.0001; enum class DigitalSignal { kLow = 0, kHigh }; enum class Result { kSuccess = 0, kFailure }; -enum class GyroscopeAxis { kX, kY, kZ }; +enum class Axis { kX, kY, kZ }; using Float = float; diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index df57d62d..589d8452 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -13,53 +13,53 @@ Gyroscope::~Gyroscope() { } -std::optional Gyroscope::read(core::GyroscopeAxis axis) +std::optional Gyroscope::read(core::Axis axis) { - if (axis == core::GyroscopeAxis::kX) { + if (axis == core::Axis::kX) { const auto gyroscope_x_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXHigh); if (!gyroscope_x_high_byte) { logger_.log( - core::LogLevel::kFatal, "Failed to, read gyroscope X-axis high at channel %d", channel_); + core::LogLevel::kFatal, "Failed to read gyroscope X-axis high at channel %d", channel_); return std::nullopt; } const auto gyroscope_x_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXLow); if (!gyroscope_x_low_byte) { logger_.log( - core::LogLevel::kFatal, "Failed to, read gyroscope X-axis low at channel %d", channel_); + core::LogLevel::kFatal, "Failed to read gyroscope X-axis low at channel %d", channel_); return std::nullopt; } const auto x_axis = ((gyroscope_x_high_byte.value() << 8) | gyroscope_x_low_byte.value()); logger_.log( core::LogLevel::kDebug, "Successfully read x-axis from gyroscope at channel %d", channel_); return x_axis; - } else if (axis == core::GyroscopeAxis::kY) { + } else if (axis == core::Axis::kY) { const auto gyroscope_y_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYHigh); if (!gyroscope_y_high_byte) { logger_.log( - core::LogLevel::kFatal, "Failed to, read gyroscope Y-axis high at channel %d", channel_); + core::LogLevel::kFatal, "Failed to read gyroscope Y-axis high at channel %d", channel_); return std::nullopt; } const auto gyroscope_y_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYLow); if (!gyroscope_y_low_byte) { logger_.log( - core::LogLevel::kFatal, "Failed to, read gyroscope Y-axis low at channel %d", channel_); + core::LogLevel::kFatal, "Failed to read gyroscope Y-axis low at channel %d", channel_); return std::nullopt; } const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); logger_.log( core::LogLevel::kDebug, "Successfully read y-axis from gyroscope at channel %d", channel_); return y_axis; - } else if (axis == core::GyroscopeAxis::kZ) { + } else if (axis == core::Axis::kZ) { const auto gyroscope_z_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZHigh); if (!gyroscope_z_high_byte) { logger_.log( - core::LogLevel::kFatal, "Failed to, read gyroscope Z-axis high at channel %d", channel_); + core::LogLevel::kFatal, "Failed to read gyroscope Z-axis high at channel %d", channel_); return std::nullopt; } const auto gyroscope_z_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZLow); if (!gyroscope_z_low_byte) { logger_.log( - core::LogLevel::kFatal, "Failed to, read gyroscope Z-axis low at channel %d", channel_); + core::LogLevel::kFatal, "Failed to read gyroscope Z-axis low at channel %d", channel_); return std::nullopt; } const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); @@ -68,7 +68,7 @@ std::optional Gyroscope::read(core::GyroscopeAxis axis) return z_axis; } else { logger_.log(core::LogLevel::kFatal, - "Failed to, read gyroscope at channel %d, axis parameters are invalid types", + "Failed to read gyroscope at channel %d, axis parameters are invalid types", channel_); return std::nullopt; } @@ -78,38 +78,44 @@ std::optional Gyroscope::create(core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel) { - const core::Result write_result1 + const core::Result write_result_from_Ctrl1 = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl1, kConfigurationSetting1); - if (write_result1 == core::Result::kFailure) { + if (write_result_from_Ctrl1 == core::Result::kFailure) { logger.log( - core::LogLevel::kFatal, "Failed to, configure gyroscope control 1 at channel %d", channel); + core::LogLevel::kFatal, + "Failed to configure the power mode setting in first control gyroscope at channel %d", + channel); return std::nullopt; } - const core::Result write_result2 + const core::Result write_result_from_Ctrl2 = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl2, kConfigurationSetting2); - if (write_result2 == core::Result::kFailure) { - logger.log( - core::LogLevel::kFatal, "Failed to, configure gyroscope control 2 at channel %d", channel); + if (write_result_from_Ctrl2 == core::Result::kFailure) { + logger.log(core::LogLevel::kFatal, + "Failed to configure the High pass filter in second control gyroscope at channel %d", + channel); return std::nullopt; } - const core::Result write_result3 + const core::Result write_result_from_Ctrl3 = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl3, kConfigurationSetting3); - if (write_result3 == core::Result::kFailure) { + if (write_result_from_Ctrl3 == core::Result::kFailure) { logger.log( - core::LogLevel::kFatal, "Failed to, configure gyroscope control 3 at channel %d", channel); + core::LogLevel::kFatal, + "Failed to configure the Boot and Interrupts in third control gyroscope at channel %d", + channel); return std::nullopt; } - const core::Result write_result5 + const core::Result write_result_from_Ctrl5 = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl5, kConfigurationSetting5); - if (write_result5 == core::Result::kFailure) { - logger.log( - core::LogLevel::kFatal, "Failed to, configure gyroscope control 5 at channel %d", channel); + if (write_result_from_Ctrl5 == core::Result::kFailure) { + logger.log(core::LogLevel::kFatal, + "Failed to enable FIFO in fifth control gyroscope at channel %d", + channel); return std::nullopt; } return Gyroscope(logger, i2c, channel); } -std::uint8_t Gyroscope::getChannel() +const std::uint8_t Gyroscope::getChannel() { return channel_; } diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index 79a0f5a1..7c0ec647 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -10,6 +10,7 @@ namespace hyped::sensors { +// Registers taken from the data sheet static constexpr std::uint8_t kDefaultGyroscopeAddress = 0x69; static constexpr std::uint8_t kDataXHigh = 0x29; static constexpr std::uint8_t kDataXLow = 0x28; @@ -33,8 +34,8 @@ class Gyroscope { const std::uint8_t channel); ~Gyroscope(); - std::optional read(core::GyroscopeAxis axis); - std::uint8_t getChannel(); + std::optional read(core::Axis axis); + const std::uint8_t getChannel(); private: Gyroscope(core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel); From 24ba2792afac8b5858fabf114622296303e216ab Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 16:40:26 +0000 Subject: [PATCH 13/17] Changing the if statements in read to switch --- lib/sensors/gyroscope.cpp | 101 +++++++++++++++++++------------------- lib/sensors/gyroscope.hpp | 2 +- 2 files changed, 51 insertions(+), 52 deletions(-) diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index 589d8452..9c79b685 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -13,64 +13,63 @@ Gyroscope::~Gyroscope() { } -std::optional Gyroscope::read(core::Axis axis) +const std::optional Gyroscope::read(core::Axis axis) { - if (axis == core::Axis::kX) { - const auto gyroscope_x_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXHigh); - if (!gyroscope_x_high_byte) { + switch (axis) { + case core::Axis::kX: { + const auto gyroscope_x_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXHigh); + if (!gyroscope_x_high_byte) { + logger_.log( + core::LogLevel::kFatal, "Failed to read gyroscope X-axis high at channel %d", channel_); + return std::nullopt; + } + const auto gyroscope_x_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXLow); + if (!gyroscope_x_low_byte) { + logger_.log( + core::LogLevel::kFatal, "Failed to read gyroscope X-axis low at channel %d", channel_); + return std::nullopt; + } + const auto x_axis = ((gyroscope_x_high_byte.value() << 8) | gyroscope_x_low_byte.value()); logger_.log( - core::LogLevel::kFatal, "Failed to read gyroscope X-axis high at channel %d", channel_); - return std::nullopt; + core::LogLevel::kDebug, "Successfully read x-axis from gyroscope at channel %d", channel_); + return x_axis; } - const auto gyroscope_x_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXLow); - if (!gyroscope_x_low_byte) { + case core::Axis::kY: { + const auto gyroscope_y_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYHigh); + if (!gyroscope_y_high_byte) { + logger_.log( + core::LogLevel::kFatal, "Failed to read gyroscope Y-axis high at channel %d", channel_); + return std::nullopt; + } + const auto gyroscope_y_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYLow); + if (!gyroscope_y_low_byte) { + logger_.log( + core::LogLevel::kFatal, "Failed to read gyroscope Y-axis low at channel %d", channel_); + return std::nullopt; + } + const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); logger_.log( - core::LogLevel::kFatal, "Failed to read gyroscope X-axis low at channel %d", channel_); - return std::nullopt; + core::LogLevel::kDebug, "Successfully read y-axis from gyroscope at channel %d", channel_); + return y_axis; } - const auto x_axis = ((gyroscope_x_high_byte.value() << 8) | gyroscope_x_low_byte.value()); - logger_.log( - core::LogLevel::kDebug, "Successfully read x-axis from gyroscope at channel %d", channel_); - return x_axis; - } else if (axis == core::Axis::kY) { - const auto gyroscope_y_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYHigh); - if (!gyroscope_y_high_byte) { + case core::Axis::kZ: { + const auto gyroscope_z_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZHigh); + if (!gyroscope_z_high_byte) { + logger_.log( + core::LogLevel::kFatal, "Failed to read gyroscope Z-axis high at channel %d", channel_); + return std::nullopt; + } + const auto gyroscope_z_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZLow); + if (!gyroscope_z_low_byte) { + logger_.log( + core::LogLevel::kFatal, "Failed to read gyroscope Z-axis low at channel %d", channel_); + return std::nullopt; + } + const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); logger_.log( - core::LogLevel::kFatal, "Failed to read gyroscope Y-axis high at channel %d", channel_); - return std::nullopt; + core::LogLevel::kDebug, "Successfully read z-axis from gyroscope at channel %d", channel_); + return z_axis; } - const auto gyroscope_y_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYLow); - if (!gyroscope_y_low_byte) { - logger_.log( - core::LogLevel::kFatal, "Failed to read gyroscope Y-axis low at channel %d", channel_); - return std::nullopt; - } - const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); - logger_.log( - core::LogLevel::kDebug, "Successfully read y-axis from gyroscope at channel %d", channel_); - return y_axis; - } else if (axis == core::Axis::kZ) { - const auto gyroscope_z_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZHigh); - if (!gyroscope_z_high_byte) { - logger_.log( - core::LogLevel::kFatal, "Failed to read gyroscope Z-axis high at channel %d", channel_); - return std::nullopt; - } - const auto gyroscope_z_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZLow); - if (!gyroscope_z_low_byte) { - logger_.log( - core::LogLevel::kFatal, "Failed to read gyroscope Z-axis low at channel %d", channel_); - return std::nullopt; - } - const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); - logger_.log( - core::LogLevel::kDebug, "Successfully read z-axis from gyroscope at channel %d", channel_); - return z_axis; - } else { - logger_.log(core::LogLevel::kFatal, - "Failed to read gyroscope at channel %d, axis parameters are invalid types", - channel_); - return std::nullopt; } } diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index 7c0ec647..d82f695b 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -34,7 +34,7 @@ class Gyroscope { const std::uint8_t channel); ~Gyroscope(); - std::optional read(core::Axis axis); + const std::optional read(core::Axis axis); const std::uint8_t getChannel(); private: From e4f1a3bd8d870b74799cf03b31d85680e1ce407e Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Sat, 4 Feb 2023 17:59:04 +0000 Subject: [PATCH 14/17] adding const to getChannel --- lib/sensors/gyroscope.cpp | 2 +- lib/sensors/gyroscope.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index 9c79b685..2beaa925 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -114,7 +114,7 @@ std::optional Gyroscope::create(core::ILogger &logger, return Gyroscope(logger, i2c, channel); } -const std::uint8_t Gyroscope::getChannel() +std::uint8_t Gyroscope::getChannel() const { return channel_; } diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index d82f695b..6830c82a 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -35,7 +35,7 @@ class Gyroscope { ~Gyroscope(); const std::optional read(core::Axis axis); - const std::uint8_t getChannel(); + std::uint8_t getChannel() const; private: Gyroscope(core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel); From 99c6aba9514011c77dd025f1fde942354fca33e0 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Thu, 9 Feb 2023 19:09:18 +0000 Subject: [PATCH 15/17] Adding the repl commands or gyroscope and making changes to adjust for the i2c pointer --- config/template.json | 5 ++++ lib/core/types.hpp | 1 - lib/debug/repl.cpp | 54 +++++++++++++++++++++++++++++++++++++++ lib/debug/repl.hpp | 2 ++ lib/sensors/gyroscope.cpp | 47 +++++++++++++++++++++------------- lib/sensors/gyroscope.hpp | 45 ++++++++++++++++++-------------- 6 files changed, 116 insertions(+), 38 deletions(-) diff --git a/config/template.json b/config/template.json index 08a2ccf1..5653f8b0 100644 --- a/config/template.json +++ b/config/template.json @@ -29,6 +29,11 @@ "enabled": true, "bus": 2, "device_address": 25 + }, + "gyroscope": { + "enabled": true, + "bus": 2, + "device_address": 105 } } } diff --git a/lib/core/types.hpp b/lib/core/types.hpp index 6a42a910..55018caf 100644 --- a/lib/core/types.hpp +++ b/lib/core/types.hpp @@ -11,7 +11,6 @@ static constexpr float kEpsilon = 0.0001; enum class DigitalSignal { kLow = 0, kHigh }; enum class Result { kSuccess = 0, kFailure }; -enum class Axis { kX, kY, kZ }; using Float = float; diff --git a/lib/debug/repl.cpp b/lib/debug/repl.cpp index 4928e4cb..953959b3 100644 --- a/lib/debug/repl.cpp +++ b/lib/debug/repl.cpp @@ -493,6 +493,60 @@ void Repl::addAccelerometerCommands(const std::uint8_t bus, const std::uint8_t d addCommand(accelerometer_read_command); } +void Repl::addGyroscopeCommands(const std::uint8_t bus, const std::uint8_t device_address) +{ + const auto optional_i2c = io::HardwareI2c::create(logger_, bus); + if (!optional_i2c) { + logger_.log(core::LogLevel::kFatal, "Failed to create I2C instance on bus %d", bus); + return; + } + const auto i2c = std::move(*optional_i2c); + const auto optional_gyroscope = sensors::Gyroscope::create(logger_, i2c, bus, device_address); + const auto gyroscope = std::make_shared(*optional_gyroscope); + Command gyroscope_read_command; + std::stringstream identifier; + identifier << "gyroscope 0x" << std::hex << static_cast(device_address) << " read"; + gyroscope_read_command.name = identifier.str(); + std::stringstream description; + description << "Read gyroscope sensor 0x" << std::hex << static_cast(device_address) + << " on " + << "I2C bus " << static_cast(bus); + gyroscope_read_command.description = description.str(); + gyroscope_read_command.handler = [this, gyroscope, bus]() { + std::uint16_t axis; + std::cout << "axis (0 is x, 1 is y, 2 is z): "; + std::cin >> axis; + std::optional value; + switch (axis) { + case 0: { + value = gyroscope->read(core::Axis::kX); + break; + } + case 1: { + value = gyroscope->read(core::Axis::kY); + break; + } + case 2: { + value = gyroscope->read(core::Axis::kZ); + break; + } + default: { + logger_.log(core::LogLevel::kFatal, + "Failed to read recognise the gyroscope axis that was inputted from bus %d", + bus); + } + } + + if (!value) { + logger_.log(core::LogLevel::kFatal, "Failed to read gyroscope from bus %d", bus); + } else { + const auto result = value.value(); + logger_.log(core::LogLevel::kInfo, "Gyroscope is : %d", result); + } + }; + addCommand(gyroscope_read_command); +} + void Repl::addUartCommands(const std::uint8_t bus) { const UartBus uart_bus = static_cast(bus); diff --git a/lib/debug/repl.hpp b/lib/debug/repl.hpp index 49e1bb25..f60d2dc8 100644 --- a/lib/debug/repl.hpp +++ b/lib/debug/repl.hpp @@ -14,6 +14,7 @@ #include #include #include +#include namespace hyped::debug { @@ -41,6 +42,7 @@ class Repl { void addPwmCommands(const std::uint8_t module); void addSpiCommands(const std::uint8_t bus); void addAccelerometerCommands(const std::uint8_t bus, const std::uint8_t device_address); + void addGyroscopeCommands(const std::uint8_t bus, const std::uint8_t device_address); void addUartCommands(const std::uint8_t bus); core::ILogger &logger_; diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index 2beaa925..b11c1021 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -2,10 +2,14 @@ namespace hyped::sensors { -Gyroscope::Gyroscope(core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel) +Gyroscope::Gyroscope(core::ILogger &logger, + std::shared_ptr i2c, + const std::uint8_t channel, + const std::uint8_t device_address) : logger_(logger), i2c_(i2c), - channel_(channel) + channel_(channel), + device_address_(device_address) { } @@ -17,13 +21,13 @@ const std::optional Gyroscope::read(core::Axis axis) { switch (axis) { case core::Axis::kX: { - const auto gyroscope_x_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXHigh); + const auto gyroscope_x_high_byte = i2c_->readByte(device_address_, kDataXHigh); if (!gyroscope_x_high_byte) { logger_.log( core::LogLevel::kFatal, "Failed to read gyroscope X-axis high at channel %d", channel_); return std::nullopt; } - const auto gyroscope_x_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataXLow); + const auto gyroscope_x_low_byte = i2c_->readByte(device_address_, kDataXLow); if (!gyroscope_x_low_byte) { logger_.log( core::LogLevel::kFatal, "Failed to read gyroscope X-axis low at channel %d", channel_); @@ -32,16 +36,17 @@ const std::optional Gyroscope::read(core::Axis axis) const auto x_axis = ((gyroscope_x_high_byte.value() << 8) | gyroscope_x_low_byte.value()); logger_.log( core::LogLevel::kDebug, "Successfully read x-axis from gyroscope at channel %d", channel_); - return x_axis; + // Todo figure out what type needs to be returned for all axis + return static_cast(x_axis * 0.00875); } case core::Axis::kY: { - const auto gyroscope_y_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYHigh); + const auto gyroscope_y_high_byte = i2c_->readByte(device_address_, kDataYHigh); if (!gyroscope_y_high_byte) { logger_.log( core::LogLevel::kFatal, "Failed to read gyroscope Y-axis high at channel %d", channel_); return std::nullopt; } - const auto gyroscope_y_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataYLow); + const auto gyroscope_y_low_byte = i2c_->readByte(device_address_, kDataYLow); if (!gyroscope_y_low_byte) { logger_.log( core::LogLevel::kFatal, "Failed to read gyroscope Y-axis low at channel %d", channel_); @@ -50,16 +55,16 @@ const std::optional Gyroscope::read(core::Axis axis) const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); logger_.log( core::LogLevel::kDebug, "Successfully read y-axis from gyroscope at channel %d", channel_); - return y_axis; + return static_cast(y_axis * 0.00875); } case core::Axis::kZ: { - const auto gyroscope_z_high_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZHigh); + const auto gyroscope_z_high_byte = i2c_->readByte(device_address_, kDataZHigh); if (!gyroscope_z_high_byte) { logger_.log( core::LogLevel::kFatal, "Failed to read gyroscope Z-axis high at channel %d", channel_); return std::nullopt; } - const auto gyroscope_z_low_byte = i2c_.readByte(kDefaultGyroscopeAddress, kDataZLow); + const auto gyroscope_z_low_byte = i2c_->readByte(device_address_, kDataZLow); if (!gyroscope_z_low_byte) { logger_.log( core::LogLevel::kFatal, "Failed to read gyroscope Z-axis low at channel %d", channel_); @@ -68,17 +73,23 @@ const std::optional Gyroscope::read(core::Axis axis) const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); logger_.log( core::LogLevel::kDebug, "Successfully read z-axis from gyroscope at channel %d", channel_); - return z_axis; + return static_cast(z_axis * 0.00875); + } + default: { + logger_.log(core::LogLevel::kFatal, "Gave an invalid axis"); + return std::nullopt; } } + } std::optional Gyroscope::create(core::ILogger &logger, - io::II2c &i2c, - const std::uint8_t channel) + std::shared_ptr i2c, + const std::uint8_t channel, + const std::uint8_t device_address) { const core::Result write_result_from_Ctrl1 - = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl1, kConfigurationSetting1); + = i2c->writeByteToRegister(device_address, kCtrl1, kConfigurationSetting1); if (write_result_from_Ctrl1 == core::Result::kFailure) { logger.log( core::LogLevel::kFatal, @@ -87,7 +98,7 @@ std::optional Gyroscope::create(core::ILogger &logger, return std::nullopt; } const core::Result write_result_from_Ctrl2 - = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl2, kConfigurationSetting2); + = i2c->writeByteToRegister(device_address, kCtrl2, kConfigurationSetting2); if (write_result_from_Ctrl2 == core::Result::kFailure) { logger.log(core::LogLevel::kFatal, "Failed to configure the High pass filter in second control gyroscope at channel %d", @@ -95,7 +106,7 @@ std::optional Gyroscope::create(core::ILogger &logger, return std::nullopt; } const core::Result write_result_from_Ctrl3 - = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl3, kConfigurationSetting3); + = i2c->writeByteToRegister(device_address, kCtrl3, kConfigurationSetting3); if (write_result_from_Ctrl3 == core::Result::kFailure) { logger.log( core::LogLevel::kFatal, @@ -104,14 +115,14 @@ std::optional Gyroscope::create(core::ILogger &logger, return std::nullopt; } const core::Result write_result_from_Ctrl5 - = i2c.writeByteToRegister(kDefaultGyroscopeAddress, kCtrl5, kConfigurationSetting5); + = i2c->writeByteToRegister(device_address, kCtrl5, kConfigurationSetting5); if (write_result_from_Ctrl5 == core::Result::kFailure) { logger.log(core::LogLevel::kFatal, "Failed to enable FIFO in fifth control gyroscope at channel %d", channel); return std::nullopt; } - return Gyroscope(logger, i2c, channel); + return Gyroscope(logger, i2c, channel, device_address); } std::uint8_t Gyroscope::getChannel() const diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index 6830c82a..4400d21f 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -3,6 +3,7 @@ #include "i2c_sensors.hpp" #include +#include #include #include @@ -11,38 +12,44 @@ namespace hyped::sensors { // Registers taken from the data sheet -static constexpr std::uint8_t kDefaultGyroscopeAddress = 0x69; -static constexpr std::uint8_t kDataXHigh = 0x29; -static constexpr std::uint8_t kDataXLow = 0x28; -static constexpr std::uint8_t kDataYHigh = 0x2B; -static constexpr std::uint8_t kDataYLow = 0x2A; -static constexpr std::uint8_t kDataZHigh = 0x2D; -static constexpr std::uint8_t kDataZLow = 0x2C; -static constexpr std::uint8_t kCtrl1 = 0x20; -static constexpr std::uint8_t kCtrl2 = 0x21; -static constexpr std::uint8_t kCtrl3 = 0x22; -static constexpr std::uint8_t kCtrl5 = 0x24; -static constexpr std::uint8_t kConfigurationSetting1 = 0xff; -static constexpr std::uint8_t kConfigurationSetting2 = 0x20; -static constexpr std::uint8_t kConfigurationSetting3 = 0xff; -static constexpr std::uint8_t kConfigurationSetting5 = 0x40; +constexpr std::uint8_t kDefaultGyroscopeAddress = 0x69; +constexpr std::uint8_t kDataXHigh = 0x29; +constexpr std::uint8_t kDataXLow = 0x28; +constexpr std::uint8_t kDataYHigh = 0x2B; +constexpr std::uint8_t kDataYLow = 0x2A; +constexpr std::uint8_t kDataZHigh = 0x2D; +constexpr std::uint8_t kDataZLow = 0x2C; +constexpr std::uint8_t kCtrl1 = 0x20; +constexpr std::uint8_t kCtrl2 = 0x21; +constexpr std::uint8_t kCtrl3 = 0x22; +constexpr std::uint8_t kCtrl5 = 0x24; +constexpr std::uint8_t kConfigurationSetting1 = 0xff; +constexpr std::uint8_t kConfigurationSetting2 = 0x20; +constexpr std::uint8_t kConfigurationSetting3 = 0xff; +constexpr std::uint8_t kConfigurationSetting5 = 0x40; class Gyroscope { public: static std::optional create(core::ILogger &logger, - io::II2c &i2c, - const std::uint8_t channel); + std::shared_ptr i2c, + const std::uint8_t channel, + const std::uint8_t device_address + = kDefaultGyroscopeAddress); ~Gyroscope(); const std::optional read(core::Axis axis); std::uint8_t getChannel() const; private: - Gyroscope(core::ILogger &logger, io::II2c &i2c, const std::uint8_t channel); + Gyroscope(core::ILogger &logger, + std::shared_ptr i2c, + const std::uint8_t channel, + const std::uint8_t device_address); core::ILogger &logger_; - io::II2c &i2c_; + std::shared_ptr i2c_; const std::uint8_t channel_; + const std::uint8_t device_address_; }; } // namespace hyped::sensors \ No newline at end of file From ed1aa156ec95d00bd4fb0bab7dbfe91400cef1e3 Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Fri, 10 Feb 2023 20:15:17 +0000 Subject: [PATCH 16/17] Changes made to the formating --- lib/debug/repl.cpp | 5 ++--- lib/sensors/gyroscope.cpp | 30 ++++++++++++++---------------- lib/sensors/gyroscope.hpp | 1 + 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/debug/repl.cpp b/lib/debug/repl.cpp index 953959b3..fdfb28f2 100644 --- a/lib/debug/repl.cpp +++ b/lib/debug/repl.cpp @@ -531,9 +531,8 @@ void Repl::addGyroscopeCommands(const std::uint8_t bus, const std::uint8_t devic break; } default: { - logger_.log(core::LogLevel::kFatal, - "Failed to read recognise the gyroscope axis that was inputted from bus %d", - bus); + logger_.log( + core::LogLevel::kFatal, "Failed to read gyroscope due to invalid axis from bus %d", bus); } } diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index b11c1021..18b88f16 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -33,11 +33,10 @@ const std::optional Gyroscope::read(core::Axis axis) core::LogLevel::kFatal, "Failed to read gyroscope X-axis low at channel %d", channel_); return std::nullopt; } - const auto x_axis = ((gyroscope_x_high_byte.value() << 8) | gyroscope_x_low_byte.value()); + const auto x_axis = ((*gyroscope_x_high_byte << 8) | *gyroscope_x_low_byte); logger_.log( core::LogLevel::kDebug, "Successfully read x-axis from gyroscope at channel %d", channel_); - // Todo figure out what type needs to be returned for all axis - return static_cast(x_axis * 0.00875); + return static_cast(x_axis * 0.00875); } case core::Axis::kY: { const auto gyroscope_y_high_byte = i2c_->readByte(device_address_, kDataYHigh); @@ -52,10 +51,10 @@ const std::optional Gyroscope::read(core::Axis axis) core::LogLevel::kFatal, "Failed to read gyroscope Y-axis low at channel %d", channel_); return std::nullopt; } - const auto y_axis = ((gyroscope_y_high_byte.value() << 8) | gyroscope_y_low_byte.value()); + const auto y_axis = ((*gyroscope_y_high_byte << 8) | *gyroscope_y_low_byte); logger_.log( core::LogLevel::kDebug, "Successfully read y-axis from gyroscope at channel %d", channel_); - return static_cast(y_axis * 0.00875); + return static_cast(y_axis * 0.00875); } case core::Axis::kZ: { const auto gyroscope_z_high_byte = i2c_->readByte(device_address_, kDataZHigh); @@ -70,17 +69,16 @@ const std::optional Gyroscope::read(core::Axis axis) core::LogLevel::kFatal, "Failed to read gyroscope Z-axis low at channel %d", channel_); return std::nullopt; } - const auto z_axis = ((gyroscope_z_high_byte.value() << 8) | gyroscope_z_low_byte.value()); + const auto z_axis = ((*gyroscope_z_high_byte << 8) | *gyroscope_z_low_byte); logger_.log( core::LogLevel::kDebug, "Successfully read z-axis from gyroscope at channel %d", channel_); - return static_cast(z_axis * 0.00875); + return static_cast(z_axis * 0.00875); } default: { logger_.log(core::LogLevel::kFatal, "Gave an invalid axis"); return std::nullopt; } } - } std::optional Gyroscope::create(core::ILogger &logger, @@ -88,35 +86,35 @@ std::optional Gyroscope::create(core::ILogger &logger, const std::uint8_t channel, const std::uint8_t device_address) { - const core::Result write_result_from_Ctrl1 + const core::Result write_result_from_ctrl1 = i2c->writeByteToRegister(device_address, kCtrl1, kConfigurationSetting1); - if (write_result_from_Ctrl1 == core::Result::kFailure) { + if (write_result_from_ctrl1 == core::Result::kFailure) { logger.log( core::LogLevel::kFatal, "Failed to configure the power mode setting in first control gyroscope at channel %d", channel); return std::nullopt; } - const core::Result write_result_from_Ctrl2 + const core::Result write_result_from_ctrl2 = i2c->writeByteToRegister(device_address, kCtrl2, kConfigurationSetting2); - if (write_result_from_Ctrl2 == core::Result::kFailure) { + if (write_result_from_ctrl2 == core::Result::kFailure) { logger.log(core::LogLevel::kFatal, "Failed to configure the High pass filter in second control gyroscope at channel %d", channel); return std::nullopt; } - const core::Result write_result_from_Ctrl3 + const core::Result write_result_from_ctrl3 = i2c->writeByteToRegister(device_address, kCtrl3, kConfigurationSetting3); - if (write_result_from_Ctrl3 == core::Result::kFailure) { + if (write_result_from_ctrl3 == core::Result::kFailure) { logger.log( core::LogLevel::kFatal, "Failed to configure the Boot and Interrupts in third control gyroscope at channel %d", channel); return std::nullopt; } - const core::Result write_result_from_Ctrl5 + const core::Result write_result_from_ctrl5 = i2c->writeByteToRegister(device_address, kCtrl5, kConfigurationSetting5); - if (write_result_from_Ctrl5 == core::Result::kFailure) { + if (write_result_from_ctrl5 == core::Result::kFailure) { logger.log(core::LogLevel::kFatal, "Failed to enable FIFO in fifth control gyroscope at channel %d", channel); diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index 4400d21f..9eb101f3 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -7,6 +7,7 @@ #include #include +#include #include namespace hyped::sensors { From 91d7bcc175e1c6a3cbc876ae89acb6598043a8ad Mon Sep 17 00:00:00 2001 From: HTTYDKing Date: Thu, 6 Apr 2023 10:37:15 +0100 Subject: [PATCH 17/17] Relevant changes with struct but issue still prosides --- lib/core/types.hpp | 12 +++++++ lib/debug/repl.cpp | 72 ++++++++++++++++++++++++--------------- lib/debug/repl.hpp | 2 ++ lib/sensors/gyroscope.cpp | 23 ++++++++++--- lib/sensors/gyroscope.hpp | 5 ++- 5 files changed, 81 insertions(+), 33 deletions(-) diff --git a/lib/core/types.hpp b/lib/core/types.hpp index 55018caf..0cdba4a3 100644 --- a/lib/core/types.hpp +++ b/lib/core/types.hpp @@ -62,6 +62,18 @@ struct RawAccelerationData { const TimePoint measured_at; }; +struct GyroscopeData { + GyroscopeData(const core::Float x, + const TimePoint measured_at) + : x(x), + measured_at(measured_at) + { + } + + const core::Float x; + const TimePoint measured_at; +}; + enum class Axis { kX = 0, kY, kZ }; } // namespace hyped::core \ No newline at end of file diff --git a/lib/debug/repl.cpp b/lib/debug/repl.cpp index fdfb28f2..332f65ca 100644 --- a/lib/debug/repl.cpp +++ b/lib/debug/repl.cpp @@ -196,6 +196,28 @@ std::optional> Repl::fromFile(const std::string &path) const auto bus = accelerometer["bus"].GetUint(); repl->addAccelerometerCommands(bus, device_address); } + + if (!sensors.HasMember("gyroscope")) { + logger_.log(core::LogLevel::kFatal, + "Missing required field 'sensors.gyroscope' in configuration file"); + return std::nullopt; + } + const auto gyroscope = sensors["gyroscope"].GetObject(); + if (!gyroscope.HasMember("enabled")) { + logger_.log(core::LogLevel::kFatal, + "Missing required field 'sensors.gyroscope.enabled' in configuration file"); + return std::nullopt; + } + if (gyroscope["enabled"].GetBool()) { + if (!gyroscope.HasMember("bus")) { + logger_.log(core::LogLevel::kFatal, + "Missing required field 'sensors.gyroscope.bus' in configuration file"); + return std::nullopt; + } + const auto device_address = gyroscope["device_address"].GetUint(); + const auto bus = gyroscope["bus"].GetUint(); + repl->addGyroscopeCommands(bus, device_address); + } return repl; } @@ -513,39 +535,33 @@ void Repl::addGyroscopeCommands(const std::uint8_t bus, const std::uint8_t devic << "I2C bus " << static_cast(bus); gyroscope_read_command.description = description.str(); gyroscope_read_command.handler = [this, gyroscope, bus]() { - std::uint16_t axis; - std::cout << "axis (0 is x, 1 is y, 2 is z): "; - std::cin >> axis; - std::optional value; - switch (axis) { - case 0: { - value = gyroscope->read(core::Axis::kX); - break; - } - case 1: { - value = gyroscope->read(core::Axis::kY); - break; - } - case 2: { - value = gyroscope->read(core::Axis::kZ); - break; - } - default: { - logger_.log( - core::LogLevel::kFatal, "Failed to read gyroscope due to invalid axis from bus %d", bus); - } - } - - if (!value) { - logger_.log(core::LogLevel::kFatal, "Failed to read gyroscope from bus %d", bus); - } else { + core::Float Angle = 0; + core::Float Old_data = 0; + auto Old_time = std::chrono::system_clock::now(); + std::optional value; + + while (true) { + std::optional value = gyroscope->read(core::Axis::kX); const auto result = value.value(); - logger_.log(core::LogLevel::kInfo, "Gyroscope is : %d", result); - } + + std::chrono::duration elapsed_seconds = result.measured_at - Old_time; + + auto time = (core::Float) elapsed_seconds.count(); + + Angle = 0.5*(Old_data+result.x)*time + Angle; + + Old_time = result.measured_at; + Old_data = result.x; + + logger_.log(core::LogLevel::kInfo, "Gyroscope is : %f", Angle); + + }; + }; addCommand(gyroscope_read_command); } + void Repl::addUartCommands(const std::uint8_t bus) { const UartBus uart_bus = static_cast(bus); diff --git a/lib/debug/repl.hpp b/lib/debug/repl.hpp index f60d2dc8..d00a36d9 100644 --- a/lib/debug/repl.hpp +++ b/lib/debug/repl.hpp @@ -15,6 +15,8 @@ #include #include #include +#include +#include namespace hyped::debug { diff --git a/lib/sensors/gyroscope.cpp b/lib/sensors/gyroscope.cpp index 18b88f16..a45d3989 100644 --- a/lib/sensors/gyroscope.cpp +++ b/lib/sensors/gyroscope.cpp @@ -17,7 +17,7 @@ Gyroscope::~Gyroscope() { } -const std::optional Gyroscope::read(core::Axis axis) +std::optional Gyroscope::read(core::Axis axis) { switch (axis) { case core::Axis::kX: { @@ -36,7 +36,12 @@ const std::optional Gyroscope::read(core::Axis axis) const auto x_axis = ((*gyroscope_x_high_byte << 8) | *gyroscope_x_low_byte); logger_.log( core::LogLevel::kDebug, "Successfully read x-axis from gyroscope at channel %d", channel_); - return static_cast(x_axis * 0.00875); + + core::Float X_Value = (core::Float)x_axis; + X_Value *= 0.00875; + const std::optional X_values(std::in_place,X_Value,std::chrono::system_clock::now()); + + return X_values; } case core::Axis::kY: { const auto gyroscope_y_high_byte = i2c_->readByte(device_address_, kDataYHigh); @@ -54,7 +59,12 @@ const std::optional Gyroscope::read(core::Axis axis) const auto y_axis = ((*gyroscope_y_high_byte << 8) | *gyroscope_y_low_byte); logger_.log( core::LogLevel::kDebug, "Successfully read y-axis from gyroscope at channel %d", channel_); - return static_cast(y_axis * 0.00875); + + core::Float Y_Value = (core::Float)y_axis; + Y_Value *= 0.00875; + const std::optional Y_values(std::in_place,Y_Value,std::chrono::system_clock::now()); + + return Y_values; } case core::Axis::kZ: { const auto gyroscope_z_high_byte = i2c_->readByte(device_address_, kDataZHigh); @@ -72,7 +82,12 @@ const std::optional Gyroscope::read(core::Axis axis) const auto z_axis = ((*gyroscope_z_high_byte << 8) | *gyroscope_z_low_byte); logger_.log( core::LogLevel::kDebug, "Successfully read z-axis from gyroscope at channel %d", channel_); - return static_cast(z_axis * 0.00875); + + core::Float Z_Value = (core::Float)z_axis; + Z_Value *= 0.00875; + const std::optional Z_values(std::in_place,Z_Value,std::chrono::system_clock::now()); + + return Z_values; } default: { logger_.log(core::LogLevel::kFatal, "Gave an invalid axis"); diff --git a/lib/sensors/gyroscope.hpp b/lib/sensors/gyroscope.hpp index 9eb101f3..714dc39b 100644 --- a/lib/sensors/gyroscope.hpp +++ b/lib/sensors/gyroscope.hpp @@ -2,12 +2,15 @@ #include "i2c_sensors.hpp" +#include + #include #include #include #include #include +#include #include namespace hyped::sensors { @@ -38,7 +41,7 @@ class Gyroscope { = kDefaultGyroscopeAddress); ~Gyroscope(); - const std::optional read(core::Axis axis); + std::optional read(core::Axis axis); std::uint8_t getChannel() const; private: