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

Get battery levels #281

Merged
merged 2 commits into from
Jul 6, 2021
Merged
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
2 changes: 1 addition & 1 deletion drivers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ add_subdirectory(${DRIVERS_DIR}/LKCoreVideo)

# Sensors drivers
add_subdirectory(${DRIVERS_DIR}/CoreHTS)
add_subdirectory(${DRIVERS_DIR}/LKCoreBattery)
add_subdirectory(${DRIVERS_DIR}/CoreBattery)
add_subdirectory(${DRIVERS_DIR}/LKCoreMicrophone)
add_subdirectory(${DRIVERS_DIR}/LKCoreLightSensor)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
# Copyright 2021 APF France handicap
# SPDX-License-Identifier: Apache-2.0

add_library(LKCoreBattery STATIC)
add_library(CoreBattery STATIC)

target_include_directories(LKCoreBattery
target_include_directories(CoreBattery
PUBLIC
include
)

target_sources(LKCoreBattery
target_sources(CoreBattery
PRIVATE
source/LKCoreBattery.cpp
source/CoreBattery.cpp
)

target_link_libraries(LKCoreBattery mbed-os)
target_link_libraries(CoreBattery mbed-os)

if (${CMAKE_PROJECT_NAME} STREQUAL "LekaOSUnitTests")

leka_unit_tests_sources(
tests/LKCoreBattery_test.cpp
tests/CoreBattery_test.cpp
)

endif()
44 changes: 44 additions & 0 deletions drivers/CoreBattery/include/CoreBattery.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Leka - LekaOS
// Copyright 2021 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#ifndef _LEKA_OS_DRIVER_LK_CORE_BATTERY_H_
#define _LEKA_OS_DRIVER_LK_CORE_BATTERY_H_

#include "drivers/AnalogIn.h"

namespace leka {

class CoreBattery
{
public:
explicit CoreBattery(PinName pin) : _pin {mbed::AnalogIn(pin, voltage::reference)} {};

auto getVoltage() -> float;
Kabroc marked this conversation as resolved.
Show resolved Hide resolved

struct capacity {
static constexpr auto max = float {12.60};
static constexpr auto min = float {7.50};
};

struct resistor {
// TODO (@Benjamin) - find the resistor values, call Mikael
static constexpr auto r1 = float {47};
static constexpr auto r2 = float {169};
};

struct voltage {
// TODO (@Benjamin) - should be float {resistor::r1 / (resistor::r1 + resistor::r2)};
static constexpr auto divider = float {0.129};
static constexpr auto reference = float {3.33};
};

private:
auto readRawVoltage() -> float;

mbed::AnalogIn _pin;
};

} // namespace leka

#endif //_LEKA_OS_DRIVER_LK_CORE_BATTERY_H_
21 changes: 21 additions & 0 deletions drivers/CoreBattery/source/CoreBattery.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Leka - LekaOS
// Copyright 2021 APF France handicap
// SPDX-License-Identifier: Apache-2.0

#include "CoreBattery.h"

namespace leka {

auto CoreBattery::getVoltage() -> float
{
auto raw = readRawVoltage();
auto voltage = raw / voltage::divider;
return voltage;
}

auto CoreBattery::readRawVoltage() -> float
{
return _pin.read_voltage();
}

} // namespace leka
47 changes: 47 additions & 0 deletions drivers/CoreBattery/tests/CoreBattery_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Leka - LekaOS
// Copyright 2021 APF rance handicap
// SPDX-License-Identifier: Apache-2.0

#include "CoreBattery.h"

#include "gtest/gtest.h"
#include "stubs/mbed/AnalogIn.h"

using namespace leka;

CoreBattery corebattery(PinName::BATTERY_VOLTAGE);

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

TEST(CoreBatteryTest, getVoltage)
{
auto AnalogIn_read_voltage_value = float {1.2};
spy_AnalogIn_setVoltageValue(AnalogIn_read_voltage_value);

auto expected = AnalogIn_read_voltage_value / CoreBattery::voltage::divider;

ASSERT_FLOAT_EQ(expected, corebattery.getVoltage());
}

TEST(CoreBatteryTest, getVoltageLowest)
{
auto AnalogIn_read_voltage_value = float {0};
spy_AnalogIn_setVoltageValue(AnalogIn_read_voltage_value);

auto expected = AnalogIn_read_voltage_value / CoreBattery::voltage::divider;

ASSERT_FLOAT_EQ(expected, corebattery.getVoltage());
}

TEST(CoreBatteryTest, getVoltageHighest)
{
auto AnalogIn_read_voltage_value = float {1.65};
spy_AnalogIn_setVoltageValue(AnalogIn_read_voltage_value);

auto expected = AnalogIn_read_voltage_value / CoreBattery::voltage::divider;

ASSERT_FLOAT_EQ(expected, corebattery.getVoltage());
}
25 changes: 0 additions & 25 deletions drivers/LKCoreBattery/include/LKCoreBattery.h

This file was deleted.

14 changes: 0 additions & 14 deletions drivers/LKCoreBattery/source/LKCoreBattery.cpp

This file was deleted.

44 changes: 0 additions & 44 deletions drivers/LKCoreBattery/tests/LKCoreBattery_test.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion spikes/lk_sensors_battery/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ target_sources(spike_lk_sensors_battery

target_link_libraries(spike_lk_sensors_battery
FileManager
LKCoreBattery
CoreBattery
CoreMotor
)

Expand Down
6 changes: 3 additions & 3 deletions spikes/lk_sensors_battery/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#include "rtos/ThisThread.h"
#include "rtos/Thread.h"

#include "CoreBattery.h"
#include "CoreMotor.h"
#include "FATFileSystem.h"
#include "HelloWorld.h"
#include "LKCoreBattery.h"
#include "LogKit.h"
#include "SDBlockDevice.h"
#include "Utils.h"
Expand All @@ -38,12 +38,12 @@ auto main() -> int
HelloWorld hello;
hello.start();

auto battery = LKCoreBattery {PinName::BATTERY_VOLTAGE};
auto battery = CoreBattery {PinName::BATTERY_VOLTAGE};

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.getVoltage(); };

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

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ leka_register_unit_tests_for_driver(LKCoreSTM32Hal)
leka_register_unit_tests_for_driver(LKCoreVideo)
leka_register_unit_tests_for_driver(LKCoreLightSensor)
leka_register_unit_tests_for_driver(LKCoreMicrophone)
leka_register_unit_tests_for_driver(LKCoreBattery)
leka_register_unit_tests_for_driver(CoreBattery)

leka_register_unit_tests_for_driver(CoreHTS)
leka_register_unit_tests_for_driver(CoreI2C)
Expand Down