Skip to content

SNS-Gyroscope - #82

Open
HTTYDKing wants to merge 18 commits into
masterfrom
sns-gyro
Open

SNS-Gyroscope#82
HTTYDKing wants to merge 18 commits into
masterfrom
sns-gyro

Conversation

@HTTYDKing

Copy link
Copy Markdown
Contributor

Separate branch for the gyroscope sensor.

@HTTYDKing
HTTYDKing requested a review from ishmis as a code owner February 2, 2023 18:54
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.hpp Outdated
Comment thread lib/sensors/gyroscope.hpp Outdated

@ishmis ishmis left a comment

Copy link
Copy Markdown
Contributor

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

Comment thread lib/sensors/gyroscope.hpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.hpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
@HTTYDKing
HTTYDKing requested a review from kshxtij as a code owner February 4, 2023 13:49
Comment thread lib/core/types.hpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.hpp Outdated
Comment thread lib/sensors/gyroscope.hpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.hpp Outdated
Comment on lines +25 to +28
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;

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

Comment thread lib/sensors/gyroscope.cpp
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_);

Copy link
Copy Markdown
Member

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 ??

Copy link
Copy Markdown
Member

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...

Copy link
Copy Markdown
Contributor

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

Comment thread 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;

Copy link
Copy Markdown
Member

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?

Comment thread lib/sensors/gyroscope.hpp
Comment thread lib/debug/repl.cpp Outdated
Comment thread lib/sensors/gyroscope.cpp Outdated
Comment thread lib/sensors/gyroscope.hpp Outdated
= kDefaultGyroscopeAddress);
~Gyroscope();

const std::optional<std::int16_t> read(core::Axis axis);

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown
Contributor

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

Comment thread lib/sensors/gyroscope.cpp
logger_.log(core::LogLevel::kFatal, "Gave an invalid axis");
return std::nullopt;
}
}

Copy link
Copy Markdown
Contributor

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.

Comment thread lib/sensors/gyroscope.cpp
Comment thread lib/sensors/gyroscope.hpp
std::shared_ptr<io::II2c> i2c,
const std::uint8_t channel,
const std::uint8_t device_address
= kDefaultGyroscopeAddress);

@TomLonergan03 TomLonergan03 Feb 10, 2023

Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
Contributor

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

Comment thread 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

auto?

Comment thread lib/sensors/gyroscope.hpp

class Gyroscope {
public:
static std::optional<Gyroscope> create(core::ILogger &logger,

Copy link
Copy Markdown
Member

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

Copy link
Copy Markdown
Contributor

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

Comment thread lib/sensors/gyroscope.cpp Outdated
{
}

const std::optional<std::int16_t> Gyroscope::read(core::Axis axis)

Copy link
Copy Markdown
Member

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?

Comment thread 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);

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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?

@ishmis

ishmis commented Feb 16, 2023

Copy link
Copy Markdown
Contributor

@HTTYDKing remember to merge master for merge conflicts

Comment thread lib/core/types.hpp
const TimePoint measured_at;
};

struct GyroscopeData {

Copy link
Copy Markdown
Member

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?

Copy link
Copy Markdown
Member

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?

Comment thread lib/debug/repl.cpp
"Missing required field 'sensors.gyroscope.bus' in configuration file");
return std::nullopt;
}
const auto device_address = gyroscope["device_address"].GetUint();

Copy link
Copy Markdown
Member

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?

Comment thread lib/debug/repl.cpp
auto Old_time = std::chrono::system_clock::now();
std::optional<core::GyroscopeData> value;

while (true) {

Copy link
Copy Markdown
Member

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.

Comment thread lib/debug/repl.cpp
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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like previous_timestamp?

Comment thread lib/debug/repl.cpp

std::chrono::duration<double> elapsed_seconds = result.measured_at - Old_time;

auto time = (core::Float) elapsed_seconds.count();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static_cast<core::Float>(...)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, more informative naming?

Comment thread lib/debug/repl.cpp
Old_time = result.measured_at;
Old_data = result.x;

logger_.log(core::LogLevel::kInfo, "Gyroscope is : %f", Angle);

Copy link
Copy Markdown
Member

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?

Comment thread lib/debug/repl.hpp
#include <io/pwm.hpp>
#include <sensors/accelerometer.hpp>
#include <sensors/gyroscope.hpp>
#include <unistd.h>

Copy link
Copy Markdown
Member

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?

Comment thread lib/debug/repl.hpp
#include <sensors/accelerometer.hpp>
#include <sensors/gyroscope.hpp>
#include <unistd.h>
#include <chrono>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

Comment thread lib/sensors/gyroscope.cpp
= i2c->writeByteToRegister(device_address, kCtrl1, kConfigurationSetting1);
if (write_result_from_ctrl1 == core::Result::kFailure) {
logger.log(
core::LogLevel::kFatal,

Copy link
Copy Markdown
Member

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?

Comment thread lib/debug/repl.cpp
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;

Copy link
Copy Markdown
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants