-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SNS-Gyroscope #82
base: master
Are you sure you want to change the base?
SNS-Gyroscope #82
Conversation
… the read still need to be done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're getting the hang of this
…th a change of parameters
…th a change of parameters
…th a change of parameters
lib/sensors/gyroscope.hpp
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment what these configuration values are doing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This just doesn't seem like it would always be worth failing the run over...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thats an amazing catch, going to push for my overhaul to get merged to we can merge master and get kWarn
here
lib/sensors/gyroscope.hpp
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
lib/sensors/gyroscope.hpp
Outdated
= kDefaultGyroscopeAddress); | ||
~Gyroscope(); | ||
|
||
const std::optional<std::int16_t> read(core::Axis axis); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it normal to declare return type constant? I haven't seen it elsewhere in the repo
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it can have two different addresses, @HTTYDKing make sure you add the "alternative address" as well, think its 0x68
lib/debug/repl.cpp
Outdated
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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auto
?
|
||
class Gyroscope { | ||
public: | ||
static std::optional<Gyroscope> create(core::ILogger &logger, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still explicitly call static
? @SnickeyX
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes here its necessary, else create
will not be able to access gyroscope specifc functions
lib/sensors/gyroscope.cpp
Outdated
{ | ||
} | ||
|
||
const std::optional<std::int16_t> Gyroscope::read(core::Axis axis) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does const
not go after instead of before?
lib/sensors/gyroscope.cpp
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is 0.00875 and where did it come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also elsewhere in the file. Maybe give it a name and store it somewhere?
@HTTYDKing remember to merge master for merge conflicts |
@@ -62,6 +62,18 @@ struct RawAccelerationData { | |||
const TimePoint measured_at; | |||
}; | |||
|
|||
struct GyroscopeData { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the use for this? Is there no more suitable alternatives?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
static_cast<core::Float>(...)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"Gyroscope is "? This doesn't make much sense?
@@ -14,6 +14,9 @@ | |||
#include <io/hardware_uart.hpp> | |||
#include <io/pwm.hpp> | |||
#include <sensors/accelerometer.hpp> | |||
#include <sensors/gyroscope.hpp> | |||
#include <unistd.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, think we use cstdint
here instead?
@@ -14,6 +14,9 @@ | |||
#include <io/hardware_uart.hpp> | |||
#include <io/pwm.hpp> | |||
#include <sensors/accelerometer.hpp> | |||
#include <sensors/gyroscope.hpp> | |||
#include <unistd.h> | |||
#include <chrono> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used?
= 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.