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 - Inverter Current #97

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/sensors/adc_mux.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <cstdint>

namespace hyped::sensors {

constexpr std::uint8_t kAdcMuxAddress = 0x1D;
Expand Down
39 changes: 39 additions & 0 deletions lib/sensors/inverter_current.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "adc_mux.hpp"

#include <inverter_current.hpp>

namespace hyped::sensors {

std::optional<InverterCurrent> InverterCurrent::create(core::ILogger &logger,
std::shared_ptr<io::II2c> i2c,
const std::uint8_t adc_mux_channel)
{
return InverterCurrent(logger, i2c, adc_mux_channel);
}

InverterCurrent::InverterCurrent(core::ILogger &logger,
std::shared_ptr<io::II2c> i2c,
const std::uint8_t adc_mux_channel)
: logger_(logger),
i2c_(i2c),
adc_mux_channel_(adc_mux_channel)
{
}

InverterCurrent::~InverterCurrent()
{
}

std::optional<core::Float> InverterCurrent::readCurrent()
{
const auto inverter_current = i2c_->readByte(kAdcMuxAddress, adc_mux_channel_);
if (!inverter_current) {
logger_.log(core::LogLevel::kFatal, "Failed to read inverter current");
return std::nullopt;
}
logger_.log(core::LogLevel::kDebug, "Successfully read inverter current");
// Output is 0-5V, which corresponds to 50-130A
return static_cast<core::Float>(*inverter_current) * 16 + 50;
}

} // namespace hyped::sensors
33 changes: 33 additions & 0 deletions lib/sensors/inverter_current.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include <cstdint>
#include <memory>
#include <optional>

#include <core/logger.hpp>
#include <core/types.hpp>
#include <io/i2c.hpp>

namespace hyped::sensors {

class InverterCurrent {
public:
static std::optional<InverterCurrent> create(core::ILogger &logger,
std::shared_ptr<io::II2c> i2c,
const std::uint8_t adc_mux_channel);
~InverterCurrent();

std::optional<core::Float> readCurrent();

private:
InverterCurrent(core::ILogger &logger,
std::shared_ptr<io::II2c> i2c,
const std::uint8_t adc_mux_channel);

private:
core::ILogger &logger_;
std::shared_ptr<io::II2c> i2c_;
const std::uint8_t adc_mux_channel_;
};

} // namespace hyped::sensors
Loading