From 26c64d2ed33c4e614c71d929d7110d4b9150e685 Mon Sep 17 00:00:00 2001 From: Emmanuel Hernandez Cruz Date: Wed, 17 Sep 2025 15:15:34 -0500 Subject: [PATCH] Creation of BMS and EPMS folders with copy of the template --- .../CHILLER/CH_Daikin_AWV026B_RTU/README.md | 48 +++++ .../CRAH_LIEBERT_80_125_SLAB_TCP/README.md | 48 +++++ .../State_Fail.cpp | 0 .../State_Running.cpp | 0 .../State_Standby.cpp | 0 .../config.h | 0 .../main.cpp | 0 .../CRAH_PETRA_PAHHC_600_C6_TCP/README.md | 48 +++++ .../State_Fail.cpp | 92 +++++++++ .../State_Running.cpp | 187 ++++++++++++++++++ .../State_Standby.cpp | 135 +++++++++++++ .../CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/config.h | 152 ++++++++++++++ .../CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/main.cpp | 86 ++++++++ src/BMS/VFD/VFD_ABB_ACH580_RTU/README.md | 48 +++++ .../ATS/ATS_Manufacturer_Model_TCP/README.md | 48 +++++ .../ATS_Manufacturer_Model_TCP/State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../ATS/ATS_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../ATS/ATS_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ .../BKR_Manufacturer_Model_TCP/README.md | 48 +++++ .../BKR_Manufacturer_Model_TCP/State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../BKR_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../BKR_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ .../GEN/GEN_Manufacturer_Model_TCP/README.md | 48 +++++ .../GEN_Manufacturer_Model_TCP/State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../GEN/GEN_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../GEN/GEN_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ .../MVG/MVG_Manufacturer_Model_TCP/README.md | 48 +++++ .../MVG_Manufacturer_Model_TCP/State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../MVG/MVG_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../MVG/MVG_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ .../PDU/PDU_Manufacturer_Model_TCP/README.md | 48 +++++ .../PDU_Manufacturer_Model_TCP/State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../PDU/PDU_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../PDU/PDU_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ .../PQM/PQM_Manufacturer_Model_TCP/README.md | 48 +++++ .../PQM_Manufacturer_Model_TCP/State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../PQM/PQM_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../PQM/PQM_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ .../SCP/SCP_Manufacturer_Model_TCP/README.md | 48 +++++ .../SCP_Manufacturer_Model_TCP/State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../SCP/SCP_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../SCP/SCP_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ .../STS/STS_Manufacturer_Model_TCP/README.md | 48 +++++ .../STS_Manufacturer_Model_TCP/State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../STS/STS_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../STS/STS_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ .../UPS/UPS_Manufacturer_Model_TCP/README.md | 48 +++++ .../UPS_Manufacturer_Model_TCP/State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../UPS/UPS_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../UPS/UPS_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ .../XFMR_Manufacturer_Model_TCP/README.md | 48 +++++ .../State_Fail.cpp | 81 ++++++++ .../State_Running.cpp | 89 +++++++++ .../State_Standby.cpp | 85 ++++++++ .../XFMR/XFMR_Manufacturer_Model_TCP/config.h | 152 ++++++++++++++ .../XFMR/XFMR_Manufacturer_Model_TCP/main.cpp | 86 ++++++++ 74 files changed, 6254 insertions(+) create mode 100644 src/BMS/CHILLER/CH_Daikin_AWV026B_RTU/README.md create mode 100644 src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/README.md rename src/BMS/CRAH/{CRAH_PAHHC_600_C6_TCP => CRAH_LIEBERT_80_125_SLAB_TCP}/State_Fail.cpp (100%) rename src/BMS/CRAH/{CRAH_PAHHC_600_C6_TCP => CRAH_LIEBERT_80_125_SLAB_TCP}/State_Running.cpp (100%) rename src/BMS/CRAH/{CRAH_PAHHC_600_C6_TCP => CRAH_LIEBERT_80_125_SLAB_TCP}/State_Standby.cpp (100%) rename src/BMS/CRAH/{CRAH_PAHHC_600_C6_TCP => CRAH_LIEBERT_80_125_SLAB_TCP}/config.h (100%) rename src/BMS/CRAH/{CRAH_PAHHC_600_C6_TCP => CRAH_LIEBERT_80_125_SLAB_TCP}/main.cpp (100%) create mode 100644 src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/README.md create mode 100644 src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Fail.cpp create mode 100644 src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Running.cpp create mode 100644 src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Standby.cpp create mode 100644 src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/config.h create mode 100644 src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/main.cpp create mode 100644 src/BMS/VFD/VFD_ABB_ACH580_RTU/README.md create mode 100644 src/EPMS/ATS/ATS_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/ATS/ATS_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/ATS/ATS_Manufacturer_Model_TCP/main.cpp create mode 100644 src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/main.cpp create mode 100644 src/EPMS/GEN/GEN_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/GEN/GEN_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/GEN/GEN_Manufacturer_Model_TCP/main.cpp create mode 100644 src/EPMS/MVG/MVG_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/MVG/MVG_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/MVG/MVG_Manufacturer_Model_TCP/main.cpp create mode 100644 src/EPMS/PDU/PDU_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/PDU/PDU_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/PDU/PDU_Manufacturer_Model_TCP/main.cpp create mode 100644 src/EPMS/PQM/PQM_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/PQM/PQM_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/PQM/PQM_Manufacturer_Model_TCP/main.cpp create mode 100644 src/EPMS/SCP/SCP_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/SCP/SCP_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/SCP/SCP_Manufacturer_Model_TCP/main.cpp create mode 100644 src/EPMS/STS/STS_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/STS/STS_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/STS/STS_Manufacturer_Model_TCP/main.cpp create mode 100644 src/EPMS/UPS/UPS_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/UPS/UPS_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/UPS/UPS_Manufacturer_Model_TCP/main.cpp create mode 100644 src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/README.md create mode 100644 src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Fail.cpp create mode 100644 src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Running.cpp create mode 100644 src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Standby.cpp create mode 100644 src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/config.h create mode 100644 src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/main.cpp diff --git a/src/BMS/CHILLER/CH_Daikin_AWV026B_RTU/README.md b/src/BMS/CHILLER/CH_Daikin_AWV026B_RTU/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/BMS/CHILLER/CH_Daikin_AWV026B_RTU/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/README.md b/src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/State_Fail.cpp b/src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/State_Fail.cpp similarity index 100% rename from src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/State_Fail.cpp rename to src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/State_Fail.cpp diff --git a/src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/State_Running.cpp b/src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/State_Running.cpp similarity index 100% rename from src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/State_Running.cpp rename to src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/State_Running.cpp diff --git a/src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/State_Standby.cpp b/src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/State_Standby.cpp similarity index 100% rename from src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/State_Standby.cpp rename to src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/State_Standby.cpp diff --git a/src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/config.h b/src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/config.h similarity index 100% rename from src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/config.h rename to src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/config.h diff --git a/src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/main.cpp b/src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/main.cpp similarity index 100% rename from src/BMS/CRAH/CRAH_PAHHC_600_C6_TCP/main.cpp rename to src/BMS/CRAH/CRAH_LIEBERT_80_125_SLAB_TCP/main.cpp diff --git a/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/README.md b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Fail.cpp b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Fail.cpp new file mode 100644 index 0000000..046bf00 --- /dev/null +++ b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Fail.cpp @@ -0,0 +1,92 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + for (const auto& alarmName : activeAlarms){ + addStrategy(alarmName, new SingleValueStrategy(1.0f, 0.0f, 1000)); + } + addStrategy("CW Valve Position", new PIDStrategy("RAT Setpoint", 1000, "RAT")); +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + Modbus_Point* alarmReset = equipment->getModbus_Point("Alarm Reset"); + int nextStateId = alarmReset ? alarmReset->getValue() : 0; + if (nextStateId == 1){ + return new StandbyState(); + } + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); + Modbus_Point* alarm_common = equipment->getModbus_Point("Alarm Common"); + alarm_common->setValue(1); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); + Modbus_Point* alarm_common = equipment->getModbus_Point("Alarm Common"); + alarm_common->setValue(0); +} \ No newline at end of file diff --git a/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Running.cpp b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Running.cpp new file mode 100644 index 0000000..b0f5399 --- /dev/null +++ b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Running.cpp @@ -0,0 +1,187 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { + addStrategy("CW Valve Position", new PIDStrategy("RAT Setpoint", 1000, "RAT")); + addStrategy("Operating Hours EC Fan #1", new TotalizerStrategy(10000)); + addStrategy("Operating Hours EC Fan #2", new TotalizerStrategy(10000)); + addStrategy("Operating Hours EC Fan #3", new TotalizerStrategy(10000)); + addStrategy("Operating Hours EC Fan #4", new TotalizerStrategy(10000)); + addStrategy("Operating Hours EC Fan #5", new TotalizerStrategy(10000)); + addStrategy("Operating Hours EC Fan #6", new TotalizerStrategy(10000)); + addStrategy("Operating Hours EC Fan #7", new TotalizerStrategy(10000)); + addStrategy("Operating Hours EC Fan #8", new TotalizerStrategy(10000)); + addStrategy("Operating Hours EC Fan #9", new TotalizerStrategy(10000)); +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + Modbus_Point* On_Off_Command = equipment->getModbus_Point("ON/OFF Command By BMS"); + int nextStateId = On_Off_Command ? On_Off_Command->getValue() : 0; + Serial.println(nextStateId); + if (nextStateId == 0){ + return new StandbyState(); + } + + Modbus_Point* faultCode = equipment->getModbus_Point("Fault Code"); + int faultCodeValue = faultCode ? faultCode->getValue() : 0; + switch (faultCodeValue){ + case 1: + return new FailState({"Alarm SAT Sensor Fault"}); + case 2: + return new FailState({"Alarm RAH Sensor Fault"}); + case 3: + return new FailState({"Alarm RAT Sensor Fault"}); + case 4: + return new FailState({"Alarm Filter DP Sensor Fault"}); + case 5: + return new FailState({"Alarm Flooding"}); + case 6: + return new FailState({"Alarm Dirty Filter"}); + case 7: + return new FailState({"Alarm High RAT"}); + case 8: + return new FailState({"Alarm Low RAT"}); + case 9: + return new FailState({"Alarm High SAT"}); + case 10: + return new FailState({"Alarm Low SAT"}); + case 11: + return new FailState({"Alarm High RAH"}); + case 12: + return new FailState({"Alarm Low RAH"}); + case 13: + return new FailState({"Alarm Phase Failure"}); + case 14: + return new FailState({"Alarm Condensate Pump"}); + case 15: + return new FailState({"Alarm Smoke"}); + case 16: + return new FailState({"Alarm Fire"}); + case 17: + return new FailState({"Alarm EC Fan #1"}); + case 18: + return new FailState({"Alarm EC Fan #2"}); + case 19: + return new FailState({"Alarm EC Fan #3"}); + case 20: + return new FailState({"Alarm EC Fan #4"}); + case 21: + return new FailState({"Alarm EC Fan #5"}); + case 22: + return new FailState({"Alarm EC Fan #6"}); + case 23: + return new FailState({"Alarm EC Fan #7"}); + case 24: + return new FailState({"Alarm EC Fan #8"}); + case 25: + return new FailState({"Alarm EC Fan #9"}); + default: + break; + } + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + + const std::vector 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) { + Modbus_Point* point = equipment->getModbus_Point(desc); + if (point) { + point->setValue(1); + } + } +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + const std::vector 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) { + Modbus_Point* point = equipment->getModbus_Point(desc); + if (point) { + point->setValue(0); + } + } +} \ No newline at end of file diff --git a/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Standby.cpp b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Standby.cpp new file mode 100644 index 0000000..1ee0de6 --- /dev/null +++ b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/State_Standby.cpp @@ -0,0 +1,135 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::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)); + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* 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(); + } + + + + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state +// A list of all alarm descriptions + const std::vector alarmDescriptions = { + "Alarm SAT Sensor Fault", "Alarm RAH Sensor Fault", "Alarm RAT Sensor Fault", + "Alarm Filter DP Sensor Fault", "Alarm Flooding", "Alarm Dirty Filter", + "Alarm High RAT", "Alarm Low RAT", "Alarm High SAT", "Alarm Low SAT", + "Alarm High RAH", "Alarm Low RAH", "Alarm Common", "Alarm Phase Failure", + "Alarm Condensate Pump", "Alarm Smoke", "Alarm Fire", "Alarm EC Fan #1", + "Alarm EC Fan #2", "Alarm EC Fan #3", "Alarm EC Fan #4", "Alarm EC Fan #5", + "Alarm EC Fan #6", "Alarm EC Fan #7", "Alarm EC Fan #8", "Alarm EC Fan #9" + }; + + // A list of all motor run status descriptions + const std::vector 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 alarms to 0 + for (const auto& desc : alarmDescriptions) { + Modbus_Point* point = equipment->getModbus_Point(desc); + if (point) { + point->setValue(0); + } + } + + // Loop through and set all motor statuses to 0 + for (const auto& desc : motorStatusDescriptions) { + Modbus_Point* point = equipment->getModbus_Point(desc); + if (point) { + point->setValue(0); + } + } +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/config.h b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/main.cpp b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/BMS/CRAH/CRAH_PETRA_PAHHC_600_C6_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/BMS/VFD/VFD_ABB_ACH580_RTU/README.md b/src/BMS/VFD/VFD_ABB_ACH580_RTU/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/BMS/VFD/VFD_ABB_ACH580_RTU/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/README.md b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/config.h b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/main.cpp b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/ATS/ATS_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/README.md b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/config.h b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/main.cpp b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/Breaker/BKR_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/README.md b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/config.h b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/main.cpp b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/GEN/GEN_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/README.md b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/config.h b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/main.cpp b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/MVG/MVG_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/README.md b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/config.h b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/main.cpp b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/PDU/PDU_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/README.md b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/config.h b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/main.cpp b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/PQM/PQM_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/README.md b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/config.h b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/main.cpp b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/SCP/SCP_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/EPMS/STS/STS_Manufacturer_Model_TCP/README.md b/src/EPMS/STS/STS_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/STS/STS_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/STS/STS_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/STS/STS_Manufacturer_Model_TCP/config.h b/src/EPMS/STS/STS_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/STS/STS_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/STS/STS_Manufacturer_Model_TCP/main.cpp b/src/EPMS/STS/STS_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/STS/STS_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/README.md b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/config.h b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/main.cpp b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/UPS/UPS_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +} diff --git a/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/README.md b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/README.md new file mode 100644 index 0000000..4f156e3 --- /dev/null +++ b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/README.md @@ -0,0 +1,48 @@ +# Daikin Chiller (RTU) Emulator + +This project is an Arduino-based emulator for a Daikin Chiller unit, communicating over Modbus RTU. It is designed to be a flexible template that can be adapted to simulate different types of chillers by modifying the configuration and state logic. + +The emulator operates on a state machine with three core states: +* **Standby**: The chiller is idle but ready. +* **Running**: The chiller is active and operational. +* **Fail**: The chiller has encountered a fault condition. + +## Features + +* **Modbus RTU Communication**: Emulates a Modbus slave device. +* **State Machine Logic**: Simulates different operational states (Standby, Running, Fail). +* **Dynamic Value Simulation**: Uses "Strategies" (e.g., PID, Ramp) to generate realistic, changing values for Modbus points. +* **Configurable Modbus Map**: The entire Modbus register map is defined in a single, easy-to-modify file (`config.h`). +* **Extensible Design**: The structure allows for the addition of new states and behaviors. + +## Hardware Prerequisites + +The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities and at least one hardware serial port for RS485 communication. + +* **Microcontroller**: ESP8266, ESP32, or similar. +* **RS485 Transceiver**: A module like the MAX485 to interface with the Modbus RTU bus. + +## Software Dependencies + +This project relies on a Modbus library. Ensure you have the correct library installed in your Arduino IDE. + +* **Modbus Library**: The code uses a library that provides `ModbusRTU.h` and optionally `ModbusIP_ESP8266.h`. + +--- + +## How to Customize for a New Chiller + +To adapt this template for a new chiller, follow these steps. + +### 1. Configure Device-Specific Parameters (`config.h`) + +Open `CH_Daikin_AWV026B_RTU/config.h`. This is the main file for device-specific settings. + +#### Modbus RTU Settings +Update the following constants for your device's serial communication setup. +```c++ +const int BAUDRATE = 19200; // The serial communication speed +const int RX_PIN = 17; // The GPIO pin for receiving data (RX) +const int TX_PIN = 16; // The GPIO pin for transmitting data (TX) +const int RST_PIN = 4; // The GPIO pin for RS485 direction control +const int MODBUS_ID = 1; // The unique slave ID for this device \ No newline at end of file diff --git a/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Fail.cpp b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Fail.cpp new file mode 100644 index 0000000..8bc0385 --- /dev/null +++ b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Fail.cpp @@ -0,0 +1,81 @@ +/** + * @file State_Fail.cpp + * @brief Implementation of the FailState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the FailState, which defines + * the behavior of the equipment when it has entered a fault condition. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new FailState object with a list of active alarms. + * + * 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::FailState(const std::vector& activeAlarms) { + // Simulate a failure: set common alarm and a specific fan alarm. + + +} + +/** + * @brief Executes the fail state's logic for one update cycle. + * + * 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 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. + */ +template<> +State* FailState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Fail update function"); + + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the fail state. + * Sets the "Alarm Common" point to 1 to indicate a general fault condition. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Fail State..."); +} + +/** + * @brief Logic to execute once when exiting the fail state. + * Clears the "Alarm Common" point to 0 before transitioning to the next state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void FailState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Fail State..."); +} \ No newline at end of file diff --git a/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Running.cpp b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Running.cpp new file mode 100644 index 0000000..b9e4a99 --- /dev/null +++ b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Running.cpp @@ -0,0 +1,89 @@ +/** + * @file State_Running.cpp + * @brief Implementation of the RunningState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the RunningState, which defines + * the behavior of the equipment when it is actively running. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "Strategies/Strategy_Totalizer.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif + +/** + * @brief Constructs a new RunningState object. + * + * 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::RunningState() { +} + +/** + * @brief Executes the running state's logic for one update cycle. + * + * 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. + */ +template<> +State* RunningState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Running update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @brief Logic to execute once when entering the running state. + * 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::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Running State..."); + // You could also update a Modbus register to show the "standby" state + +} + +/** + * @brief Logic to execute once when exiting the running state. + * 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::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Running State..."); + +} \ No newline at end of file diff --git a/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Standby.cpp b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Standby.cpp new file mode 100644 index 0000000..20029d1 --- /dev/null +++ b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/State_Standby.cpp @@ -0,0 +1,85 @@ +/** + * @file State_Standby.cpp + * @brief Implementation of the StandbyState class. + * @author Emmanuel Hernandez Cruz + * @date 2025-09-05 + * + * This file contains the implementation for the StandbyState, which defines + * the behavior of the equipment when it is in an idle or standby mode. + */ +#include "ModbusPoints/Modbus_Point.h" +#include "ModbusPoints/Modbus_FloatDecorator.h" +#include "Equipment/Equipment.h" +#include "Strategies/Strategy_Ramp.h" +#include "Strategies/Strategy_Random.h" +#include "Strategies/Strategy_Saw.h" +#include "Strategies/Strategy_SingleValue.h" +#include "Strategies/Strategy_Square.h" +#include "Strategies/Strategy_PID.h" +#include "States/State_Standby.h" +#include "States/State_Running.h" +#include "States/State_Fail.h" +#include "States/State.h" +#include +#include +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +/** + * @brief Constructs a new StandbyState object. + * + * 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::StandbyState() { + // You can add initialization code here if needed + + +} + +/** + * @brief Executes the standby state's logic for one update cycle. + * + * This method applies the strategies defined for the standby state (e.g., + * ramping values to zero). + * + * @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* StandbyState::update(Equipment* equipment) { + // STATE control, add conditions if change to a different state is needed + Serial.println("Standby update function"); + + // Apply any strategies defined for the standby state + _applyStrategies(equipment); + return nullptr; +} + +/** + * @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<> +void StandbyState::enterState(Equipment* equipment) { + // Logic to run when the equipment enters this state + Serial.println("Enter Standby State..."); +} + +/** + * @brief Logic to execute once when exiting the standby state. + * @param equipment Pointer to the Equipment instance. + */ +template<> +void StandbyState::exitState(Equipment* equipment) { + // Cleanup logic to run when the equipment leaves this state + Serial.println("Exit Standby State..."); +} \ No newline at end of file diff --git a/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/config.h b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/config.h new file mode 100644 index 0000000..bd4ad09 --- /dev/null +++ b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/config.h @@ -0,0 +1,152 @@ +/** + * @file config.h + * @brief Main configuration file for the CRAH Unit (TCP) 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. + */ + +#ifndef CONFIG_H +#define CONFIG_H + +#include "core.h" +#include "Equipment/Equipment.h" + +#if defined(USE_MODBUS_IP) +/** + * @defgroup ModbusTCPConfig Modbus IP Configuration + * @brief Parameters for Modbus TCP communication. + * @{ + */ + #include + const char *ssid = "wifi_name"; /**< @brief The SSID of the WiFi network. */ + const char *password = "wifi_password"; /**< @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 + 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 + + + +/** + * @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. + * The `description` field is crucial as it's used to look up points within the application logic. + */ +modbusMap mb_map[] = +{ + {HR, 15, 0, "State Control"}, //Internal to control from Modscan + {HR, 16, 0, "Fault Code"}, + {HR_FLOAT, 18, 0, "RAT"}, //Internal Fault code from Modscan + {HR_FLOAT, 1, 0, "SAT Setpoint"}, + {HR_FLOAT, 681, 0, "RAT Setpoint"}, + {HR_FLOAT, 111, 0, "High RAT Limit"}, + {HR_FLOAT, 114, 0, "Low RAT Limit"}, + {HR_FLOAT, 118, 0, "High SAT Limit"}, + {HR_FLOAT, 122, 0, "Low SAT Limit"}, + {HR_FLOAT, 685, 0, "High RAH Limit"}, + {HR_FLOAT, 689, 0, "Low RAH Limit"}, + {HR, 5, 0, "Setting the EC Fan Max Speed"}, + {HR, 695, 0, "Setting the EC Fan Min Speed"}, + {HR_FLOAT, 693, 0, "Setting Room Temp"}, + {HR, 691, 0, "Setting EC Fan Speed "}, + {DI, 146, 0, "Alarm SAT Sensor Fault"}, + {DI, 1246, 0, "Alarm RAH Sensor Fault"}, + {DI, 1245, 0, "Alarm RAT Sensor Fault"}, + {DI, 1250, 0, "Alarm Filter DP Sensor Fault"}, + {DI, 51, 0, "Alarm Flooding"}, + {DI, 1096, 0, "Alarm Dirty Filter"}, + {DI, 1367, 0, "Alarm High RAT"}, + {DI, 1099, 0, "Alarm Low RAT"}, + {DI, 118, 0, "Alarm High SAT"}, + {DI, 122, 0, "Alarm Low SAT"}, + {DI, 1307, 0, "Alarm High RAH"}, + {DI, 1308, 0, "Alarm Low RAH"}, + {DI, 1342, 0, "Alarm Common"}, + {DI, 148, 0, "Alarm Phase Failure"}, + {DI, 1370, 0, "Alarm Condensate Pump"}, + {DI, 1368, 0, "Alarm Smoke"}, + {DI, 1369, 0, "Alarm Fire"}, + {DI, 131, 0, "Alarm EC Fan #1"}, + {DI, 132, 0, "Alarm EC Fan #2"}, + {DI, 133, 0, "Alarm EC Fan #3"}, + {DI, 134, 0, "Alarm EC Fan #4"}, + {DI, 135, 0, "Alarm EC Fan #5"}, + {DI, 136, 0, "Alarm EC Fan #6"}, + {DI, 1360, 0, "Alarm EC Fan #7"}, + {DI, 1361, 0, "Alarm EC Fan #8"}, + {DI, 1362, 0, "Alarm EC Fan #9"}, + {DI, 138, 0, "Run Status EC Fan #1"}, + {DI, 139, 0, "Run Status EC Fan #2"}, + {DI, 140, 0, "Run Status EC Fan #3"}, + {DI, 141, 0, "Run Status EC Fan #4"}, + {DI, 142, 0, "Run Status EC Fan #5"}, + {DI, 143, 0, "Run Status EC Fan #6"}, + {DI, 1363, 0, "Run Status EC Fan #7"}, + {DI, 1364, 0, "Run Status EC Fan #8"}, + {DI, 1365, 0, "Run Status EC Fan #9"}, + {IR_FLOAT, 99, 0, "SAT Reading"}, + {IR_FLOAT, 70, 0, "RAH Reading"}, + {IR_FLOAT, 101, 0, "RAT Reading"}, + {IR_FLOAT, 106, 0, "Filter DP Reading"}, + {IR_FLOAT, 496, 0, "CW Valve Position"}, + {IR, 53, 0, "Speed EC Fan #1"}, + {IR, 228, 0, "Speed EC Fan #2"}, + {IR, 229, 0, "Speed EC Fan #3"}, + {IR, 230, 0, "Speed EC Fan #4"}, + {IR, 231, 0, "Speed EC Fan #5"}, + {IR, 232, 0, "Speed EC Fan #6"}, + {IR, 678, 0, "Speed EC Fan #7"}, + {IR, 679, 0, "Speed EC Fan #8"}, + {IR, 680, 0, "Speed EC Fan #9"}, + {IR, 274, 0, "Operating Hours EC Fan #1"}, + {IR, 233, 0, "Operating Hours EC Fan #2"}, + {IR, 244, 0, "Operating Hours EC Fan #3"}, + {IR, 235, 0, "Operating Hours EC Fan #4"}, + {IR, 236, 0, "Operating Hours EC Fan #5"}, + {IR, 245, 0, "Operating Hours EC Fan #6"}, + {IR, 486, 0, "Operating Hours EC Fan #7"}, + {IR, 487, 0, "Operating Hours EC Fan #8"}, + {IR, 488, 0, "Operating Hours EC Fan #9"}, + {COIL, 301, 0, "ON/OFF Command By BMS"}, + {COIL, 302, 0, "Enable Off By Supervisory"}, + {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 diff --git a/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/main.cpp b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/main.cpp new file mode 100644 index 0000000..286a98c --- /dev/null +++ b/src/EPMS/XFMR/XFMR_Manufacturer_Model_TCP/main.cpp @@ -0,0 +1,86 @@ +/** + * @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 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 TCP server. + * - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h. + * + * The loop() function continuously: + * - 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 Strategies/Strategy_Behavior.h for value generation strategies. + * @see Modbus_Point.h for the base class for all Modbus points. + */ +//================================================================================================================================= +//Libraries and declaration of variables. +#include +#include "config.h" +#include "ModbusPoints/Modbus_PointFactory.h" +#if defined(USE_MODBUS_IP) + #include +#else + #include +#endif +//================================================================================================================================= +/** + * @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`. + */ +void setup() { + Serial.begin(115200); //Serial comm start + WiFi.config(local_IP, gateway, subnet); // Wifi service start + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + delay(1000); + Serial.print("."); + } + Serial.println("Connected!!"); + mb.server(); //Modbus server start + Serial.println("Server Created"); + Serial.println(map_size); + for(int i = 0; i < map_size; i++){ + Modbus_Point* point = createModbus_Point(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description); + if (point) { + point->addToModbusServer(); + EquipmentInstance.addModbus_Point(mb_map[i].description, point); + } + } + Serial.println("All modbus Points created"); + Serial.println("Setup function ended"); +} +//================================================================================================================================= +/** + * @brief The main application loop. + * @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(); + unsigned long currentMillis = millis(); + if (currentMillis - previousMillis >= interval) { + previousMillis = currentMillis; + unsigned long startTime = millis(); + EquipmentInstance.update(); + unsigned long endTime = millis(); + unsigned long elapsedTime = endTime - startTime; + Serial.printf("Control Execution time: %d ms\n", elapsedTime); + } +}