From ac8e030089c557acbdbe0d10d53662ca8950b24e Mon Sep 17 00:00:00 2001 From: fastium Date: Tue, 5 Nov 2024 15:30:12 +0100 Subject: [PATCH] ADD speedometer implementation --- common/speedometer.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/common/speedometer.cpp b/common/speedometer.cpp index 7f1b4c1..19ba566 100644 --- a/common/speedometer.cpp +++ b/common/speedometer.cpp @@ -107,10 +107,13 @@ void Speedometer::computeSpeed() { // = 6.99m If you ride at 80 pedal turns / min, you run a distance of 6.99 * 80 / min // ~= 560 m / min = 33.6 km/h - // TODO + // TODO : done //Distance run with one pedal turn = tray size / rear gear size * circumference of the wheel - std::chrono::seconds pedal_rotation_time = std::chrono::duration_cast(this->_pedalRotationTime).count(); - this->_currentSpeed = static_cast(kTraySize) / this->_gearSize * this->kWheelCircumference * ; + float ms_in_hour = static_cast(3600 * 1000); + float pedal_rotation_per_hour = ms_in_hour / static_cast(_pedalRotationTime.count()); + float gear_ratio = static_cast(kTraySize) / static_cast(this->_gearSize); + float wheel_dist_km = static_cast(this->kWheelCircumference) / 1000.0; + this->_currentSpeed = gear_ratio * wheel_dist_km * pedal_rotation_per_hour; } void Speedometer::computeDistance() { @@ -122,8 +125,14 @@ void Speedometer::computeDistance() { // ~= 560 m / min = 33.6 km/h. We then multiply the speed by the time for getting the // distance traveled. - // TODO - + // TODO : done + Speedometer::computeSpeed(); + // compute distance + float last_time_in_hour = static_cast(std::chrono::duration_cast(this->_lastTime).count()); + float traveled_dist = this->_currentSpeed * last_time_in_hour; + this->_totalDistanceMutex.lock(); + this->_totalDistance += traveled_dist; + this->_totalDistanceMutex.unlock(); } } // namespace bike_computer \ No newline at end of file