From 1f849e3455ad8f1f6db64757acfd424e0b0b738b Mon Sep 17 00:00:00 2001 From: GarandPLG Date: Wed, 17 Jun 2026 05:49:37 +0200 Subject: [PATCH] Refactor Engine: remove begin() and dedupe helpers Removed the empty Engine::begin() method and its declaration. Moved getRPM, getSpeedKmh, rpmToPwm, and setMotor implementations to the top of Engine.cpp and deleted their duplicate definitions at the bottom. Fixed wheel motor PWM calculation by using the raw wheelRPM instead of wheelRPM * 10.0f. Deleted the call to engine.begin() in the Arduino setup. --- Engine.cpp | 46 ++++++++++++++++++++++----------------------- Engine.h | 2 -- Projekt_Arduino.ino | 1 - 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/Engine.cpp b/Engine.cpp index 914d604..631301c 100644 --- a/Engine.cpp +++ b/Engine.cpp @@ -7,12 +7,31 @@ Engine::Engine(uint8_t throttlePin, uint8_t brakePin) : _throttlePin(throttlePin), _clutchPin(clutchPin), _brakePin(brakePin) {} -void Engine::begin() {} - float Engine::readNormalized(uint8_t pin) const { return analogRead(pin) / 1023.0f; } +int Engine::getRPM() const { + return static_cast(_engineRPM + 0.5f); +} + +int Engine::getSpeedKmh() const { + return static_cast((_speedMs * 3.6f) + 0.5f); +} + +uint8_t Engine::rpmToPwm(float rpm) { + rpm = constrain(rpm, 0.0f, RPM_MAX); + return static_cast(roundf(rpm / RPM_MAX * 255.0f)); +} + +void Engine::setMotor(uint8_t inA, uint8_t inB, uint8_t ena, + float rpm, bool reverse) { + digitalWrite(inA, reverse ? LOW : HIGH); + digitalWrite(inB, reverse ? HIGH : LOW); + + analogWrite(ena, rpmToPwm(rpm)); +} + void Engine::update(float dt, uint8_t gearIdx) { const float throttle = readNormalized(_throttlePin); const float clutch = readNormalized(_clutchPin); @@ -41,26 +60,5 @@ void Engine::update(float dt, uint8_t gearIdx) { setMotor(IN_3, IN_4, ENA_2, targetRPM, false); bool reverseWheel = (gearIdx == -1); - setMotor(IN_1, IN_2, ENA_1, wheelRPM * 10.0f, reverseWheel); -} - -int Engine::getRPM() const { - return static_cast(_engineRPM + 0.5f); -} - -int Engine::getSpeedKmh() const { - return static_cast((_speedMs * 3.6f) + 0.5f); -} - -uint8_t Engine::rpmToPwm(float rpm) { - rpm = constrain(rpm, 0.0f, RPM_MAX); - return static_cast(roundf(rpm / RPM_MAX * 255.0f)); -} - -void Engine::setMotor(uint8_t inA, uint8_t inB, uint8_t ena, - float rpm, bool reverse) { - digitalWrite(inA, reverse ? LOW : HIGH); - digitalWrite(inB, reverse ? HIGH : LOW); - - analogWrite(ena, rpmToPwm(rpm)); + setMotor(IN_1, IN_2, ENA_1, wheelRPM, reverseWheel); } diff --git a/Engine.h b/Engine.h index 9e49cfa..637f2e0 100644 --- a/Engine.h +++ b/Engine.h @@ -28,8 +28,6 @@ public: uint8_t clutchPin, uint8_t brakePin); - void begin(); - void setCarMass(float mass) { _carMass = mass; } void update(float dt, uint8_t gearIdx); diff --git a/Projekt_Arduino.ino b/Projekt_Arduino.ino index fea30ac..846321c 100644 --- a/Projekt_Arduino.ino +++ b/Projekt_Arduino.ino @@ -46,7 +46,6 @@ void setup() { rfid.begin(); display.begin(LCD_COLS, LCD_ROWS); - engine.begin(); } void loop() {