EtherCAT Graphical User Interface Code 0.0.1
EtherCAT GUI that uses different communication protocols with EtherCAT Master server.
Loading...
Searching...
No Matches
ec_gui_utils.h
1#ifndef __EC_GUI_UTILS_H__
2#define __EC_GUI_UTILS_H__
3
4
5#include <QtUiTools/QtUiTools>
6#include <QWidget>
7#include <QLabel>
8#include <functional>
9
10#include <stdexcept>
11#include <boost/variant.hpp>
12
13template <typename SignalType>
15
16public:
17
18 typedef std::shared_ptr<SecondOrderFilter<SignalType>> Ptr;
19
21 _omega(1.0),
22 _eps(0.8),
23 _ts(0.01),
24 _reset_has_been_called(false)
25 {
26 computeCoeff();
27 }
28
29 SecondOrderFilter(double omega, double eps, double ts, const SignalType& initial_state):
30 _omega(omega),
31 _eps(eps),
32 _ts(ts),
33 _reset_has_been_called(false)
34 {
35 computeCoeff();
36 reset(initial_state);
37 }
38
39 void reset(const SignalType& initial_state){
40 _reset_has_been_called = true;
41 _u = initial_state;
42 _y = initial_state;
43 _yd = initial_state;
44 _ydd = initial_state;
45 _udd = initial_state;
46 _ud = initial_state;
47 }
48
49 const SignalType& process(const SignalType& input){
50
51 if(!_reset_has_been_called) reset(input*0);
52
53
54 _ydd = _yd;
55 _yd = _y;
56 _udd = _ud;
57 _ud = _u;
58
59
60 _u = input;
61 _y = 1.0/_a0 * ( _u + _b1*_ud + _b2*_udd - _a1*_yd - _a2*_ydd );
62
63 return _y;
64 }
65
66 const SignalType& getOutput() const {
67 return _y;
68 }
69
70 void setOmega(double omega){
71 _omega = omega;
72 computeCoeff();
73 }
74
75 double getOmega()
76 {
77 return _omega;
78 }
79
80 void setDamping(double eps){
81 _eps = eps;
82 computeCoeff();
83 }
84
85 double getDamping()
86 {
87 return _eps;
88 }
89
90 void setTimeStep(double ts){
91 _ts = ts;
92 computeCoeff();
93 }
94
95 double getTimeStep()
96 {
97 return _ts;
98 }
99
100private:
101
102 void computeCoeff(){
103 _b1 = 2.0;
104 _b2 = 1.0;
105
106 _a0 = 1.0 + 4.0*_eps/(_omega*_ts) + 4.0/std::pow(_omega*_ts, 2.0);
107 _a1 = 2 - 8.0/std::pow(_omega*_ts, 2.0);
108 _a2 = 1.0 + 4.0/std::pow(_omega*_ts, 2.0) - 4.0*_eps/(_omega*_ts);
109
110 }
111
112 double _omega;
113 double _eps;
114 double _ts;
115
116 double _b1, _b2;
117 double _a0, _a1, _a2;
118
119 bool _reset_has_been_called;
120
121 SignalType _y, _yd, _ydd, _u, _ud, _udd;
122
123};
124
125template <size_t n, typename... T>
126boost::variant<T...> dynamic_get_impl(size_t i, const std::tuple<T...>& tpl)
127{
128 if (i == n)
129 return std::get<n>(tpl);
130 else if (n == sizeof...(T) - 1)
131 throw std::out_of_range("Tuple element out of range.");
132 else
133 return dynamic_get_impl<(n < sizeof...(T)-1 ? n+1 : 0)>(i, tpl);
134}
135
136template <typename... T>
137boost::variant<T...> dynamic_get(size_t i, const std::tuple<T...>& tpl)
138{
139 return dynamic_get_impl<0>(i, tpl);
140}
141
142#endif // EC_GUI_UTILS_H
Definition ec_gui_utils.h:14