Merge pull request #8 from emmanuelsrlok/ecruz/TestTCPDevice

CRAC Munters configuration added
This commit is contained in:
Emmanuel HC
2025-09-24 09:52:17 -05:00
committed by GitHub
7 changed files with 654 additions and 11 deletions

View File

@@ -9,13 +9,7 @@
; https://docs.platformio.org/page/projectconf.html
[platformio]
default_envs = CH_York_XXXXX_RTU ; Select here the name of the configuration you want to download
[env:CH_York_XXXXX_RTU]
platform = espressif32
board = dfrobot_firebeetle2_esp32e
extends = common_env_options
build_src_filter = -<*> +<BMS/CHILLER/CH_York_XXXXX_RTU>
default_envs = CRAC_Munters_SysCooll_TCP ; Select here the name of the configuration you want to download
[env]
upload_port = COM100
@@ -25,7 +19,8 @@ framework = arduino
monitor_speed = 115200
lib_ldf_mode = chain+
lib_compat_mode = soft
;---------------------------------------------------------------------------------------------------
; TEMPLATES
;Base configuration for RTU equipment
[env:Base_RTU]
platform = espressif32
@@ -39,19 +34,26 @@ board = dfrobot_firebeetle2_esp32e
extends = common_env_options
build_flags = -D USE_MODBUS_IP ;Importat configuration, this flags is used to configure the program
build_src_filter = -<*> +<Folder/path/to/equipment> ;Add the specific folder path here
;----------------------------------------------------------------------------------------------------
[env:CRAC_Munters_SysCooll_TCP]
platform = espressif32
board = dfrobot_firebeetle2_esp32e
extends = common_env_options
build_flags = -D USE_MODBUS_IP
build_src_filter = -<*> +<BMS/CRAC/CRAC_Munters_SysCooll_TCP>
[env:Testing_RTU]
platform = espressif32
board = dfrobot_firebeetle2_esp32e
extends = common_env_options
build_src_filter = -<*> +</Testing_RTU> ;Add the specific folder path here
build_src_filter = -<*> +</Testing_RTU>
[env:Testing_TCP]
platform = espressif32
board = dfrobot_firebeetle2_esp32e
extends = common_env_options
build_flags = -D USE_MODBUS_IP ;Importat configuration, this flags is used to configure the program
build_src_filter = -<*> +</Testing_TCP> ;Add the specific folder path here
build_flags = -D USE_MODBUS_IP
build_src_filter = -<*> +</Testing_TCP>
[env:CH_Daikin_AWV026B_RTU]
platform = espressif32
@@ -73,4 +75,9 @@ board = dfrobot_firebeetle2_esp32e
extends = common_env_options
build_src_filter = -<*> +<BMS/VFD/VFD_ABB_ACH580_RTU>
[env:CH_York_XXXXX_RTU]
platform = espressif32
board = dfrobot_firebeetle2_esp32e
extends = common_env_options
build_src_filter = -<*> +<BMS/CHILLER/CH_York_XXXXX_RTU>

View File

@@ -0,0 +1,33 @@
# EQUIPMENT_TYPE MANUFACTURER MODEL TCP
## Brief Introduction
Equipment specifc details that make it different from other devices
## List of Equipmentt
This cofiguration has been used for these models:
* **Model**: 09-15-22
* **Model**: 09-15-23
* **Model**: 09-15-25
## Hardware Prerequisites
The code is written for an ESP8266/ESP32-style microcontroller with WiFi capabilities.
* **Microcontroller**: [Firebeetle 2 ESP32.](https://www.dfrobot.com/product-2231.html)
---
## States and Strategies
Provide a brief description of what variables and strategies were used in this configuraiton
### Standby State
* **Equipment running**: set to 0
* **Common Alarm**: set to 0
* **SAT temperature**: set to 85
### Running State
* **Equipment running**: set to 1
* **SAT temperature**: **Ramp Strategy** set to 65 deg setpoint
### Fail State
* **Commong Alarm**: set to 1
* **SAT temperature**: **Ramp Strategy** set to 105 deg setpointset

View File

@@ -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 <ModbusIP_ESP8266.h>
#else
#include <ModbusRTU.h>
#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<ModbusIP>::FailState(const std::vector<std::string>& 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<ModbusIP>* FailState<ModbusIP>::update(Equipment<ModbusIP>* 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<ModbusIP>::enterState(Equipment<ModbusIP>* 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<ModbusIP>::exitState(Equipment<ModbusIP>* equipment) {
// Cleanup logic to run when the equipment leaves this state
Serial.println("Exit Fail State...");
}

View File

@@ -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 <vector>
#include <string>
#if defined(USE_MODBUS_IP)
#include <ModbusIP_ESP8266.h>
#else
#include <ModbusRTU.h>
#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<ModbusIP>::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<ModbusIP>* RunningState<ModbusIP>::update(Equipment<ModbusIP>* 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<ModbusIP>::enterState(Equipment<ModbusIP>* 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<ModbusIP>::exitState(Equipment<ModbusIP>* equipment) {
// Cleanup logic to run when the equipment leaves this state
Serial.println("Exit Running State...");
}

View File

@@ -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 <vector>
#include <string>
#if defined(USE_MODBUS_IP)
#include <ModbusIP_ESP8266.h>
#else
#include <ModbusRTU.h>
#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<ModbusIP>::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<ModbusIP>* StandbyState<ModbusIP>::update(Equipment<ModbusIP>* 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<ModbusIP>::enterState(Equipment<ModbusIP>* 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<ModbusIP>::exitState(Equipment<ModbusIP>* equipment) {
// Cleanup logic to run when the equipment leaves this state
Serial.println("Exit Standby State...");
}

View File

@@ -0,0 +1,262 @@
/**
* @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 <ModbusIP_ESP8266.h>
const char *ssid = "Wifi1"; /**< @brief The SSID of the WiFi network. */
const char *password = "123abc456"; /**< @brief The password for the WiFi network. */
IPAddress local_IP(172, 17, 22, 130); /**< @brief The static IP address for the device. */
IPAddress gateway(172, 17, 22, 254); /**< @brief The gateway IP address. */
IPAddress subnet(255, 255, 255, 0); /**< @brief The subnet mask. */
ModbusIP mb;
#else
/**
* @defgroup ModbusRTUConfig Modbus RTU Configuration
* @brief Parameters for serial Modbus RTU communication.
* @{
*/
#include <ModbusRTU.h>
const int BAUDRATE = 19200; /**< @brief The serial communication speed in bits per second. */
const int RX_PIN = 17; /**< @brief The GPIO pin used for receiving data (RX). */
const int TX_PIN = 16; /**< @brief The GPIO pin used for transmitting data (TX). */
const int RST_PIN = 4; /**< @brief The GPIO pin connected to the RS485 driver's DE/RE pins for direction control. */
const int MODBUS_ID = 1; /**< @brief The unique slave ID for this device on the Modbus bus. */
/** @} */
/** @brief Global instance of the Modbus RTU server. */
ModbusRTU mb;
#endif
/**
* @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[] = {
{COIL, 0, 0, "Start_CMD" },
{COIL, 1, 0, "Reset_CMD" },
{COIL, 2, 0, "Start_CMD" },
{COIL, 3, 0, "Reset_CMD" },
{DI, 2, 0, "BldgSafety" },
{DI, 31, 0, "SystemOnSts" },
{DI, 35, 0, "CoolingStatus" },
{DI, 42, 0, "ComAlm" },
{DI, 43, 0, "TSAlerts" },
{DI, 44, 0, "DXAlerts" },
{DI, 45, 0, "PwrMonAlm" },
{DI, 101, 0, "RAFilterAlm" },
{DI, 126, 0, "SmokeAlm" },
{DI, 171, 0, "SACmnAlm" },
{DI, 112, 0, "FanDPASnsr" },
{DI, 113, 0, "FanDPBSnsr" },
{DI, 66, 0, "CondComsLost" },
{DI, 4, 0, "HPSwitchCkt1" },
{DI, 5, 0, "HPSwitchCkt2" },
{DI, 6, 0, "HPSwitchCkt3" },
{DI, 7, 0, "HPSwitchCkt4" },
{DI, 19, 0, "Comp1AEnable" },
{DI, 8, 0, "Comp1Status" },
{DI, 160, 0, "Comp1A Alm" },
{DI, 21, 0, "Comp1BEnable" },
{DI, 9, 0, "Comp1BStatus" },
{DI, 161, 0, "Comp1BAlm" },
{DI, 22, 0, "Comp2AEnable" },
{DI, 13, 0, "Comp2AStatus" },
{DI, 162, 0, "Comp2AAlm" },
{DI, 23, 0, "Comp2BEnable" },
{DI, 14, 0, "Comp2BStatus" },
{DI, 163, 0, "Comp2BAlm" },
{DI, 24, 0, "Comp3AEnable" },
{DI, 15, 0, "Comp3AStatus" },
{DI, 164, 0, "Comp3AAlm" },
{DI, 25, 0, "Comp3BEnable" },
{DI, 16, 0, "Comp3BStatus" },
{DI, 165, 0, "Comp3BAlm" },
{DI, 26, 0, "Comp4AEnable" },
{DI, 17, 0, "Comp4AStatus" },
{DI, 166, 0, "Comp4AAlm" },
{DI, 28, 0, "Comp4BEnable" },
{DI, 18, 0, "Comp4BStatus" },
{DI, 167, 0, "Comp4BAlm" },
{DI, 47, 0, "Fan1AAlm" },
{DI, 48, 0, "Fan1BAlm" },
{DI, 49, 0, "Fan1CAlm" },
{DI, 50, 0, "Fan1DAlm" },
{DI, 51, 0, "Fan1EAlm" },
{DI, 52, 0, "Fan1FAlm" },
{DI, 53, 0, "Fan1GAlm" },
{DI, 54, 0, "Fan1HAlm" },
{DI, 57, 0, "Fan2AAlm" },
{DI, 58, 0, "Fan2BAlm" },
{DI, 59, 0, "Fan2CAlm" },
{DI, 60, 0, "Fan2DAlm" },
{DI, 61, 0, "Fan2EAlm" },
{DI, 62, 0, "Fan2FAlm" },
{DI, 63, 0, "Fan2GAlm" },
{DI, 64, 0, "Fan2HAlm" },
{DI, 55, 0, "SAFlowAlm" },
{DI, 56, 0, "SAFansAlm" },
{DI, 75, 0, "Ckt1LoWrn" },
{DI, 83, 0, "Ckt2LoWrn" },
{DI, 90, 0, "Ckt3LoWrn" },
{DI, 97, 0, "Ckt4LoWrn" },
{DI, 104, 0, "RA_FAULT_1" },
{DI, 105, 0, "RA_FAULT_2" },
{DI, 246, 0, "Cond2AStatus" },
{DI, 249, 0, "Cond2BStatus" },
{DI, 252, 0, "Cond2CStatus" },
{DI, 255, 0, "Cond2DStatus" },
{DI, 258, 0, "Cond2EStatus" },
{DI, 261, 0, "Cond2FStatus" },
{DI, 264, 0, "Cond2GStatus" },
{DI, 267, 0, "Cond2Status" },
{DI, 65, 0, "CondFansAlm" },
{DI, 123, 0, "CondPot" },
{DI, 243, 0, "SupFan1ASts" },
{DI, 180, 0, "SupFan1BSts" },
{DI, 189, 0, "SupFan1CSts" },
{DI, 198, 0, "SupFan1DSts" },
{DI, 207, 0, "SupFan1ESts" },
{DI, 216, 0, "SupFan1FSts" },
{DI, 225, 0, "SupFan1GSts" },
{DI, 234, 0, "SupFan1HSts" },
{IR, 1, 0, "OA Temp" },
{IR, 2, 0, "OA Temp" },
{IR, 3, 0, "Ckt1 DisPrs" },
{IR, 4, 0, "Ckt1 DisPrs" },
{IR, 7, 0, "Ckt2 DisPrs" },
{IR, 8, 0, "Ckt2 DisPrs" },
{IR, 9, 0, "Ckt3 DisPrs" },
{IR, 10, 0, "Ckt3 DisPrs" },
{IR, 11, 0, "Ckt4 DisPrs" },
{IR, 12, 0, "Ckt4 DisPrs" },
{IR, 19, 0, "RA Hum" },
{IR, 20, 0, "RA Hum" },
{IR, 25, 0, "RA Temp1" },
{IR, 26, 0, "RA Temp1" },
{IR, 27, 0, "RA Temp2" },
{IR, 28, 0, "RA Temp2" },
{IR, 33, 0, "RF Diff" },
{IR, 34, 0, "RF Diff" },
{IR, 37, 0, "SF Diff" },
{IR, 38, 0, "SF Diff" },
{IR, 37, 0, "RA FilterDP" },
{IR, 38, 0, "RA FilterDP" },
{IR, 105, 0, "RA Temp Avg" },
{IR, 106, 0, "RA Temp Avg" },
{IR, 107, 0, "SA Temp Avg" },
{IR, 108, 0, "SA Temp Avg" },
{IR, 113, 0, "SA Flow Total MSB" },
{IR, 114, 0, "SA Flow Total LSB" },
{IR, 133, 0, "RA Dew Calc" },
{IR, 134, 0, "RA Dew Calc" },
{IR, 143, 0, "Clg Pcnt" },
{IR, 144, 0, "Clg Pcnt" },
{IR, 147, 0, "TS1 Low" },
{IR, 148, 0, "TS1 Low" },
{IR, 149, 0, "TS1 Up" },
{IR, 150, 0, "TS1 Up" },
{IR, 151, 0, "TS2 Low" },
{IR, 152, 0, "TS2 Low" },
{IR, 153, 0, "TS2 Up" },
{IR, 154, 0, "TS2 Up" },
{IR, 155, 0, "Sys Stat" },
{IR, 156, 0, "" },
{IR, 185, 0, "Fan1B Spd MSB" },
{IR, 186, 0, "Fan1B Spd LSB" },
{IR, 197, 0, "Fan1C Spd MSB" },
{IR, 198, 0, "Fan1C Spd LSB" },
{IR, 205, 0, "Fan1D Spd MSB" },
{IR, 206, 0, "Fan1D Spd LSB" },
{IR, 221, 0, "Fan1E Spd MSB" },
{IR, 222, 0, "Fan1E Spd LSB" },
{IR, 233, 0, "Fan1F Spd MSB" },
{IR, 234, 0, "Fan1F Spd LSB" },
{IR, 245, 0, "Fan1G Spd MSB" },
{IR, 246, 0, "Fan1G Spd LSB" },
{IR, 257, 0, "Fan1H Spd MSB" },
{IR, 258, 0, "Fan1H Spd LSB" },
{IR, 269, 0, "Fan1A Spd MSB" },
{IR, 270, 0, "Fan1A Spd LSB" },
{IR, 426, 0, "EVM1SuctPrsA" },
{IR, 427, 0, "EVM1SuctPrsA" },
{IR, 428, 0, "EVM1SuctPrsB" },
{IR, 429, 0, "EVM1SuctPrsB" },
{IR, 430, 0, "EVM1SuctTmpA" },
{IR, 431, 0, "EVM1SuctTmpA" },
{IR, 432, 0, "EVM1SuctTmpB" },
{IR, 433, 0, "EVM1SuctTmpB" },
{IR, 438, 0, "EVM1ValveA" },
{IR, 439, 0, "EVM1ValveA" },
{IR, 440, 0, "EVM1ValveB" },
{IR, 441, 0, "EVM1ValveB" },
{IR, 462, 0, "EVM3SuctPrsA" },
{IR, 463, 0, "EVM3SuctPrsA" },
{IR, 464, 0, "EVM3SuctPrsB" },
{IR, 465, 0, "EVM3SuctPrsB" },
{IR, 466, 0, "EVM3SuctTmpA" },
{IR, 467, 0, "EVM3SuctTmpA" },
{IR, 468, 0, "EVM3SuctTmpB" },
{IR, 469, 0, "EVM3SuctTmpB" },
{IR, 474, 0, "EVM3Valve A" },
{IR, 475, 0, "EVM3Valve A" },
{IR, 476, 0, "EVM3Valve B" },
{IR, 477, 0, "EVM3Valve B" },
{IR, 526, 0, "EVM1ErrCode" },
{IR, 527, 0, "EVM2ErrCode" },
{IR, 528, 0, "EVM3ErrCode" },
{IR, 529, 0, "EVM4ErrCode" },
{HR, 1, 0, "RATemp SP" },
{HR, 2, 0, "RATemp SP" },
{HR, 3, 0, "SATemp SP" },
{HR, 4, 0, "SATemp SP" },
{HR, 5, 0, "SATemp DF" },
{HR, 6, 0, "SATemp DF" },
{HR, 7, 0, "SAFSpdSP" },
{HR, 8, 0, "SAFSpdSP" },
{HR, 9, 0, "SAFlMaxSpdSP" },
{HR, 10, 0, "SAFlMaxSpdSP" },
{HR, 11, 0, "SAFlMinSpdSP" },
{HR, 12, 0, "SAFlMinSpdSP" },
{HR, 13, 0, "RAFltrDPSP" },
{HR, 14, 0, "RAFltrDPSP" },
};
//Size of modbus map used in FOR cycles, automatically calculated.
/**
* @brief The total number of entries in the `mb_map` array.
* This is calculated at compile time and used for iterating over the map.
*/
const int map_size = sizeof(mb_map) / sizeof(mb_map[0]);
/** @brief The main loop update interval in milliseconds. */
int interval = 250;
/** @} */ // End of ModbusMapConfig group
#endif // CONFIG_H

View File

@@ -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 <WiFi.h>
#include "config.h"
#include "ModbusPoints/Modbus_PointFactory.h"
#if defined(USE_MODBUS_IP)
#include <ModbusIP_ESP8266.h>
#else
#include <ModbusRTU.h>
#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<ModbusIP>* 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);
}
}