First stable compilation with ModbusRTU and ModbusTCP

This commit is contained in:
2025-09-14 12:01:55 -05:00
commit ac20b82496
59 changed files with 6841 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
/**
* @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 "States/State_Fail.h"
#include "States/State_Standby.h"
#include "Categories/ModbusPoint.h"
#include "Equipment/Equipment.h"
#include "Strategies/Strategy_SingleValue.h"
#include "Strategies/Strategy_PID.h"
#include <vector>
#include <string>
#if defined(USE_MODBUS_IP)
#include <ModbusIP_ESP8266.h>
#else
#include <ModbusRTU.h>
#endif
/**
* @brief Constructs a new FailState object.
*
* This constructor initializes behavior strategies to simulate a failure
* scenario. In this example, it sets a common alarm bit, triggers a specific
* alarm for "EC Fan #1", and ramps down all fan speeds to zero.
*/
template<>
FailState<ModbusRTU>::FailState(const std::vector<std::string>& 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 "State Control" Modbus point for a command to
* transition back to Standby, which would typically happen after a fault
* is cleared. If no transition is requested, it applies the failure
* strategies (e.g., keeping fans off and alarms active).
*
* @param equipment Pointer to the Equipment instance.
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
*/
template<>
State<ModbusRTU>* FailState<ModbusRTU>::update(Equipment<ModbusRTU>* equipment) {
// STATE control, add conditions if change to a different state is needed
Serial.println("Fail update function");
ModbusPoint<ModbusRTU>* alarmReset = equipment->getModbusPoint("Alarm Reset");
int nextStateId = alarmReset ? alarmReset->getValue() : 0;
if (nextStateId == 1){
return new StandbyState<ModbusRTU>();
}
_applyStrategies(equipment);
return nullptr;
}
/**
* @brief Logic to execute once when entering the fail state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
*/
template<>
void FailState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
// Logic to run when the equipment enters this state
Serial.println("Enter Fail State...");
ModbusPoint<ModbusRTU>* alarm_common = equipment->getModbusPoint("Alarm Common");
alarm_common->setValue(1);
}
/**
* @brief Logic to execute once when exiting the fail state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
*/
template<>
void FailState<ModbusRTU>::exitState(Equipment<ModbusRTU>* equipment) {
// Cleanup logic to run when the equipment leaves this state
Serial.println("Exit Fail State...");
ModbusPoint<ModbusRTU>* alarm_common = equipment->getModbusPoint("Alarm Common");
alarm_common->setValue(0);
}

View File

@@ -0,0 +1,167 @@
/**
* @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 "States/State_Running.h"
#include "States/State_Standby.h"
#include "States/State_Fail.h"
#include "Strategies/Strategy_Behavior.h"
#include "Strategies/Strategy_PID.h"
#include "Strategies/Strategy_Totalizer.h"
#include "Equipment/Equipment.h"
#include "Categories/ModbusPoint.h"
#include "Categories/ModbusFloatDecorator.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 the behavior strategies for various Modbus points
* that are active during the running state. For example, it sets different
* dynamic behaviors for the speeds of EC fans 1 through 5.
*/
template<>
RunningState<ModbusRTU>::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 checks the "State Control" Modbus point for a command to
* transition to a different state (e.g., back to Standby). If no transition
* is requested, it applies the strategies defined for the running state.
*
* @param equipment Pointer to the Equipment instance.
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
*/
template<>
State<ModbusRTU>* RunningState<ModbusRTU>::update(Equipment<ModbusRTU>* equipment) {
// STATE control, add conditions if change to a different state is needed
Serial.println("Running update function");
ModbusPoint<ModbusRTU>* On_Off_Command = equipment->getModbusPoint("ON/OFF Command By BMS");
int nextStateId = On_Off_Command ? On_Off_Command->getValue() : 0;
Serial.println(nextStateId);
if (nextStateId == 0){
return new StandbyState<ModbusRTU>();
}
ModbusPoint<ModbusRTU>* faultCode = equipment->getModbusPoint("Fault Code");
int faultCodeValue = faultCode ? faultCode->getValue() : 0;
switch (faultCodeValue){
case 1:
return new FailState<ModbusRTU>({"Alarm SAT Sensor Fault"});
case 2:
return new FailState<ModbusRTU>({"Alarm RAH Sensor Fault"});
case 3:
return new FailState<ModbusRTU>({"Alarm RAT Sensor Fault"});
case 4:
return new FailState<ModbusRTU>({"Alarm Filter DP Sensor Fault"});
case 5:
return new FailState<ModbusRTU>({"Alarm Flooding"});
case 6:
return new FailState<ModbusRTU>({"Alarm Dirty Filter"});
case 7:
return new FailState<ModbusRTU>({"Alarm High RAT"});
case 8:
return new FailState<ModbusRTU>({"Alarm Low RAT"});
case 9:
return new FailState<ModbusRTU>({"Alarm High SAT"});
case 10:
return new FailState<ModbusRTU>({"Alarm Low SAT"});
case 11:
return new FailState<ModbusRTU>({"Alarm High RAH"});
case 12:
return new FailState<ModbusRTU>({"Alarm Low RAH"});
case 13:
return new FailState<ModbusRTU>({"Alarm Phase Failure"});
case 14:
return new FailState<ModbusRTU>({"Alarm Condensate Pump"});
case 15:
return new FailState<ModbusRTU>({"Alarm Smoke"});
case 16:
return new FailState<ModbusRTU>({"Alarm Fire"});
case 17:
return new FailState<ModbusRTU>({"Alarm EC Fan #1"});
case 18:
return new FailState<ModbusRTU>({"Alarm EC Fan #2"});
case 19:
return new FailState<ModbusRTU>({"Alarm EC Fan #3"});
case 20:
return new FailState<ModbusRTU>({"Alarm EC Fan #4"});
case 21:
return new FailState<ModbusRTU>({"Alarm EC Fan #5"});
case 22:
return new FailState<ModbusRTU>({"Alarm EC Fan #6"});
case 23:
return new FailState<ModbusRTU>({"Alarm EC Fan #7"});
case 24:
return new FailState<ModbusRTU>({"Alarm EC Fan #8"});
case 25:
return new FailState<ModbusRTU>({"Alarm EC Fan #9"});
default:
break;
}
ModbusPoint<ModbusRTU>* point = equipment->getModbusPoint("Setting the EC Fan Max Speed");
point->setValue(45);
// Apply any strategies defined for the standby state
_applyStrategies(equipment);
return nullptr;
}
/**
* @brief Logic to execute once when entering the running state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
*/
template<>
void RunningState<ModbusRTU>::enterState(Equipment<ModbusRTU>* 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<std::string> motorStatusDescriptions = {
"Run Status EC Fan #1", "Run Status EC Fan #2", "Run Status EC Fan #3",
"Run Status EC Fan #4", "Run Status EC Fan #5", "Run Status EC Fan #6",
"Run Status EC Fan #7", "Run Status EC Fan #8", "Run Status EC Fan #9"
};
// Loop through and set all motor statuses to 0
for (const auto& desc : motorStatusDescriptions) {
ModbusPoint<ModbusRTU>* point = equipment->getModbusPoint(desc);
if (point) {
point->setValue(1);
}
}
}
/**
* @brief Logic to execute once when exiting the running state.
* @param equipment Pointer to the Equipment instance (unused in this implementation).
*/
template<>
void RunningState<ModbusRTU>::exitState(Equipment<ModbusRTU>* equipment) {
// Cleanup logic to run when the equipment leaves this state
Serial.println("Exit Running State...");
}

View File

@@ -0,0 +1,106 @@
/**
* @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 "States/State_Standby.h"
#include "Categories/ModbusPoint.h"
#include "Categories/ModbusFloatDecorator.h"
#include "Equipment/Equipment.h"
#include "Strategies/Strategy_Random.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 can be used to
* define specific behaviors for Modbus points that should occur during standby,
* such as setting fan speeds to zero.
*/
template<>
StandbyState<ModbusRTU>::StandbyState() {
// You can add initialization code here if needed
/*
addStrategy("SAT Reading", new SingleValueStrategy(100.0f, 0.1f, 1000));
addStrategy("CW Valve Position", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #1", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #2", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #3", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #4", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #5", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #6", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #7", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #8", new RampStrategy(0.0f, 5.0f, 1000));
addStrategy("Speed EC Fan #9", new RampStrategy(0.0f, 5.0f, 1000));
*/
addStrategy("Chiller Local-Network", new RandomStrategy(1000));
addStrategy("Chiller Enable Output", new RandomStrategy(1000));
addStrategy("Run Enabled", new RandomStrategy(1000));
addStrategy("Chiller Capacity Limited", new RandomStrategy(1000));
addStrategy("Alm Digital Output", new RandomStrategy(1000));
}
/**
* @brief Executes the running state's logic for one update cycle.
*
* This method checks the "State Control" Modbus point for a command to
* transition to a different state (e.g., back to Standby). If no transition
* is requested, it applies the strategies defined for the running state.
*
* @param equipment Pointer to the Equipment instance.
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
*/
template<>
State<ModbusRTU>* StandbyState<ModbusRTU>::update(Equipment<ModbusRTU>* equipment) {
// STATE control, add conditions if change to a different state is needed
Serial.println("Standby update function");
/*
ModbusPoint* On_Off_Command = equipment->getModbusPoint("ON/OFF Command By BMS");
int nextStateId = On_Off_Command ? On_Off_Command->getValue() : 0;
Serial.println(nextStateId);
if (nextStateId == 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.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void StandbyState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
// Logic to run when the equipment enters this state
// A list of all alarm descriptions
Serial.println("Enter Standby State...");
int CH_ON_OFF = getPointValue(equipment, "Chiller On-Off");
int Ch_Sts = getPointValue(equipment, "Chiller Sts");
}
/**
* @brief Logic to execute once when exiting the standby state.
* @param equipment Pointer to the Equipment instance.
*/
template<>
void StandbyState<ModbusRTU>::exitState(Equipment<ModbusRTU>* equipment) {
// Cleanup logic to run when the equipment leaves this state
Serial.println("Exit Standby State...");
}

View File

@@ -0,0 +1,99 @@
/**
* @file config.h
* @brief Main configuration file for the Equipment 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 <ModbusRTU.h>
#include "core.h"
#include "Equipment/Equipment.h"
/**
* @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, 100, 0, "State Control"}, //Internal to control from Modscan
{HR, 101, 0, "Fault Code"},
{HR_FLOAT, 102, 0, "Supply Temp"},
{HR, 0, 0, "Chiller Local-Network"},
{HR, 1, 0, "Chiller Enable Output"},
{HR, 2, 0, "Run Enabled"},
{HR, 3, 0, "Chiller Capacity Limited"},
{HR, 4, 0, "Alm Digital Output"},
{HR, 6, 0, "Evap Flow Switch Sts"},
{HR, 7, 0, "Cond Flow Switch Sts"},
{HR, 8, 0, "Chiller On-Off"},
{HR, 9, 0, "Chiller Enable SP"},
{HR, 10, 0, "Clear Alm"},
{HR, 11, 0, "Chiller Mode Output"},
{HR_10x, 12, 0, "Active SP"},
{HR_10x, 13, 0, "Actual Capacity"},
{HR_10x, 14, 0, "Active Capacity Limit"},
{HR, 15, 0, "Chiller Sts"},
{HR_10x, 16, 0, "Evap Entering Fluid Temp"},
{HR_10x, 17, 0, "Evap Leaving Fluid Temp"},
{HR, 18, 0, "Evap Fluid Flow Rate"},
{HR_10x, 19, 0, "Cond Entering Fluid Temp"},
{HR_10x, 20, 0, "Cond Leaving Fluid Temp"},
{HR, 21, 0, "Cond Fluid Flow Rate"},
{HR_10x, 24, 0, "Outdoor Air Temp"},
{HR, 25, 0, "Chiller Current"},
{HR, 27, 0, "Total Kw"},
{HR, 28, 0, "Warning Alm Idx"},
{HR, 29, 0, "Problem Alm Idx"},
{HR, 30, 0, "Fault Alm Idx"},
{HR, 31, 0, "Warning Alm Code"},
{HR, 32, 0, "Problem Alm Code"},
{HR, 33, 0, "Fault Alm Code"},
{HR, 34, 0, "Chiller Mode SP"},
{HR_10x, 35, 0, "Cool SP - Network"},
{HR_10x, 36, 0, "Ice SP"},
{HR_10x, 38, 0, "Capacity Limit SP"},
{HR_10x, 39, 0, "Cond Refrig Pressure"},
{HR_10x, 40, 0, "Cond Saturated Refrig Temp"},
{HR_10x, 41, 0, "Evap Refrig Pressure"},
{HR_10x, 42, 0, "Evap Saturated Refrig Temp"},
{HR, 65, 0, "Comp Suction Refrig Temp"},
{HR_10x, 68, 0, "Comp Discharge Refrig Temp"},
{HR, 69, 0, "Comp1 Percent RLA"},
{HR, 70, 0, "Comp1 Current"},
{HR, 71, 0, "Comp Voltage"},
{HR, 72, 0, "Comp Power"},
{HR, 73, 0, "Comp Starts"},
{HR, 74, 0, "Comp Run Hours"},
{HR, 75, 0, "Comp Run Hours"},
{HR, 82, 0, "Comp2 Percent RLA"},
{HR, 303, 0, "Evap Pump Run Hours"},
{HR, 304, 0, "Evap Pump Run Hours"},
{HR, 305, 0, "Evap Pump Sts"},
{HR, 316, 0, "Units"},
{HR, 317, 0, "Chiller Model"},
{HR, 1849, 0, "Oil Feed Pessure"},
{HR, 1854, 0, "Wtrside Econo State"},
{HR, 1855, 0, "Wtrside Econo En SP"},
};
//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;
ModbusRTU mb;
#endif // CONFIG_H

View File

@@ -0,0 +1,79 @@
/**
* @file BaseEmulator.ino
* @brief Main execution program for the Arduino Emulator.
* @author Emmanuel Hernandez Cruz
* @date 2025-09-02
*
* @details This file contains the main execution program for an Arduino-based emulator of a equipment unit.
* The program uses a Wi-Fi connection to communicate via the Modbus IP protocol.
*
* The setup() function initializes the following:
* - Serial communication for debugging.
* - Wi-Fi connection using credentials from config.h.
* - A Modbus IP server.
* - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h.
*
* The loop() function continuously:
* - Services the Modbus IP server.
* - Reads values from the Modbus server into internal data structures.
* - Updates the state of the emulated equipment.
* - Writes updated values back to the Modbus server.
*
* @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 different value generation strategies.
* @see ModbusPoint.h for the base class for all Modbus points.
*/
//=================================================================================================================================
//Libraries and declaration of variables.
#include <Arduino.h>
#include "config.h"
#include "Categories/ModbusPointFactory.h"
//=================================================================================================================================
/**
* @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`.
*/
const int rtsPin = 4;
void setup() {
Serial.begin(115200);
Serial.println("Setup function started");
Serial2.begin(19200, SERIAL_8N1, 17, 16);
mb.begin(&Serial2, 4); // Start the server
mb.slave(1); // Set the slave ID
for(int i = 0; i < map_size; i++){
ModbusPoint<ModbusRTU>* point = createModbusPoint(&mb, mb_map[i].category, mb_map[i].address, mb_map[i].value, mb_map[i].description);
if (point) {
point->addToModbusServer();
EquipmentInstance.addModbusPoint(mb_map[i].description, point);
}
}
Serial.println("Setup function ended");
}
//=================================================================================================================================
/**
* @brief The main application loop.
* @details This function runs repeatedly after setup() has completed. It performs the following actions in order:
* 1. Services the Modbus server by calling `mb.task()`.
* 2. Reads the current values from the Modbus registers into the `ModbusPoint` objects by calling `readRegisters()`.
* 3. After a specified interval, it updates the equipment's state by calling `EquipmentInstance.update()`.
* 4. Writes any changed values from the `ModbusPoint` objects back to the Modbus registers by calling `writeRegisters()`.
*/
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);
}
}