Remove debug prints, correct reverse gear, tidy loop
- Comment out all Serial debugging statements and the artificial delay in Engine::update. - Change reverse gear detection from `gearIdx == 5` to `gearIdx == -1`. - Simplify the main loop in Projekt.ino by removing unnecessary braces, eliminating temporary blink variables, and calling `display.updateDirection` directly with the pin reads.
This commit is contained in:
+35
-35
@@ -14,74 +14,74 @@ float Engine::readNormalized(uint8_t pin) const {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Engine::update(float dt, uint8_t gearIdx) {
|
void Engine::update(float dt, uint8_t gearIdx) {
|
||||||
Serial.println("----------------");
|
// Serial.println("----------------");
|
||||||
|
|
||||||
const float throttle = readNormalized(_throttlePin);
|
const float throttle = readNormalized(_throttlePin);
|
||||||
Serial.print("Throttle: ");
|
// Serial.print("Throttle: ");
|
||||||
Serial.println(throttle);
|
// Serial.println(throttle);
|
||||||
|
|
||||||
const float clutch = readNormalized(_clutchPin);
|
const float clutch = readNormalized(_clutchPin);
|
||||||
Serial.print("Clutch: ");
|
// Serial.print("Clutch: ");
|
||||||
Serial.println(clutch);
|
// Serial.println(clutch);
|
||||||
|
|
||||||
const float brake = readNormalized(_brakePin);
|
const float brake = readNormalized(_brakePin);
|
||||||
Serial.print("Brake: ");
|
// Serial.print("Brake: ");
|
||||||
Serial.println(brake);
|
// Serial.println(brake);
|
||||||
|
|
||||||
const float targetRPM = RPM_IDLE + throttle * (RPM_MAX - RPM_IDLE);
|
const float targetRPM = RPM_IDLE + throttle * (RPM_MAX - RPM_IDLE);
|
||||||
Serial.print("targetRPM: ");
|
// Serial.print("targetRPM: ");
|
||||||
Serial.println(targetRPM);
|
// Serial.println(targetRPM);
|
||||||
|
|
||||||
const float wheelRPM = (_speedMs / (2.0f * PI * WHEEL_RADIUS)) * 60.0f;
|
const float wheelRPM = (_speedMs / (2.0f * PI * WHEEL_RADIUS)) * 60.0f;
|
||||||
Serial.print("wheelRPM: ");
|
// Serial.print("wheelRPM: ");
|
||||||
Serial.println(wheelRPM);
|
// Serial.println(wheelRPM);
|
||||||
|
|
||||||
const float gearRatio = GEAR_RATIO[gearIdx];
|
const float gearRatio = GEAR_RATIO[gearIdx];
|
||||||
Serial.print("gearRatio: ");
|
// Serial.print("gearRatio: ");
|
||||||
Serial.println(gearRatio);
|
// Serial.println(gearRatio);
|
||||||
|
|
||||||
const float drivenRPM = wheelRPM * fabs(gearRatio) * FINAL_DRIVE;
|
const float drivenRPM = wheelRPM * fabs(gearRatio) * FINAL_DRIVE;
|
||||||
Serial.print("drivenRPM: ");
|
// Serial.print("drivenRPM: ");
|
||||||
Serial.println(drivenRPM);
|
// Serial.println(drivenRPM);
|
||||||
|
|
||||||
_engineRPM = clutch * drivenRPM + (1.0f - clutch) * targetRPM;
|
_engineRPM = clutch * drivenRPM + (1.0f - clutch) * targetRPM;
|
||||||
Serial.print("_engineRPM: ");
|
// Serial.print("_engineRPM: ");
|
||||||
Serial.println(_engineRPM);
|
// Serial.println(_engineRPM);
|
||||||
|
|
||||||
_engineRPM = constrain(_engineRPM, RPM_IDLE, 7500.0f);
|
_engineRPM = constrain(_engineRPM, RPM_IDLE, 7500.0f);
|
||||||
Serial.print("_engineRPM (constrain): ");
|
// Serial.print("_engineRPM (constrain): ");
|
||||||
Serial.println(_engineRPM);
|
// Serial.println(_engineRPM);
|
||||||
|
|
||||||
const float engineForce = (_engineRPM / RPM_MAX) * (1.0f - clutch) * ENGINE_FORCE_FACTOR;
|
const float engineForce = (_engineRPM / RPM_MAX) * (1.0f - clutch) * ENGINE_FORCE_FACTOR;
|
||||||
Serial.print("engineForce: ");
|
// Serial.print("engineForce: ");
|
||||||
Serial.println(engineForce);
|
// Serial.println(engineForce);
|
||||||
|
|
||||||
const float brakeForce = brake * BRAKE_FORCE_MAX;
|
const float brakeForce = brake * BRAKE_FORCE_MAX;
|
||||||
Serial.print("brakeForce: ");
|
// Serial.print("brakeForce: ");
|
||||||
Serial.println(brakeForce);
|
// Serial.println(brakeForce);
|
||||||
|
|
||||||
const float dragForce = 0.5f * DRAG_COEFF * FRONTAL_AREA * (_speedMs * _speedMs);
|
const float dragForce = 0.5f * DRAG_COEFF * FRONTAL_AREA * (_speedMs * _speedMs);
|
||||||
Serial.print("dragForce: ");
|
// Serial.print("dragForce: ");
|
||||||
Serial.println(dragForce);
|
// Serial.println(dragForce);
|
||||||
|
|
||||||
const int direction = (gearIdx == 5) ? -1 : 1;
|
const int direction = (gearIdx == -1) ? -1 : 1;
|
||||||
Serial.print("direction: ");
|
// Serial.print("direction: ");
|
||||||
Serial.println(direction);
|
// Serial.println(direction);
|
||||||
|
|
||||||
const float netForce = direction * engineForce - brakeForce - dragForce;
|
const float netForce = direction * engineForce - brakeForce - dragForce;
|
||||||
Serial.print("netForce: ");
|
// Serial.print("netForce: ");
|
||||||
Serial.println(netForce);
|
// Serial.println(netForce);
|
||||||
|
|
||||||
const float acceleration = netForce / CAR_MASS;
|
const float acceleration = netForce / CAR_MASS;
|
||||||
Serial.print("acceleration: ");
|
// Serial.print("acceleration: ");
|
||||||
Serial.println(acceleration);
|
// Serial.println(acceleration);
|
||||||
|
|
||||||
_speedMs += acceleration * dt;
|
_speedMs += acceleration * dt;
|
||||||
if (_speedMs < 0.0f) _speedMs = 0.0f;
|
if (_speedMs < 0.0f) _speedMs = 0.0f;
|
||||||
Serial.print("_speedMs: ");
|
// Serial.print("_speedMs: ");
|
||||||
Serial.println(_speedMs);
|
// Serial.println(_speedMs);
|
||||||
|
|
||||||
delay(1000);
|
// delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Engine::getRPM() const {
|
int Engine::getRPM() const {
|
||||||
|
|||||||
+4
-5
@@ -30,13 +30,12 @@ void setup() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
if (gear.update()) {
|
if (gear.update())
|
||||||
display.updateGear(gear.getGearChar());
|
display.updateGear(gear.getGearChar());
|
||||||
}
|
|
||||||
|
|
||||||
leftBlink = digitalRead(TS_LEFT);
|
display.updateDirection(
|
||||||
rightBlink = digitalRead(TS_RIGHT);
|
digitalRead(TS_LEFT),
|
||||||
display.updateDirection(leftBlink, rightBlink);
|
digitalRead(TS_RIGHT));
|
||||||
|
|
||||||
unsigned long now = millis();
|
unsigned long now = millis();
|
||||||
float dt = (now - prevUpdate) / 1000.0f;
|
float dt = (now - prevUpdate) / 1000.0f;
|
||||||
|
|||||||
Reference in New Issue
Block a user