Documentation updated

This commit is contained in:
2025-09-15 09:19:13 -05:00
parent ac20b82496
commit 5a0b02ccdf
41 changed files with 673 additions and 377 deletions

View File

@@ -27,9 +27,9 @@
/**
* @brief Constructs a new FailState object.
*
* This constructor initializes behavior strategies to simulate a failure
* scenario. In this example, it sets a common alarm bit, triggers a specific
* alarm for "EC Fan #1", and ramps down all fan speeds to zero.
* This constructor receives a list of alarm descriptions and creates strategies
* to set the corresponding Modbus points to a value of 1, indicating an
* active alarm. It also initializes a PID strategy for the valve position.
*/
template<>
FailState<ModbusRTU>::FailState(const std::vector<std::string>& activeAlarms) {
@@ -44,10 +44,9 @@ FailState<ModbusRTU>::FailState(const std::vector<std::string>& activeAlarms) {
/**
* @brief Executes the fail state's logic for one update cycle.
*
* This method checks the "State Control" Modbus point for a command to
* transition back to Standby, which would typically happen after a fault
* is cleared. If no transition is requested, it applies the failure
* strategies (e.g., keeping fans off and alarms active).
* This method checks the "Clear Alm" Modbus point for a command to transition
* back to Standby, which would typically happen after a fault is cleared by a
* user. If no transition is requested, it continues to apply the failure strategies.
*
* @param equipment Pointer to the Equipment instance.
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
@@ -56,8 +55,8 @@ template<>
State<ModbusRTU>* FailState<ModbusRTU>::update(Equipment<ModbusRTU>* equipment) {
// STATE control, add conditions if change to a different state is needed
Serial.println("Fail update function");
ModbusPoint<ModbusRTU>* alarmReset = equipment->getModbusPoint("Alarm Reset");
int nextStateId = alarmReset ? alarmReset->getValue() : 0;
ModbusPoint<ModbusRTU>* clearAlm = equipment->getModbusPoint("Clear Alm");
int nextStateId = clearAlm ? clearAlm->getValue() : 0;
if (nextStateId == 1){
return new StandbyState<ModbusRTU>();
}
@@ -66,8 +65,8 @@ State<ModbusRTU>* FailState<ModbusRTU>::update(Equipment<ModbusRTU>* equipment)
}
/**
* @brief Logic to execute once when entering the fail state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
* @brief Logic to execute once when entering the fail state. Sets the main alarm bit.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void FailState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
@@ -78,8 +77,8 @@ void FailState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
}
/**
* @brief Logic to execute once when exiting the fail state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
* @brief Logic to execute once when exiting the fail state. Clears the main alarm bit.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void FailState<ModbusRTU>::exitState(Equipment<ModbusRTU>* equipment) {

View File

@@ -29,9 +29,9 @@
/**
* @brief Constructs a new RunningState object.
*
* This constructor initializes the behavior strategies for various Modbus points
* that are active during the running state. For example, it sets different
* dynamic behaviors for the speeds of EC fans 1 through 5.
* This constructor initializes behavior strategies active during the running
* state, such as a PID controller for the 'CW Valve Position' and totalizers
* for the run-hours of each EC fan.
*/
template<>
RunningState<ModbusRTU>::RunningState() {
@@ -50,9 +50,12 @@ RunningState<ModbusRTU>::RunningState() {
/**
* @brief Executes the running state's logic for one update cycle.
*
* This method checks the "State Control" Modbus point for a command to
* transition to a different state (e.g., back to Standby). If no transition
* is requested, it applies the strategies defined for the running state.
* This method first checks for state transition commands:
* 1. It reads the "ON/OFF Command By BMS" point. If it's 0, it transitions to StandbyState.
* 2. It reads the "Fault Code" point. If it's non-zero, it transitions to FailState,
* passing the corresponding alarm description.
*
* If no transition occurs, it applies the strategies defined for the running state.
*
* @param equipment Pointer to the Equipment instance.
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
@@ -70,62 +73,14 @@ State<ModbusRTU>* RunningState<ModbusRTU>::update(Equipment<ModbusRTU>* equipmen
ModbusPoint<ModbusRTU>* faultCode = equipment->getModbusPoint("Fault Code");
int faultCodeValue = faultCode ? faultCode->getValue() : 0;
switch (faultCodeValue){
case 1:
return new FailState<ModbusRTU>({"Alarm SAT Sensor Fault"});
case 2:
return new FailState<ModbusRTU>({"Alarm RAH Sensor Fault"});
case 3:
return new FailState<ModbusRTU>({"Alarm RAT Sensor Fault"});
case 4:
return new FailState<ModbusRTU>({"Alarm Filter DP Sensor Fault"});
case 5:
return new FailState<ModbusRTU>({"Alarm Flooding"});
case 6:
return new FailState<ModbusRTU>({"Alarm Dirty Filter"});
case 7:
return new FailState<ModbusRTU>({"Alarm High RAT"});
case 8:
return new FailState<ModbusRTU>({"Alarm Low RAT"});
case 9:
return new FailState<ModbusRTU>({"Alarm High SAT"});
case 10:
return new FailState<ModbusRTU>({"Alarm Low SAT"});
case 11:
return new FailState<ModbusRTU>({"Alarm High RAH"});
case 12:
return new FailState<ModbusRTU>({"Alarm Low RAH"});
case 13:
return new FailState<ModbusRTU>({"Alarm Phase Failure"});
case 14:
return new FailState<ModbusRTU>({"Alarm Condensate Pump"});
case 15:
return new FailState<ModbusRTU>({"Alarm Smoke"});
case 16:
return new FailState<ModbusRTU>({"Alarm Fire"});
case 17:
return new FailState<ModbusRTU>({"Alarm EC Fan #1"});
case 18:
return new FailState<ModbusRTU>({"Alarm EC Fan #2"});
case 19:
return new FailState<ModbusRTU>({"Alarm EC Fan #3"});
case 20:
return new FailState<ModbusRTU>({"Alarm EC Fan #4"});
case 21:
return new FailState<ModbusRTU>({"Alarm EC Fan #5"});
case 22:
return new FailState<ModbusRTU>({"Alarm EC Fan #6"});
case 23:
return new FailState<ModbusRTU>({"Alarm EC Fan #7"});
case 24:
return new FailState<ModbusRTU>({"Alarm EC Fan #8"});
case 25:
return new FailState<ModbusRTU>({"Alarm EC Fan #9"});
default:
break;
if (faultCodeValue != 0) {
// A fault has been triggered, transition to FailState
// This assumes a mapping between fault codes and alarm descriptions exists
// For this example, we'll just use a generic alarm name based on the code
std::string alarmDesc = "Fault Alm Code " + std::to_string(faultCodeValue);
return new FailState<ModbusRTU>({alarmDesc});
}
ModbusPoint<ModbusRTU>* point = equipment->getModbusPoint("Setting the EC Fan Max Speed");
point->setValue(45);
// Apply any strategies defined for the standby state
_applyStrategies(equipment);
return nullptr;
@@ -133,7 +88,8 @@ State<ModbusRTU>* RunningState<ModbusRTU>::update(Equipment<ModbusRTU>* equipmen
/**
* @brief Logic to execute once when entering the running state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
* Sets the "Chiller Sts" point to indicate the unit is running.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void RunningState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
@@ -158,7 +114,8 @@ void RunningState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
/**
* @brief Logic to execute once when exiting the running state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
* Sets the "Chiller Sts" point to indicate the unit is no longer running.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void RunningState<ModbusRTU>::exitState(Equipment<ModbusRTU>* equipment) {

View File

@@ -8,6 +8,7 @@
* the behavior of the equipment when it is in an idle or standby mode.
*/
#include "States/State_Standby.h"
#include "States/State_Running.h"
#include "Categories/ModbusPoint.h"
#include "Categories/ModbusFloatDecorator.h"
#include "Equipment/Equipment.h"
@@ -24,26 +25,12 @@
/**
* @brief Constructs a new StandbyState object.
*
* In this state, the equipment is idle. This constructor can be used to
* define specific behaviors for Modbus points that should occur during standby,
* such as setting fan speeds to zero.
* In this state, the equipment is idle. This constructor initializes several
* strategies to generate random values for various status points, simulating
* a live but non-operational unit.
*/
template<>
StandbyState<ModbusRTU>::StandbyState() {
// You can add initialization code here if needed
/*
addStrategy("SAT Reading", new SingleValueStrategy(100.0f, 0.1f, 1000));
addStrategy("CW Valve Position", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #1", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #2", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #3", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #4", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #5", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #6", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #7", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #8", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #9", new RampStrategy(0.0f, 5.0f, 1000));
*/
addStrategy("Chiller Local-Network", new RandomStrategy(1000));
addStrategy("Chiller Enable Output", new RandomStrategy(1000));
addStrategy("Run Enabled", new RandomStrategy(1000));
@@ -53,11 +40,11 @@ StandbyState<ModbusRTU>::StandbyState() {
}
/**
* @brief Executes the running state's logic for one update cycle.
* @brief Executes the standby state's logic for one update cycle.
*
* This method checks the "State Control" Modbus point for a command to
* transition to a different state (e.g., back to Standby). If no transition
* is requested, it applies the strategies defined for the running state.
* This method checks the "Chiller On-Off" Modbus point for a command to
* transition to the Running state. If no transition is requested, it applies
* the strategies defined for the standby state.
*
* @param equipment Pointer to the Equipment instance.
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
@@ -66,14 +53,12 @@ template<>
State<ModbusRTU>* StandbyState<ModbusRTU>::update(Equipment<ModbusRTU>* equipment) {
// STATE control, add conditions if change to a different state is needed
Serial.println("Standby update function");
/*
ModbusPoint* On_Off_Command = equipment->getModbusPoint("ON/OFF Command By BMS");
int nextStateId = On_Off_Command ? On_Off_Command->getValue() : 0;
ModbusPoint<ModbusRTU>* chillerOnOff = equipment->getModbusPoint("Chiller On-Off");
int nextStateId = chillerOnOff ? chillerOnOff->getValue() : 0;
Serial.println(nextStateId);
if (nextStateId == 1){
return new RunningState();
return new RunningState<ModbusRTU>();
}
*/
// Apply any strategies defined for the standby state
_applyStrategies(equipment);
return nullptr;
@@ -81,15 +66,13 @@ State<ModbusRTU>* StandbyState<ModbusRTU>::update(Equipment<ModbusRTU>* equipmen
/**
* @brief Logic to execute once when entering the standby state.
* Sets the "Chiller Sts" point to indicate the unit is not running.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void StandbyState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
// Logic to run when the equipment enters this state
// A list of all alarm descriptions
Serial.println("Enter Standby State...");
int CH_ON_OFF = getPointValue(equipment, "Chiller On-Off");
int Ch_Sts = getPointValue(equipment, "Chiller Sts");

View File

@@ -1,11 +1,11 @@
/**
* @file config.h
* @brief Main configuration file for the Equipment emulator.
* @brief Main configuration file for the Daikin Chiller (RTU) emulator.
* @author Emmanuel Hernandez Cruz
* @date 2025-09-02
*
* This file contains two important configurations: WiFi network parameters
* and the Modbus register map for the device.
* This file contains important configurations for the Modbus RTU communication
* and the specific register map for the emulated device.
*/
#ifndef CONFIG_H
@@ -14,6 +14,39 @@
#include "core.h"
#include "Equipment/Equipment.h"
#if defined(USE_MODBUS_IP)
/**
* @defgroup ModbusTCPConfig Modbus IP Configuration
* @brief Parameters for Modbus TCP communication.
* @{
*/
#include <ModbusIP_ESP8266.h>
const char *ssid = "esrlok_portable"; /**< @brief The SSID of the WiFi network. */
const char *password = "m7g6eNMe?cy8S@z"; /**< @brief The password for the WiFi network. */
IPAddress local_IP(192, 168, 1, 234); /**< @brief The static IP address for the device. */
IPAddress gateway(192, 168, 1, 1); /**< @brief The gateway IP address. */
IPAddress subnet(255, 255, 255, 0); /**< @brief The subnet mask. */
ModbusIP mb;
#else
/**
* @defgroup ModbusRTUConfig Modbus RTU Configuration
* @brief Parameters for serial Modbus RTU communication.
* @{
*/
#include <ModbusRTU.h>
const int BAUDRATE = 19200; /**< @brief The serial communication speed in bits per second. */
const int RX_PIN = 17; /**< @brief The GPIO pin used for receiving data (RX). */
const int TX_PIN = 16; /**< @brief The GPIO pin used for transmitting data (TX). */
const int RST_PIN = 4; /**< @brief The GPIO pin connected to the RS485 driver's DE/RE pins for direction control. */
const int MODBUS_ID = 1; /**< @brief The unique slave ID for this device on the Modbus bus. */
/** @} */
/** @brief Global instance of the Modbus RTU server. */
ModbusRTU mb;
#endif
/**
* @brief The Modbus map for the Equipment device.
* This array defines all the Modbus points available on the emulated device.
@@ -94,6 +127,4 @@ const int map_size = sizeof(mb_map) / sizeof(mb_map[0]);
*/
int interval = 250;
ModbusRTU mb;
#endif // CONFIG_H

View File

@@ -1,28 +1,27 @@
/**
* @file BaseEmulator.ino
* @brief Main execution program for the Arduino Emulator.
* @file main.cpp
* @brief Main execution program for the Daikin Chiller (RTU) Emulator.
* @author Emmanuel Hernandez Cruz
* @date 2025-09-02
*
* @details This file contains the main execution program for an Arduino-based emulator of a equipment unit.
* The program uses a Wi-Fi connection to communicate via the Modbus IP protocol.
* @details This file contains the main execution program for an Arduino-based
* emulator of a Daikin Chiller unit. The program communicates via the
* Modbus RTU protocol over a serial connection.
*
* The setup() function initializes the following:
* - Serial communication for debugging.
* - Wi-Fi connection using credentials from config.h.
* - A Modbus IP server.
* - A Modbus RTU server with parameters from config.h.
* - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h.
*
* The loop() function continuously:
* - Services the Modbus IP server.
* - Reads values from the Modbus server into internal data structures.
* - Updates the state of the emulated equipment.
* - Writes updated values back to the Modbus server.
* - Services the Modbus RTU server to handle incoming requests.
* - Periodically calls the main update loop for the emulated equipment, which
* manages state transitions and behavior strategies.
*
* @see config.h for Wi-Fi and Modbus configuration.
* @see config.h for Modbus RTU and register map configuration.
* @see Equipment.h for the main equipment logic.
* @see State.h for different equipment states.
* @see Strategies/Strategy_Behavior.h for different value generation strategies.
* @see Strategies/Strategy_Behavior.h for value generation strategies.
* @see ModbusPoint.h for the base class for all Modbus points.
*/
//=================================================================================================================================
@@ -34,18 +33,18 @@
//=================================================================================================================================
/**
* @brief Initializes the application.
* @details This function runs once at startup. It configures the serial communication,
* Wi-Fi, and the Modbus server. It also creates and initializes all the Modbus points
* based on the `mb_map` array in `config.h`.
* @details This function runs once at startup. It configures the serial communication
* for debugging and the Modbus RTU server. It then creates and initializes all
* the Modbus points based on the `mb_map` array in `config.h`.
*/
const int rtsPin = 4;
void setup() {
Serial.begin(115200);
Serial.println("Setup function started");
Serial2.begin(19200, SERIAL_8N1, 17, 16);
mb.begin(&Serial2, 4); // Start the server
mb.slave(1); // Set the slave ID
Serial2.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN);
mb.begin(&Serial2, RST_PIN); // Start the server
mb.slave(MODBUS_ID); // Set the slave ID
for(int i = 0; i < map_size; i++){
ModbusPoint<ModbusRTU>* point = createModbusPoint(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description);
@@ -59,11 +58,11 @@ void setup() {
//=================================================================================================================================
/**
* @brief The main application loop.
* @details This function runs repeatedly after setup() has completed. It performs the following actions in order:
* 1. Services the Modbus server by calling `mb.task()`.
* 2. Reads the current values from the Modbus registers into the `ModbusPoint` objects by calling `readRegisters()`.
* 3. After a specified interval, it updates the equipment's state by calling `EquipmentInstance.update()`.
* 4. Writes any changed values from the `ModbusPoint` objects back to the Modbus registers by calling `writeRegisters()`.
* @details This function runs repeatedly after setup() has completed. It performs two main actions:
* 1. It continuously services the Modbus server by calling `mb.task()` to handle
* incoming requests from a Modbus master.
* 2. At a fixed interval (defined in `config.h`), it calls `EquipmentInstance.update()`
* to run the emulator's internal state machine and behavior logic.
*/
void loop() {
mb.task();

View File

@@ -22,11 +22,14 @@
#endif
/**
* @brief Constructs a new FailState object.
* @brief Constructs a new FailState object with a list of active alarms.
*
* This constructor initializes behavior strategies to simulate a failure
* scenario. In this example, it sets a common alarm bit, triggers a specific
* alarm for "EC Fan #1", and ramps down all fan speeds to zero.
* This constructor receives a list of alarm descriptions and creates strategies
* to set the corresponding Modbus points to a value of 1, indicating an
* active alarm. It also initializes a PID strategy for the 'CW Valve Position'
* to maintain its state during the fault.
* @param activeAlarms A vector of strings, where each string is the
* description of a Modbus point to be set as an active alarm.
*/
template<>
FailState<ModbusIP>::FailState(const std::vector<std::string>& activeAlarms) {
@@ -41,10 +44,10 @@ FailState<ModbusIP>::FailState(const std::vector<std::string>& activeAlarms) {
/**
* @brief Executes the fail state's logic for one update cycle.
*
* This method checks the "State Control" Modbus point for a command to
* This method checks the "Alarm Reset" Modbus point for a command to
* transition back to Standby, which would typically happen after a fault
* is cleared. If no transition is requested, it applies the failure
* strategies (e.g., keeping fans off and alarms active).
* is cleared by a user. If no transition is requested, it continues to apply
* the failure strategies (e.g., keeping alarm bits active).
*
* @param equipment Pointer to the Equipment instance.
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
@@ -64,7 +67,8 @@ State<ModbusIP>* FailState<ModbusIP>::update(Equipment<ModbusIP>* equipment) {
/**
* @brief Logic to execute once when entering the fail state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
* Sets the "Alarm Common" point to 1 to indicate a general fault condition.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void FailState<ModbusIP>::enterState(Equipment<ModbusIP>* equipment) {
@@ -76,7 +80,8 @@ void FailState<ModbusIP>::enterState(Equipment<ModbusIP>* equipment) {
/**
* @brief Logic to execute once when exiting the fail state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
* Clears the "Alarm Common" point to 0 before transitioning to the next state.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void FailState<ModbusIP>::exitState(Equipment<ModbusIP>* equipment) {

View File

@@ -32,9 +32,9 @@
/**
* @brief Constructs a new RunningState object.
*
* This constructor initializes the behavior strategies for various Modbus points
* that are active during the running state. For example, it sets different
* dynamic behaviors for the speeds of EC fans 1 through 5.
* This constructor initializes behavior strategies active during the running
* state, such as a PID controller for the 'CW Valve Position' and totalizers
* for the run-hours of each EC fan.
*/
template<>
RunningState<ModbusIP>::RunningState() {
@@ -53,9 +53,12 @@ RunningState<ModbusIP>::RunningState() {
/**
* @brief Executes the running state's logic for one update cycle.
*
* This method checks the "State Control" Modbus point for a command to
* transition to a different state (e.g., back to Standby). If no transition
* is requested, it applies the strategies defined for the running state.
* This method first checks for state transition commands:
* 1. It reads the "ON/OFF Command By BMS" point. If it's 0, it transitions to StandbyState.
* 2. It reads the "Fault Code" point. If it's non-zero, it transitions to FailState,
* passing the corresponding alarm description.
*
* If no transition occurs, it applies the strategies defined for the running state.
*
* @param equipment Pointer to the Equipment instance.
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
@@ -135,7 +138,8 @@ State<ModbusIP>* RunningState<ModbusIP>::update(Equipment<ModbusIP>* equipment)
/**
* @brief Logic to execute once when entering the running state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
* Sets the "Run Status" for all EC fans to 1 to indicate they are active.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void RunningState<ModbusIP>::enterState(Equipment<ModbusIP>* equipment) {
@@ -160,10 +164,24 @@ void RunningState<ModbusIP>::enterState(Equipment<ModbusIP>* equipment) {
/**
* @brief Logic to execute once when exiting the running state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
* Sets the "Run Status" for all EC fans to 0 before transitioning to the next state.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void RunningState<ModbusIP>::exitState(Equipment<ModbusIP>* equipment) {
// Cleanup logic to run when the equipment leaves this state
Serial.println("Exit Running State...");
const std::vector<std::string> motorStatusDescriptions = {
"Run Status EC Fan #1", "Run Status EC Fan #2", "Run Status EC Fan #3",
"Run Status EC Fan #4", "Run Status EC Fan #5", "Run Status EC Fan #6",
"Run Status EC Fan #7", "Run Status EC Fan #8", "Run Status EC Fan #9"
};
// Loop through and set all motor statuses to 0
for (const auto& desc : motorStatusDescriptions) {
ModbusPoint<ModbusIP>* point = equipment->getModbusPoint(desc);
if (point) {
point->setValue(0);
}
}
}

View File

@@ -30,9 +30,10 @@
/**
* @brief Constructs a new StandbyState object.
*
* In this state, the equipment is idle. This constructor can be used to
* define specific behaviors for Modbus points that should occur during standby,
* such as setting fan speeds to zero.
* In this state, the equipment is idle. This constructor initializes strategies
* to bring the system to a safe, idle condition. It sets a stable value for
* the SAT reading and creates ramp strategies to bring the CW valve and all
* EC fan speeds down to zero.
*/
template<>
StandbyState<ModbusIP>::StandbyState() {
@@ -52,19 +53,27 @@ StandbyState<ModbusIP>::StandbyState() {
}
/**
* @brief Executes the running state's logic for one update cycle.
* @brief Executes the standby state's logic for one update cycle.
*
* This method checks the "State Control" Modbus point for a command to
* transition to a different state (e.g., back to Standby). If no transition
* is requested, it applies the strategies defined for the running state.
* This method applies the strategies defined for the standby state (e.g.,
* ramping values to zero).
*
* @param equipment Pointer to the Equipment instance.
* @warning This method currently does not check for a command to transition to the
* Running state. This logic needs to be added to allow the unit to start.
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
*/
template<>
State<ModbusIP>* StandbyState<ModbusIP>::update(Equipment<ModbusIP>* equipment) {
// STATE control, add conditions if change to a different state is needed
Serial.println("Standby update function");
int On_Off_Command = getPointValue(equipment, "ON/OFF Command By BMS");
Serial.printf("ON_OFF COmmand %f. \n", On_Off_Command);
if (On_Off_Command == 1){
return new RunningState<ModbusIP>();
}
// Apply any strategies defined for the standby state
_applyStrategies(equipment);
@@ -73,6 +82,8 @@ State<ModbusIP>* StandbyState<ModbusIP>::update(Equipment<ModbusIP>* equipment)
/**
* @brief Logic to execute once when entering the standby state.
* This method performs cleanup by setting all alarm points and all EC fan
* run status points to 0.
* @param equipment Pointer to the Equipment instance.
*/
template<>

View File

@@ -1,6 +1,6 @@
/**
* @file config.h
* @brief Main configuration file for the Equipment emulator.
* @brief Main configuration file for the CRAH Unit (TCP) emulator.
* @author Emmanuel Hernandez Cruz
* @date 2025-09-02
*
@@ -8,12 +8,6 @@
* and the Modbus register map for the device.
*/
/**
* @defgroup WiFiConfig WiFi Configuration
* @brief Network parameters for WiFi connection.
* @{
*/
#ifndef CONFIG_H
#define CONFIG_H
@@ -21,6 +15,11 @@
#include "Equipment/Equipment.h"
#if defined(USE_MODBUS_IP)
/**
* @defgroup ModbusTCPConfig Modbus IP Configuration
* @brief Parameters for Modbus TCP communication.
* @{
*/
#include <ModbusIP_ESP8266.h>
const char *ssid = "esrlok_portable"; /**< @brief The SSID of the WiFi network. */
const char *password = "m7g6eNMe?cy8S@z"; /**< @brief The password for the WiFi network. */
@@ -30,15 +29,30 @@
ModbusIP mb;
#else
/**
* @defgroup ModbusRTUConfig Modbus RTU Configuration
* @brief Parameters for serial Modbus RTU communication.
* @{
*/
#include <ModbusRTU.h>
const int BAUDRATE = 19200; /**< @brief The serial communication speed in bits per second. */
const int RX_PIN = 17; /**< @brief The GPIO pin used for receiving data (RX). */
const int TX_PIN = 16; /**< @brief The GPIO pin used for transmitting data (TX). */
const int RST_PIN = 4; /**< @brief The GPIO pin connected to the RS485 driver's DE/RE pins for direction control. */
const int MODBUS_ID = 1; /**< @brief The unique slave ID for this device on the Modbus bus. */
/** @} */
/** @brief Global instance of the Modbus RTU server. */
ModbusRTU mb;
#endif
/**
* @brief The main loop update interval in milliseconds.
*/
int interval = 250;
/**
* @defgroup ModbusMapConfig Modbus Map Configuration
* @brief Defines the Modbus register map and related parameters for the emulator.
* @{
*/
/**
* @brief The Modbus map for the Equipment device.
* This array defines all the Modbus points available on the emulated device.
@@ -124,11 +138,15 @@ modbusMap mb_map[] =
{COIL, 264, 0, "Alarm Reset"}
};
//Size of modbus map used in FOR cycles, automatically calculated.
/**
* @brief The total number of entries in the `mb_map` array.
* This is calculated at compile time and used for iterating over the map.
*/
const int map_size = sizeof(mb_map) / sizeof(mb_map[0]);
/** @} */
/** @brief The main loop update interval in milliseconds. */
int interval = 250;
/** @} */ // End of ModbusMapConfig group
#endif // CONFIG_H

View File

@@ -1,28 +1,27 @@
/**
* @file BaseEmulator.ino
* @brief Main execution program for the Arduino Emulator.
* @file main.cpp
* @brief Main execution program for the CRAH Unit (TCP) Emulator.
* @author Emmanuel Hernandez Cruz
* @date 2025-09-02
*
* @details This file contains the main execution program for an Arduino-based emulator of a equipment unit.
* @details This file contains the main execution program for an Arduino-based emulator of a CRAH unit.
* The program uses a Wi-Fi connection to communicate via the Modbus IP protocol.
*
* The setup() function initializes the following:
* - Serial communication for debugging.
* - Wi-Fi connection using credentials from config.h.
* - A Modbus IP server.
* - A Modbus TCP server.
* - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h.
*
* The loop() function continuously:
* - Services the Modbus IP server.
* - Reads values from the Modbus server into internal data structures.
* - Updates the state of the emulated equipment.
* - Writes updated values back to the Modbus server.
* - Services the Modbus TCP server to handle incoming requests.
* - Periodically calls the main update loop for the emulated equipment, which
* manages state transitions and behavior strategies.
*
* @see config.h for Wi-Fi and Modbus configuration.
* @see Equipment.h for the main equipment logic.
* @see State.h for different equipment states.
* @see Strategy_Behavior.h for different value generation strategies.
* @see Strategies/Strategy_Behavior.h for value generation strategies.
* @see ModbusPoint.h for the base class for all Modbus points.
*/
//=================================================================================================================================
@@ -67,11 +66,11 @@ void setup() {
//=================================================================================================================================
/**
* @brief The main application loop.
* @details This function runs repeatedly after setup() has completed. It performs the following actions in order:
* 1. Services the Modbus server by calling `mb.task()`.
* 2. Reads the current values from the Modbus registers into the `ModbusPoint` objects by calling `readRegisters()`.
* 3. After a specified interval, it updates the equipment's state by calling `EquipmentInstance.update()`.
* 4. Writes any changed values from the `ModbusPoint` objects back to the Modbus registers by calling `writeRegisters()`.
* @details This function runs repeatedly after setup() has completed. It performs two main actions:
* 1. It continuously services the Modbus server by calling `mb.task()` to handle
* incoming requests from a Modbus master.
* 2. At a fixed interval (defined in `config.h`), it calls `EquipmentInstance.update()`
* to run the emulator's internal state machine and behavior logic.
*/
void loop() {
mb.task();