sr_deadband.hpp
Go to the documentation of this file.
1 
26 #ifndef _SR_DEADBAND_HPP_
27 #define _SR_DEADBAND_HPP_
28 
30 #include <deque>
31 
32 namespace sr_deadband
33 {
44 template<class T>
45 static inline bool simple_deadband(T value, T deadband)
46 {
47  return (fabs(value) > deadband);
48 }
49 
50 template<class T>
52 {
53 public:
61  last_demand(static_cast<T>(0.0)), entered_small_deadband(false)
62  {
63  };
64 
66  {
67  };
68 
83  bool is_in_deadband(T demand, T error, T deadband,
84  double deadband_multiplicator = 5.0,
85  unsigned int nb_errors_for_avg = 50)
86  {
87  bool is_in_deadband = false;
88 
89  last_errors.push_back(error);
90  double avg_error = 0.0;
91  for (unsigned int i = 0; i < last_errors.size(); ++i)
92  {
93  avg_error += last_errors[i];
94  }
95  avg_error /= last_errors.size();
96 
97  // Received a new command:
98  if (last_demand != demand)
99  {
100  entered_small_deadband = false;
101  last_demand = demand;
102  }
103  else
104  {
105  // check if we entered the small deadband
107  {
108  entered_small_deadband = fabs(avg_error) < deadband;
109  }
110 
111  // we always compute the error if we haven't entered the small deadband
113  {
114  is_in_deadband = false;
115  }
116  else
117  {
118  if (fabs(avg_error) > deadband_multiplicator * deadband)
119  {
120  // we're outside of the big deadband -> compute the error
121  is_in_deadband = false;
122  // when we leave the big deadband we wait until we're back in the small deadband before stopping the motor
123  entered_small_deadband = false;
124  }
125  else
126  {
127  // we're in the big deadband -> send a force demand of 0.0
128  is_in_deadband = true;
129  }
130  }
131  }
132 
133  if (last_errors.size() > nb_errors_for_avg)
134  {
135  last_errors.pop_front();
136  }
137 
138  return is_in_deadband;
139  };
140 
141 private:
142  T deadband;
144  std::deque<T> last_errors;
146 };
147 
148 } // namespace sr_deadband
149 
150 /* For the emacs weenies in the crowd.
151 Local Variables:
152  c-basic-offset: 2
153 End:
154 */
155 
156 #endif
static bool simple_deadband(T value, T deadband)
Definition: sr_deadband.hpp:45
bool is_in_deadband(T demand, T error, T deadband, double deadband_multiplicator=5.0, unsigned int nb_errors_for_avg=50)
Definition: sr_deadband.hpp:83
This is a header library used to implement some useful math functions. It is used in our different pa...


sr_utilities
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:49