From 6c0a1fc15b9c986ece48408cfeb4015b323ff1af Mon Sep 17 00:00:00 2001 From: GarandPLG Date: Wed, 17 Jun 2026 00:13:16 +0200 Subject: [PATCH] Make car mass configurable at runtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the compile‑time CAR_MASS constant with a runtime‑settable _member _carMass. Add Engine::setCarMass, default it to 1200 kg, and use it in the acceleration calculation. Prompt the user for the car mass on startup via Serial and update the enable/disable messages. --- Engine.cpp | 2 +- Engine.h | 4 +++- Projekt_Arduino.ino | 31 +++++++++++++++++++++++++++++-- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Engine.cpp b/Engine.cpp index f7114ab..914d604 100644 --- a/Engine.cpp +++ b/Engine.cpp @@ -33,7 +33,7 @@ void Engine::update(float dt, uint8_t gearIdx) { const float dragForce = 0.5f * DRAG_COEFF * FRONTAL_AREA * (_speedMs * _speedMs); const int direction = (gearIdx == -1) ? -1 : 1; const float netForce = direction * engineForce - brakeForce - dragForce; - const float acceleration = netForce / CAR_MASS; + const float acceleration = netForce / _carMass; _speedMs += acceleration * dt; if (_speedMs < 0.0f) _speedMs = 0.0f; diff --git a/Engine.h b/Engine.h index 9c1c07a..9e49cfa 100644 --- a/Engine.h +++ b/Engine.h @@ -13,7 +13,6 @@ constexpr float GEAR_RATIO[6] = { constexpr float FINAL_DRIVE = 3.74f; // przekładnia różnicowa constexpr float WHEEL_RADIUS = 0.30f; // m -constexpr float CAR_MASS = 1200.0f; // kg constexpr float RPM_IDLE = 800.0f; // obroty jałowe constexpr float RPM_MAX = 7000.0f; // maksymalne obroty @@ -31,6 +30,8 @@ public: void begin(); + void setCarMass(float mass) { _carMass = mass; } + void update(float dt, uint8_t gearIdx); int getRPM() const; @@ -46,6 +47,7 @@ private: float _engineRPM = EngineConsts::RPM_IDLE; float _speedMs = 0.0f; + float _carMass = 1200.0f; float readNormalized(uint8_t pin) const; }; diff --git a/Projekt_Arduino.ino b/Projekt_Arduino.ino index 7b54db6..01d83a2 100644 --- a/Projekt_Arduino.ino +++ b/Projekt_Arduino.ino @@ -21,6 +21,29 @@ bool seatBelts = false; void setup() { Serial.begin(9600); + // ----------------------------------------------------------------- + // 1. Ask the user for the car mass (kg) via the Serial monitor. + // We block only until a valid number is received – this is OK + // because it happens once at startup. + // ----------------------------------------------------------------- + Serial.println(F("Enter car mass in kg (e.g. 1200):")); + while (true) { + // Wait until something is typed + if (Serial.available() > 0) { + float mass = Serial.parseFloat(); // reads the number + if (mass > 0) { // basic sanity check + engine.setCarMass(mass); + Serial.print(F("Car mass set to ")); + Serial.print(mass); + Serial.println(F(" kg")); + break; + } else { + Serial.println(F("Invalid value – try again:")); + } + } + } + + pinMode(IN_1, OUTPUT); pinMode(IN_2, OUTPUT); pinMode(ENA_1, OUTPUT); @@ -41,8 +64,12 @@ void loop() { if (rfid.check() && rfid.matches(AUTH_UID, AUTH_UID_SIZE)) { carEnabled = !carEnabled; - Serial.print("Auto działa: "); - Serial.println(carEnabled); + + if (carEnabled) + Serial.println("Uruchamiam auto"); + else + Serial.println("Gaszę auto"); + display.startOrEnd(carEnabled); };