Skip to content
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

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open

SNS-Gyroscope #82

wants to merge 18 commits into from

Conversation

HTTYDKing
Copy link
Contributor

Separate branch for the gyroscope sensor.

@HTTYDKing HTTYDKing requested a review from ishmis as a code owner February 2, 2023 18:54
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.hpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.hpp Outdated Show resolved Hide resolved
Copy link
Contributor

@ishmis ishmis left a 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

lib/sensors/gyroscope.hpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.hpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
@HTTYDKing HTTYDKing requested a review from kshxtij as a code owner February 4, 2023 13:49
lib/core/types.hpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.hpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.hpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
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
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
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

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

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

lib/sensors/gyroscope.hpp Show resolved Hide resolved
lib/debug/repl.cpp Outdated Show resolved Hide resolved
lib/sensors/gyroscope.cpp Outdated Show resolved Hide resolved
= kDefaultGyroscopeAddress);
~Gyroscope();

const std::optional<std::int16_t> read(core::Axis axis);
Copy link
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
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

logger_.log(core::LogLevel::kFatal, "Gave an invalid axis");
return std::nullopt;
}
}
Copy link
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.

lib/sensors/gyroscope.cpp Show resolved Hide resolved
std::shared_ptr<io::II2c> i2c,
const std::uint8_t channel,
const std::uint8_t device_address
= kDefaultGyroscopeAddress);
Copy link
Contributor

@TomLonergan03 TomLonergan03 Feb 10, 2023

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

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
Member

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,
Copy link
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
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

{
}

const std::optional<std::int16_t> Gyroscope::read(core::Axis axis)
Copy link
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?

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
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
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
Copy link
Contributor

ishmis commented Feb 16, 2023

@HTTYDKing remember to merge master for merge conflicts

@@ -62,6 +62,18 @@ struct RawAccelerationData {
const TimePoint measured_at;
};

struct GyroscopeData {
Copy link
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
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?

"Missing required field 'sensors.gyroscope.bus' in configuration file");
return std::nullopt;
}
const auto device_address = gyroscope["device_address"].GetUint();
Copy link
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?

auto Old_time = std::chrono::system_clock::now();
std::optional<core::GyroscopeData> value;

while (true) {
Copy link
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.

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


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

auto time = (core::Float) elapsed_seconds.count();
Copy link
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
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?

Old_time = result.measured_at;
Old_data = result.x;

logger_.log(core::LogLevel::kInfo, "Gyroscope is : %f", Angle);
Copy link
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?

@@ -14,6 +14,9 @@
#include <io/hardware_uart.hpp>
#include <io/pwm.hpp>
#include <sensors/accelerometer.hpp>
#include <sensors/gyroscope.hpp>
#include <unistd.h>
Copy link
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?

@@ -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>
Copy link
Member

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,
Copy link
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?

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