5 #ifndef LODESTAR_DEADZONEBLOCK_HPP
6 #define LODESTAR_DEADZONEBLOCK_HPP
9 #include "Lodestar/blocks/Block.hpp"
10 #include "Lodestar/aux/CompileTimeQualifiers.hpp"
11 #include "Eigen/Dense"
16 enum class DeadzoneBlockOperator {
21 enum class DeadzoneBlockParameter {
26 template<
typename TType,
27 DeadzoneBlockOperator TOps = DeadzoneBlockOperator::Scalar,
28 DeadzoneBlockParameter TPar = DeadzoneBlockParameter::Parametric>
30 static_assert(!::std::is_same<TType, TType>::value,
31 "DeadzoneBlock not defined for this type.");
34 template<
typename TType, DeadzoneBlockOperator TOps>
38 DeadzoneBlockParameter::Parametric
43 ::std::tuple<TType, TType>
50 DeadzoneBlockParameter::Parametric
57 ::std::tuple<TType, TType>
67 return this->
template p<0>();
70 TType &lower(
const TType lo)
72 this->
template p<0>() = lo;
73 return this->
template p<0>();
78 return this->
template p<0>();
83 return this->
template p<1>();
86 TType &upper(
const TType up)
88 this->
template p<1>() = up;
89 return this->
template p<1>();
94 return this->
template p<1>();
100 this->equation = ::std::bind(
101 &type::triggerFunction,
103 ::std::placeholders::_1
107 TType deadzone(
const TType x)
const
109 return (x < lower() || x > upper()) ? x : 0;
112 void triggerFunction(
Base &b)
114 b.template o<0>() = deadzone(b.template i<0>().object);
118 template<
typename TType, DeadzoneBlockOperator TOps>
122 DeadzoneBlockParameter::AdditionalInput
125 ::std::tuple<TType, TType, TType>,
134 DeadzoneBlockParameter::AdditionalInput
139 ::std::tuple<TType, TType, TType>,
151 return this->
template i<1>();
156 this->
template i<1>() = lo;
157 return this->
template i<1>();
162 this->
template i<1>() = lo;
163 return this->
template i<1>();
168 return this->
template i<1>();
173 return this->
template i<2>();
178 this->
template i<2>() = up;
179 return this->
template i<2>();
184 this->
template i<2>() = up;
185 return this->
template i<2>();
190 return this->
template i<2>();
196 this->equation = ::std::bind(
197 &type::triggerFunction,
199 ::std::placeholders::_1
203 TType deadzone(
const TType x)
const
205 return (x < lower().
object || x > upper().
object) ? x : 0;
208 void triggerFunction(
Base &b)
210 b.template o<0>() = deadzone(b.template i<0>().object);
214 template<
typename TScalar,
int TRows,
int TCols>
216 Eigen::Matrix<TScalar, TRows, TCols>,
217 DeadzoneBlockOperator::Scalar,
218 DeadzoneBlockParameter::Parametric
221 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
222 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
223 ::std::tuple<TScalar, TScalar>
228 Eigen::Matrix<TScalar, TRows, TCols>,
229 DeadzoneBlockOperator::Scalar,
230 DeadzoneBlockParameter::Parametric
235 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
236 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
237 ::std::tuple<TScalar, TScalar>
247 return this->
template p<0>();
250 TScalar &lower(
const TScalar lo)
252 this->
template p<0>() = lo;
253 return this->
template p<0>();
256 TScalar lower()
const
258 return this->
template p<0>();
263 return this->
template p<1>();
266 TScalar &upper(
const TScalar up)
268 this->
template p<1>() = up;
269 return this->
template p<1>();
272 TScalar upper()
const
274 return this->
template p<1>();
280 this->equation = ::std::bind(
281 &type::triggerFunction,
283 ::std::placeholders::_1
287 TScalar deadzone(
const TScalar x)
const
289 return ((x < lower() || x > upper()) ? x : 0);
292 void triggerFunction(
Base &b)
294 b.template o<0>() = b.template i<0>().object.unaryExpr(
298 ::std::placeholders::_1
304 template<
typename TScalar,
int TRows,
int TCols>
306 Eigen::Matrix<TScalar, TRows, TCols>,
307 DeadzoneBlockOperator::Scalar,
308 DeadzoneBlockParameter::AdditionalInput
311 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>, TScalar, TScalar>,
312 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
318 Eigen::Matrix<TScalar, TRows, TCols>,
319 DeadzoneBlockOperator::Scalar,
320 DeadzoneBlockParameter::AdditionalInput
325 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>, TScalar, TScalar>,
326 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
337 return this->
template i<1>();
342 this->
template i<1>() = lo;
343 return this->
template i<1>();
348 this->
template i<1>() = lo;
349 return this->
template i<1>();
354 return this->
template i<1>();
359 return this->
template i<2>();
364 this->
template i<2>() = up;
365 return this->
template i<2>();
370 this->
template i<2>() = up;
371 return this->
template i<2>();
376 return this->
template i<2>();
382 this->equation = ::std::bind(
383 &type::triggerFunction,
385 ::std::placeholders::_1
389 TScalar deadzone(
const TScalar x)
const
391 return ((x < lower().
object || x > upper().
object) ? x : 0);
394 void triggerFunction(
Base &b)
396 b.template o<0>() = b.template i<0>().object.unaryExpr(
400 ::std::placeholders::_1
406 template<
typename TScalar,
int TRows,
int TCols>
408 Eigen::Matrix<TScalar, TRows, TCols>,
409 DeadzoneBlockOperator::Elementwise,
410 DeadzoneBlockParameter::Parametric
413 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
414 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
415 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>, Eigen::Matrix<TScalar, TRows, TCols>>
420 Eigen::Matrix<TScalar, TRows, TCols>,
421 DeadzoneBlockOperator::Elementwise,
422 DeadzoneBlockParameter::Parametric
427 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
428 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
429 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>, Eigen::Matrix<TScalar, TRows, TCols>>
432 using BoundMatrix = Eigen::Matrix<TScalar, TRows, TCols>;
441 return this->
template p<0>();
444 BoundMatrix &lower(
const BoundMatrix &lo)
446 this->
template p<0>() = lo;
447 return this->
template p<0>();
450 BoundMatrix lower()
const
452 return this->
template p<0>();
457 return this->
template p<1>();
460 BoundMatrix &upper(
const BoundMatrix &up)
462 this->
template p<1>() = up;
463 return this->
template p<1>();
466 BoundMatrix upper()
const
468 return this->
template p<1>();
474 this->equation = ::std::bind(
475 &type::triggerFunction,
477 ::std::placeholders::_1
481 TScalar deadzone(
const TScalar x,
int i = 0,
int j = 0)
const
483 return (x < lower()(i, j) || x > upper()(i, j)) ? x : 0;
486 int row(
int idx)
const
488 return floor<int, double>(idx / TCols);
491 int col(
int idx)
const
493 return idx - floor<int, double>(idx / TCols) * TCols;
496 void triggerFunction(
Base &b)
502 for (
auto x: b.template i<0>().object.reshaped()) {
506 b.template o<0>().object(i, j) = deadzone(x, i, j);
510 b.template o<0>().propagate();
514 template<
typename TScalar,
int TRows,
int TCols>
516 Eigen::Matrix<TScalar, TRows, TCols>,
517 DeadzoneBlockOperator::Elementwise,
518 DeadzoneBlockParameter::AdditionalInput
521 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>, Eigen::Matrix<TScalar, TRows, TCols>, Eigen::Matrix<TScalar, TRows, TCols>>,
522 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
528 Eigen::Matrix<TScalar, TRows, TCols>,
529 DeadzoneBlockOperator::Elementwise,
530 DeadzoneBlockParameter::AdditionalInput
535 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>, Eigen::Matrix<TScalar, TRows, TCols>, Eigen::Matrix<TScalar, TRows, TCols>>,
536 ::std::tuple<Eigen::Matrix<TScalar, TRows, TCols>>,
540 using BoundMatrix = Eigen::Matrix<TScalar, TRows, TCols>;
549 return this->
template i<1>();
554 this->
template i<1>() = lo;
555 return this->
template i<1>();
560 this->
template i<1>() = lo;
561 return this->
template i<1>();
566 return this->
template i<1>();
571 return this->
template i<2>();
576 this->
template i<2>() = up;
577 return this->
template i<2>();
582 this->
template i<2>() = up;
583 return this->
template i<2>();
588 return this->
template i<2>();
594 this->equation = ::std::bind(
595 &type::triggerFunction,
597 ::std::placeholders::_1
601 TScalar deadzone(
const TScalar x,
int i = 0,
int j = 0)
const
603 return (x < lower().
object(i, j) || x > upper().
object(i, j)) ? x : 0;
606 int row(
int idx)
const
608 return floor<int, double>(idx / TCols);
611 int col(
int idx)
const
613 return idx - floor<int, double>(idx / TCols) * TCols;
616 void triggerFunction(
Base &b)
622 for (
auto x: b.template i<0>().object.reshaped()) {
626 b.template o<0>().object(i, j) = deadzone(x, i, j);
630 b.template o<0>().propagate();
634 template<
typename TType, DeadzoneBlockOperator TOps>
638 template<
typename TType, std::DeadzoneBlockOperator TOps>
643 directFeedthrough =
true
647 using Base =
typename type::Base;
655 static const ::std::array<::std::string, kIns> inTypes;
656 static const ::std::array<::std::string, kOuts> outTypes;
657 static const ::std::array<::std::string, kPars> parTypes;
659 static const ::std::array<::std::string, 3> templateTypes;
662 template<
typename TType, std::DeadzoneBlockOperator TOps>
663 const ::std::array<::std::string, BlockTraits<std::DeadzoneBlock<TType, TOps, std::DeadzoneBlockParameter::Parametric>>::kIns>
665 {demangle(
typeid(TType).name())};
667 template<
typename TType, std::DeadzoneBlockOperator TOps>
668 const ::std::array<::std::string, BlockTraits<std::DeadzoneBlock<TType, TOps, std::DeadzoneBlockParameter::Parametric>>::kOuts>
669 BlockTraits<std::DeadzoneBlock<TType, TOps, std::DeadzoneBlockParameter::Parametric>>::outTypes =
670 {demangle(
typeid(TType).name())};
672 template<
typename TType, std::DeadzoneBlockOperator TOps>
673 const ::std::array<::std::string, BlockTraits<std::DeadzoneBlock<TType, TOps, std::DeadzoneBlockParameter::Parametric>>::kPars>
674 BlockTraits<std::DeadzoneBlock<TType, TOps, std::DeadzoneBlockParameter::Parametric>>::parTypes =
675 {demangle(
typeid(TType).name()), demangle(
typeid(TType).name())};
677 template<
typename TType, std::DeadzoneBlockOperator TOps>
678 const ::std::array<::std::string, 3>
679 BlockTraits<std::DeadzoneBlock<TType, TOps, std::DeadzoneBlockParameter::Parametric>>::templateTypes =
680 {demangle(
typeid(TType).name()), demangle(
typeid(std::DeadzoneBlockOperator).name()), demangle(
typeid(std::DeadzoneBlockParameter).name())};
685 #endif //LODESTAR_DEADZONEBLOCK_HPP