123 lines
2.4 KiB
C++
123 lines
2.4 KiB
C++
|
|
/*
|
|||
|
|
* SoftSwitch.cpp
|
|||
|
|
*
|
|||
|
|
* Created on: 30 <EFBFBD><EFBFBD><EFBFBD>. 2020 <EFBFBD>.
|
|||
|
|
* Author: LeonidTitov
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
#include "SoftSwitch.hh"
|
|||
|
|
|
|||
|
|
#include <cmath>
|
|||
|
|
|
|||
|
|
bool processing::logic::SoftSwitch::Setting::isValid() {
|
|||
|
|
|
|||
|
|
using namespace std;
|
|||
|
|
|
|||
|
|
bool result = isfinite( toggle_time ) and toggle_time >= 0.0f;
|
|||
|
|
|
|||
|
|
if( not result )
|
|||
|
|
toggle_time = -1.0f;
|
|||
|
|
|
|||
|
|
return result;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
processing::logic::SoftSwitch::SoftSwitch(systemic::ISignal &a_signal,
|
|||
|
|
systemic::IStatus &a_valid,
|
|||
|
|
systemic::ISignal &b_signal,
|
|||
|
|
systemic::IStatus &b_valid ) : a(a_signal, a_valid, a_valid), b(b_signal, b_valid, a.active ? false : b_valid), output(NAN) {}
|
|||
|
|
|
|||
|
|
void processing::logic::SoftSwitch::configure( Setting & setting ) {
|
|||
|
|
|
|||
|
|
_Tp = setting.toggle_time;
|
|||
|
|
dpart_dt = _Ts / _Tp;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void processing::logic::SoftSwitch::setSampleTime(float ts_in_second) {
|
|||
|
|
|
|||
|
|
_Ts = ts_in_second;
|
|||
|
|
dpart_dt = _Ts / _Tp;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void processing::logic::SoftSwitch::process() {
|
|||
|
|
|
|||
|
|
a.update_correctness();
|
|||
|
|
b.update_correctness();
|
|||
|
|
|
|||
|
|
if( not a.active and not b.active ) {
|
|||
|
|
delta = d_delta = 0.0f;
|
|||
|
|
|
|||
|
|
{
|
|||
|
|
a.select();
|
|||
|
|
} if( not a.active ) {
|
|||
|
|
b.select();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if( a.invalcheck() ) {
|
|||
|
|
|
|||
|
|
b.select();
|
|||
|
|
delta = a.value - b.value;
|
|||
|
|
d_delta = - delta * dpart_dt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if( b.invalcheck() ) {
|
|||
|
|
|
|||
|
|
a.select();
|
|||
|
|
delta = b.value - a.value;
|
|||
|
|
d_delta = - delta * dpart_dt;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
delta += d_delta;
|
|||
|
|
|
|||
|
|
using namespace std;
|
|||
|
|
|
|||
|
|
if( signbit(d_delta) == signbit(delta) ) {
|
|||
|
|
delta = d_delta = 0.0f;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
output = a.active ? a.value + delta :
|
|||
|
|
b.active ? b.value + delta : NAN;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const bool & processing::logic::SoftSwitch::activeSignalA() const {
|
|||
|
|
|
|||
|
|
return a.active;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const bool & processing::logic::SoftSwitch::activeSignalB() const {
|
|||
|
|
|
|||
|
|
return b.active;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
bool processing::logic::SoftSwitch::ValidSignal::invalcheck() {
|
|||
|
|
|
|||
|
|
bool invalidation = false;
|
|||
|
|
|
|||
|
|
if( correctness ) {
|
|||
|
|
value = signal;
|
|||
|
|
} else if( active ) {
|
|||
|
|
active = false;
|
|||
|
|
invalidation = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return invalidation;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void processing::logic::SoftSwitch::ValidSignal::update_correctness() {
|
|||
|
|
|
|||
|
|
correctness = valid;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void processing::logic::SoftSwitch::ValidSignal::select() {
|
|||
|
|
|
|||
|
|
active = correctness;
|
|||
|
|
|
|||
|
|
}
|