PX4 Firmware
PX4 Autopilot Software http://px4.io
ManualSmoothingZ.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 SmoothingZ.hpp
36  *
37  * This Class is used for smoothing the velocity setpoints in Z-direction.
38  * In manual altitude control apply acceleration limit based on stick input.
39  */
40 
41 #pragma once
42 
43 #include <px4_platform_common/module_params.h>
44 
45 /**
46  * User-intention.
47  * - brake: vehicle should stop
48  * - acceleration: vehicle should move up or down
49  */
50 enum class ManualIntentionZ {
51  brake,
53 };
54 
55 class ManualSmoothingZ : public ModuleParams
56 {
57 public:
58  ManualSmoothingZ(ModuleParams *parent, const float &vel, const float &stick);
59  ~ManualSmoothingZ() = default;
60 
61  /**
62  * Smooth velocity setpoint based on flight direction.
63  * @param vel_sp[2] array: vel_sp[0] = current velocity set-point;
64  * vel_sp[1] = previous velocity set-point
65  * vel_sp will contain smoothed current / previous set-point.
66  * @param dt: time delta in seconds
67  */
68  void smoothVelFromSticks(float &vel_sp, const float dt);
69 
70  /**
71  * Get max accleration.
72  */
73  float getMaxAcceleration() { return _max_acceleration; }
74 
75  /**
76  * Get user intention.
77  * @see ManualIntentionZ
78  */
79  ManualIntentionZ getIntention() { return _intention; }
80 
81  /*
82  * Overwrite methods:
83  * Needed if different parameter values than default required.
84  */
85  void overwriteAccelerationUp(float acc_max_up) { _param_mpc_acc_up_max.set(acc_max_up); }
86  void overwriteAccelerationDown(float acc_max_down) {_param_mpc_acc_down_max.set(acc_max_down); }
87  void overwriteJerkMax(float jerk_max) {_param_mpc_jerk_max.set(jerk_max); }
88 
89 private:
90  /**
91  * Add delay to velocity setpoint change.
92  * This method is used to smooth the velocity setpoint change.
93  * @param vel_sp current velocity setpoint
94  * @param dt delta-time
95  */
96  void velocitySlewRate(float &vel_sp, const float dt);
97 
98  /**
99  * Computes the velocity setpoint change limit.
100  * This method computes the limit with which the velocity setpoint change
101  * is limited.
102  * @see velocitySlewRate
103  * @param vel_sp current velocity setpoint
104  * @param dt delta-time
105  */
106  void updateAcceleration(float &vel_sp, const float dt);
107 
108  /**
109  * Set maximum acceleration.
110  * The maximum acceleration depends on the desired direction (up vs down).
111  * @see _max_acceleration
112  */
113  void setMaxAcceleration();
114 
115  /**
116  * User intention
117  * @see ManualIntentionZ
118  */
120 
121  const float &_vel; /**< vehicle velocity (dependency injection) */
122  const float &_stick; /**< stick input (dependency injection) */
123 
124  float _acc_state_dependent; /**< acceleration that depends on _intention */
125  float _max_acceleration; /**< can be up or down maximum acceleration */
126  float _vel_sp_prev; /**< previous velocity setpoint */
127 
128  DEFINE_PARAMETERS(
129  (ParamFloat<px4::params::MPC_ACC_UP_MAX>) _param_mpc_acc_up_max,
130  (ParamFloat<px4::params::MPC_ACC_DOWN_MAX>) _param_mpc_acc_down_max,
131  (ParamFloat<px4::params::MPC_JERK_MAX>) _param_mpc_jerk_max
132  )
133 };
float _vel_sp_prev
previous velocity setpoint
float _max_acceleration
can be up or down maximum acceleration
const float & _vel
vehicle velocity (dependency injection)
void overwriteAccelerationDown(float acc_max_down)
float _acc_state_dependent
acceleration that depends on _intention
void overwriteAccelerationUp(float acc_max_up)
const float & _stick
stick input (dependency injection)
void overwriteJerkMax(float jerk_max)
ManualIntentionZ getIntention()
Get user intention.
float dt
Definition: px4io.c:73
float getMaxAcceleration()
Get max accleration.
ManualIntentionZ
User-intention.