Compare commits

..

5 Commits

Author SHA1 Message Date
GarandPLG 1f849e3455 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.
2026-06-17 05:49:37 +02:00
GarandPLG a2bb6c557d Remove unused LcdChar enum from Display.h 2026-06-17 04:38:33 +02:00
GarandPLG 05af6951b0 Localize prompts and fix seat belt logic
Replace English serial messages with Polish, rename `prevUpdate` to
`prevEngineUpdate` for clearer engine timing, simplify RFID checks by
removing unused variables, and correct seat‑belt logic to reset when
distance ≥ 200 cm.
2026-06-17 03:13:22 +02:00
GarandPLG 7585dfe3e9 Refactor seat belt logic for clarity 2026-06-17 01:22:34 +02:00
GarandPLG 0a4a0b862c Enable RFID UID output and tidy car mass input 2026-06-17 01:20:39 +02:00
5 changed files with 51 additions and 74 deletions
-7
View File
@@ -2,13 +2,6 @@
#include <rgb_lcd.h> #include <rgb_lcd.h>
#include <Arduino.h> #include <Arduino.h>
enum LcdChar : uint8_t {
CHAR_LEFT = 0,
CHAR_RIGHT = 1,
CHAR_LEFT_BLINK = 2,
CHAR_RIGHT_BLINK = 3
};
class Display { class Display {
public: public:
void begin(int cols, int rows); void begin(int cols, int rows);
+22 -24
View File
@@ -7,12 +7,31 @@ Engine::Engine(uint8_t throttlePin,
uint8_t brakePin) uint8_t brakePin)
: _throttlePin(throttlePin), _clutchPin(clutchPin), _brakePin(brakePin) {} : _throttlePin(throttlePin), _clutchPin(clutchPin), _brakePin(brakePin) {}
void Engine::begin() {}
float Engine::readNormalized(uint8_t pin) const { float Engine::readNormalized(uint8_t pin) const {
return analogRead(pin) / 1023.0f; return analogRead(pin) / 1023.0f;
} }
int Engine::getRPM() const {
return static_cast<int>(_engineRPM + 0.5f);
}
int Engine::getSpeedKmh() const {
return static_cast<int>((_speedMs * 3.6f) + 0.5f);
}
uint8_t Engine::rpmToPwm(float rpm) {
rpm = constrain(rpm, 0.0f, RPM_MAX);
return static_cast<uint8_t>(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) { void Engine::update(float dt, uint8_t gearIdx) {
const float throttle = readNormalized(_throttlePin); const float throttle = readNormalized(_throttlePin);
const float clutch = readNormalized(_clutchPin); 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); setMotor(IN_3, IN_4, ENA_2, targetRPM, false);
bool reverseWheel = (gearIdx == -1); bool reverseWheel = (gearIdx == -1);
setMotor(IN_1, IN_2, ENA_1, wheelRPM * 10.0f, reverseWheel); setMotor(IN_1, IN_2, ENA_1, wheelRPM, reverseWheel);
}
int Engine::getRPM() const {
return static_cast<int>(_engineRPM + 0.5f);
}
int Engine::getSpeedKmh() const {
return static_cast<int>((_speedMs * 3.6f) + 0.5f);
}
uint8_t Engine::rpmToPwm(float rpm) {
rpm = constrain(rpm, 0.0f, RPM_MAX);
return static_cast<uint8_t>(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));
} }
-2
View File
@@ -28,8 +28,6 @@ public:
uint8_t clutchPin, uint8_t clutchPin,
uint8_t brakePin); uint8_t brakePin);
void begin();
void setCarMass(float mass) { _carMass = mass; } void setCarMass(float mass) { _carMass = mass; }
void update(float dt, uint8_t gearIdx); void update(float dt, uint8_t gearIdx);
+25 -35
View File
@@ -13,7 +13,7 @@ Sonar sonar(TRIG_AND_ECHO_PIN, SONAR_MAX_DIST_CM);
RFIDReader rfid(SS_PIN, RST_PIN); RFIDReader rfid(SS_PIN, RST_PIN);
Engine engine(THROTTLE_PIN, CLUTCH_PIN, BRAKE_PIN); Engine engine(THROTTLE_PIN, CLUTCH_PIN, BRAKE_PIN);
unsigned long prevUpdate = 0; unsigned long prevEngineUpdate = 0;
bool carEnabled = false; bool carEnabled = false;
unsigned long prevSonarUpdate = 0; unsigned long prevSonarUpdate = 0;
bool seatBelts = false; bool seatBelts = false;
@@ -21,28 +21,21 @@ bool seatBelts = false;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
// ----------------------------------------------------------------- Serial.println(F("Podaj wagę samochodu w kg (np. 1200):"));
// 1. Ask the user for the car mass (kg) via the Serial monitor. while (true) {
// We block only until a valid number is received this is OK if (Serial.available() > 0) {
// because it happens once at startup. float mass = Serial.parseFloat();
// ----------------------------------------------------------------- if (mass > 0) {
Serial.println(F("Enter car mass in kg (e.g. 1200):")); engine.setCarMass(mass);
while (true) { Serial.print(F("Waga samochodu ustawiona na: "));
// Wait until something is typed Serial.print(mass);
if (Serial.available() > 0) { Serial.println(F(" kg"));
float mass = Serial.parseFloat(); // reads the number break;
if (mass > 0) { // basic sanity check } else {
engine.setCarMass(mass); Serial.println(F("Niepoprawna wartość, spróbuj ponownie:"));
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_1, OUTPUT);
pinMode(IN_2, OUTPUT); pinMode(IN_2, OUTPUT);
@@ -53,15 +46,9 @@ void setup() {
rfid.begin(); rfid.begin();
display.begin(LCD_COLS, LCD_ROWS); display.begin(LCD_COLS, LCD_ROWS);
engine.begin();
} }
void loop() { void loop() {
bool check = rfid.check();
bool matches = rfid.matches(AUTH_UID, AUTH_UID_SIZE);
bool rfidOk = check && matches;
if (rfid.check() && rfid.matches(AUTH_UID, AUTH_UID_SIZE)) { if (rfid.check() && rfid.matches(AUTH_UID, AUTH_UID_SIZE)) {
carEnabled = !carEnabled; carEnabled = !carEnabled;
@@ -78,21 +65,24 @@ void loop() {
display.updateGear(gear.getGearChar()); display.updateGear(gear.getGearChar());
unsigned long now = millis(); unsigned long now = millis();
float dt = (now - prevUpdate) / 1000.0f; float dt = (now - prevEngineUpdate) / 1000.0f;
if (dt >= 0.02f) { if (dt >= 0.02f) {
prevUpdate = now; prevEngineUpdate = now;
engine.update(dt, gear.getGear()); engine.update(dt, gear.getGear());
display.updateRPM(engine.getRPM()); display.updateRPM(engine.getRPM());
display.updateSpeed(engine.getSpeedKmh()); display.updateSpeed(engine.getSpeedKmh());
} }
if (millis() - prevSonarUpdate >= 100) { unsigned long now = millis();
prevSonarUpdate = millis(); if (now - prevSonarUpdate >= 100) {
prevSonarUpdate = now;
long dist = sonar.measure(); long dist = sonar.measure();
if (!seatBelts) if (!seatBelts) {
if (dist < 200) if (dist < 200) seatBelts = true;
seatBelts = true; }
else if (dist >= 200) seatBelts = true; else {
if (dist >= 200) seatBelts = false;
}
} }
} }
} }
+4 -6
View File
@@ -11,8 +11,6 @@ void RFIDReader::begin() {
bool RFIDReader::check() { bool RFIDReader::check() {
if (!_rfid.PICC_IsNewCardPresent()) return false; if (!_rfid.PICC_IsNewCardPresent()) return false;
if (!_rfid.PICC_ReadCardSerial()) return false; if (!_rfid.PICC_ReadCardSerial()) return false;
// _rfid.PICC_HaltA(); // stop the tag
// _rfid.PCD_StopCrypto1(); // clear RC522 crypto
return true; return true;
} }
@@ -31,10 +29,10 @@ bool RFIDReader::matches(const byte* expectedUid, byte expectedSize) const {
void RFIDReader::printUID() const { void RFIDReader::printUID() const {
Serial.println(F("RFID Tag UID:")); Serial.println(F("RFID Tag UID:"));
// for (byte i = 0; i < _rfid.uid.size; i++) { for (byte i = 0; i < _rfid.uid.size; i++) {
// Serial.print(_rfid.uid.uidByte[i] < 0x10 ? " 0" : " "); Serial.print(_rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
// Serial.print(_rfid.uid.uidByte[i], HEX); Serial.print(_rfid.uid.uidByte[i], HEX);
// } }
} }
const byte* RFIDReader::getUID() const { const byte* RFIDReader::getUID() const {