xentara-cpp-control v1.0.1
The Xentara C++ Control Framework
Loading...
Searching...
No Matches
Using Multiple Inputs and Outputs for Sum and Difference

Overview

In this tutorial, we will create a control that reads two inputs and produces two outputs: one for the sum of the inputs and one for the difference. The step function will read the values from the inputs, calculate the sum and difference, and write each result to the respective output.

The objectives of this tutorial are to:

  • Define two inputs and two outputs as Xentara data points.
  • Implement the logic to read the inputs, calculate their sum and difference, and write each result to the corresponding output.

Step 1: Define the Control Class with Two Inputs and Two Outputs

In this version of the control class, we define two xentara::Float64Input data points for the inputs and two xentara::Float64Output data points for the outputs (one for the sum and one for the difference).

Here is the code for SumDifferenceControl.hpp:

#pragma once
#include <xentara/cpp-control.h>
class SumDifferenceControl final : public xentara::Control
{
public:
/// Initializes the control, setting up input and output data points.
void initialize(xentara::InitContext &context) override;
/// Executes the main control logic, calculating sum and difference and setting the outputs.
void step(xentara::RunContext &context) override;
private:
/// Xentara-managed input data points
xentara::Float64Input input1;
xentara::Float64Input input2;
/// Xentara-managed output data points
xentara::Float64Output sumOutput;
xentara::Float64Output differenceOutput;
};
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 Inputs and Outputs

The following JSON file configures the two inputs and two outputs for this control. Input 1, Input 2, Sum Output, and Difference Output are Xentara data points defined in the configuration.

{
"children": [
{
"@Skill.CPP.Control": {
"name": "Control",
"UUID": "5f89fb86-2259-4516-adc1-14a6d0fa7708",
"controlPath": "libcontrol.so",
"parameters": {
"input1": "Input 1",
"input2": "Input 2",
"sumOutput": "Sum Output",
"differenceOutput": "Difference Output"
}
}
},
{
"@Skill.SignalFlow.Register": {
"name": "Input 1",
"UUID": "2e57993a-9697-4177-b639-8fbd40c42f99",
"defaultValue": [ "float64", 10.0 ]
}
},
{
"@Skill.SignalFlow.Register": {
"name": "Input 2",
"UUID": "e33244fe-0f06-4fef-abd4-602ad05b65df",
"defaultValue": [ "float64", 5.0 ]
}
},
{
"@Skill.SignalFlow.Register": {
"name": "Sum Output",
"UUID": "3d63ba68-c8f0-463d-99a9-c8bd80d4e25a",
"defaultValue": [ "float64", 0.0 ]
}
},
{
"@Skill.SignalFlow.Register": {
"name": "Difference Output",
"UUID": "fc556d29-b89f-48ed-b564-33e3481624a7",
"defaultValue": [ "float64", 0.0 ]
}
},
{
"@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": "Control.step"
}
]
}
]
}
}
]
}
}
]
}

In this configuration:

  • Input 1 and Input 2 are Xentara-managed data points for the inputs.
  • Sum Output and Difference Output are Xentara-managed data points for the outputs, where we will store the calculated sum and difference.

Step 3: Implement the Xentara Data Point Logic for Sum and Difference

In the implementation file, SumDifferenceControl.cpp, we will initialize the input and output data points in the initialize method. In the step function, we will read the inputs, calculate their sum and difference, and write each result to the respective output.

Here is the code for SumDifferenceControl.cpp:

#include "SumDifferenceControl.hpp"
/// Registers the SumDifferenceControl with Xentara.
void SumDifferenceControl::initialize(xentara::InitContext &context)
{
// Initialize input and output data points
input1 = context.config().getDataPoint("input1");
input2 = context.config().getDataPoint("input2");
sumOutput = context.config().getDataPoint("sumOutput");
differenceOutput = context.config().getDataPoint("differenceOutput");
}
void SumDifferenceControl::step(xentara::RunContext &context)
{
// Read values from the inputs
auto value1 = input1.read(context);
auto value2 = input2.read(context);
// Calculate sum and write results to the output
double sum = value1 + value2;
sumOutput.write(context, sum);
// Calculate difference and write results to the output
double difference = value1 - value2;
differenceOutput.write(context, difference);
}
A class used to export your control class with Xentara.
Definition ControlExporter.hpp:38
auto config() const -> const Config &
Gets the configuration.

Conclusion

In this tutorial, we created a control that reads two inputs and calculates their sum and difference. Each result is written to a separate output, demonstrating a control operation with multiple inputs and outputs. This setup shows how to handle multiple values in control logic, expanding functionality to support more complex operations.