Make car mass configurable at runtime
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.
This commit is contained in:
+1
-1
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
+29
-2
@@ -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);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user