limiters.h
Go to the documentation of this file.
1 //=================================================================================================
2 // Copyright (c) 2012-2016, Institute of Flight Systems and Automatic Control,
3 // Technische Universität Darmstadt.
4 // All rights reserved.
5 
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of hector_quadrotor nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 //=================================================================================================
28 
29 #ifndef HECTOR_QUADROTOR_INTERFACE_LIMITERS_H
30 #define HECTOR_QUADROTOR_INTERFACE_LIMITERS_H
31 
32 #include <ros/node_handle.h>
33 #include <limits>
34 #include <geometry_msgs/Wrench.h>
35 #include <geometry_msgs/Vector3.h>
36 #include <geometry_msgs/Twist.h>
37 #include <geometry_msgs/Point.h>
38 #include <hector_uav_msgs/AttitudeCommand.h>
39 #include <hector_uav_msgs/ThrustCommand.h>
40 #include <hector_uav_msgs/YawrateCommand.h>
41 
43 {
44 
45 using namespace geometry_msgs;
46 using namespace hector_uav_msgs;
47 
48 template <typename T>
50 {
51 public:
53  : min_(-std::numeric_limits<T>::infinity())
54  , max_( std::numeric_limits<T>::infinity())
55  {}
56 
57  void init(const ros::NodeHandle &nh, const std::string &field = std::string())
58  {
59  std::string prefix = !field.empty() ? field + "/" : "";
60  if (nh.hasParam(prefix + "max") || nh.hasParam(prefix + "min")) {
61  nh.param<T>(prefix + "max", max_, std::numeric_limits<T>::infinity());
62  nh.param<T>(prefix + "min", min_, -max_);
63  ROS_INFO_STREAM("Limits " << nh.getNamespace() + "/" + field << " initialized " << field << " with min " << min_ << " and max " << max_);
64  }
65  }
66 
67  T limit(const T &value)
68  {
69  return std::max(min_, std::min(max_, value));
70  }
71 
72  T operator()(const T &value)
73  {
74  return limit(value);
75  }
76 
77  T min_, max_;
78 };
79 
81 {
82 public:
83  void init(const ros::NodeHandle &nh, const std::string &field = std::string())
84  {
85  ros::NodeHandle field_nh(nh, field);
86  x.init(field_nh, "x");
87  y.init(field_nh, "y");
88  z.init(field_nh, "z");
89  }
90 
91  Point limit(const Point &input)
92  {
93  Point output;
94  output.x = x.limit(input.x);
95  output.y = y.limit(input.y);
96  output.z = z.limit(input.z);
97  return output;
98  }
99 
100  Point operator()(const Point &input)
101  {
102  return limit(input);
103  }
104 
106 };
107 
109 {
110 public:
111  void init(const ros::NodeHandle &nh, const std::string &field = std::string())
112  {
113  ros::NodeHandle field_nh(nh, field);
114  x.init(field_nh, "x");
115  y.init(field_nh, "y");
116  z.init(field_nh, "z");
117  field_nh.param("max", absolute_maximum, std::numeric_limits<double>::infinity());
118  field_nh.param("max_xy", absolute_maximum_xy, std::numeric_limits<double>::infinity());
119  }
120 
121  Vector3 limit(const Vector3 &input)
122  {
123  Vector3 output;
124  output.x = x.limit(input.x);
125  output.y = y.limit(input.y);
126  output.z = z.limit(input.z);
127 
128  double absolute_value_xy = sqrt(output.x * output.x + output.y * output.y);
129  if (absolute_value_xy > absolute_maximum_xy) {
130  output.x *= absolute_maximum_xy / absolute_value_xy;
131  output.y *= absolute_maximum_xy / absolute_value_xy;
132  output.z *= absolute_maximum_xy / absolute_value_xy;
133  }
134 
135  double absolute_value = sqrt(output.x * output.x + output.y * output.y + output.z * output.z);
136  if (absolute_value > absolute_maximum) {
137  output.x *= absolute_maximum / absolute_value;
138  output.y *= absolute_maximum / absolute_value;
139  output.z *= absolute_maximum / absolute_value;
140  }
141 
142  return output;
143  }
144 
145  Vector3 operator()(const Vector3 &input)
146  {
147  return limit(input);
148  }
149 
151  double absolute_maximum, absolute_maximum_xy;
152 };
153 
154 
156 {
157 public:
158  void init(const ros::NodeHandle &nh, const std::string &field = std::string())
159  {
160  ros::NodeHandle field_nh(nh, field);
161  linear.init(field_nh, "linear");
162  angular.init(field_nh, "angular");
163  }
164 
165  Twist limit(const Twist &input)
166  {
167  Twist output;
168  output.linear = linear.limit(input.linear);
169  output.angular = angular.limit(input.angular);
170  return output;
171  }
172 
173  Twist operator()(const Twist &input)
174  {
175  return limit(input);
176  }
177 
179 };
180 
182 {
183 public:
184  void init(const ros::NodeHandle &nh, const std::string &field = std::string())
185  {
186  ros::NodeHandle field_nh(nh, field);
187  force.init(field_nh, "force");
188  torque.init(field_nh, "torque");
189  }
190 
191  Wrench limit(const Wrench &input)
192  {
193  Wrench output;
194  output.force = force.limit(input.force);
195  output.torque = torque.limit(input.torque);
196  return output;
197  }
198 
199  Wrench operator()(const Wrench &input)
200  {
201  return limit(input);
202  }
203 
205 };
206 
208 {
209 public:
210  void init(const ros::NodeHandle &nh, const std::string &field = std::string())
211  {
212  ros::NodeHandle field_nh(nh, field);
213  roll.init(field_nh, "roll");
214  pitch.init(field_nh, "pitch");
215  field_nh.param("max_roll_pitch", absolute_max, std::numeric_limits<double>::infinity());
216  }
217 
218  AttitudeCommand limit(const AttitudeCommand &input)
219  {
220  AttitudeCommand output;
221  output.header = input.header;
222  output.roll = roll.limit(input.roll);
223  output.pitch = pitch.limit(input.pitch);
224 
225  double absolute_value = sqrt(output.roll * output.roll + output.pitch * output.pitch);
226  if (absolute_value > absolute_max) {
227  output.roll *= absolute_max / absolute_value;
228  output.pitch *= absolute_max / absolute_value;
229  }
230 
231  return output;
232  }
233 
234  AttitudeCommand operator()(const AttitudeCommand &input)
235  {
236  return limit(input);
237  }
238 
240  double absolute_max;
241 };
242 
244 {
245 
246 public:
247  void init(const ros::NodeHandle &nh, const std::string &field = std::string())
248  {
249  turnrate.init(nh, field);
250  }
251 
252  YawrateCommand limit(const YawrateCommand &input)
253  {
254  YawrateCommand output;
255  output.header = input.header;
256  output.turnrate = turnrate.limit(input.turnrate);
257  return output;
258  }
259 
260  YawrateCommand operator()(const YawrateCommand &input)
261  {
262  return limit(input);
263  }
264 
266 };
267 
269 {
270 public:
271  void init(const ros::NodeHandle &nh, const std::string &field = std::string())
272  {
273  thrust.init(nh, field);
274  }
275 
276  ThrustCommand limit(const ThrustCommand &input)
277  {
278  ThrustCommand output;
279  output.header = input.header;
280  output.thrust = thrust.limit(input.thrust);
281  return output;
282  }
283 
284  ThrustCommand operator()(const ThrustCommand &input)
285  {
286  return limit(input);
287  }
288 
290 };
291 
292 } // namespace hector_quadrotor_interface
293 
294 #endif // HECTOR_QUADROTOR_INTERFACE_LIMITERS_H
void init(const ros::NodeHandle &nh, const std::string &field=std::string())
Definition: limiters.h:111
Twist operator()(const Twist &input)
Definition: limiters.h:173
ThrustCommand operator()(const ThrustCommand &input)
Definition: limiters.h:284
YawrateCommand operator()(const YawrateCommand &input)
Definition: limiters.h:260
void init(const ros::NodeHandle &nh, const std::string &field=std::string())
Definition: limiters.h:184
AttitudeCommand operator()(const AttitudeCommand &input)
Definition: limiters.h:234
Wrench limit(const Wrench &input)
Definition: limiters.h:191
YawrateCommand limit(const YawrateCommand &input)
Definition: limiters.h:252
void init(const ros::NodeHandle &nh, const std::string &field=std::string())
Definition: limiters.h:158
Point operator()(const Point &input)
Definition: limiters.h:100
void init(const ros::NodeHandle &nh, const std::string &field=std::string())
Definition: limiters.h:247
void init(const ros::NodeHandle &nh, const std::string &field=std::string())
Definition: limiters.h:210
bool param(const std::string &param_name, T &param_val, const T &default_val) const
const std::string & getNamespace() const
void init(const ros::NodeHandle &nh, const std::string &field=std::string())
Definition: limiters.h:57
Point limit(const Point &input)
Definition: limiters.h:91
Twist limit(const Twist &input)
Definition: limiters.h:165
INLINE Rall1d< T, V, S > sqrt(const Rall1d< T, V, S > &arg)
void init(const ros::NodeHandle &nh, const std::string &field=std::string())
Definition: limiters.h:271
AttitudeCommand limit(const AttitudeCommand &input)
Definition: limiters.h:218
#define ROS_INFO_STREAM(args)
Vector3 limit(const Vector3 &input)
Definition: limiters.h:121
Wrench operator()(const Wrench &input)
Definition: limiters.h:199
bool hasParam(const std::string &key) const
Vector3 operator()(const Vector3 &input)
Definition: limiters.h:145
ThrustCommand limit(const ThrustCommand &input)
Definition: limiters.h:276
void init(const ros::NodeHandle &nh, const std::string &field=std::string())
Definition: limiters.h:83


hector_quadrotor_interface
Author(s): Johannes Meyer , Paul Bovbel
autogenerated on Mon Jun 10 2019 13:36:46