PX4 Firmware
PX4 Autopilot Software http://px4.io
BezierQuad.hpp
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 /**
35  * @file BerzierQuad.hpp
36  *
37  * Quadratic bezier lib
38  *
39  * A quadratic bezier function/spline is completely defined by three 3D points in space and a time scaling factor.
40  * pt0 and pt1 define the start and end points of the spline. ctrl point is a point in space that effects the curvature
41  * of the spline. The time scaling factor (= duration) defines the time it takes to travel along the spline from pt0 to
42  * pt1.
43  * A bezier spline is a continuous function from which position, velocity and acceleration can be extracted. For a given spline,
44  * acceleration stays constant.
45  */
46 
47 
48 #pragma once
49 
50 #include <matrix/math.hpp>
51 
52 namespace bezier
53 {
54 template<typename Tp>
56 {
57 public:
58 
60 
61  /**
62  * Empty constructor
63  */
66 
67  /**
68  * Constructor from array
69  */
70  BezierQuad(const Tp pt0[3], const Tp ctrl[3], const Tp pt1[3], Tp duration = 1.0f) :
71  _pt0(Vector3_t(pt0)), _ctrl(Vector3_t(ctrl)), _pt1(Vector3_t(pt1)), _duration(duration) {}
72 
73  /**
74  * Constructor from vector
75  */
76  BezierQuad(const Vector3_t &pt0, const Vector3_t &ctrl, const Vector3_t &pt1,
77  Tp duration = 1.0f):
78  _pt0(pt0), _ctrl(ctrl), _pt1(pt1), _duration(duration) {}
79 
80 
81  /*
82  * Get bezier points
83  */
84  void getBezier(Vector3_t &pt0, Vector3_t &ctrl, Vector3_t &pt1);
85 
86  /*
87  * Return pt0
88  */
89  Vector3_t getPt0() {return _pt0;}
90 
91  /*
92  * Return ctrl
93  */
94  Vector3_t getCtrl() {return _ctrl;}
95 
96  /*
97  * Return pt1
98  */
99  Vector3_t getPt1() {return _pt1;}
100 
101  /**
102  * Set new bezier points and duration
103  */
104  void setBezier(const Vector3_t &pt0, const Vector3_t &ctrl, const Vector3_t &pt1,
105  Tp duration = (Tp)1);
106 
107  /*
108  * Set duration
109  *
110  * @param time is the total time it takes to travel along the bezier spline.
111  */
112  void setDuration(const Tp time) {_duration = time;}
113 
114  /**
115  * Return point on bezier point corresponding to time t
116  *
117  * @param t is a time in seconds in between [0, duration]
118  * @return a point on bezier
119  */
120  Vector3_t getPoint(const Tp t);
121 
122  /*
123  * Distance to closest point given a position
124  *
125  * @param pose is a position in 3D space from which distance to bezier is computed.
126  * @return distance to closest point on bezier
127  */
128  Tp getDistToClosestPoint(const Vector3_t &pose);
129 
130  /*
131  * Return velocity on bezier corresponding to time t
132  *
133  * @param t is a time in seconds in between [0, duration]
134  * @return velocity vector at time t
135  */
136  Vector3_t getVelocity(const Tp t);
137 
138  /*
139  * Return acceleration on bezier corresponding to time t
140  *
141  * @return constant acceleration of bezier
142  */
144 
145  /*
146  * Get all states on bezier corresponding to time t
147  */
148  void getStates(Vector3_t &point, Vector3_t &vel, Vector3_t &acc, const Tp t);
149 
150  /*
151  * Get states on bezier which are closest to pose in space
152  *
153  * @param point is a posiiton on the spline that is closest to a given pose
154  * @param vel is the velocity at that given point
155  * @param acc is the acceleration for that spline
156  * @param pose represent a position in space from which closest point is computed
157  */
158  void getStatesClosest(Vector3_t &point, Vector3_t &vel, Vector3_t &acc,
159  const Vector3_t pose);
160 
161  /*
162  * Compute bezier from velocity at bezier end points and ctrl point
163  *
164  * The bezier end points are fully defined by a given control point ctrl, the duration and
165  * the desired velocity vectors at the end points.
166  */
167  void setBezFromVel(const Vector3_t &ctrl, const Vector3_t &vel0, const Vector3_t &vel1,
168  const Tp duration = (Tp)1);
169 
170  /*
171  * Return the arc length of a bezier spline
172  *
173  * The arc length is computed with simpsons integration.
174  * @param resolution in meters.
175  */
176  Tp getArcLength(const Tp resolution);
177 
178 private:
179 
180  Vector3_t _pt0; /**< Bezier starting point */
181  Vector3_t _ctrl; /**< Bezier control point */
182  Vector3_t _pt1; /**< bezier end point */
183  Tp _duration = (Tp)1; /**< Total time to travle along spline */
184 
185  Tp _cached_arc_length = (Tp)0; /**< The saved arc length of the spline */
186  Tp _cached_resolution = (Tp)(-1); /**< The resolution used to compute the arc length.
187  Negative number means that cache is not up to date. */
188 
189  /*
190  * Golden section search
191  */
192  Tp _goldenSectionSearch(const Vector3_t &pose);
193 
194  /*
195  * Get squared distance from 3D pose in space and a point on bezier.
196  *
197  * @param t is the time in between [0, duration] that defines a point on the bezier.
198  * @param pose is a 3D pose in space.
199  */
200  Tp _getDistanceSquared(const Tp t, const Vector3_t &pose);
201 
202 
203 };
204 
207 }
208 
209 // include implementation
210 #include "BezierQuad.cpp"
Tp _cached_resolution
The resolution used to compute the arc length.
Definition: BezierQuad.hpp:186
Bezier function.
Vector3_t getVelocity(const Tp t)
Definition: BezierQuad.cpp:75
Vector3_t getCtrl()
Definition: BezierQuad.hpp:94
BezierQuad(const Vector3_t &pt0, const Vector3_t &ctrl, const Vector3_t &pt1, Tp duration=1.0f)
Constructor from vector.
Definition: BezierQuad.hpp:76
void getBezier(Vector3_t &pt0, Vector3_t &ctrl, Vector3_t &pt1)
Definition: BezierQuad.cpp:59
Tp getArcLength(const Tp resolution)
Definition: BezierQuad.cpp:119
Tp _cached_arc_length
The saved arc length of the spline.
Definition: BezierQuad.hpp:185
BezierQuad()
Empty constructor.
Definition: BezierQuad.hpp:64
Vector3_t getPt1()
Definition: BezierQuad.hpp:99
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
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
void setDuration(const Tp time)
Definition: BezierQuad.hpp:112
void getStates(Vector3_t &point, Vector3_t &vel, Vector3_t &acc, const Tp t)
Definition: BezierQuad.cpp:87
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
BezierQuad(const Tp pt0[3], const Tp ctrl[3], const Tp pt1[3], Tp duration=1.0f)
Constructor from array.
Definition: BezierQuad.hpp:70
Vector3_t _pt0
Bezier starting point.
Definition: BezierQuad.hpp:180
Vector3_t _pt1
bezier end point
Definition: BezierQuad.hpp:182
Vector3_t getPt0()
Definition: BezierQuad.hpp:89
Tp _getDistanceSquared(const Tp t, const Vector3_t &pose)
Definition: BezierQuad.cpp:204
Tp _goldenSectionSearch(const Vector3_t &pose)
Definition: BezierQuad.cpp:179
Tp _duration
Total time to travle along spline.
Definition: BezierQuad.hpp:183
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
Vector3_t _ctrl
Bezier control point.
Definition: BezierQuad.hpp:181