xentara-cpp-control v1.0.1
The Xentara C++ Control Framework
Loading...
Searching...
No Matches
Creating an Empty Control

Overview

This tutorial guides you through the process of creating a basic control in Xentara. We will define a control class with an empty structure, preparing it for future functionalities. Our goal is to create a simple control with two key methods: initialize, for setup, and step, for executing control actions periodically. By the end, we will have a foundational control ready to be extended in future tutorials.

In Xentara, a control class represents a component that manages specific control logic, executing actions and managing data points as part of an industrial control process. To create a control, we will derive a new class from xentara::Control, Xentara base class for custom control components. Our control class will include:

  • A header file (EmptyControl.hpp) defining the class structure.
  • An implementation file (EmptyControl.cpp) implementing the methods.

Step 1: Define the Control Class Structure

Begin by creating a header file, EmptyControl.hpp, to define the basic structure of our control class. This class will inherit from xentara::Control, giving it the necessary characteristics to function as a control element in Xentara. The control class will have two primary methods:

  • initialize: Called once to set up any configuration or resources the control needs.
  • step: Called periodically to perform the control's main logic.

Here is the code for EmptyControl.hpp:

#pragma once
#include <xentara/cpp-control.h>
class EmptyControl final : public xentara::Control
{
public:
void initialize(xentara::InitContext &context) override;
void step(xentara::RunContext &context) override;
};
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

This code sets up an empty control class with an initialize method to handle setup tasks and a step method to execute periodic control actions. Although these methods are currently placeholders, this layout will help us build more complex control functionalities in later tutorials.

Step 2: Implement the Control Logic

Next, create a .cpp file, EmptyControl.cpp, to implement the methods defined in the header file. While the methods are initially empty, they provide the necessary structure to integrate more advanced logic over time.

In this file, we will also register the control with Xentara using ControlExporter, allowing Xentara to recognize and manage this control within its configuration.

Here is the code for EmptyControl.cpp:

#include "EmptyControl.hpp"
/// Registers the EmptyControl with Xentara.
void EmptyControl::initialize(xentara::InitContext &context)
{
// Placeholder for future initialization code
}
void EmptyControl::step(xentara::RunContext &context)
{
// Placeholder for future control logic
}
A class used to export your control class with Xentara.
Definition ControlExporter.hpp:38

The initialize method in this file will eventually handle setup tasks, while the step method will execute the control logic each time it's called. In the context of this empty control, these methods remain unfilled but lay the groundwork for future functionality.

The ControlExporter at the beginning of the file registers EmptyControl with Xentara, making it accessible in Xentara's configuration and allowing it to function as a control unit in industrial automation setups.

Though this tutorial uses the traditional return type style, we recommend the modern trailing return type syntax, where the return type appears after the function signature. This style can improve readability, especially with complex types.

For example:

void initialize(xentara::InitContext &context); // Traditional style
auto initialize(xentara::InitContext &context) -> void; // Recommended modern trailing return type

Both are valid, but trailing return types are a good modern C++ practice.

Conclusion

This tutorial has walked you through the process of setting up a simple, empty control class in Xentara. By creating a header file and implementing basic methods in a .cpp file, we have prepared a foundation for adding control functionality in subsequent tutorials.

Our next steps will involve expanding this control class by adding data points, thresholds, and specific logic, turning this basic structure into a functional control.