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 <Arduino.h>
enum LcdChar : uint8_t {
CHAR_LEFT = 0,
CHAR_RIGHT = 1,
CHAR_LEFT_BLINK = 2,
CHAR_RIGHT_BLINK = 3
};
class Display {
public:
void begin(int cols, int rows);
+22 -24
View File
@@ -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<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) {
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<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));
setMotor(IN_1, IN_2, ENA_1, wheelRPM, reverseWheel);
}
-2
View File
@@ -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);
+17 -27
View File
@@ -13,7 +13,7 @@ Sonar sonar(TRIG_AND_ECHO_PIN, SONAR_MAX_DIST_CM);
RFIDReader rfid(SS_PIN, RST_PIN);
Engine engine(THROTTLE_PIN, CLUTCH_PIN, BRAKE_PIN);
unsigned long prevUpdate = 0;
unsigned long prevEngineUpdate = 0;
bool carEnabled = false;
unsigned long prevSonarUpdate = 0;
bool seatBelts = false;
@@ -21,29 +21,22 @@ 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):"));
Serial.println(F("Podaj wagę samochodu w kg (np. 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
float mass = Serial.parseFloat();
if (mass > 0) {
engine.setCarMass(mass);
Serial.print(F("Car mass set to "));
Serial.print(F("Waga samochodu ustawiona na: "));
Serial.print(mass);
Serial.println(F(" kg"));
break;
} else {
Serial.println(F("Invalid value try again:"));
Serial.println(F("Niepoprawna wartość, spróbuj ponownie:"));
}
}
}
pinMode(IN_1, OUTPUT);
pinMode(IN_2, OUTPUT);
pinMode(ENA_1, OUTPUT);
@@ -53,15 +46,9 @@ void setup() {
rfid.begin();
display.begin(LCD_COLS, LCD_ROWS);
engine.begin();
}
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)) {
carEnabled = !carEnabled;
@@ -78,21 +65,24 @@ void loop() {
display.updateGear(gear.getGearChar());
unsigned long now = millis();
float dt = (now - prevUpdate) / 1000.0f;
float dt = (now - prevEngineUpdate) / 1000.0f;
if (dt >= 0.02f) {
prevUpdate = now;
prevEngineUpdate = now;
engine.update(dt, gear.getGear());
display.updateRPM(engine.getRPM());
display.updateSpeed(engine.getSpeedKmh());
}
if (millis() - prevSonarUpdate >= 100) {
prevSonarUpdate = millis();
unsigned long now = millis();
if (now - prevSonarUpdate >= 100) {
prevSonarUpdate = now;
long dist = sonar.measure();
if (!seatBelts)
if (dist < 200)
seatBelts = true;
else if (dist >= 200) seatBelts = true;
if (!seatBelts) {
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() {
if (!_rfid.PICC_IsNewCardPresent()) return false;
if (!_rfid.PICC_ReadCardSerial()) return false;
// _rfid.PICC_HaltA(); // stop the tag
// _rfid.PCD_StopCrypto1(); // clear RC522 crypto
return true;
}
@@ -31,10 +29,10 @@ bool RFIDReader::matches(const byte* expectedUid, byte expectedSize) const {
void RFIDReader::printUID() const {
Serial.println(F("RFID Tag UID:"));
// for (byte i = 0; i < _rfid.uid.size; i++) {
// Serial.print(_rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
// Serial.print(_rfid.uid.uidByte[i], HEX);
// }
for (byte i = 0; i < _rfid.uid.size; i++) {
Serial.print(_rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(_rfid.uid.uidByte[i], HEX);
}
}
const byte* RFIDReader::getUID() const {