Skip to content

Commit

Permalink
✨ (drivers): Add getVoltage function, getADCVoltage now return a aver…
Browse files Browse the repository at this point in the history
…aged voltage value between 0 and 1, add min and max voltage for current battery
  • Loading branch information
Kabroc committed Jul 2, 2021
1 parent e392e10 commit 3bf893c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
28 changes: 27 additions & 1 deletion drivers/CoreBattery/include/CoreBattery.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,40 @@

namespace leka {

namespace battery {

namespace component {

constexpr int resistor1 = 47;
constexpr int resistor2 = 169;

} // namespace component

namespace coefficient {

constexpr float adc = 3.33;
// constexpr float voltage_divider = resistor1 / (resistor1+resistor2); //Theorical values of resistor doesn't
// fit actuals
constexpr float voltage_divider = 0.218;

} // namespace coefficient

} // namespace battery

class CoreBattery
{
public:
const float min_capacity = 13.20;
const float max_capacity = 7.64;

explicit CoreBattery(PinName pin) : _pin {mbed::AnalogIn(pin)} {};

float readVoltage(void);
auto getVoltage() -> float;
auto getADCVoltage() -> float;

private:
auto readVoltage() -> float;

mbed::AnalogIn _pin;
};

Expand Down
20 changes: 19 additions & 1 deletion drivers/CoreBattery/source/CoreBattery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,25 @@

namespace leka {

float CoreBattery::readVoltage(void)
auto CoreBattery::getVoltage() -> float
{
auto digitalVoltage = getADCVoltage();
return (digitalVoltage * battery::coefficient::adc / battery::coefficient::voltage_divider);
}

auto CoreBattery::getADCVoltage() -> float
{
auto average = 0.f;
auto sample_size = 20;

for (auto i = 0; i < sample_size; ++i) {
average += readVoltage();
}

return average / static_cast<float>(sample_size);
}

auto CoreBattery::readVoltage() -> float
{
return _pin.read();
}
Expand Down
27 changes: 9 additions & 18 deletions drivers/CoreBattery/tests/CoreBattery_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,25 @@ using namespace leka;

CoreBattery corebattery(PinName::BATTERY_VOLTAGE);

float test_set_Voltage(float value)
{
spy_AnalogIn_setValue(value);
return value;
}

TEST(CoreBatteryTest, initialization)
{
ASSERT_NE(&corebattery, nullptr);
}

TEST(CoreBatteryTest, readMinVoltage)
TEST(CoreBatteryTest, readLowDigitalVoltage)
{
auto expected = test_set_Voltage(0.0f);
auto expected = 0.0f;
spy_AnalogIn_setValue(expected);

ASSERT_EQ(expected, corebattery.readVoltage());
ASSERT_EQ(expected, corebattery.getADCVoltage());
}

TEST(CoreBatteryTest, readMiddleVoltage)
TEST(CoreBatteryTest, readHighVoltage)
{
auto expected = test_set_Voltage(0.25f);
auto expected_digital_voltage = 1.0f;
auto expected_voltage = 15.275229;

ASSERT_EQ(expected, corebattery.readVoltage());
}

TEST(CoreBatteryTest, readMaxVoltage)
{
auto expected = test_set_Voltage(1.0f);
spy_AnalogIn_setValue(expected_digital_voltage);

ASSERT_EQ(expected, corebattery.readVoltage());
ASSERT_EQ((int)(1000 * expected_voltage), (int)(1000 * corebattery.getVoltage()));
}
2 changes: 1 addition & 1 deletion spikes/lk_sensors_battery/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ auto main() -> int
auto battery_thread = rtos::Thread {};
auto battery_lambda = [&battery] {
auto now = [] { return static_cast<int>(rtos::Kernel::Clock::now().time_since_epoch().count()); };
auto voltage = [&] { return battery.readVoltage(); };
auto voltage = [&] { return battery.getADCVoltage(); };

auto buffer = std::array<char, 64> {};

Expand Down

0 comments on commit 3bf893c

Please sign in to comment.