3
0
Fork 0

Refactor generator configuration to use defined constants

This commit is contained in:
Siwat Sirichai 2025-06-08 22:09:39 +07:00
parent 6ac762b1ec
commit 863d9c1cf3
3 changed files with 80 additions and 6 deletions

View file

@ -5,6 +5,15 @@ GasolineGenerator::GasolineGenerator()
}
/**
* @brief Initialize the Gasoline Generator.
*
* @note This does not start the generator, it only initializes the input and output cards,
*
* @param inputCard The digital input card used for monitoring the generator state
* @param outputCard The digital output card used for controlling the generator components
* @param config The configuration settings for the generator, pins are of input and output cards specified earlier.
*/
void GasolineGenerator::initialize(DigitalInputCard* inputCard, DigitalOutputCard* outputCard, const GasolineGeneratorConfig &config)
{
this->config = &config;
@ -29,6 +38,10 @@ void GasolineGenerator::initialize(DigitalInputCard* inputCard, DigitalOutputCar
state = GeneratorState::STOPPED;
}
/**
* @brief Main loop for the Gasoline Generator.
* * This function should be called periodically to handle the generator state machine.
*/
void GasolineGenerator::loop()
{
unsigned long currentTime = millis();
@ -65,6 +78,13 @@ void GasolineGenerator::loop()
}
}
/**
* @brief Start the gasoline generator.
*
* This function initiates the startup sequence for the generator.
* It will close the carburetor valve, power up the ignition system,
* and engage the starter relay to start the engine.
*/
void GasolineGenerator::start()
{
if (state != GeneratorState::STOPPED)
@ -86,6 +106,12 @@ void GasolineGenerator::start()
ignitionStartTime = millis();
}
/**
* @brief Shutdown the gasoline generator.
*
* This function stops the generator by turning off all systems,
* including the starter, ignition system, and carburetor valve.
*/
void GasolineGenerator::shutdown()
{
// Turn off all systems
@ -97,23 +123,47 @@ void GasolineGenerator::shutdown()
isEngineRunning = false;
}
/**
* @brief Set the carburetor valve state.
*
* @param open If true, opens the carburetor valve; if false, closes it.
* The actual pin state is reversed based on the configuration.
*/
void GasolineGenerator::setCarburetorValve(bool open)
{
bool pinState = config->carburetorValveReverse ? !open : open;
outputCard->digitalWrite(config->carburetorValvePin, pinState);
}
/**
* @brief Set the ignition system state.
*
* @param enable If true, powers the ignition system; if false, turns it off.
*/
void GasolineGenerator::setIgnitionSystem(bool enable)
{
outputCard->digitalWrite(config->ignitionSystemPowerRelayPin, enable);
}
/**
* @brief Set the starter relay state.
*
* @param enable If true, engages the starter; if false, disengages it.
*/
void GasolineGenerator::setStarter(bool enable)
{
outputCard->digitalWrite(config->starterRelayPin, enable);
starterEngaged = enable;
}
/**
* @brief Handle the startup sequence of the gasoline generator.
*
* This function manages the timing and state transitions during the startup process,
* including engaging the starter and checking for successful engine start.
*
* @param currentTime The current time in milliseconds since system start.
*/
void GasolineGenerator::handleStartupSequence(unsigned long currentTime)
{
// Step 3: After ignition delay, engage starter
@ -158,6 +208,12 @@ void GasolineGenerator::handleStartupSequence(unsigned long currentTime)
}
}
/**
* @brief Callback function called when the engine has successfully started.
*
* This function is called when the engine starts successfully after engaging the starter.
* It turns off the starter, opens the carburetor valve, and transitions to the RUNNING state.
*/
void GasolineGenerator::onEngineStarted()
{
isEngineRunning = true;
@ -171,6 +227,15 @@ void GasolineGenerator::onEngineStarted()
state = GeneratorState::RUNNING;
}
/**
* @brief Handle input changes from the digital input card.
*
* This function processes changes in the state of input pins, particularly for the power output sensor.
* It checks if the engine has started or stopped based on the power output sensor state.
*
* @param pin The pin number that changed state.
* @param state The new state of the pin (true for HIGH, false for LOW).
*/
void GasolineGenerator::handleInputChanges(uint8_t pin, bool state)
{
// Handle power output sensor changes

View file

@ -0,0 +1,9 @@
#pragma once
// Generator Configuration
#define GENERATOR_IGNITION_SYSTEM_POWER_RELAY_PIN 0
#define GENERATOR_STARTER_RELAY_PIN 1
#define GENERATOR_CARBURETOR_VALVE_PIN 2
#define GENERATOR_CARBURETOR_VALVE_REVERSE false
#define GENERATOR_POWER_OUTPUT_SENSOR_PIN -1
#define GENERATOR_USE_POWER_OUTPUT_SENSOR false

View file

@ -54,12 +54,12 @@ void setup()
// Initialize generator
GasolineGeneratorConfig generatorConfig;
generatorConfig.ignitionSystemPowerRelayPin = 0;
generatorConfig.starterRelayPin = 1;
generatorConfig.carburetorValvePin = 2;
generatorConfig.carburetorValveReverse = false;
generatorConfig.powerOutputSensorPin = -1;
generatorConfig.usePowerOutputSensor = false;
generatorConfig.ignitionSystemPowerRelayPin = GENERATOR_IGNITION_SYSTEM_POWER_RELAY_PIN;
generatorConfig.starterRelayPin = GENERATOR_STARTER_RELAY_PIN;
generatorConfig.carburetorValvePin = GENERATOR_CARBURETOR_VALVE_PIN;
generatorConfig.carburetorValveReverse = GENERATOR_CARBURETOR_VALVE_REVERSE;
generatorConfig.powerOutputSensorPin = GENERATOR_POWER_OUTPUT_SENSOR_PIN;
generatorConfig.usePowerOutputSensor = GENERATOR_USE_POWER_OUTPUT_SENSOR;
generator.initialize(&espmega.inputs, &espmega.outputs, generatorConfig);