PX4 Firmware
PX4 Autopilot Software http://px4.io
BezierQuad.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 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  * @file BezierQuad.cpp
35  * Bezier function
36  *
37  * @author Dennis Mannhart <dennis.mannhart@gmail.com>
38  */
39 
40 #include "BezierQuad.hpp"
41 
42 namespace bezier
43 {
44 static constexpr double GOLDEN_RATIO = 1.6180339887; //(sqrt(5)+1)/2
45 static constexpr double RESOLUTION = 0.0001; //End criterion for golden section search
46 
47 template<typename Tp>
48 void BezierQuad<Tp>::setBezier(const Vector3_t &pt0, const Vector3_t &ctrl, const Vector3_t &pt1,
49  Tp duration)
50 {
51  _pt0 = pt0;
52  _ctrl = ctrl;
53  _pt1 = pt1;
54  _duration = duration;
55  _cached_resolution = (Tp)(-1);
56 }
57 
58 template<typename Tp>
60 {
61  pt0 = _pt0;
62  ctrl = _ctrl;
63  pt1 = _pt1;
64 }
65 
66 template<typename Tp>
68 {
69  return (_pt0 * ((Tp)1 - t / _duration) * ((Tp)1 - t / _duration) + _ctrl * (Tp)2 * ((
70  Tp)1 - t / _duration) * t / _duration + _pt1 *
71  t / _duration * t / _duration);
72 }
73 
74 template<typename Tp>
76 {
77  return (((_ctrl - _pt0) * _duration + (_pt0 - _ctrl * (Tp)2 + _pt1) * t) * (Tp)2 / (_duration * _duration));
78 }
79 
80 template<typename Tp>
82 {
83  return ((_pt0 - _ctrl * (Tp)2 + _pt1) * (Tp)2 / (_duration * _duration));
84 }
85 
86 template<typename Tp>
87 void BezierQuad<Tp>::getStates(Vector3_t &point, Vector3_t &vel, Vector3_t &acc, const Tp time)
88 {
89  point = getPoint(time);
90  vel = getVelocity(time);
91  acc = getAcceleration();
92 }
93 
94 template<typename Tp>
96  const Vector3_t pose)
97 {
98  // get t that corresponds to point closest on bezier point
99  Tp t = _goldenSectionSearch(pose);
100 
101  // get states corresponding to t
102  getStates(point, vel, acc, t);
103 
104 }
105 
106 template<typename Tp>
107 void BezierQuad<Tp>::setBezFromVel(const Vector3_t &ctrl, const Vector3_t &vel0, const Vector3_t &vel1,
108  const Tp duration)
109 {
110  // update bezier points
111  _ctrl = ctrl;
112  _duration = duration;
113  _pt0 = _ctrl - vel0 * _duration / (Tp)2;
114  _pt1 = _ctrl + vel1 * _duration / (Tp)2;
115  _cached_resolution = (Tp)(-1);
116 }
117 
118 template<typename Tp>
119 Tp BezierQuad<Tp>::getArcLength(const Tp resolution)
120 {
121  // we don't need to recompute arc length if:
122  // 1. _cached_resolution is up to date; 2. _cached_resolution is smaller than desired resolution (= more accurate)
123  if ((_cached_resolution > (Tp)0) && (_cached_resolution <= resolution)) {
124  return _cached_arc_length;
125  }
126 
127  // get number of elements
128  int n = (int)(roundf(_duration / resolution));
129 
130  // check if n is even
131  if (n % 2 == 1) {
132  n += 1;
133  }
134 
135  // step size
136  Tp h = (_duration) / n;
137 
138  // get integration
139  _cached_arc_length = (Tp)0;
140  Vector3_t y;
141 
142  for (int i = 1; i < n; i++) {
143 
144  y = getVelocity(h * i);
145 
146  if (i % 2 == 1) {
147  _cached_arc_length += (Tp)4 * y.length();
148 
149  } else {
150  _cached_arc_length += (Tp)2 * y.length();
151  }
152  }
153 
154  // velocity length at start and end points
155  Tp y0 = getVelocity((Tp)0).length();
156  Tp yn = getVelocity(_duration).length();
157 
158  // 1/3 simpsons rule
159  _cached_arc_length = h / (Tp)3 * (y0 + yn + _cached_arc_length);
160 
161  // update cache
162  _cached_resolution = resolution;
163 
164  return _cached_arc_length;
165 }
166 
167 template<typename Tp>
169 {
170  // get t that corresponds to point closest on bezier point
171  Tp t = _goldenSectionSearch(pose);
172 
173  // get closest point
174  Vector3_t point = getPoint(t);
175  return (pose - point).length();
176 }
177 
178 template<typename Tp>
180 {
181  Tp a, b, c, d;
182  a = (Tp)0; // represents most left point
183  b = _duration * (Tp)1; // represents most right point
184 
185  c = b - (b - a) / GOLDEN_RATIO;
186  d = a + (b - a) / GOLDEN_RATIO;
187 
188  while (fabsf(c - d) > RESOLUTION) {
189  if (_getDistanceSquared(c, pose) < _getDistanceSquared(d, pose)) {
190  b = d;
191 
192  } else {
193  a = c;
194  }
195 
196  c = b - (b - a) / GOLDEN_RATIO;
197  d = a + (b - a) / GOLDEN_RATIO;
198  }
199 
200  return (b + a) / (Tp)2;
201 }
202 
203 template<typename Tp>
205 {
206  // get point on bezier
207  Vector3_t vec = getPoint(t);
208 
209  // get vector from point to pose
210  vec = vec - pose;
211 
212  // norm squared
213  return (vec * vec);
214 }
215 }
Vector3_t getVelocity(const Tp t)
Definition: BezierQuad.cpp:75
void getBezier(Vector3_t &pt0, Vector3_t &ctrl, Vector3_t &pt1)
Definition: BezierQuad.cpp:59
Tp getArcLength(const Tp resolution)
Definition: BezierQuad.cpp:119
static constexpr double GOLDEN_RATIO
Definition: BezierQuad.cpp:44
void setBezFromVel(const Vector3_t &ctrl, const Vector3_t &vel0, const Vector3_t &vel1, const Tp duration=(Tp) 1)
Definition: BezierQuad.cpp:107
Vector3_t getAcceleration()
Definition: BezierQuad.cpp:81
void getStatesClosest(Vector3_t &point, Vector3_t &vel, Vector3_t &acc, const Vector3_t pose)
Definition: BezierQuad.cpp:95
void getStates(Vector3_t &point, Vector3_t &vel, Vector3_t &acc, const Tp t)
Definition: BezierQuad.cpp:87
Type length() const
Definition: Vector.hpp:83
Tp getDistToClosestPoint(const Vector3_t &pose)
Definition: BezierQuad.cpp:168
Vector3_t getPoint(const Tp t)
Return point on bezier point corresponding to time t.
Definition: BezierQuad.cpp:67
Tp _getDistanceSquared(const Tp t, const Vector3_t &pose)
Definition: BezierQuad.cpp:204
Tp _goldenSectionSearch(const Vector3_t &pose)
Definition: BezierQuad.cpp:179
void setBezier(const Vector3_t &pt0, const Vector3_t &ctrl, const Vector3_t &pt1, Tp duration=(Tp) 1)
Set new bezier points and duration.
Definition: BezierQuad.cpp:48
static constexpr double RESOLUTION
Definition: BezierQuad.cpp:45