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.
This commit is contained in:
+22
-24
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -46,7 +46,6 @@ void setup() {
|
||||
|
||||
rfid.begin();
|
||||
display.begin(LCD_COLS, LCD_ROWS);
|
||||
engine.begin();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
Reference in New Issue
Block a user