Overview
In this tutorial, we will explore error handling during the initialization and operational phases of a HysteresisControl.
Error handling ensures the control system can detect, respond to, and recover from errors gracefully.
The process includes:
- Detecting configuration errors in the
initialize phase.
- Handling runtime errors, such as BadQuality inputs, during the
step phase.
The objectives of this tutorial are to:
- Implement error handling during initialization to validate configurations.
- Handle runtime errors during the operational phase using
RunContext.
Step 1: Define the HysteresisControl Class
The HysteresisControl class implements a hysteresis-based temperature control system. It includes error handling during initialization for configuration validation and in the operational phase.
#pragma once
#include <xentara/cpp-control.h>
{
public:
private:
xentara::Float64Input temperature;
xentara::BooleanOutput heaterState;
double lowerThreshold {0.0};
double upperThreshold {0.0};
};
An interface class which provides declaration of all the callbacks for this control.
Definition Control.hpp:19
virtual auto initialize(InitContext &context) -> void=0
Callback that is called by the framework to initialize the control object.
virtual auto step(RunContext &context) -> void=0
Main callback responsible for executing the core control logic.
An initialization context with configuration information and access to Xentara Elements.
Definition InitContext.hpp:35
An run context with execution information and is required for the read and write for Input and Output...
Definition RunContext.hpp:46
Step 2: JSON Configuration for Hysteresis Control
The JSON configuration file defines the data points for the control, along with the hysteresis thresholds.
{
"children": [
{
"@Skill.CPP.Control": {
"name": "Simple Hysteresis Temperature Control",
"UUID": "d95a4f25-4ccd-4496-a37f-aa9730a30633",
"controlPath": "libsimple-hysteresis-temperature-control.so",
"parameters": {
"lowerThreshold": 18.0,
"upperThreshold": 22.0,
"temperatureInput": "Temperature Input",
"heaterState": "Heater State"
}
}
},
{
"@Skill.SignalFlow.Register": {
"name": "Temperature Input",
"UUID": "59a39dfb-b465-4849-a24d-dc3403030f1e",
"defaultValue": [ "float64", 20.0 ]
}
},
{
"@Skill.SignalFlow.Register": {
"name": "Heater State",
"UUID": "af680196-9d8c-4c87-8a8f-4e92c9a351df",
"defaultValue": [ "bool", false ]
}
},
{
"@Track": {
"name": "Control Track",
"UUID": "fb0a26f8-7d84-4af9-9345-4145263d09c7",
"children": [
{
"@Timer": {
"name": "Timer",
"UUID": "e6580c24-dbf9-43a0-aa81-c5ed8da6f734",
"period": "50ms"
}
},
{
"@Pipeline": {
"name": "Pipeline",
"UUID": "040f156e-2744-4008-8195-cd7f17078668",
"triggers": [
"Control Track.Timer.expired"
],
"checkpoints": [
{},
{}
],
"segments": [
{
"start": 0,
"end": 1,
"tasks": [
{
"function": "Simple Hysteresis Temperature Control.step"
}
]
}
]
}
}
]
}
}
]
}
Step 3: Implement Error Handling Logic
The initialize method validates configuration parameters, and the step method handles runtime errors by checking the error in the RunContext.
#include "HysteresisControl.hpp"
{
temperature = context.
config().getDataPoint(
"temperature");
heaterState = context.
config().getDataPoint(
"heaterState");
lowerThreshold = context.
config().getDouble(
"lowerThreshold");
upperThreshold = context.
config().getDouble(
"upperThreshold");
if (lowerThreshold >= upperThreshold)
{
throw std::runtime_error(
"Invalid configuration: lowerThreshold must be less than upperThreshold.");
}
}
{
auto currentTemperature = temperature.read(context);
{
heaterState.write(context, false);
return;
}
if (currentTemperature < lowerThreshold)
{
heaterState.write(context, true);
}
else if (currentTemperature > upperThreshold)
{
heaterState.write(context, false);
}
else
{
}
}
A class used to export your control class with Xentara.
Definition ControlExporter.hpp:38
auto config() const -> const Config &
Gets the configuration.
auto error() const -> std::error_code
Gets the error that occurred during read or write operations.
Conclusion
In this tutorial, we implemented error handling for the HysteresisControl:
- The control validates configuration parameters during initialization to detect logical errors early.
- It uses
RunContext to detect and handle errors during the operational phase, ensuring safety and stability.
This approach ensures a robust and resilient control system capable of handling errors gracefully in industrial environments.