-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: master
Are you sure you want to change the base?
Changes from all commits
f2566c2
4ef7138
66077c1
c0a06a7
9e768fc
18e1193
05e1ed6
fe7c532
6ac1399
fe35bc9
9ca47b6
00bf6a8
a96b23a
705fc9b
261e6a0
a8bcff9
507a733
eaf8eff
4f8e82f
626d9cb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "inverter_current_commands.hpp" | ||
|
||
#include <cstdint> | ||
|
||
namespace hyped::debug { | ||
|
||
core::Result InverterCurrentCommands::addCommands(core::ILogger &logger, | ||
std::shared_ptr<Repl> repl, | ||
toml::v3::node_view<toml::v3::node> config) | ||
{ | ||
// Get the I2C bus number from the config file | ||
const auto optional_bus = config["bus"].value<std::uint8_t>(); | ||
if (!optional_bus) { | ||
logger.log(core::LogLevel::kFatal, "No I2C bus specified"); | ||
return core::Result::kFailure; | ||
}; | ||
const auto bus = *optional_bus; | ||
// Get the ADC mux channel from the config file | ||
const auto optional_adc_mux_channel = config["adc_mux_channel"].value<std::uint8_t>(); | ||
if (!optional_adc_mux_channel) { | ||
logger.log(core::LogLevel::kFatal, "No ADC mux channel specified"); | ||
return core::Result::kFailure; | ||
}; | ||
const auto adc_mux_channel = *optional_adc_mux_channel; | ||
// Get the I2C instance | ||
const auto optional_i2c = repl->getI2c(bus); | ||
if (!optional_i2c) { | ||
logger.log(core::LogLevel::kFatal, "Error getting I2C bus"); | ||
return core::Result::kFailure; | ||
}; | ||
const auto i2c = std::move(*optional_i2c); | ||
|
||
// Create the sensor instance | ||
auto inverter_current_sensor = std::make_shared<sensors::InverterCurrent>( | ||
logger, i2c, static_cast<sensors::AdcMuxChannel>(adc_mux_channel)); | ||
|
||
// Create the read command | ||
const auto read_command_name = "current read"; | ||
const auto read_command_description = "Read inverter current"; | ||
const auto read_command_handler = [&logger, inverter_current_sensor]() { | ||
const auto current = inverter_current_sensor->readCurrent(); | ||
if (!current) { | ||
logger.log(core::LogLevel::kFatal, "Failed to read inverter current"); | ||
return; | ||
} | ||
logger.log(core::LogLevel::kDebug, "Inverter current: %d", *current); | ||
}; | ||
auto read_command | ||
= std::make_unique<Command>(read_command_name, read_command_description, read_command_handler); | ||
repl->addCommand(std::move(read_command)); | ||
return core::Result::kSuccess; | ||
} | ||
|
||
} // namespace hyped::debug |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#pragma once | ||
#include "command.hpp" | ||
|
||
#include <memory> | ||
|
||
#include <core/logger.hpp> | ||
#include <core/types.hpp> | ||
#include <debug/repl.hpp> | ||
#include <sensors/inverter_current.hpp> | ||
|
||
namespace hyped::debug { | ||
|
||
class InverterCurrentCommands { | ||
public: | ||
static core::Result addCommands(core::ILogger &logger, | ||
std::shared_ptr<Repl> repl, | ||
toml::v3::node_view<toml::v3::node> config); | ||
}; | ||
|
||
} // namespace hyped::debug |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#include "adc_mux.hpp" | ||
|
||
#include <cstdint> | ||
|
||
#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; | ||
} | ||
|
||
// The sensor maps -75A to 75A into -3V to 3V | ||
// Since the ADC isn't differential, we need to differentiate the reference signal and the output | ||
// signal | ||
static const int MAX_CURRENT = 75; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
static const int MIN_CURRENT = -75; | ||
static const core::Float MAX_VOLTAGE = 3.3; | ||
static const core::Float MIN_VOLTAGE = -3.3; | ||
|
||
// These can be adjusted | ||
const int reference_voltage = 1.65; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
most type understanding webdev smdh (i'm totally not one now) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💀
Comment on lines
+34
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all of these should be in the header There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point |
||
const core::Float virtual_ground = MAX_VOLTAGE / 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is the difference between reference voltage and virtual ground? |
||
|
||
// Calculate the current | ||
const int inverter_current_voltage | ||
= *inverter_current * (MAX_VOLTAGE - virtual_ground) + virtual_ground; | ||
const core::Float current | ||
= inverter_current_voltage * ((MAX_CURRENT - MIN_CURRENT) / (MAX_VOLTAGE - MIN_VOLTAGE)) | ||
+ MIN_CURRENT; | ||
const core::Float reference_voltage_biased = reference_voltage + virtual_ground; | ||
const core::Float current_difference = current - reference_voltage_biased; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this do? I doubt it's valid to subtract a voltage from an amperage |
||
return current_difference; | ||
} | ||
|
||
} // namespace hyped::sensors |
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 |
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.
There should also be a usage entry here