Skip to content

Commit

Permalink
LVSS current sensing
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoLHendrix committed Mar 13, 2024
1 parent 9e3f84e commit 1b17784
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 20 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,12 @@ project(${BOARD_LIB_NAME}
LANGUAGES CXX C
)

add_library(${PROJECT_NAME} STATIC
include/INA138.hpp
src/INA138.cpp)
add_library(${PROJECT_NAME} STATIC)

# Add sources
target_sources(${PROJECT_NAME} PRIVATE
src/LVSS.cpp
src/INA138.cpp
)

###############################################################################
Expand Down
Binary file not shown.
13 changes: 4 additions & 9 deletions include/INA138.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
#ifndef LVSS_INA138_HPP
#define LVSS_INA138_HPP

#include "EVT/dev/LCD.hpp"
#include "EVT/io/ADC.hpp"
#include "EVT/io/CANopen.hpp"
#include "EVT/io/GPIO.hpp"
#include "EVT/io/SPI.hpp"
#include "EVT/utils/log.hpp"
#include "LVSS.hpp"
#include <EVT/io/ADC.hpp>
#include <EVT/utils/log.hpp>

namespace IO = EVT::core::IO;
namespace DEV = EVT::core::DEV;
Expand Down Expand Up @@ -39,9 +34,9 @@ class INA138 {
///Read INA138 datasheet page 3
//Resistor variables
static constexpr uint32_t fixedPoint = 1000; //Use fixpoint variable to multiply everything by 1k
static constexpr uint32_t rShunt = 0.05 * fixedPoint;//0.5 ohm resistor
static constexpr uint32_t rShunt = 0.05 * fixedPoint;//0.05 ohm resistor
static constexpr uint32_t r1 = 5000 * fixedPoint; //5k ohm
static constexpr uint32_t r3 = 50000 * fixedPoint; //50k ohm
static constexpr uint32_t r3 = 50000; //50k ohm
};

}// namespace LVSS
Expand Down
12 changes: 4 additions & 8 deletions src/INA138.cpp
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
#include "INA138.hpp"
#include <INA138.hpp>

namespace LVSS {

INA138::INA138(IO::ADC& adc0) : ADC(adc0) {
}

uint32_t INA138::readCurrent(IO::ADC& adc0) {
//Gets voltage from adc
uint32_t voltage = static_cast<uint32_t>(adc0.read() * 1000);
voltage *= fixedPoint;
//Gets adcCounts from adc
uint32_t adcCounts = static_cast<uint32_t>(adc0.read() * 1000);

//Current = (Vout * 5k ohms) / (Rshunt * R3)
// Current = ((adcCounts * 3.3) * 5k ohms * 100) / ((Rshunt * 100) * R3 * 4096)
uint32_t current = ((adcCounts * r1 * 330) / (rShunt * r3 * 4096));

//divide the current by 1k fixed point get the actual number
current /= fixedPoint;

return current;
}

Expand Down

0 comments on commit 1b17784

Please sign in to comment.