Chiller daikin logic added
This commit is contained in:
@@ -87,6 +87,12 @@ protected:
|
||||
* @param strategy A pointer to the Strategy_Behavior object. The State takes ownership.
|
||||
*/
|
||||
void addStrategy(const std::string& pointDescription, Strategy_Behavior* strategy);
|
||||
/**
|
||||
* @brief returns a behavior strategy for a specific Modbus point in this state.
|
||||
* @param pointDescription The description of the Modbus point your need to get.
|
||||
*/
|
||||
Strategy_Behavior* getStrategy(const std::string& pointDescription);
|
||||
|
||||
std::map<std::string, Strategy_Behavior*> _strategies; /**< @brief Map of strategies active in this state, keyed by point description. */
|
||||
};
|
||||
|
||||
@@ -110,6 +116,20 @@ template<typename T>
|
||||
void State<T>::addStrategy(const std::string& pointDescription, Strategy_Behavior* strategy){
|
||||
this->_strategies[pointDescription] = strategy;
|
||||
}
|
||||
/**
|
||||
* @brief returns a behavior strategy for a specific Modbus point in this state.
|
||||
* @param pointDescription The description of the Modbus point your need to get.
|
||||
*/
|
||||
template<typename T>
|
||||
Strategy_Behavior* State<T>::getStrategy(const std::string& pointDescription){
|
||||
{
|
||||
auto it = _strategies.find(pointDescription);
|
||||
if (it != _strategies.end()) {
|
||||
return it->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Gets the value of a Modbus point, correctly handling float types.
|
||||
|
||||
@@ -59,6 +59,17 @@ void PIDStrategy::setInput(float input) {
|
||||
_input = input;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Manually sets the lower and upper limit of the PID.
|
||||
* @note This is typically not needed as the `execute` method receives the current
|
||||
* limit value on each call from the `_applyStrategies` loop.
|
||||
* @param input The new process variable value.
|
||||
*/
|
||||
void PIDStrategy::setLimits(float lowerLimit, float upperLimit) {
|
||||
_lowerLimit = lowerLimit;
|
||||
_upperLimit = upperLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Executes one cycle of the PID control algorithm.
|
||||
*
|
||||
|
||||
@@ -66,7 +66,8 @@ public:
|
||||
void setGains(float kp, float ki, float kd);
|
||||
/** @brief Manually sets the process variable (input) value. */
|
||||
void setInput(float input);
|
||||
|
||||
/** @brief Manually sets the lower and upper limits of the PID. */
|
||||
void setLimits(float lowerLimit, float upperLimit);
|
||||
|
||||
private:
|
||||
float _kp; /**< @brief Proportional gain. */
|
||||
@@ -77,6 +78,8 @@ private:
|
||||
float _input; /**< @brief The current value of the process variable. */
|
||||
float _lastError; /**< @brief The error from the previous calculation. */
|
||||
float _integral; /**< @brief The accumulated integral term. */
|
||||
float _lowerLimit; /**< @brief The lower limit for the output. */
|
||||
float _upperLimit; /**< @brief The upper limit for the output. */
|
||||
std::string _inputSensorName; /**< @brief The description key for the input sensor point. */
|
||||
std::string _setpointName; /**< @brief The description key for the setpoint point. */
|
||||
};
|
||||
|
||||
@@ -11,6 +11,9 @@
|
||||
[platformio]
|
||||
default_envs = CH_Daikin_AWV026B_RTU
|
||||
|
||||
[env]
|
||||
upload_port = COM15
|
||||
|
||||
[common_env_options]
|
||||
framework = arduino
|
||||
monitor_speed = 115200
|
||||
@@ -27,6 +30,5 @@ build_src_filter = -<*> +<CH_Daikin_AWV026B_RTU>
|
||||
platform = espressif32
|
||||
board = dfrobot_firebeetle2_esp32e
|
||||
extends = common_env_options
|
||||
build_flags =
|
||||
-D USE_MODBUS_IP
|
||||
build_flags = -D USE_MODBUS_IP
|
||||
build_src_filter = -<*> +<CRAH_PAHHC_600_C6_TCP>
|
||||
|
||||
77
src/00_Base_RTU/State_Fail.cpp
Normal file
77
src/00_Base_RTU/State_Fail.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @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_Standby.h"
|
||||
#include "States/State_Fail.h"
|
||||
#include "ModbusPoints/Modbus_Point.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 receives a list of alarm descriptions and creates strategies
|
||||
* to set the corresponding Modbus points to a value of 1, indicating an
|
||||
* active alarm. It also initializes a PID strategy for the valve position.
|
||||
*/
|
||||
template<>
|
||||
FailState<ModbusRTU>::FailState(const std::vector<std::string>& activeAlarms) {
|
||||
// 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 "Clear Alm" Modbus point for a command to transition
|
||||
* back to Standby, which would typically happen after a fault is cleared by a
|
||||
* user. If no transition is requested, it continues to apply the failure strategies.
|
||||
*
|
||||
* @param equipment Pointer to the Equipment instance.
|
||||
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
|
||||
*/
|
||||
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");
|
||||
|
||||
_applyStrategies(equipment);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Logic to execute once when entering the fail state. Sets the main alarm bit.
|
||||
* @param equipment Pointer to the Equipment instance.
|
||||
*/
|
||||
template<>
|
||||
void FailState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
|
||||
// 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 main alarm bit.
|
||||
* @param equipment Pointer to the Equipment instance.
|
||||
*/
|
||||
template<>
|
||||
void FailState<ModbusRTU>::exitState(Equipment<ModbusRTU>* equipment) {
|
||||
// Cleanup logic to run when the equipment leaves this state
|
||||
Serial.println("Exit Fail State...");
|
||||
|
||||
}
|
||||
89
src/00_Base_RTU/State_Running.cpp
Normal file
89
src/00_Base_RTU/State_Running.cpp
Normal 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 "States/State_Standby.h"
|
||||
#include "States/State_Running.h"
|
||||
#include "States/State_Fail.h"
|
||||
#include "Strategies/Strategy_Behavior.h"
|
||||
#include "Strategies/Strategy_PID.h"
|
||||
#include "Strategies/Strategy_Ramp.h"
|
||||
#include "Strategies/Strategy_Totalizer.h"
|
||||
#include "Equipment/Equipment.h"
|
||||
#include "ModbusPoints/Modbus_Point.h"
|
||||
#include "ModbusPoints/Modbus_FloatDecorator.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<ModbusRTU>::RunningState() {
|
||||
//Add strategies
|
||||
//addStrategy("Actual Capacity", new PIDStrategy("Active SP", 1000, "Supply Temp"));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @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<ModbusRTU>* RunningState<ModbusRTU>::update(Equipment<ModbusRTU>* 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 "Chiller Sts" point to indicate the unit is running.
|
||||
* @param equipment Pointer to the Equipment instance.
|
||||
*/
|
||||
template<>
|
||||
void RunningState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
|
||||
// Logic to run when the equipment enters this state
|
||||
Serial.println("Enter Running State...");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Logic to execute once when exiting the running state.
|
||||
* Sets the "Chiller Sts" point to indicate the unit is no longer running.
|
||||
* @param equipment Pointer to the Equipment instance.
|
||||
*/
|
||||
template<>
|
||||
void RunningState<ModbusRTU>::exitState(Equipment<ModbusRTU>* equipment) {
|
||||
// Cleanup logic to run when the equipment leaves this state
|
||||
Serial.println("Exit Running State...");
|
||||
}
|
||||
77
src/00_Base_RTU/State_Standby.cpp
Normal file
77
src/00_Base_RTU/State_Standby.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* @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_Running.h"
|
||||
#include "States/State_Fail.h"
|
||||
#include "ModbusPoints/Modbus_Point.h"
|
||||
#include "ModbusPoints/Modbus_FloatDecorator.h"
|
||||
#include "Equipment/Equipment.h"
|
||||
#include "Strategies/Strategy_Ramp.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 several
|
||||
* strategies to generate random values for various status points, simulating
|
||||
* a live but non-operational unit.
|
||||
*/
|
||||
template<>
|
||||
StandbyState<ModbusRTU>::StandbyState() {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Executes the standby state's logic for one update cycle.
|
||||
*
|
||||
* This method checks the "Chiller On-Off" Modbus point for a command to
|
||||
* transition to the Running state. If no transition is requested, it applies
|
||||
* the strategies defined for the standby state.
|
||||
*
|
||||
* @param equipment Pointer to the Equipment instance.
|
||||
* @return A pointer to a new State if a transition should occur, otherwise nullptr.
|
||||
*/
|
||||
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");
|
||||
|
||||
// Apply any strategies defined for the standby state
|
||||
_applyStrategies(equipment);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Logic to execute once when entering the standby state.
|
||||
* Sets the "Chiller Sts" point to indicate the unit is not running.
|
||||
* @param equipment Pointer to the Equipment instance.
|
||||
*/
|
||||
template<>
|
||||
void StandbyState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
|
||||
// Logic to run when the equipment enters this state
|
||||
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<ModbusRTU>::exitState(Equipment<ModbusRTU>* equipment) {
|
||||
// Cleanup logic to run when the equipment leaves this state
|
||||
|
||||
}
|
||||
130
src/00_Base_RTU/config.h
Normal file
130
src/00_Base_RTU/config.h
Normal file
@@ -0,0 +1,130 @@
|
||||
/**
|
||||
* @file config.h
|
||||
* @brief Main configuration file for the Daikin Chiller (RTU) emulator.
|
||||
* @author Emmanuel Hernandez Cruz
|
||||
* @date 2025-09-02
|
||||
*
|
||||
* This file contains important configurations for the Modbus RTU communication
|
||||
* and the specific register map for the emulated device.
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_H
|
||||
#define CONFIG_H
|
||||
#include <ModbusRTU.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 = "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 <ModbusRTU.h>
|
||||
const int BAUDRATE = 19200; /**< @brief The serial communication speed in bits per second. */
|
||||
const int RX_PIN = 17; /**< @brief The GPIO pin used for receiving data (RX). */
|
||||
const int TX_PIN = 16; /**< @brief The GPIO pin used for transmitting data (TX). */
|
||||
const int RST_PIN = 4; /**< @brief The GPIO pin connected to the RS485 driver's DE/RE pins for direction control. */
|
||||
const int MODBUS_ID = 1; /**< @brief The unique slave ID for this device on the Modbus bus. */
|
||||
/** @} */
|
||||
|
||||
/** @brief Global instance of the Modbus RTU server. */
|
||||
ModbusRTU mb;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief The Modbus map for the Equipment device.
|
||||
* This array defines all the Modbus points available on the emulated device.
|
||||
* 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"},
|
||||
{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;
|
||||
|
||||
#endif // CONFIG_H
|
||||
78
src/00_Base_RTU/main.cpp
Normal file
78
src/00_Base_RTU/main.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* @file main.cpp
|
||||
* @brief Main execution program for the Daikin Chiller (RTU) Emulator.
|
||||
* @author Emmanuel Hernandez Cruz
|
||||
* @date 2025-09-02
|
||||
*
|
||||
* @details This file contains the main execution program for an Arduino-based
|
||||
* emulator of a Daikin Chiller unit. The program communicates via the
|
||||
* Modbus RTU protocol over a serial connection.
|
||||
*
|
||||
* The setup() function initializes the following:
|
||||
* - Serial communication for debugging.
|
||||
* - A Modbus RTU server with parameters from config.h.
|
||||
* - Modbus points (Coils, Holding Registers, etc.) based on a predefined map in config.h.
|
||||
*
|
||||
* The loop() function continuously:
|
||||
* - Services the Modbus RTU server to handle incoming requests.
|
||||
* - Periodically calls the main update loop for the emulated equipment, which
|
||||
* manages state transitions and behavior strategies.
|
||||
*
|
||||
* @see config.h for Modbus RTU and register map configuration.
|
||||
* @see Equipment.h for the main equipment logic.
|
||||
* @see State.h for different equipment states.
|
||||
* @see Strategies/Strategy_Behavior.h for value generation strategies.
|
||||
* @see Modbus_Point.h for the base class for all Modbus points.
|
||||
*/
|
||||
//=================================================================================================================================
|
||||
//Libraries and declaration of variables.
|
||||
#include <Arduino.h>
|
||||
#include "config.h"
|
||||
#include "ModbusPoints/Modbus_PointFactory.h"
|
||||
|
||||
//=================================================================================================================================
|
||||
/**
|
||||
* @brief Initializes the application.
|
||||
* @details This function runs once at startup. It configures the serial communication
|
||||
* for debugging and the Modbus RTU server. It then creates and initializes all
|
||||
* the Modbus points based on the `mb_map` array in `config.h`.
|
||||
*/
|
||||
const int rtsPin = 4;
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Setup function started");
|
||||
|
||||
Serial2.begin(BAUDRATE, SERIAL_8N1, RX_PIN, TX_PIN);
|
||||
mb.begin(&Serial2, RST_PIN); // Start the server
|
||||
mb.slave(MODBUS_ID); // Set the slave ID
|
||||
|
||||
for(int i = 0; i < map_size; i++){
|
||||
Modbus_Point<ModbusRTU>* 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("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);
|
||||
}
|
||||
}
|
||||
81
src/00_Base_TCP/State_Fail.cpp
Normal file
81
src/00_Base_TCP/State_Fail.cpp
Normal 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...");
|
||||
}
|
||||
89
src/00_Base_TCP/State_Running.cpp
Normal file
89
src/00_Base_TCP/State_Running.cpp
Normal 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...");
|
||||
|
||||
}
|
||||
85
src/00_Base_TCP/State_Standby.cpp
Normal file
85
src/00_Base_TCP/State_Standby.cpp
Normal 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...");
|
||||
}
|
||||
152
src/00_Base_TCP/config.h
Normal file
152
src/00_Base_TCP/config.h
Normal file
@@ -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 <ModbusIP_ESP8266.h>
|
||||
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 <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[] =
|
||||
{
|
||||
{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
|
||||
86
src/00_Base_TCP/main.cpp
Normal file
86
src/00_Base_TCP/main.cpp
Normal 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);
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@
|
||||
#include "States/State_Fail.h"
|
||||
#include "Strategies/Strategy_Behavior.h"
|
||||
#include "Strategies/Strategy_PID.h"
|
||||
#include "Strategies/Strategy_Ramp.h"
|
||||
#include "Strategies/Strategy_Totalizer.h"
|
||||
#include "Equipment/Equipment.h"
|
||||
#include "ModbusPoints/Modbus_Point.h"
|
||||
@@ -36,16 +37,9 @@
|
||||
*/
|
||||
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));
|
||||
addStrategy("Actual Capacity", new PIDStrategy("Active SP", 1000, "Supply Temp"));
|
||||
addStrategy("Comp1 Percent RLA", new RampStrategy(0.0f, 5.0f, 1000));
|
||||
addStrategy("Comp2 Percent RLA", new RampStrategy(0.0f, 5.0f, 1000));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -65,23 +59,68 @@ 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");
|
||||
Modbus_Point<ModbusRTU>* 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){
|
||||
int CH_Enable_SP = getPointValue(equipment, "Chiller Enable SP");
|
||||
if (CH_Enable_SP == 0){
|
||||
return new StandbyState<ModbusRTU>();
|
||||
}
|
||||
|
||||
Modbus_Point<ModbusRTU>* faultCode = equipment->getModbus_Point("Fault Code");
|
||||
int faultCodeValue = faultCode ? faultCode->getValue() : 0;
|
||||
if (faultCodeValue != 0) {
|
||||
// A fault has been triggered, transition to FailState
|
||||
// This assumes a mapping between fault codes and alarm descriptions exists
|
||||
// For this example, we'll just use a generic alarm name based on the code
|
||||
std::string alarmDesc = "Fault Alm Code " + std::to_string(faultCodeValue);
|
||||
return new FailState<ModbusRTU>({alarmDesc});
|
||||
// Declare currentSP outside the switch so it's accessible later.
|
||||
float highCapacityLimit = getPointValue(equipment, "Active Capacity Limit");
|
||||
Strategy_Behavior* PID_Strat = getStrategy("Actual Capacity");
|
||||
static_cast<PIDStrategy*>(PID_Strat)->setLimits(0.0f, highCapacityLimit);
|
||||
float currentSP = 50.0f; // Default value
|
||||
int currentMode = getPointValue(equipment, "Chiller Mode Output");
|
||||
|
||||
// Determine the correct setpoint based on the current operating mode.
|
||||
switch(currentMode){
|
||||
case 1:
|
||||
currentSP = getPointValue(equipment, "Ice SP");
|
||||
currentSP = currentSP - 20;
|
||||
break; // Added break to prevent fall-through
|
||||
case 2:
|
||||
currentSP = getPointValue(equipment, "Cool SP");
|
||||
currentSP = currentSP + 20;
|
||||
break; // Added break
|
||||
default:
|
||||
// The default value is already set.
|
||||
break;
|
||||
}
|
||||
Strategy_Behavior* ramp_strategy1 = getStrategy("Comp1 Percent RLA");
|
||||
Strategy_Behavior* ramp_strategy2 = getStrategy("Comp2 Percent RLA");
|
||||
int actualCapacity = getPointValue(equipment, "Actual Capacity");
|
||||
if (actualCapacity < 50){
|
||||
actualCapacity = actualCapacity * 2;
|
||||
if (actualCapacity > 100) actualCapacity = 100;
|
||||
static_cast<RampStrategy*>(ramp_strategy1)->setTarget(actualCapacity);
|
||||
static_cast<RampStrategy*>(ramp_strategy2)->setTarget(0);
|
||||
} else {
|
||||
if (actualCapacity > 100) actualCapacity = 100;
|
||||
static_cast<RampStrategy*>(ramp_strategy1)->setTarget(actualCapacity);
|
||||
int actualCapacity2 = (actualCapacity - 50)*4;
|
||||
if (actualCapacity2 > 100) actualCapacity2 = 100;
|
||||
static_cast<RampStrategy*>(ramp_strategy2)->setTarget(actualCapacity2);
|
||||
}
|
||||
|
||||
// 1. Get the strategy by its name.
|
||||
Strategy_Behavior* strategy = getStrategy("Actual Capacity");
|
||||
// 2. Check if the strategy exists and is a PID type.
|
||||
if (strategy && strategy->isPID()) {
|
||||
// 3. Cast it to a PIDStrategy pointer and call setSetpoint.
|
||||
static_cast<PIDStrategy*>(strategy)->setSetpoint(currentSP);
|
||||
}
|
||||
|
||||
float OutdoorTemp = getPointValue(equipment, "Outdoor Air Temp");
|
||||
Serial.printf("Outdoor Temp: %f\n", OutdoorTemp);
|
||||
if (OutdoorTemp >50.0f) {
|
||||
setPointValue(equipment, "Chiller Mode SP", 1.0f);
|
||||
setPointValue(equipment, "Chiller Mode Output", 1.0f);
|
||||
}else {
|
||||
setPointValue(equipment, "Chiller Mode SP", 2.0f);
|
||||
setPointValue(equipment, "Chiller Mode Output", 2.0f);
|
||||
}
|
||||
|
||||
|
||||
setPointValue(equipment, "Active SP", currentSP);
|
||||
// Apply any strategies defined for the standby state
|
||||
_applyStrategies(equipment);
|
||||
return nullptr;
|
||||
@@ -97,20 +136,7 @@ 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) {
|
||||
Modbus_Point<ModbusRTU>* point = equipment->getModbus_Point(desc);
|
||||
if (point) {
|
||||
point->setValue(1);
|
||||
}
|
||||
}
|
||||
setPointValue(equipment, "Run Enabled", 1);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#include "ModbusPoints/Modbus_Point.h"
|
||||
#include "ModbusPoints/Modbus_FloatDecorator.h"
|
||||
#include "Equipment/Equipment.h"
|
||||
#include "Strategies/Strategy_Random.h"
|
||||
#include "Strategies/Strategy_Ramp.h"
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
@@ -31,12 +31,8 @@
|
||||
*/
|
||||
template<>
|
||||
StandbyState<ModbusRTU>::StandbyState() {
|
||||
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));
|
||||
|
||||
addStrategy("Comp1 Percent RLA", new RampStrategy(0,5,1000));
|
||||
addStrategy("Comp2 Percent RLA", new RampStrategy(0,5,1000));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,12 +49,17 @@ 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");
|
||||
Modbus_Point<ModbusRTU>* chillerOnOff = equipment->getModbus_Point("Chiller On-Off");
|
||||
int nextStateId = chillerOnOff ? chillerOnOff->getValue() : 0;
|
||||
Serial.println(nextStateId);
|
||||
if (nextStateId == 1){
|
||||
int CH_Enable_SP = getPointValue(equipment, "Chiller Enable SP");
|
||||
if (CH_Enable_SP == 1){
|
||||
return new RunningState<ModbusRTU>();
|
||||
}
|
||||
float OutdoorTemp = getPointValue(equipment, "Outdoor Air Temp");
|
||||
Serial.printf("Outdoor Temp: %f\n", OutdoorTemp);
|
||||
if (OutdoorTemp >50.0f) {
|
||||
setPointValue(equipment, "Chiller Mode SP", 1);
|
||||
}else {
|
||||
setPointValue(equipment, "Chiller Mode SP", 2);
|
||||
}
|
||||
// Apply any strategies defined for the standby state
|
||||
_applyStrategies(equipment);
|
||||
return nullptr;
|
||||
@@ -73,9 +74,7 @@ template<>
|
||||
void StandbyState<ModbusRTU>::enterState(Equipment<ModbusRTU>* equipment) {
|
||||
// Logic to run when the equipment enters this state
|
||||
Serial.println("Enter Standby State...");
|
||||
int CH_ON_OFF = getPointValue(equipment, "Chiller On-Off");
|
||||
int Ch_Sts = getPointValue(equipment, "Chiller Sts");
|
||||
|
||||
setPointValue(equipment, "Run Enabled", 0);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -22,8 +22,8 @@
|
||||
* @{
|
||||
*/
|
||||
#include <ModbusIP_ESP8266.h>
|
||||
const char *ssid = "esrlok_portable"; /**< @brief The SSID of the WiFi network. */
|
||||
const char *password = "m7g6eNMe?cy8S@z"; /**< @brief The password for the WiFi network. */
|
||||
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. */
|
||||
@@ -88,7 +88,7 @@ modbusMap mb_map[] =
|
||||
{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, 35, 0, "Cool SP"},
|
||||
{HR_10x, 36, 0, "Ice SP"},
|
||||
{HR_10x, 38, 0, "Capacity Limit SP"},
|
||||
{HR_10x, 39, 0, "Cond Refrig Pressure"},
|
||||
|
||||
@@ -21,8 +21,8 @@
|
||||
* @{
|
||||
*/
|
||||
#include <ModbusIP_ESP8266.h>
|
||||
const char *ssid = "esrlok_portable"; /**< @brief The SSID of the WiFi network. */
|
||||
const char *password = "m7g6eNMe?cy8S@z"; /**< @brief The password for the WiFi network. */
|
||||
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. */
|
||||
|
||||
Reference in New Issue
Block a user