Documentation updated
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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<>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user