EEROS  1.0.0.0
API for the EEROS Real-Time Robotics Framework
Switch.hpp
Go to the documentation of this file.
1 #ifndef ORG_EEROS_CONTROL_SWITCH_HPP_
2 #define ORG_EEROS_CONTROL_SWITCH_HPP_
3 
9 // #include <cmath>
10 
11 namespace eeros {
12  namespace control {
13  using namespace safety;
14 
15  template < uint8_t N = 2, typename T = double >
16  class Switch : public Block1o<T> {
17  public:
18  Switch(uint8_t initInputIndex) : currentInput(initInputIndex) {
19  for(uint8_t i = 0; i < N; i++) in[i].setOwner(this);
20  }
21 
22  virtual void run() {
23  auto val = this->in[currentInput].getSignal().getValue();
24  if (armed && !switched) {
25  if (val < (switchLevel + delta) && val > (switchLevel - delta)) {
26  switchToInput(nextInput);
27  switched = true;
28  armed = false;
29  if(safetySystem != nullptr && safetyEvent != nullptr) {
30  safetySystem->triggerEvent(*safetyEvent);
31  }
32  }
33  }
34 
35  this->out.getSignal().setValue(this->in[currentInput].getSignal().getValue());
36  this->out.getSignal().setTimestamp(this->in[currentInput].getSignal().getTimestamp());
37  }
38 
39  virtual Input<T>& getIn(uint8_t index) {
40  return in[index];
41  }
42 
43  virtual bool switchToInput(uint8_t index) {
44  if(index >= 0 && index < N) {
45  currentInput = index;
46  return true;
47  }
48  return false;
49  }
50 
51  virtual uint8_t getCurrentInput() const {
52  return currentInput;
53  }
54 
56  safetySystem = &ss;
57  safetyEvent = &e;
58  }
59 
60  virtual void setCondition(T switchLevel, T delta, uint8_t index) {
61  this->switchLevel = switchLevel;
62  this->delta = delta;
63  nextInput = index;
64  }
65 
66  virtual void arm() {
67  armed = true;
68  switched = false;
69  }
70 
71  virtual bool triggered() const {
72  return switched;
73  }
74 
75  protected:
76  Input<T> in[N];
77  uint8_t currentInput, nextInput;
78  T switchLevel, delta;
79  bool armed = false;
80  bool switched = false;
81  SafetySystem* safetySystem = nullptr;
82  SafetyEvent* safetyEvent = nullptr;
83  };
84  };
85 };
86 
87 #endif /* ORG_EEROS_CONTROL_SWITCH_HPP_ */
Safety system.
Definition: SafetySystem.hpp:19
Definition: Switch.hpp:16
uint8_t nextInput
Definition: Switch.hpp:77
virtual Input< T > & getIn(uint8_t index)
Definition: Switch.hpp:39
virtual bool triggered() const
Definition: Switch.hpp:71
T switchLevel
Definition: Switch.hpp:78
Definition: Block1o.hpp:12
Definition: Config.hpp:14
virtual bool switchToInput(uint8_t index)
Definition: Switch.hpp:43
virtual uint8_t getCurrentInput() const
Definition: Switch.hpp:51
virtual void arm()
Definition: Switch.hpp:66
virtual void setCondition(T switchLevel, T delta, uint8_t index)
Definition: Switch.hpp:60
virtual void registerSafetyEvent(SafetySystem &ss, SafetyEvent &e)
Definition: Switch.hpp:55
virtual void run()
Definition: Switch.hpp:22
Switch(uint8_t initInputIndex)
Definition: Switch.hpp:18
Definition: SafetyLevel.hpp:21
Definition: Input.hpp:13