ChimeraTK-ApplicationCore 04.06.00
Loading...
Searching...
No Matches
LimitValue.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Deutsches Elektronen-Synchrotron DESY, MSK, ChimeraTK Project <chimeratk-support@desy.de>
2// SPDX-License-Identifier: LGPL-3.0-or-later
3#pragma once
4
5#include "ApplicationModule.h"
6#include "ScalarAccessor.h"
7
8namespace ChimeraTK {
9
10 template<typename UserType>
13
14 ScalarPushInput<UserType> input{this, "input", "", "The input value to be limited into the range."};
15 ScalarOutput<UserType> output{this, "output", "", "The output value after limiting."};
17 this, "isLimited", "", "Boolean set to true if the value was limited and to false otherwise."};
18
19 void applyLimit(UserType min, UserType max) {
20 bool wasLimited = isLimited;
21
22 // clamp input value into given range
23 UserType value = input;
24 if(value > max) {
25 output = max;
26 isLimited = true;
27 }
28 else if(value < min) {
29 output = min;
30 isLimited = true;
31 }
32 else {
33 output = value;
34 isLimited = false;
35 }
36
37 // write output. isLimited is only written when changed
38 output.write();
39 if(isLimited != wasLimited) isLimited.write();
40 wasLimited = isLimited;
41 }
42 };
43
44 template<typename UserType>
45 struct LimitValue : public LimitValueModuleBase<UserType> {
46 using LimitValueModuleBase<UserType>::LimitValueModuleBase;
47 ScalarPushInput<UserType> min{this, "min", "", "The minimum allowed value."};
48 ScalarPushInput<UserType> max{this, "max", "", "The maximum allowed value."};
49 using LimitValueModuleBase<UserType>::input;
51
52 void mainLoop() {
53 auto readGroup = this->readAnyGroup();
54 while(true) {
56 // wait for new input values (at the end, since we want to process the
57 // initial values first)
58 readGroup.readAny();
59 }
60 }
61 };
62
63 template<typename UserType, UserType min, UserType max>
64 struct FixedLimitValue : public LimitValueModuleBase<UserType> {
65 using LimitValueModuleBase<UserType>::LimitValueModuleBase;
66 using LimitValueModuleBase<UserType>::input;
68
69 void mainLoop() {
70 while(true) {
71 applyLimit(min, max);
72 // wait for new input values (at the end, since we want to process the
73 // initial values first)
74 input.read();
75 }
76 }
77 };
78
79} // namespace ChimeraTK
ApplicationModule()=default
Default constructor: Allows late initialisation of modules (e.g.
ChimeraTK::ReadAnyGroup readAnyGroup()
Create a ChimeraTK::ReadAnyGroup for all readable variables in this Module.
Definition Module.cc:54
bool write(ChimeraTK::VersionNumber versionNumber)=delete
Convenience class for input scalar accessors with UpdateMode::push.
InvalidityTracer application module.
void mainLoop()
To be implemented by the user: function called in a separate thread executing the main loop of the mo...
Definition LimitValue.h:69
ScalarPushInput< UserType > min
Definition LimitValue.h:47
ScalarPushInput< UserType > max
Definition LimitValue.h:48
void mainLoop()
To be implemented by the user: function called in a separate thread executing the main loop of the mo...
Definition LimitValue.h:52
ScalarOutput< UserType > output
Definition LimitValue.h:15
ScalarOutput< int > isLimited
Definition LimitValue.h:16
void applyLimit(UserType min, UserType max)
Definition LimitValue.h:19
ScalarPushInput< UserType > input
Definition LimitValue.h:14
Convenience class for output scalar accessors (always UpdateMode::push)