Add GasolineGenerator class and configuration for generator management
- Implement GasolineGenerator class with methods for initialization, startup, shutdown, and state management. - Introduce GasolineGeneratorConfig structure for configuration parameters. - Update main.cpp to initialize and use the GasolineGenerator instance. - Modify platformio.ini to include build flags for debugging and versioning.
This commit is contained in:
parent
7dbf37007a
commit
6ac762b1ec
6 changed files with 286 additions and 0 deletions
191
src/GasolineGenerator.cpp
Normal file
191
src/GasolineGenerator.cpp
Normal file
|
@ -0,0 +1,191 @@
|
|||
#include "GasolineGenerator.hpp"
|
||||
|
||||
GasolineGenerator::GasolineGenerator()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void GasolineGenerator::initialize(DigitalInputCard* inputCard, DigitalOutputCard* outputCard, const GasolineGeneratorConfig &config)
|
||||
{
|
||||
this->config = &config;
|
||||
this->inputCard = inputCard;
|
||||
this->outputCard = outputCard;
|
||||
|
||||
// Initialize all outputs to safe state
|
||||
setIgnitionSystem(false);
|
||||
setStarter(false);
|
||||
setCarburetorValve(false); // Close for startup
|
||||
|
||||
// Set debounce time for power output sensor if enabled
|
||||
if (config.usePowerOutputSensor && config.powerOutputSensorPin >= 0)
|
||||
{
|
||||
this->inputCard->setDebounceTime(config.powerOutputSensorPin, POWER_SENSOR_DEBOUNCE_MS);
|
||||
}
|
||||
|
||||
// Register input change callback
|
||||
this->inputCard->registerCallback([this](uint8_t pin, bool state)
|
||||
{ this->handleInputChanges(pin, state); });
|
||||
|
||||
state = GeneratorState::STOPPED;
|
||||
}
|
||||
|
||||
void GasolineGenerator::loop()
|
||||
{
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case GeneratorState::STARTING:
|
||||
handleStartupSequence(currentTime);
|
||||
break;
|
||||
|
||||
case GeneratorState::RUNNING:
|
||||
// Monitor engine status and handle any issues
|
||||
if (config->usePowerOutputSensor && config->powerOutputSensorPin >= 0)
|
||||
{
|
||||
bool hasPowerOutput = inputCard->digitalRead(config->powerOutputSensorPin);
|
||||
if (!hasPowerOutput)
|
||||
{
|
||||
// Engine stopped unexpectedly - no power output detected
|
||||
isEngineRunning = false;
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case GeneratorState::SHUTTING_DOWN:
|
||||
// Complete shutdown sequence
|
||||
state = GeneratorState::STOPPED;
|
||||
break;
|
||||
|
||||
case GeneratorState::STOPPED:
|
||||
default:
|
||||
// Nothing to do in stopped state
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void GasolineGenerator::start()
|
||||
{
|
||||
if (state != GeneratorState::STOPPED)
|
||||
{
|
||||
return; // Already starting or running
|
||||
}
|
||||
|
||||
state = GeneratorState::STARTING;
|
||||
startupStartTime = millis();
|
||||
ignitionStartTime = 0;
|
||||
isEngineRunning = false;
|
||||
starterEngaged = false;
|
||||
|
||||
// Step 1: Close carburetor valve for rich mixture
|
||||
setCarburetorValve(false);
|
||||
|
||||
// Step 2: Power up ignition system
|
||||
setIgnitionSystem(true);
|
||||
ignitionStartTime = millis();
|
||||
}
|
||||
|
||||
void GasolineGenerator::shutdown()
|
||||
{
|
||||
// Turn off all systems
|
||||
setStarter(false);
|
||||
setIgnitionSystem(false);
|
||||
setCarburetorValve(false); // Close carburetor valve
|
||||
|
||||
state = GeneratorState::STOPPED;
|
||||
isEngineRunning = false;
|
||||
}
|
||||
|
||||
void GasolineGenerator::setCarburetorValve(bool open)
|
||||
{
|
||||
bool pinState = config->carburetorValveReverse ? !open : open;
|
||||
outputCard->digitalWrite(config->carburetorValvePin, pinState);
|
||||
}
|
||||
|
||||
void GasolineGenerator::setIgnitionSystem(bool enable)
|
||||
{
|
||||
outputCard->digitalWrite(config->ignitionSystemPowerRelayPin, enable);
|
||||
}
|
||||
|
||||
void GasolineGenerator::setStarter(bool enable)
|
||||
{
|
||||
outputCard->digitalWrite(config->starterRelayPin, enable);
|
||||
starterEngaged = enable;
|
||||
}
|
||||
|
||||
void GasolineGenerator::handleStartupSequence(unsigned long currentTime)
|
||||
{
|
||||
// Step 3: After ignition delay, engage starter
|
||||
if (ignitionStartTime > 0 &&
|
||||
(currentTime - ignitionStartTime) >= IGNITION_DELAY_MS &&
|
||||
!starterEngaged)
|
||||
{
|
||||
|
||||
setStarter(true);
|
||||
}
|
||||
|
||||
// Check if we have power output AND starter is off (engine is running independently)
|
||||
if (config->usePowerOutputSensor && config->powerOutputSensorPin >= 0)
|
||||
{
|
||||
bool hasPowerOutput = inputCard->digitalRead(config->powerOutputSensorPin);
|
||||
if (hasPowerOutput && !starterEngaged && !isEngineRunning)
|
||||
{
|
||||
onEngineStarted();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// No power sensor - fallback to time-based detection
|
||||
if ((currentTime - startupStartTime) >= (IGNITION_DELAY_MS + 3000) && !isEngineRunning)
|
||||
{
|
||||
onEngineStarted();
|
||||
}
|
||||
}
|
||||
|
||||
// Timeout protection - stop starter after maximum duration
|
||||
if (ignitionStartTime > 0 &&
|
||||
(currentTime - ignitionStartTime) >= (IGNITION_DELAY_MS + STARTER_DURATION_MS))
|
||||
{
|
||||
|
||||
setStarter(false);
|
||||
|
||||
if (!isEngineRunning)
|
||||
{
|
||||
// Failed to start
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void GasolineGenerator::onEngineStarted()
|
||||
{
|
||||
isEngineRunning = true;
|
||||
|
||||
// Turn off starter
|
||||
setStarter(false);
|
||||
|
||||
// Open carburetor valve for normal operation
|
||||
setCarburetorValve(true);
|
||||
|
||||
state = GeneratorState::RUNNING;
|
||||
}
|
||||
|
||||
void GasolineGenerator::handleInputChanges(uint8_t pin, bool state)
|
||||
{
|
||||
// Handle power output sensor changes
|
||||
if (pin == config->powerOutputSensorPin)
|
||||
{
|
||||
if (state && this->state == GeneratorState::STARTING && !starterEngaged && !isEngineRunning)
|
||||
{
|
||||
// Power detected during startup with starter off - engine has started successfully
|
||||
onEngineStarted();
|
||||
}
|
||||
else if (!state && this->state == GeneratorState::RUNNING)
|
||||
{
|
||||
// Power lost during operation - engine may have stopped
|
||||
isEngineRunning = false;
|
||||
shutdown();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue