Firmware
VelocitySmoothing.hpp
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 #pragma once
35 
55 {
56 public:
57  VelocitySmoothing(float initial_accel = 0.f, float initial_vel = 0.f, float initial_pos = 0.f);
58  ~VelocitySmoothing() = default;
59 
66  void reset(float accel, float vel, float pos);
67 
74  void updateDurations(float dt, float vel_setpoint);
75 
84  void integrate(float &accel_setpoint_smooth, float &vel_setpoint_smooth, float &pos_setpoint_smooth);
85  void integrate(float dt, float integration_scale_factor, float &accel_setpoint_smooth, float &vel_setpoint_smooth,
86  float &pos_setpoint_smooth);
87 
88  /* Get / Set constraints (constraints can be updated at any time) */
89  float getMaxJerk() const { return _max_jerk; }
90  void setMaxJerk(float max_jerk) { _max_jerk = max_jerk; }
91 
92  float getMaxAccel() const { return _max_accel; }
93  void setMaxAccel(float max_accel) { _max_accel = max_accel; }
94 
95  float getMaxVel() const { return _max_vel; }
96  void setMaxVel(float max_vel) { _max_vel = max_vel; }
97 
98  float getCurrentJerk() const { return _jerk; }
99  void setCurrentAcceleration(const float accel) { _accel = accel; }
100  float getCurrentAcceleration() const { return _accel; }
101  void setCurrentVelocity(const float vel) { _vel = vel; }
102  float getCurrentVelocity() const { return _vel; }
103  void setCurrentPosition(const float pos) { _pos = pos; }
104  float getCurrentPosition() const { return _pos; }
105 
113  static void timeSynchronization(VelocitySmoothing *traj, int n_traj);
114 
115  float getTotalTime() const { return _T1 + _T2 + _T3; }
116  float getT1() const { return _T1; }
117  float getT2() const { return _T2; }
118  float getT3() const { return _T3; }
119  float getVelSp() const { return _vel_sp; }
120 
121 private:
122 
128  void updateDurations(float T123 = -1.f);
132  inline float computeT1(float accel_prev, float vel_prev, float vel_setpoint, float max_jerk);
136  inline float computeT1(float T123, float accel_prev, float vel_prev, float vel_setpoint, float max_jerk);
137  inline float saturateT1ForAccel(float accel_prev, float max_jerk, float T1);
141  inline float computeT2(float T1, float T3, float accel_prev, float vel_prev, float vel_setpoint, float max_jerk);
145  inline float computeT2(float T123, float T1, float T3);
149  inline float computeT3(float T1, float accel_prev, float max_jerk);
150 
154  inline void integrateT(float dt, float jerk, float accel_prev, float vel_prev, float pos_prev,
155  float &accel_out, float &vel_out, float &pos_out);
156 
157  /* Inputs */
158  float _vel_sp{0.0f};
159  float _dt = 1.f;
160 
161  /* Constraints */
162  float _max_jerk = 22.f;
163  float _max_accel = 8.f;
164  float _max_vel = 6.f;
165 
166  /* State (previous setpoints) */
167  float _jerk = 0.f;
168  float _accel = 0.f;
169  float _vel = 0.f;
170  float _pos = 0.f;
171 
172  float _max_jerk_T1 = 0.f;
173 
174  /* Duration of each phase */
175  float _T1 = 0.f;
176  float _T2 = 0.f;
177  float _T3 = 0.f;
178 
179  static constexpr float max_pos_err = 1.f;
180 };
void reset(float accel, float vel, float pos)
Reset the state.
Definition: VelocitySmoothing.cpp:46
void integrate(float &accel_setpoint_smooth, float &vel_setpoint_smooth, float &pos_setpoint_smooth)
Generate the trajectory (acceleration, velocity and position) by integrating the current jerk...
Definition: VelocitySmoothing.cpp:234
static void timeSynchronization(VelocitySmoothing *traj, int n_traj)
Synchronize several trajectories to have the same total time.
Definition: VelocitySmoothing.cpp:282
TODO: document the algorithm |T1| T2 |T3| __| |____ __ Jerk |_| / \ Acceleration ___/ ___ ;" / / V...
Definition: VelocitySmoothing.hpp:54
void updateDurations(float dt, float vel_setpoint)
Compute T1, T2, T3 depending on the current state and velocity setpoint.
Definition: VelocitySmoothing.cpp:196