PX4 Firmware
PX4 Autopilot Software http://px4.io
ManualVelocitySmoothingZ.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2019 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 
35 
36 #include <mathlib/mathlib.h>
37 #include <float.h>
38 
39 void ManualVelocitySmoothingZ::reset(float accel, float vel, float pos)
40 {
41  _trajectory.reset(accel, vel, pos);
42 
44 }
45 
47 {
48  _position_lock_active = false;
50 }
51 
52 void ManualVelocitySmoothingZ::update(float dt, float velocity_target)
53 {
54  // Update state
56 
57  // Set max accel/vel/jerk
58  // Has to be done before _updateTrajDurations()
59  updateTrajConstraints(velocity_target);
60 
61  // Lock or unlock position
62  // Has to be done before _updateTrajDurations()
63  checkPositionLock(velocity_target);
64 
65  // Update durations
66  _trajectory.updateDurations(velocity_target);
67 }
68 
70 {
72 
77 }
78 
80 {
81  if (velocity_target < 0.f) { // up
84 
85  } else { // down
88  }
89 }
90 
92 {
93  /**
94  * During a position lock -> position unlock transition, we have to make sure that the velocity setpoint
95  * is continuous. We know that the output of the position loop (part of the velocity setpoint)
96  * will suddenly become null
97  * and only the feedforward (generated by this flight task) will remain.
98  * This is why the previous input of the velocity controller
99  * is used to set current velocity of the trajectory.
100  */
101  if (fabsf(_state.v) < 0.1f &&
102  fabsf(_state.a) < .2f &&
103  fabsf(velocity_target) <= FLT_EPSILON) {
104  // Lock position
105  _position_lock_active = true;
107 
108  } else {
109  // Unlock position
110  if (_position_lock_active) {
111  // Start the trajectory at the current velocity setpoint
115  }
116 
118  }
119 }
struct ManualVelocitySmoothingZ::@65 _state
float getCurrentVelocity() const
#define FLT_EPSILON
float getCurrentJerk() const
void update(float dt, float velocity_target)
void setCurrentVelocity(const float vel)
void reset(float accel, float vel, float pos)
void updateTraj(float dt, float time_stretch=1.f)
Generate the trajectory (acceleration, velocity and position) by integrating the current jerk...
float getCurrentAcceleration() const
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
void reset(float accel, float vel, float pos)
Reset the state.
void updateDurations(float vel_setpoint)
Compute T1, T2, T3 depending on the current state and velocity setpoint.
void setCurrentPosition(const float pos)
float getCurrentPosition() const
VelocitySmoothing _trajectory
Trajectory in z direction.
float dt
Definition: px4io.c:73
void setMaxVel(float max_vel)
void updateTrajConstraints(float vel_target)
void setMaxAccel(float max_accel)
void checkPositionLock(float velocity_target)