LCOV - code coverage report
Current view: top level - system - BatteryController.cpp (source / functions) Hit Total Coverage
Test: filtered.info Lines: 26 28 92.9 %
Date: 2025-07-25 11:48:17 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /*!
       2             :  * @file BatteryController.cpp
       3             :  * @brief Implementation of the BatteryController class.
       4             :  * @version 0.1
       5             :  * @date 2025-01-31
       6             :  * @details This file contains the implementation of the BatteryController
       7             :  * class, which is used to control the battery of the vehicle.
       8             :  * @note This class is used to control the battery of the vehicle, including
       9             :  * monitoring the battery level and charging status.
      10             :  * @author Félix LE BIHAN (@Fle-bihh)
      11             :  * @author Tiago Pereira (@t-pereira06)
      12             :  * @author Ricardo Melo (@reomelo)
      13             :  * @author Michel Batista (@MicchelFAB)
      14             :  * @warning Ensure that the INA219 is properly connected and configured.
      15             :  * @see BatteryController.hpp for the class definition.
      16             :  * @copyright Copyright (c) 2025
      17             :  */
      18             : 
      19             : #include "BatteryController.hpp"
      20             : #include "I2CController.hpp"
      21             : 
      22             : /*! @def REG_CALIBRATION The register address for the calibration register. */
      23             : #define REG_CALIBRATION 0x05
      24             : /*! @def REG_BUSVOLTAGE The register address for the bus voltage register. */
      25             : #define REG_BUSVOLTAGE 0x02
      26             : /*! @def REG_SHUNTVOLTAGE The register address for the shunt voltage register.
      27             :  */
      28             : #define REG_SHUNTVOLTAGE 0x01
      29             : 
      30             :         /*!
      31             :          * @brief Construct a new BatteryController object.
      32             :          * @param i2cController The I2C controller to use for communication with the
      33             :          * INA219. If `nullptr`, a default I2C controller is created.
      34             :          * @details This constructor initializes the BatteryController object with
      35             :          * the specified I2C controller and address. If `i2cController` is `nullptr`,
      36             :          * a default I2C controller is created.
      37             :          */
      38           3 : BatteryController::BatteryController(II2CController *i2cController)
      39           3 :         : m_i2cController(i2cController ? i2cController : new I2CController("/dev/i2c-1", 0x41))
      40           3 :         , m_ownI2CController(i2cController == nullptr)
      41             : {
      42           3 :         setCalibration32V2A();
      43           3 : }
      44             : 
      45             :         /*!
      46             :          * @brief Destroy the BatteryController object
      47             :          * @details This destructor releases any resources allocated by the
      48             :          * BatteryController object. If the object created its own I2C controller,
      49             :          * it is deleted.
      50             :          */
      51           6 : BatteryController::~BatteryController()
      52             : {
      53           3 :         if (m_ownI2CController) {
      54           0 :                 delete m_i2cController;
      55             :         }
      56           6 : }
      57             : 
      58             : /*!
      59             :  * @brief Set the calibration for 32V and 2A.
      60             :  * @details This function writes a predefined calibration value to the 
      61             :  * calibration register of the INA219 sensor to configure it for a 
      62             :  * voltage range of 32V and a maximum current of 2A.
      63             :  */
      64             : 
      65           3 : void BatteryController::setCalibration32V2A()
      66             : {
      67           3 :         m_i2cController->writeRegister(REG_CALIBRATION, 4096);
      68           3 : }
      69             : 
      70             : /*!
      71             :  * @brief Get the bus voltage in volts.
      72             :  * @return float The bus voltage in volts.
      73             :  * @details This function reads the raw bus voltage register value from the
      74             :  * INA219 sensor, shifts it to align with the measurement resolution, and
      75             :  * converts it to volts.
      76             :  */
      77             : 
      78           1 : float BatteryController::getBusVoltage_V()
      79             : {
      80           1 :         uint16_t raw = m_i2cController->readRegister(REG_BUSVOLTAGE);
      81           1 :         return ((raw >> 3) * 0.004); // Convert to volts
      82             : }
      83             : 
      84             :         /*!
      85             :          * @brief Get the shunt voltage in volts.
      86             :          * @return float The shunt voltage in volts.
      87             :          * @details This function reads the raw shunt voltage register value from the
      88             :          * INA219 sensor, shifts it to align with the measurement resolution, and
      89             :          * converts it to volts.
      90             :          */
      91           1 : float BatteryController::getShuntVoltage_V()
      92             : {
      93           1 :         int16_t raw = static_cast<int16_t>(m_i2cController->readRegister(REG_SHUNTVOLTAGE));
      94           1 :         return raw * 0.01; // Convert to volts
      95             : }
      96             : 
      97             : /*!
      98             :  * @brief Get the battery percentage.
      99             :  * @return float The battery percentage.
     100             :  * @details This function calculates the battery percentage based on the bus and
     101             :  * shunt voltages.
     102             :  */
     103           1 : float BatteryController::getBatteryPercentage() {
     104           1 :   float busVoltage = getBusVoltage_V();
     105           1 :   float shuntVoltage = getShuntVoltage_V();
     106           1 :   float loadVoltage = busVoltage + shuntVoltage;
     107             : 
     108             :   // Calculate percentage
     109           1 :   float percentage = (loadVoltage - 6.0F) / 2.4f * 100.0F;
     110           1 :   if (percentage > 100.0F)
     111           0 :         percentage = 100.0F;
     112           1 :   if (percentage < 0.0F)
     113           1 :         percentage = 0.0F;
     114           1 :   return percentage;
     115             : }

Generated by: LCOV version 1.14