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 Temperature #105

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions config/debug.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ address = 56
enabled = false
bus = "can1"

[sensors.inverter_temperature]
enabled = false
bus = 2
adc_mux_channel = 0

[motors]
enabled = false
bus = "can1"
Expand Down
54 changes: 54 additions & 0 deletions lib/debug/commands/inverter_temperature_commands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "inverter_temperature_commands.hpp"

#include <cstdint>

namespace hyped::debug {

core::Result InverterTemperatureCommands::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_temperature_sensor = std::make_shared<sensors::InverterTemperature>(
logger, i2c, static_cast<sensors::AdcMuxChannel>(adc_mux_channel));

// Create the read command
const auto read_command_name = "temperature read";
const auto read_command_description = "Read inverter temperature";
const auto read_command_handler = [&logger, inverter_temperature_sensor]() {
const auto temperature = inverter_temperature_sensor->readTemperature();
if (!temperature) {
logger.log(core::LogLevel::kFatal, "Failed to read inverter temperature");
return;
}
logger.log(core::LogLevel::kDebug, "Inverter temperature: %d", *temperature);
};
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
20 changes: 20 additions & 0 deletions lib/debug/commands/inverter_temperature_commands.hpp
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_temperature.hpp>

namespace hyped::debug {

class InverterTemperatureCommands {
public:
static core::Result addCommands(core::ILogger &logger,
std::shared_ptr<Repl> repl,
toml::v3::node_view<toml::v3::node> config);
};

} // namespace hyped::debug
9 changes: 9 additions & 0 deletions lib/debug/repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "commands/can_commands.hpp"
#include "commands/gpio_commands.hpp"
#include "commands/i2c_commands.hpp"
#include "commands/inverter_temperature_commands.hpp"
#include "commands/pwm_commands.hpp"
#include "commands/spi_commands.hpp"
#include "commands/uart_commands.hpp"
Expand Down Expand Up @@ -76,6 +77,14 @@ std::optional<std::shared_ptr<Repl>> Repl::create(core::ILogger &logger,
return std::nullopt;
}
}
if (config["sensors"]["inverter_temperature"]["enabled"].value_or(false)) {
const auto result = InverterTemperatureCommands::addCommands(
logger, repl, config["sensors"]["inverter_temperature"]);
if (result == core::Result::kFailure) {
logger.log(core::LogLevel::kFatal, "Error adding inverter temperature commands");
return std::nullopt;
}
}
const auto aliases = config["aliases"].as_table();
for (auto [alias, command] : *aliases) {
const std::string alias_alias = static_cast<std::string>(alias.str());
Expand Down
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
31 changes: 31 additions & 0 deletions lib/sensors/inverter_temperature.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "adc_mux.hpp"

#include <inverter_temperature.hpp>

namespace hyped::sensors {

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

InverterTemperature::~InverterTemperature()
{
}

std::optional<core::Float> InverterTemperature::readTemperature()
{
const auto inverter_temperature
= i2c_->readByte(kAdcMuxAddress, static_cast<std::uint8_t>(adc_mux_channel_));
if (!inverter_temperature) {
logger_.log(core::LogLevel::kFatal, "Failed to read inverter temperature");
return std::nullopt;
}
return 0; // something
}

} // namespace hyped::sensors
31 changes: 31 additions & 0 deletions lib/sensors/inverter_temperature.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 InverterTemperature {
public:
InverterTemperature(core::ILogger &logger,
std::shared_ptr<io::II2c> i2c,
const AdcMuxChannel adc_mux_channel);

~InverterTemperature();

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

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

} // namespace hyped::sensors
Loading