cleanup
This commit is contained in:
parent
3266981b44
commit
5c9f0727be
@ -35,10 +35,6 @@ Joystick::Joystick(Component *owner,
|
||||
{
|
||||
setNetCapability(OBJECT_NET_CAPS::E_NCAPS_MODBUS);
|
||||
|
||||
Log.verboseln(JOY_L("Joystick ID:%d: Created. Pins: Up:%d Down:%d Left:%d Right:%d"),
|
||||
id, pinUp, pinDown, pinLeft, pinRight);
|
||||
|
||||
// Setup ModbusBlocks
|
||||
memset(modbusBlocks, 0, sizeof(modbusBlocks));
|
||||
const char *joyGroup = "Joystick - 4P";
|
||||
|
||||
@ -90,8 +86,6 @@ short Joystick::reset() {
|
||||
confirmCount = DEBOUNCE_COUNT;
|
||||
positionStartMs = now;
|
||||
|
||||
Log.verboseln(JOY_L("Joystick ID:%d: Reset. Position: %d"),
|
||||
id, static_cast<int>(currentPosition));
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
@ -177,8 +171,7 @@ short Joystick::info(short, short) {
|
||||
useDebouncing ? "ENABLED" : "DISABLED");
|
||||
Log.infoln(JOY_L(" Current Position: %d, Last Position: %d"),
|
||||
static_cast<int>(currentPosition), static_cast<int>(lastPosition));
|
||||
Log.infoln(JOY_L(" Position Holding Time: %lu ms"), getHoldingTime());
|
||||
|
||||
Log.infoln(JOY_L(" Position Holding Time: %lu ms"), getHoldingTime());
|
||||
if (mode == E_MODE::REMOTE) {
|
||||
Log.infoln(JOY_L(" Override Position: %d"), static_cast<int>(overridePosition));
|
||||
}
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
#include <Arduino.h>
|
||||
#include "PlungerSettings.h"
|
||||
|
||||
const bool debug_jam = true;
|
||||
const bool debug_states = true;
|
||||
const bool debug_jam = false;
|
||||
const bool debug_states = false;
|
||||
|
||||
const char *_plungerStateToString(PlungerState state)
|
||||
{
|
||||
@ -37,8 +37,6 @@ const char *_plungerStateToString(PlungerState state)
|
||||
return "UNKNOWN_STATE";
|
||||
}
|
||||
}
|
||||
|
||||
// Re-add _fillStateToString
|
||||
const char *_fillStateToString(FillState state)
|
||||
{
|
||||
switch (state)
|
||||
@ -57,8 +55,6 @@ const char *_fillStateToString(FillState state)
|
||||
return "UNKNOWN_FILL_STATE";
|
||||
}
|
||||
}
|
||||
|
||||
// Add _postFlowStateToString
|
||||
const char *_postFlowStateToString(PostFlowState state)
|
||||
{
|
||||
switch (state)
|
||||
@ -132,12 +128,8 @@ short Plunger::init()
|
||||
|
||||
if (loadSettings)
|
||||
{
|
||||
// _settings is already initialized with compile-time defaults by its constructor.
|
||||
// Now, attempt to load from file, overriding defaults if successful.
|
||||
Log.infoln("[%s] init() called. Attempting to load settings...", name.c_str());
|
||||
if (_settings.load())
|
||||
{ // Uses default path "/plunger.json"
|
||||
Log.infoln("[%s] Settings loaded successfully from file during init.", name.c_str());
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -152,8 +144,6 @@ short Plunger::init()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Now that settings are finalized (loaded or compile-time defaults), proceed with dependent initializations.
|
||||
_updatePotValues(); // Recalculates _calculatedPlungingSpeedHz based on potentially loaded _settings.speedFastHz
|
||||
_recordedPlungeDurationMs = _settings.replayDurationMs;
|
||||
|
||||
@ -169,10 +159,6 @@ short Plunger::init()
|
||||
_postFlowStartTimeMs = 0;
|
||||
_currentPostFlowState = PostFlowState::NONE;
|
||||
_postFlowSubStateTimer.detach();
|
||||
|
||||
Log.infoln("[%s] Final Plunger settings after init process:", name.c_str());
|
||||
_settings.print(); // Log the finalized settings
|
||||
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
@ -208,7 +194,6 @@ void Plunger::_vfdStop()
|
||||
|
||||
void Plunger::_vfdResetJam()
|
||||
{
|
||||
Log.verboseln("[%s] VFD Resetting Fault/Jam", name.c_str());
|
||||
_vfd->resetFault();
|
||||
}
|
||||
|
||||
@ -253,10 +238,6 @@ void Plunger::_checkVfdForJam()
|
||||
|
||||
if (_vfd->hasFault())
|
||||
{
|
||||
Log.errorln("[%s] JAMMED (VFD FAULT)! VFD reports fault code %d. State: %s, FillState: %s",
|
||||
name.c_str(), _vfd->getFaultCode(),
|
||||
_plungerStateToString(_currentState),
|
||||
_currentState == PlungerState::FILLING ? _fillStateToString(_currentFillState) : "N/A");
|
||||
_transitionToState(PlungerState::JAMMED);
|
||||
return;
|
||||
}
|
||||
@ -265,9 +246,6 @@ void Plunger::_checkVfdForJam()
|
||||
{
|
||||
if (_jammedStartTimeMs != 0)
|
||||
{
|
||||
Log.verboseln("[%s] JAM CHECK: Motor not expected active. Jam timer RESET. State: %s, FillState: %s",
|
||||
name.c_str(), _plungerStateToString(_currentState),
|
||||
_currentState == PlungerState::FILLING ? _fillStateToString(_currentFillState) : "N/A");
|
||||
_jammedStartTimeMs = 0;
|
||||
}
|
||||
return;
|
||||
@ -333,7 +311,6 @@ void Plunger::_checkVfdForJam()
|
||||
{
|
||||
if (_currentState == PlungerState::PLUNGING_AUTO && _settings.enablePostFlow)
|
||||
{
|
||||
Log.infoln("[%s] Jam in PLUNGING_AUTO & postFlow enabled. -> POST_FLOW", name.c_str());
|
||||
_transitionToState(PlungerState::POST_FLOW);
|
||||
}
|
||||
else if (_currentState == PlungerState::FILLING)
|
||||
@ -341,14 +318,12 @@ void Plunger::_checkVfdForJam()
|
||||
_vfdStop();
|
||||
if (_currentFillState == FillState::PLUNGING)
|
||||
{
|
||||
Log.infoln("[%s] Fill: Jam during PLUNGING. -> PLUNGED. Wait %lu ms.", name.c_str(), _settings.fillPlungedWaitDurationMs);
|
||||
_currentFillState = FillState::PLUNGED;
|
||||
_fillSubStateTimer.once_ms(_settings.fillPlungedWaitDurationMs, &Plunger::_fillSubStateTimerRelay, this);
|
||||
_jammedStartTimeMs = 0;
|
||||
}
|
||||
else if (_currentFillState == FillState::HOMING)
|
||||
{
|
||||
Log.infoln("[%s] Fill: Jam during HOMING. -> HOMED. Wait %lu ms.", name.c_str(), _settings.fillHomedWaitDurationMs);
|
||||
_currentFillState = FillState::HOMED;
|
||||
_fillSubStateTimer.once_ms(_settings.fillHomedWaitDurationMs, &Plunger::_fillSubStateTimerRelay, this);
|
||||
_jammedStartTimeMs = 0;
|
||||
@ -377,7 +352,6 @@ short Plunger::loop()
|
||||
Component::loop();
|
||||
_updatePotValues();
|
||||
_checkVfdForJam();
|
||||
|
||||
if (debug_states)
|
||||
{
|
||||
unsigned long currentTimeMs = millis();
|
||||
@ -406,15 +380,7 @@ short Plunger::loop()
|
||||
Log.infoln("[%s] --- END STATE LOG DUMP ---", name.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
if (_currentState == PlungerState::PLUNGING_MANUAL || _currentState == PlungerState::PLUNGING_AUTO)
|
||||
{
|
||||
// _calculatedPlungingSpeedHz is in 0.01Hz units, SAKO_VFD::setFrequency expects Hz
|
||||
// _vfd->setFrequency(static_cast<uint16_t>(_calculatedPlungingSpeedHz / 100.0f));
|
||||
}
|
||||
// _vfd->setFrequency(static_cast<uint16_t>(_currentSpeedPotValue / 100.0f));
|
||||
Joystick::E_POSITION currentJoystickDir = static_cast<Joystick::E_POSITION>(_joystick->getValue());
|
||||
// Generic Max Operation Time Check (Safety Net)
|
||||
if (_operationStartTimeMs > 0 && _currentMaxOperationTimeMs > 0)
|
||||
{
|
||||
bool isMonitoredState = false;
|
||||
@ -542,19 +508,16 @@ short Plunger::cmd_plunge()
|
||||
{
|
||||
if (!_autoModeEnabled)
|
||||
{
|
||||
Log.warningln("[%s] cmd_plunge ignored. Auto mode is disabled.", name.c_str());
|
||||
return 1;
|
||||
}
|
||||
if (_currentState == PlungerState::IDLE)
|
||||
{
|
||||
Log.infoln("[%s] Initiating PLUNGING_AUTO from command.", name.c_str());
|
||||
_vfdStartForward(static_cast<uint16_t>(_calculatedPlungingSpeedHz));
|
||||
_transitionToState(PlungerState::PLUNGING_AUTO);
|
||||
return E_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.warningln("[%s] cmd_plunge ignored. Current state is %d (not IDLE).", name.c_str(), static_cast<int>(_currentState));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -563,19 +526,16 @@ short Plunger::cmd_home()
|
||||
{
|
||||
if (!_autoModeEnabled)
|
||||
{
|
||||
Log.warningln("[%s] cmd_home ignored. Auto mode is disabled.", name.c_str());
|
||||
return 1;
|
||||
}
|
||||
if (_currentState == PlungerState::IDLE)
|
||||
{
|
||||
Log.infoln("[%s] Initiating HOMING_AUTO from command.", name.c_str());
|
||||
_vfdStartReverse(static_cast<uint16_t>(_settings.speedSlowHz * 100.0f));
|
||||
_transitionToState(PlungerState::HOMING_AUTO);
|
||||
return E_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.warningln("[%s] cmd_home ignored. Current state is %d (not IDLE).", name.c_str(), static_cast<int>(_currentState));
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@ -612,39 +572,30 @@ short Plunger::cmd_fill()
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
// New command implementation to save settings
|
||||
short Plunger::cmd_save_settings()
|
||||
{
|
||||
Log.infoln("[%s] cmd_save_settings: Attempting to save current settings to file...", name.c_str());
|
||||
if (_settings.save())
|
||||
{
|
||||
Log.infoln("[%s] Settings successfully saved via command.", name.c_str());
|
||||
return E_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
Log.errorln("[%s] Failed to save settings via command.", name.c_str());
|
||||
return 1; // Or a more specific error code
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Definition for reset
|
||||
short Plunger::reset()
|
||||
{
|
||||
Log.infoln("[%s] reset() called. Stopping VFD, clearing faults, and re-initializing.", name.c_str());
|
||||
_vfdStop();
|
||||
_vfdResetJam();
|
||||
return this->init();
|
||||
}
|
||||
|
||||
// Definition for setAutoModeEnabled
|
||||
void Plunger::setAutoModeEnabled(bool enabled)
|
||||
{
|
||||
_autoModeEnabled = enabled;
|
||||
Log.infoln("[%s] Auto mode %s.", name.c_str(), enabled ? "ENABLED" : "DISABLED");
|
||||
}
|
||||
|
||||
// Definition for isAutoModeEnabled
|
||||
bool Plunger::isAutoModeEnabled() const
|
||||
{
|
||||
return _autoModeEnabled;
|
||||
@ -675,63 +626,38 @@ bool Plunger::updateSettingsFromJson(const JsonObject& json) {
|
||||
Log.errorln("[%s] Failed to update settings from JSON in updateSettingsFromJson.", name.c_str());
|
||||
return false;
|
||||
}
|
||||
Log.infoln("[%s] Settings updated from JSON, attempting to save...", name.c_str());
|
||||
if (!_settings.save()) { // Utilize the existing save method in PlungerSettings
|
||||
if (!_settings.save()) {
|
||||
Log.errorln("[%s] Failed to save updated settings in updateSettingsFromJson.", name.c_str());
|
||||
// Decide if this is a hard failure for the method.
|
||||
// For now, if fromJson succeeded but save failed, we might still return true
|
||||
// as settings are updated in memory, but log the error. Or return false.
|
||||
// Let's return false if save fails, to indicate the full operation wasn't successful.
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
Log.infoln("[%s] Settings successfully updated from JSON and saved.", name.c_str());
|
||||
// After successful update and save, re-initialize parts of Plunger that depend on settings
|
||||
// For example, if _recordedPlungeDurationMs or _calculatedPlungingSpeedHz might change.
|
||||
_recordedPlungeDurationMs = _settings.replayDurationMs;
|
||||
_updatePotValues(); // This recalculates _calculatedPlungingSpeedHz
|
||||
// Consider if a more full re-init or specific re-calculations are needed here.
|
||||
// For now, updating these two common ones.
|
||||
_settings.print();
|
||||
_updatePotValues();
|
||||
return true;
|
||||
}
|
||||
|
||||
short Plunger::cmd_load_default_settings()
|
||||
{
|
||||
Log.infoln("[%s] cmd_load_default_settings: Attempting to load default settings...", name.c_str());
|
||||
if (loadDefaultSettings()) { // Uses default paths defined in Plunger.h declaration
|
||||
Log.infoln("[%s] Default settings loaded and applied successfully via command.", name.c_str());
|
||||
if (loadDefaultSettings()) {
|
||||
return E_OK;
|
||||
} else {
|
||||
Log.errorln("[%s] Failed to load or apply default settings via command.", name.c_str());
|
||||
return 1; // Or a more specific error code
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool Plunger::loadDefaultSettings(const char* defaultPath, const char* operationalPath) {
|
||||
Log.infoln("[%s] Attempting to load settings from default path: %s", name.c_str(), defaultPath);
|
||||
PlungerSettings tempSettings = _settings; // Create a copy to attempt loading into
|
||||
|
||||
PlungerSettings tempSettings = _settings;
|
||||
if (!tempSettings.load(defaultPath)) {
|
||||
Log.errorln("[%s] Failed to load settings from default file: %s", name.c_str(), defaultPath);
|
||||
return false;
|
||||
}
|
||||
Log.infoln("[%s] Successfully loaded settings from %s. Now applying and saving to %s.", name.c_str(), defaultPath, operationalPath);
|
||||
|
||||
_settings = tempSettings; // Apply loaded settings to the main _settings object
|
||||
|
||||
}
|
||||
_settings = tempSettings;
|
||||
if (!_settings.save(operationalPath)) {
|
||||
Log.errorln("[%s] Failed to save the loaded default settings to operational path: %s", name.c_str(), operationalPath);
|
||||
// Depending on requirements, this might still be considered a partial success if memory update is enough.
|
||||
// For now, let's say if save fails, the operation wasn't fully successful.
|
||||
return false;
|
||||
}
|
||||
|
||||
// Re-apply any settings-dependent internal states
|
||||
_recordedPlungeDurationMs = _settings.replayDurationMs;
|
||||
_updatePotValues(); // This recalculates _calculatedPlungingSpeedHz
|
||||
_settings.print(); // Log the newly applied settings
|
||||
|
||||
Log.infoln("[%s] Default settings loaded from %s, applied, and saved to %s.", name.c_str(), defaultPath, operationalPath);
|
||||
_updatePotValues();
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -750,10 +676,7 @@ short Plunger::cmd_replay()
|
||||
{
|
||||
Log.warningln("[%s] cmd_replay ignored. Auto mode is disabled.", name.c_str());
|
||||
return 1;
|
||||
} // Depending on requirements, you might allow replay even if auto mode is off.
|
||||
|
||||
Log.infoln("[%s] Initiating REPLAY from command.", name.c_str());
|
||||
}
|
||||
_transitionToState(PlungerState::REPLAY);
|
||||
// _handleReplayState will start the VFD and set timers.
|
||||
return E_OK;
|
||||
}
|
||||
|
||||
@ -1,8 +1,6 @@
|
||||
#include <ArduinoLog.h>
|
||||
|
||||
#include <modbus/ModbusTypes.h>
|
||||
#include <modbus/ModbusRTU.h>
|
||||
|
||||
#include <components/RS485.h>
|
||||
|
||||
bool RTU_Base::triggerRTUWrite()
|
||||
@ -162,47 +160,35 @@ void RTU_Base::updateState(ModbusRTU &manager)
|
||||
{
|
||||
if (state != ERROR)
|
||||
{
|
||||
// Log.warningln("Device %d timeout detected. Setting state to ERROR.", slaveId);
|
||||
errorCount++;
|
||||
setState(ERROR);
|
||||
}
|
||||
return; // If timeout occurred, don't process further state changes here
|
||||
}
|
||||
|
||||
bool hasPendingOps = manager.hasPendingOperations(slaveId);
|
||||
|
||||
switch (state)
|
||||
{
|
||||
case UNINITIALIZED:
|
||||
// Stays UNINITIALIZED until initialize() is called
|
||||
break;
|
||||
case INITIALIZING:
|
||||
// Transition to IDLE only if initialization writes are complete
|
||||
if (!hasPendingOps)
|
||||
{
|
||||
Log.infoln("Device %d initialization complete. Transitioning to IDLE.", slaveId);
|
||||
setState(IDLE);
|
||||
}
|
||||
// Timeout check handled above
|
||||
break;
|
||||
case IDLE:
|
||||
// Transition to RUNNING if new operations are queued
|
||||
if (hasPendingOps)
|
||||
{
|
||||
setState(RUNNING);
|
||||
}
|
||||
break;
|
||||
case RUNNING:
|
||||
// Transition back to IDLE when operations complete
|
||||
if (!hasPendingOps)
|
||||
{
|
||||
setState(IDLE);
|
||||
}
|
||||
// Timeout check handled above
|
||||
break;
|
||||
case ERROR:
|
||||
// Stays in ERROR until a response is received (handled in handleResponseReceived)
|
||||
// or explicitly reset. Timeout check handled above.
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -77,7 +77,6 @@ struct WiFiNetworkSettings {
|
||||
short loadSettings(JsonObject& doc) {
|
||||
Log.infoln("WiFiNetworkSettings::load - Loading WiFi settings from JSON...");
|
||||
IPAddress tempIp;
|
||||
|
||||
// STA Settings
|
||||
JsonVariant sta_ssid_val = doc["sta_ssid"];
|
||||
if (sta_ssid_val.is<const char*>()) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user