Line data Source code
1 : /*! 2 : * @file MileageCalculator.cpp 3 : * @brief Implementation of the MileageCalculator class. 4 : * @version 0.1 5 : * @date 2025-01-31 6 : * @details This file contains the implementation of the MileageCalculator 7 : * class, which is used to calculate the distance traveled by the vehicle. 8 : * @note This class is used to calculate the distance traveled by the vehicle 9 : * based on the speed values received. 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 interval timer is properly started. 15 : * @see MileageCalculator.hpp 16 : * @copyright Copyright (c) 2025 17 : */ 18 : 19 : #include "MileageCalculator.hpp" 20 : #include <QDebug> 21 : 22 : /*! 23 : * @brief Construct a new MileageCalculator object. 24 : * @details This constructor initializes the MileageCalculator object with a 25 : * started interval timer. 26 : */ 27 5 : MileageCalculator::MileageCalculator() { m_intervalTimer.start(); } 28 : 29 : /*! 30 : * @brief Add a speed value to the calculator. 31 : * @param speed The speed value to add. 32 : * @details This function adds a speed value to the calculator with the current 33 : * interval time. 34 : */ 35 10 : void MileageCalculator::addSpeed(float speed) { 36 10 : if (m_intervalTimer.isValid()) { 37 10 : const qint64 interval = m_intervalTimer.restart(); 38 10 : QPair<float, qint64> newValue; 39 10 : newValue.first = speed; 40 10 : newValue.second = interval; 41 10 : m_speedValues.append(newValue); 42 : 43 : } else { 44 0 : qDebug() << "MileageCalculator Interval Timer was not valid"; 45 : } 46 10 : } 47 : 48 : /*! 49 : * @brief Calculate the distance traveled by the vehicle. 50 : * @return double The distance traveled by the vehicle. 51 : * @details This function calculates the distance traveled by the vehicle based 52 : * on the speed values received. 53 : */ 54 5 : double MileageCalculator::calculateDistance() { 55 : // qDebug() << "Calculate distances " << m_speedValues.size(); 56 5 : double totalDistance = 0.0; 57 : 58 15 : for (QPair<float, qint64> value : m_speedValues) { 59 10 : double speedInMetersPerSecond = value.first * (1000.0 / 3600.0); 60 10 : double intervalInSeconds = value.second / 1000.0; 61 : // qDebug() << "Interval: " << value.second << " in seconds: " << 62 : // intervalInSeconds; 63 10 : totalDistance += speedInMetersPerSecond * intervalInSeconds; 64 : } 65 : 66 5 : m_speedValues.clear(); 67 : 68 : // qDebug() << "Total distance: " << totalDistance; 69 : 70 5 : return totalDistance; 71 : }