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 8 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
23 changes: 14 additions & 9 deletions lib/sensors/adc_mux.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
#pragma once

#include <cstdint>

namespace hyped::sensors {

constexpr std::uint8_t kAdcMuxAddress = 0x1D;
constexpr std::uint8_t kAdcMuxChannel1 = 0x20;
constexpr std::uint8_t kAdcMuxChannel2 = 0x21;
constexpr std::uint8_t kAdcMuxChannel3 = 0x22;
constexpr std::uint8_t kAdcMuxChannel4 = 0x23;
constexpr std::uint8_t kAdcMuxChannel5 = 0x24;
constexpr std::uint8_t kAdcMuxChannel6 = 0x25;
constexpr std::uint8_t kAdcMuxChannel7 = 0x26;
constexpr std::uint8_t kAdcMuxChannel0 = 0x27;
constexpr std::uint8_t kAdcMuxAddress = 0x1D;

enum class AdcMuxChannel : std::uint8_t {
kAdcMuxChannel1 = 0x20,
kAdcMuxChannel2 = 0x21,
kAdcMuxChannel3 = 0x22,
kAdcMuxChannel4 = 0x23,
kAdcMuxChannel5 = 0x24,
kAdcMuxChannel6 = 0x25,
kAdcMuxChannel7 = 0x26,
kAdcMuxChannel0 = 0x27,
};

} // namespace hyped::sensors
32 changes: 32 additions & 0 deletions lib/sensors/inverter_current.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "adc_mux.hpp"

#include <inverter_current.hpp>

namespace hyped::sensors {

InverterCurrent::InverterCurrent(core::ILogger &logger,
std::shared_ptr<io::II2c> i2c,
const AdcMuxChannel 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, static_cast<std::uint8_t>(adc_mux_channel_));
if (!inverter_current) {
logger_.log(core::LogLevel::kFatal, "Failed to read inverter current");
return std::nullopt;
}
// Output is 0-5V, which corresponds to 50-130A
return static_cast<core::Float>(*inverter_current) * 16 + 50;
}

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

#include "adc_mux.hpp"

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

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

namespace hyped::sensors {

class InverterCurrent {
public:
InverterCurrent(core::ILogger &logger,
std::shared_ptr<io::II2c> i2c,
const AdcMuxChannel adc_mux_channel);

~InverterCurrent();

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

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

} // namespace hyped::sensors
Loading