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 - IMD #110

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ project(${project} CXX)
enable_testing()

find_package (Eigen3 REQUIRED NO_MODULE)
find_package(Boost REQUIRED)
# find_package(Boost REQUIRED)
include(FetchContent)
FetchContent_Declare(
rapidjson
Expand Down
5 changes: 5 additions & 0 deletions config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
"enabled": false,
"bus": 2,
"device_address": 56
},
"low-power-current": {
"enabled": false,
"bus": 2,
"device_address": 64
}
}
}
Expand Down
61 changes: 61 additions & 0 deletions lib/debug/repl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,33 @@ std::optional<std::unique_ptr<Repl>> Repl::fromFile(const std::string &path)
const auto bus = temperature["bus"].GetUint();
repl->addTemperatureCommands(bus, device_address);
}
if (!sensors.HasMember("low-power-current")) {
logger_.log(core::LogLevel::kFatal,
"Missing required field 'sensors.low_power_current' in configuration file");
return std::nullopt;
}
const auto low_power_current = sensors["low-power-current"].GetObject();
if (!low_power_current.HasMember("enabled")) {
logger_.log(core::LogLevel::kFatal,
"Missing required field 'sensors.low_power_current.enabled' in configuration file");
return std::nullopt;
}
if (low_power_current["enabled"].GetBool()) {
if (!low_power_current.HasMember("bus")) {
logger_.log(core::LogLevel::kFatal,
"Missing required field 'sensors.low_power_current.bus' in configuration file");
return std::nullopt;
}
if (!low_power_current.HasMember("device_address")) {
logger_.log(
core::LogLevel::kFatal,
"Missing required field 'sensors.low_power_current.device_address' in configuration file");
return std::nullopt;
}
const auto device_address = low_power_current["device_address"].GetUint();
const auto bus = low_power_current["bus"].GetUint();
repl->addLowPowerCurrentCommands(bus, device_address);
}
return repl;
}

Expand Down Expand Up @@ -609,4 +636,38 @@ void Repl::addTemperatureCommands(const std::uint8_t bus, const std::uint8_t dev
addCommand(temperature_read_command);
}

void Repl::addLowPowerCurrentCommands(const std::uint8_t bus, const std::uint8_t device_address)
{
const auto optional_i2c = io::HardwareI2c::create(logger_, bus);
if (!optional_i2c) {
logger_.log(core::LogLevel::kFatal, "Failed to create I2C instance on bus %d", bus);
return;
}
const auto i2c = std::move(*optional_i2c);
const auto optional_low_power_current
sa-fx marked this conversation as resolved.
Show resolved Hide resolved
= sensors::LowPowerCurrent::create(logger_, i2c, device_address);
const auto low_power_current
= std::make_shared<sensors::LowPowerCurrent>(*optional_low_power_current);
Command low_power_current_read_command;
std::stringstream identifier;
identifier << "low power current 0x" << std::hex << static_cast<int>(device_address) << " read";
low_power_current_read_command.name = identifier.str();
std::stringstream description;
description << "Read low power current sensor 0x" << std::hex << static_cast<int>(device_address)
<< " on "
<< "I2C bus " << static_cast<int>(bus);
low_power_current_read_command.description = description.str();
low_power_current_read_command.handler = [this, low_power_current, bus]() {
const auto value = low_power_current->readCurrent();
if (!value) {
logger_.log(core::LogLevel::kFatal, "Failed to read the low power current from bus %d", bus);
} else {
const core::Float low_power_current_result = *value;
logger_.log(
core::LogLevel::kInfo, "Acceleration in mg: \n x %.4f A\n", low_power_current_result);
}
};
addCommand(low_power_current_read_command);
}

} // namespace hyped::debug
2 changes: 2 additions & 0 deletions lib/debug/repl.hpp
sa-fx marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <io/hardware_uart.hpp>
#include <io/pwm.hpp>
#include <sensors/accelerometer.hpp>
#include <sensors/low_power_current.hpp>
#include <sensors/temperature.hpp>

namespace hyped::debug {
Expand Down Expand Up @@ -45,6 +46,7 @@ class Repl {
void addSpiCommands(const std::uint8_t bus);
void addAccelerometerCommands(const std::uint8_t bus, const std::uint8_t device_address);
void addTemperatureCommands(const std::uint8_t bus, const std::uint8_t device_address);
void addLowPowerCurrentCommands(const std::uint8_t bus, const std::uint8_t device_address);
void addUartCommands(const std::uint8_t bus);

core::ILogger &logger_;
Expand Down
5 changes: 5 additions & 0 deletions lib/sensors/imd_processor.cpp
sa-fx marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <imd_processor.hpp>

namespace hyped::sensors {

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

#include <core/logger.hpp>
#include <io/can.hpp>

// Current plan: Make a processor based on ICanProcessor

namespace hyped::sensors {
class ImdProcessor : public io::ICanProcessor {
public:
ImdProcessor(core::ILogger &logger);
ImdProcessor();

void processMessage(const io::CanFrame &frame);

private:
const core::ILogger &logger_;
};
} // namespace hyped::sensors
6 changes: 3 additions & 3 deletions lib/sensors/low_power_current.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ constexpr std::uint8_t kLowPowerCurrentAddress4 = 0x45;
// TODOLater: Test this code with hardware
class LowPowerCurrent {
public:
std::optional<LowPowerCurrent> create(core::ILogger &logger,
std::shared_ptr<io::II2c> i2c,
const std::uint8_t device_address);
static std::optional<LowPowerCurrent> create(core::ILogger &logger,
std::shared_ptr<io::II2c> i2c,
const std::uint8_t device_address);
~LowPowerCurrent();

std::optional<core::Float> readCurrent();
Expand Down
2 changes: 1 addition & 1 deletion test/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ target_link_libraries(${target}
hyped_core
hyped_utils
Eigen3::Eigen
Boost::boost
# Boost::boost
sa-fx marked this conversation as resolved.
Show resolved Hide resolved
)
gtest_discover_tests(${target})