3
0
Fork 0

millis gen

This commit is contained in:
Siwat Sirichai 2025-06-10 22:02:30 +07:00
parent 863d9c1cf3
commit 73068f7b1f
2 changed files with 27 additions and 28 deletions

View file

@ -2,24 +2,23 @@
GasolineGenerator::GasolineGenerator() GasolineGenerator::GasolineGenerator()
{ {
} }
/** /**
* @brief Initialize the Gasoline Generator. * @brief Initialize the Gasoline Generator.
* *
* @note This does not start the generator, it only initializes the input and output cards, * @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 inputCard The digital input card used for monitoring the generator state
* @param outputCard The digital output card used for controlling the generator components * @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. * @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) void GasolineGenerator::initialize(DigitalInputCard *inputCard, DigitalOutputCard *outputCard, const GasolineGeneratorConfig &config)
{ {
this->config = &config; this->config = &config;
this->inputCard = inputCard; this->inputCard = inputCard;
this->outputCard = outputCard; this->outputCard = outputCard;
// Initialize all outputs to safe state // Initialize all outputs to safe state
setIgnitionSystem(false); setIgnitionSystem(false);
setStarter(false); setStarter(false);
@ -44,7 +43,7 @@ void GasolineGenerator::initialize(DigitalInputCard* inputCard, DigitalOutputCar
*/ */
void GasolineGenerator::loop() void GasolineGenerator::loop()
{ {
unsigned long currentTime = millis(); unsigned long currentTime = (xTaskGetTickCount() * 1000) / configTICK_RATE_HZ;
switch (state) switch (state)
{ {
@ -80,7 +79,7 @@ void GasolineGenerator::loop()
/** /**
* @brief Start the gasoline generator. * @brief Start the gasoline generator.
* *
* This function initiates the startup sequence for the generator. * This function initiates the startup sequence for the generator.
* It will close the carburetor valve, power up the ignition system, * It will close the carburetor valve, power up the ignition system,
* and engage the starter relay to start the engine. * and engage the starter relay to start the engine.
@ -93,7 +92,7 @@ void GasolineGenerator::start()
} }
state = GeneratorState::STARTING; state = GeneratorState::STARTING;
startupStartTime = millis(); startupStartTime = (xTaskGetTickCount() * 1000) / configTICK_RATE_HZ;
ignitionStartTime = 0; ignitionStartTime = 0;
isEngineRunning = false; isEngineRunning = false;
starterEngaged = false; starterEngaged = false;
@ -103,12 +102,12 @@ void GasolineGenerator::start()
// Step 2: Power up ignition system // Step 2: Power up ignition system
setIgnitionSystem(true); setIgnitionSystem(true);
ignitionStartTime = millis(); ignitionStartTime = (xTaskGetTickCount() * 1000) / configTICK_RATE_HZ;
} }
/** /**
* @brief Shutdown the gasoline generator. * @brief Shutdown the gasoline generator.
* *
* This function stops the generator by turning off all systems, * This function stops the generator by turning off all systems,
* including the starter, ignition system, and carburetor valve. * including the starter, ignition system, and carburetor valve.
*/ */
@ -125,7 +124,7 @@ void GasolineGenerator::shutdown()
/** /**
* @brief Set the carburetor valve state. * @brief Set the carburetor valve state.
* *
* @param open If true, opens the carburetor valve; if false, closes it. * @param open If true, opens the carburetor valve; if false, closes it.
* The actual pin state is reversed based on the configuration. * The actual pin state is reversed based on the configuration.
*/ */
@ -137,7 +136,7 @@ void GasolineGenerator::setCarburetorValve(bool open)
/** /**
* @brief Set the ignition system state. * @brief Set the ignition system state.
* *
* @param enable If true, powers the ignition system; if false, turns it off. * @param enable If true, powers the ignition system; if false, turns it off.
*/ */
void GasolineGenerator::setIgnitionSystem(bool enable) void GasolineGenerator::setIgnitionSystem(bool enable)
@ -147,7 +146,7 @@ void GasolineGenerator::setIgnitionSystem(bool enable)
/** /**
* @brief Set the starter relay state. * @brief Set the starter relay state.
* *
* @param enable If true, engages the starter; if false, disengages it. * @param enable If true, engages the starter; if false, disengages it.
*/ */
void GasolineGenerator::setStarter(bool enable) void GasolineGenerator::setStarter(bool enable)
@ -158,10 +157,10 @@ void GasolineGenerator::setStarter(bool enable)
/** /**
* @brief Handle the startup sequence of the gasoline generator. * @brief Handle the startup sequence of the gasoline generator.
* *
* This function manages the timing and state transitions during the startup process, * This function manages the timing and state transitions during the startup process,
* including engaging the starter and checking for successful engine start. * including engaging the starter and checking for successful engine start.
* *
* @param currentTime The current time in milliseconds since system start. * @param currentTime The current time in milliseconds since system start.
*/ */
void GasolineGenerator::handleStartupSequence(unsigned long currentTime) void GasolineGenerator::handleStartupSequence(unsigned long currentTime)
@ -210,7 +209,7 @@ void GasolineGenerator::handleStartupSequence(unsigned long currentTime)
/** /**
* @brief Callback function called when the engine has successfully started. * @brief Callback function called when the engine has successfully started.
* *
* This function is called when the engine starts successfully after engaging the starter. * 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. * It turns off the starter, opens the carburetor valve, and transitions to the RUNNING state.
*/ */
@ -229,10 +228,10 @@ void GasolineGenerator::onEngineStarted()
/** /**
* @brief Handle input changes from the digital input card. * @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. * 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. * 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 pin The pin number that changed state.
* @param state The new state of the pin (true for HIGH, false for LOW). * @param state The new state of the pin (true for HIGH, false for LOW).
*/ */

View file

@ -3,7 +3,7 @@
/** /**
* @brief Configuration structure for the Gasoline Generator. * @brief Configuration structure for the Gasoline Generator.
* *
* This structure holds the configuration parameters for the gasoline generator, * This structure holds the configuration parameters for the gasoline generator,
* including input and output cards, pin assignments, and sensor settings. * including input and output cards, pin assignments, and sensor settings.
* It is used to initialize the GasolineGenerator class. * It is used to initialize the GasolineGenerator class.
@ -15,28 +15,28 @@
struct GasolineGeneratorConfig struct GasolineGeneratorConfig
{ {
int8_t ignitionSystemPowerRelayPin; // Pin for ignition system power relay int8_t ignitionSystemPowerRelayPin; // Pin for ignition system power relay
int8_t starterRelayPin; // Pin for starter relay int8_t starterRelayPin; // Pin for starter relay
int8_t carburetorValvePin; // Pin for carburetor control int8_t carburetorValvePin; // Pin for carburetor control
bool carburetorValveReverse; // If true, 0 means open, 1 means closed else 0 means closed, 1 means open bool carburetorValveReverse; // If true, 0 means open, 1 means closed else 0 means closed, 1 means open
int8_t powerOutputSensorPin = -1; // Pin for monitoring generator power output (-1 to disable) int8_t powerOutputSensorPin = -1; // Pin for monitoring generator power output (-1 to disable)
bool usePowerOutputSensor = false; // Enable/disable power output sensor monitoring bool usePowerOutputSensor = false; // Enable/disable power output sensor monitoring
}; };
/** /**
* @brief Enumeration for the generator states. * @brief Enumeration for the generator states.
* This enum defines the possible states of the gasoline generator. * This enum defines the possible states of the gasoline generator.
*/ */
enum class GeneratorState { enum class GeneratorState
{
STOPPED, STOPPED,
STARTING, STARTING,
RUNNING, RUNNING,
SHUTTING_DOWN SHUTTING_DOWN
}; };
/** /**
* @brief Gasoline Generator class for managing a gasoline-powered generator. * @brief Gasoline Generator class for managing a gasoline-powered generator.
* *
* This class handles the ignition system, starter relay, and carburetor control * This class handles the ignition system, starter relay, and carburetor control
* For startup first it closes the carburetor valve, creating a rich air-fuel mixture, * For startup first it closes the carburetor valve, creating a rich air-fuel mixture,
* then it powers the ignition system, wait a bit then spin the starter relay to start the engine. * then it powers the ignition system, wait a bit then spin the starter relay to start the engine.
@ -46,7 +46,7 @@ class GasolineGenerator
{ {
public: public:
GasolineGenerator(); GasolineGenerator();
void initialize(DigitalInputCard* inputCard, DigitalOutputCard* outputCard, const GasolineGeneratorConfig &config); void initialize(DigitalInputCard *inputCard, DigitalOutputCard *outputCard, const GasolineGeneratorConfig &config);
void loop(); void loop();
void start(); void start();
void shutdown(); void shutdown();