Conversation
… the read still need to be done
ishmis
left a comment
There was a problem hiding this comment.
you're getting the hang of this
…th a change of parameters
…th a change of parameters
…th a change of parameters
| 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; |
There was a problem hiding this comment.
comment what these configuration values are doing
There was a problem hiding this comment.
also remove the static since it isn't actually helping much with const values
| 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_); |
There was a problem hiding this comment.
I thought the gyroscope only mattered if the active suspension was being used? Therefore would it not make more sense to log fatal only if active suspension is being used and just logging kWarn if active suspension is disabled @SnickeyX @kshxtij ??
There was a problem hiding this comment.
This just doesn't seem like it would always be worth failing the run over...
There was a problem hiding this comment.
thats an amazing catch, going to push for my overhaul to get merged to we can merge master and get kWarn here
| 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; |
There was a problem hiding this comment.
I know we usually use full names when in doubt but kConfigurationSetting1 seems quite verbose. Could we shorten this to kConfigSetting1 since don't think this harms readability as it is a common abbreviation?
…r the i2c pointer
| = kDefaultGyroscopeAddress); | ||
| ~Gyroscope(); | ||
|
|
||
| const std::optional<std::int16_t> read(core::Axis axis); |
There was a problem hiding this comment.
is it normal to declare return type constant? I haven't seen it elsewhere in the repo
There was a problem hiding this comment.
this actually makes sense since this just enforeces that member variables arent modifed; although the const should be suffixed
| logger_.log(core::LogLevel::kFatal, "Gave an invalid axis"); | ||
| return std::nullopt; | ||
| } | ||
| } |
There was a problem hiding this comment.
i think this should probably be restructured to have a readAxis helper function and maybe pass in kDataXHigh and kDataXLow as parameters, right now this is a lot of almost duplicate code.
| std::shared_ptr<io::II2c> i2c, | ||
| const std::uint8_t channel, | ||
| const std::uint8_t device_address | ||
| = kDefaultGyroscopeAddress); |
There was a problem hiding this comment.
i am not keen on having default parameters, if gyroscopes will always have the address kDefaultGyroscopeAddress then rename to kGyroscopeAddress and drop the device_address parameter and instead use the constant, or if a gyro might have other addresses then the address should always be passed in when constructing.
There was a problem hiding this comment.
it can have two different addresses, @HTTYDKing make sure you add the "alternative address" as well, think its 0x68
| std::uint16_t axis; | ||
| std::cout << "axis (0 is x, 1 is y, 2 is z): "; | ||
| std::cin >> axis; | ||
| std::optional<std::int16_t> value; |
|
|
||
| class Gyroscope { | ||
| public: | ||
| static std::optional<Gyroscope> create(core::ILogger &logger, |
There was a problem hiding this comment.
Do we still explicitly call static? @SnickeyX
There was a problem hiding this comment.
yes here its necessary, else create will not be able to access gyroscope specifc functions
| { | ||
| } | ||
|
|
||
| const std::optional<std::int16_t> Gyroscope::read(core::Axis axis) |
There was a problem hiding this comment.
Does const not go after instead of before?
| 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<std::uint16_t>(x_axis * 0.00875); |
There was a problem hiding this comment.
What is 0.00875 and where did it come from?
There was a problem hiding this comment.
Also elsewhere in the file. Maybe give it a name and store it somewhere?
|
@HTTYDKing remember to merge master for merge conflicts |
| const TimePoint measured_at; | ||
| }; | ||
|
|
||
| struct GyroscopeData { |
There was a problem hiding this comment.
What's the use for this? Is there no more suitable alternatives?
There was a problem hiding this comment.
Shouldn't this be a static thing that can get passed somewhere for full processing?
| "Missing required field 'sensors.gyroscope.bus' in configuration file"); | ||
| return std::nullopt; | ||
| } | ||
| const auto device_address = gyroscope["device_address"].GetUint(); |
There was a problem hiding this comment.
Did you check this exists first?
| auto Old_time = std::chrono::system_clock::now(); | ||
| std::optional<core::GyroscopeData> value; | ||
|
|
||
| while (true) { |
There was a problem hiding this comment.
In general, we try to avoid making loops like this. Although not exactly adhered to, we try to follow NASA's rules for safety critical code, one of which (specifically rule 2) is that all loops must have a statically defined upper bound to avoid infinite loops and other such upsetting happenings.
| std::optional<core::GyroscopeData> value = gyroscope->read(core::Axis::kX); | ||
| const auto result = value.value(); | ||
|
|
||
| std::chrono::duration<double> elapsed_seconds = result.measured_at - Old_time; |
There was a problem hiding this comment.
Something like previous_timestamp?
|
|
||
| std::chrono::duration<double> elapsed_seconds = result.measured_at - Old_time; | ||
|
|
||
| auto time = (core::Float) elapsed_seconds.count(); |
There was a problem hiding this comment.
static_cast<core::Float>(...)
There was a problem hiding this comment.
also, more informative naming?
| Old_time = result.measured_at; | ||
| Old_data = result.x; | ||
|
|
||
| logger_.log(core::LogLevel::kInfo, "Gyroscope is : %f", Angle); |
There was a problem hiding this comment.
"Gyroscope is "? This doesn't make much sense?
| #include <io/pwm.hpp> | ||
| #include <sensors/accelerometer.hpp> | ||
| #include <sensors/gyroscope.hpp> | ||
| #include <unistd.h> |
There was a problem hiding this comment.
For consistency, think we use cstdint here instead?
| #include <sensors/accelerometer.hpp> | ||
| #include <sensors/gyroscope.hpp> | ||
| #include <unistd.h> | ||
| #include <chrono> |
| = i2c->writeByteToRegister(device_address, kCtrl1, kConfigurationSetting1); | ||
| if (write_result_from_ctrl1 == core::Result::kFailure) { | ||
| logger.log( | ||
| core::LogLevel::kFatal, |
There was a problem hiding this comment.
Again, isn't this only critical failure if we're actually using active suspension?
| std::optional<core::GyroscopeData> value = gyroscope->read(core::Axis::kX); | ||
| const auto result = value.value(); | ||
|
|
||
| std::chrono::duration<double> elapsed_seconds = result.measured_at - Old_time; |
There was a problem hiding this comment.
Have a look in core. Most of that functionality should already be implemented and if possible, we want to keep the ugly chrono notation contained in there.
Separate branch for the gyroscope sensor.