EEROS  1.0.0.0
API for the EEROS Real-Time Robotics Framework
Transition.hpp
Go to the documentation of this file.
1 #ifndef ORG_EEROS_CONTROL_TRANSITION_HPP_
2 #define ORG_EEROS_CONTROL_TRANSITION_HPP_
3 
4 #include <mutex>
8 #include <iostream>
9 namespace eeros {
10  namespace control {
11 
12  template < typename T > class Transition;
13 
14  template < typename T = double >
15  class TransitionInBlock : public Block1i<T> {
16  public:
18  virtual ~TransitionInBlock() { }
19 
20  virtual void run() {
21  if (container->steady) {
22  container->mtx.lock();
23  container->in = this->getIn().getSignal();
24  container->mtx.unlock();
25 // std::cout << " do in: " << this->getIn().getSignal().getTimestamp() << std::endl;
26  } else {
27  if (up) { // up
28  container->mtx.lock();
29  container->prevIn = container->in;
30  container->in = this->getIn().getSignal();
31  container->refresh = true;
32  container->mtx.unlock();
33  } else { //down
34  container->mtx.lock();
35  auto val = this->getIn().getSignal();
36  container->buf.push_back(val);
37  container->mtx.unlock();
38  }
39  }
40  }
41 
42  bool up;
43 
44  protected:
46  };
47 
48  template < typename T = double >
49  class TransitionOutBlock : public Block1i1o<T> {
50  public:
51  TransitionOutBlock(Transition<T>* c) : container(c), count(0) {in.clear(); prevIn.clear();}
52  virtual ~TransitionOutBlock() { }
53 
54  virtual void run() {
55  if (container->steady) {
56  container->mtx.lock();
57  this->getOut().getSignal().setValue(container->in.getValue());
58  this->getOut().getSignal().setTimestamp(container->in.getTimestamp());
59  container->mtx.unlock();
60 // std::cout << " do out" << std::endl;
61  } else {
62  if (up) { // up
63  if (container->refresh) {
64  container->mtx.lock();
65  prevIn = container->prevIn;
66  in = container->in;
67  container->refresh = false;
68  container->mtx.unlock();
69  count = 0;
70  dVal = (in.getValue() - prevIn.getValue()) / container->ratio;
71  dTime= (in.getTimestamp() - prevIn.getTimestamp()) / container->ratio;
72  }
73  T val = prevIn.getValue() + dVal * count;
74  this->getOut().getSignal().setValue(val);
75  timestamp_t time = prevIn.getTimestamp() + count * dTime;
76  this->getOut().getSignal().setTimestamp(time);
77  count++;
78  } else { //down
79  auto time = this->getIn().getSignal().getTimestamp();
80  container->mtx.lock();
81  int i = 0;
82  while (i < container->buf.size() && time > container->buf[i].getTimestamp()) i++;
83  if (i > 0) i--;
84  Signal<T> sig = container->buf[i];
85  container->buf.clear();
86  container->mtx.unlock();
87  this->getOut().getSignal().setValue(sig.getValue());
88  this->getOut().getSignal().setTimestamp(sig.getTimestamp());
89  }
90  }
91  }
92 
93  bool up;
94 
95  protected:
98  T dVal;
99  double dTime;
100  uint32_t count;
101  };
102 
103  template < typename T = double >
104  class Transition {
105  friend class TransitionInBlock<T>;
106  friend class TransitionOutBlock<T>;
107  public:
108  Transition(double ratio, bool steady = false) : ratio(ratio), inBlock(this), outBlock(this), steady(steady) {
109  if (ratio >= 1.0) { // slow to fast time domain
110  inBlock.up = true;
111  outBlock.up = true;
112  refresh = false;
113  in.clear();
114  prevIn.clear();
115  } else { // fast to slow time domain
116  inBlock.up = false;
117  outBlock.up = false;
118  bufSize = 1 / ratio;
119  }
120  }
121  virtual ~Transition() { }
122 
125 
126  private:
127  std::vector<Signal<T>> buf;
128  Signal<T> in, prevIn;
129  bool refresh;
130  bool steady;
131  double ratio;
132  uint32_t bufSize;
133  std::mutex mtx;
134  };
135 
136  /********** Print functions **********/
137  template <typename T>
138  std::ostream& operator<<(std::ostream& os, Transition<T>& t) {
139  os << "Block transition: '" << t.getName() << "'";
140  }
141 
142  };
143 };
144 
145 #endif /* ORG_EEROS_CONTROL_TRANSITION_HPP_ */
uint32_t count
Definition: Transition.hpp:100
virtual T getValue() const
Definition: Signal.hpp:49
Definition: Block1i.hpp:12
virtual void run()
Definition: Transition.hpp:20
Transition< T > * container
Definition: Transition.hpp:96
bool up
Definition: Transition.hpp:42
virtual ~TransitionInBlock()
Definition: Transition.hpp:18
Definition: Config.hpp:14
virtual void run()
Definition: Transition.hpp:54
TransitionOutBlock< T > outBlock
Definition: Transition.hpp:124
double dTime
Definition: Transition.hpp:99
TransitionInBlock(Transition< T > *c)
Definition: Transition.hpp:17
Transition(double ratio, bool steady=false)
Definition: Transition.hpp:108
Definition: Transition.hpp:12
virtual ~Transition()
Definition: Transition.hpp:121
bool up
Definition: Transition.hpp:93
Definition: Block1i1o.hpp:12
virtual Input< T > & getIn()
Definition: Block1i.hpp:16
Signal< T > prevIn
Definition: Transition.hpp:97
virtual timestamp_t getTimestamp() const
Definition: Signal.hpp:62
virtual ~TransitionOutBlock()
Definition: Transition.hpp:52
TransitionInBlock< T > inBlock
Definition: Transition.hpp:123
Transition< T > * container
Definition: Transition.hpp:45
Definition: Signal.hpp:17
Definition: Transition.hpp:15
Definition: Transition.hpp:49
Input< T > in
Definition: Block1i.hpp:21
uint64_t timestamp_t
Definition: types.hpp:12
T dVal
Definition: Transition.hpp:98
TransitionOutBlock(Transition< T > *c)
Definition: Transition.hpp:51