PX4 Firmware
PX4 Autopilot Software http://px4.io
Functions.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file Functions.hpp
36  *
37  * collection of rather simple mathematical functions that get used over and over again
38  */
39 
40 #pragma once
41 
42 #include "Limits.hpp"
43 
44 namespace math
45 {
46 
47 // Type-safe signum function
48 template<typename T>
49 int sign(T val)
50 {
51  return (T(FLT_EPSILON) < val) - (val < T(FLT_EPSILON));
52 }
53 
54 // Type-safe signum function with zero treated as positive
55 template<typename T>
56 int signNoZero(T val)
57 {
58  return (T(0) <= val) - (val < T(0));
59 }
60 
61 /*
62  * So called exponential curve function implementation.
63  * It is essentially a linear combination between a linear and a cubic function.
64  * @param value [-1,1] input value to function
65  * @param e [0,1] function parameter to set ratio between linear and cubic shape
66  * 0 - pure linear function
67  * 1 - pure cubic function
68  * @return result of function output
69  */
70 template<typename T>
71 const T expo(const T &value, const T &e)
72 {
73  T x = constrain(value, (T) - 1, (T) 1);
74  T ec = constrain(e, (T) 0, (T) 1);
75  return (1 - ec) * x + ec * x * x * x;
76 }
77 
78 /*
79  * So called SuperExpo function implementation.
80  * It is a 1/(1-x) function to further shape the rc input curve intuitively.
81  * I enhanced it compared to other implementations to keep the scale between [-1,1].
82  * @param value [-1,1] input value to function
83  * @param e [0,1] function parameter to set ratio between linear and cubic shape (see expo)
84  * @param g [0,1) function parameter to set SuperExpo shape
85  * 0 - pure expo function
86  * 0.99 - very strong bent curve, stays zero until maximum stick input
87  * @return result of function output
88  */
89 template<typename T>
90 const T superexpo(const T &value, const T &e, const T &g)
91 {
92  T x = constrain(value, (T) - 1, (T) 1);
93  T gc = constrain(g, (T) 0, (T) 0.99);
94  return expo(x, e) * (1 - gc) / (1 - fabsf(x) * gc);
95 }
96 
97 /*
98  * Deadzone function being linear and continuous outside of the deadzone
99  * 1 ------
100  * /
101  * --
102  * /
103  * -1 ------
104  * -1 -dz +dz 1
105  * @param value [-1,1] input value to function
106  * @param dz [0,1) ratio between deazone and complete span
107  * 0 - no deadzone, linear -1 to 1
108  * 0.5 - deadzone is half of the span [-0.5,0.5]
109  * 0.99 - almost entire span is deadzone
110  */
111 template<typename T>
112 const T deadzone(const T &value, const T &dz)
113 {
114  T x = constrain(value, (T) - 1, (T) 1);
115  T dzc = constrain(dz, (T) 0, (T) 0.99);
116  // Rescale the input such that we get a piecewise linear function that will be continuous with applied deadzone
117  T out = (x - sign(x) * dzc) / (1 - dzc);
118  // apply the deadzone (values zero around the middle)
119  return out * (fabsf(x) > dzc);
120 }
121 
122 template<typename T>
123 const T expo_deadzone(const T &value, const T &e, const T &dz)
124 {
125  return expo(deadzone(value, dz), e);
126 }
127 
128 
129 /*
130  * Constant, linear, constant function with the two corner points as parameters
131  * y_high -------
132  * /
133  * /
134  * /
135  * y_low -------
136  * x_low x_high
137  */
138 template<typename T>
139 const T gradual(const T &value, const T &x_low, const T &x_high, const T &y_low, const T &y_high)
140 {
141  if (value < x_low) {
142  return y_low;
143 
144  } else if (value > x_high) {
145  return y_high;
146 
147  } else {
148  /* linear function between the two points */
149  T a = (y_high - y_low) / (x_high - x_low);
150  T b = y_low - a * x_low;
151  return a * value + b;
152  }
153 }
154 
155 /*
156  * Exponential function of the form Y_out = a*b^X + c
157  *
158  * Y_max | *
159  * | *
160  * | *
161  * | *
162  * | *
163  * Y_middle | *
164  * | *
165  * Y_min | * *
166  * | __________________________________
167  * 0 1 2
168  *
169  *
170  * @param X in the range [0,2]
171  * @param Y_min minimum output at X = 2
172  * @param Y_mid middle output at X = 1
173  * @param Y_max maximum output at X = 0
174  */
175 template<typename T>
176 const T expontialFromLimits(const T &X_in, const T &Y_min, const T &Y_mid, const T &Y_max)
177 {
178  const T delta = (T)0.001;
179  // constrain X_in to the range of 0 and 2
180  T X = math::constrain(X_in, (T)0, (T)2);
181  // If Y_mid is exactly in the middle, then just apply linear approach.
182  bool use_linear_approach = false;
183 
184  if (((Y_max + Y_min) * (T)0.5) - Y_mid < delta) {
185  use_linear_approach = true;
186  }
187 
188  T Y_out;
189 
190  if (use_linear_approach) {
191  // Y_out = m*x+q
192  float slope = -(Y_max - Y_min) / (T)2.0;
193  Y_out = slope * X + Y_max;
194 
195  } else {
196  // Y_out = a*b^X + c with constraints Y_max = 0, Y_middle = 1, Y_min = 2
197  T a = -((Y_mid - Y_max) * (Y_mid - Y_max))
198  / ((T)2.0 * Y_mid - Y_max - Y_min);
199  T c = Y_max - a;
200  T b = (Y_mid - c) / a;
201  Y_out = a * powf(b, X) + c;
202  }
203 
204  // Y_out needs to be in between max and min
205  return constrain(Y_out, Y_min, Y_max);
206 
207 }
208 } /* namespace math */
constexpr _Tp constrain(_Tp val, _Tp min_val, _Tp max_val)
Definition: Limits.hpp:66
int signNoZero(T val)
Definition: Functions.hpp:56
const T superexpo(const T &value, const T &e, const T &g)
Definition: Functions.hpp:90
Limiting / constrain helper functions.
#define FLT_EPSILON
const T deadzone(const T &value, const T &dz)
Definition: Functions.hpp:112
const T gradual(const T &value, const T &x_low, const T &x_high, const T &y_low, const T &y_high)
Definition: Functions.hpp:139
const T expo(const T &value, const T &e)
Definition: Functions.hpp:71
const T expontialFromLimits(const T &X_in, const T &Y_min, const T &Y_mid, const T &Y_max)
Definition: Functions.hpp:176
const T expo_deadzone(const T &value, const T &e, const T &dz)
Definition: Functions.hpp:123
int sign(T val)
Definition: Functions.hpp:49