sr_deadband.hpp
Go to the documentation of this file.
1 /*
2 * Copyright 2011 Shadow Robot Company Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 
27 #ifndef _SR_DEADBAND_HPP_
28 #define _SR_DEADBAND_HPP_
29 
31 #include <deque>
32 
33 namespace sr_deadband
34 {
45 template<class T>
46 static inline bool simple_deadband(T value, T deadband)
47 {
48  return (fabs(value) > deadband);
49 }
50 
51 template<class T>
53 {
54 public:
62  last_demand(static_cast<T>(0.0)), entered_small_deadband(false)
63  {
64  };
65 
67  {
68  };
69 
84  bool is_in_deadband(T demand, T error, T deadband,
85  double deadband_multiplicator = 5.0,
86  unsigned int nb_errors_for_avg = 50)
87  {
88  bool is_in_deadband = false;
89 
90  last_errors.push_back(error);
91  double avg_error = 0.0;
92  for (unsigned int i = 0; i < last_errors.size(); ++i)
93  {
94  avg_error += last_errors[i];
95  }
96  avg_error /= last_errors.size();
97 
98  // Received a new command:
99  if (last_demand != demand)
100  {
101  entered_small_deadband = false;
102  last_demand = demand;
103  }
104  else
105  {
106  // check if we entered the small deadband
108  {
109  entered_small_deadband = fabs(avg_error) < deadband;
110  }
111 
112  // we always compute the error if we haven't entered the small deadband
114  {
115  is_in_deadband = false;
116  }
117  else
118  {
119  if (fabs(avg_error) > deadband_multiplicator * deadband)
120  {
121  // we're outside of the big deadband -> compute the error
122  is_in_deadband = false;
123  // when we leave the big deadband we wait until we're back in the small deadband before stopping the motor
124  entered_small_deadband = false;
125  }
126  else
127  {
128  // we're in the big deadband -> send a force demand of 0.0
129  is_in_deadband = true;
130  }
131  }
132  }
133 
134  if (last_errors.size() > nb_errors_for_avg)
135  {
136  last_errors.pop_front();
137  }
138 
139  return is_in_deadband;
140  };
141 
142 private:
143  T deadband;
145  std::deque<T> last_errors;
147 };
148 
149 } // namespace sr_deadband
150 
151 /* For the emacs weenies in the crowd.
152 Local Variables:
153  c-basic-offset: 2
154 End:
155 */
156 
157 #endif
static bool simple_deadband(T value, T deadband)
Definition: sr_deadband.hpp:46
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:84
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 Mon Feb 28 2022 23:52:19