PX4 Firmware
PX4 Autopilot Software http://px4.io
ManualSmoothingXY.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 SmoothingXY.hpp
36  *
37  * This Class is used for smoothing the velocity setpoint in XY-direction.
38  * The velocity setpoint is smoothed by applying a velocity change limit, which
39  * we call acceleration. Depending on the user intention, the acceleration limit
40  * will differ.
41  * In manual mode we consider four states with different acceleration handling:
42  * 1. user wants to stop
43  * 2. user wants to quickly change direction
44  * 3. user wants to accelerate
45  * 4. user wants to decelerate
46  */
47 
48 #pragma once
49 
50 #include <px4_platform_common/module_params.h>
51 #include <matrix/matrix/math.hpp>
52 
53 class ManualSmoothingXY : public ModuleParams
54 {
55 public:
56  ManualSmoothingXY(ModuleParams *parent, const matrix::Vector2f &vel);
57  ~ManualSmoothingXY() = default;
58 
59  /**
60  * Maximum velocity is required to detect user intention.
61  * Maximum velocity depends on flight-task.
62  * In order to deduce user intention from velocity, the maximum
63  * allowed velocity has to be updated.
64  * @param vel_max corresponds to vehicle constraint
65  */
66  void updateMaxVelocity(const float &vel_max) {_vel_max = vel_max;};
67 
68  /**
69  * Smoothing of velocity setpoint horizontally based
70  * on flight direction.
71  * @param vel_sp: velocity setpoint in xy
72  * @param dt: time delta in seconds
73  */
74  void smoothVelocity(matrix::Vector2f &vel_sp, const matrix::Vector2f &vel, const float &yaw,
75  const float &yawrate_sp, const float dt);
76 
77  /*
78  * User intention.
79  * - brake when user demands a brake
80  * - acceleration when vehicle keeps speed or accelerates in the same direction
81  * - deceleration when vehicle slows down in the same direction
82  * - direction_change whne vehcile demands an abrupt direction change
83  */
84  enum class Intention {
85  brake,
89  };
90  /**
91  * Get user intention.
92  * @see Intention
93  */
95 
96  /**
97  * Overwrite methods:
98  * Needed if different parameter values than default required.
99  */
100  void overwriteHoverAcceleration(float acc) { _param_mpc_acc_hor_max.set(acc); }
101  void overwriteMaxAcceleration(float acc) { _param_mpc_acc_hor.set(acc); }
102  void overwriteDecelerationMin(float dec) { _param_mpc_dec_hor_slow.set(dec); }
103  void overwriteJerkMax(float jerk) { _param_mpc_jerk_max.set(jerk); }
104  void overwriteJerkMin(float jerk) { _param_mpc_jerk_min.set(jerk); }
105 
106 private:
107  /**
108  * Sets velocity change limits (=acceleration).
109  * Depending on the user intention, the acceleration differs.
110  * @param vel_sp is desired velocity setpoint before slewrate.
111  * @param vel is current velocity in horizontal direction
112  * @param yaw is vehicle yaw
113  * @param yawrate_sp is desired yawspeed
114  * @param dt is delta-time
115  */
116  void _updateAcceleration(matrix::Vector2f &vel_sp, const matrix::Vector2f &vel, const float &yaw,
117  const float &yawrate_sp, const float dt);
118 
119  /**
120  * Gets user intention.
121  * The intention is deduced from desired velocity setpoint.
122  * @param vel_sp is desired velocity setpoint before slewrate.
123  * @param vel is vehicle velocity in xy-direction.
124  * @param yaw is vehicle yaw
125  * @param yawrate_sp is the desired yaw-speed
126  */
127  Intention _getIntention(const matrix::Vector2f &vel_sp, const matrix::Vector2f &vel, const float &yaw,
128  const float &yawrate_sp);
129 
130  /**
131  * Set acceleration depending on Intention.
132  * @param vel_sp is desired velocity septoint in xy-direction
133  * @param vel is vehicle velociy in xy-direction
134  * @param intention is the user intention during flight
135  * @param dt is delta-time
136  */
137  void _setStateAcceleration(const matrix::Vector2f &vel_sp, const matrix::Vector2f &vel, const Intention &intention,
138  const float dt);
139 
140  /**
141  * Limits the velocity setpoint change.
142  * @param vel_sp that gets limited based on acceleration and previous velocity setpoint
143  * @param dt is delta-time
144  */
145  void _velocitySlewRate(matrix::Vector2f &vel_sp, const float dt);
146 
147  /**
148  * Rotate vector from local frame into heading frame.
149  * @param vec is an arbitrary vector in local frame
150  * @param yaw is the vehicle heading
151  */
152  matrix::Vector2f _getWorldToHeadingFrame(const matrix::Vector2f &vec, const float &yaw);
153 
154  /**
155  * Rotate vector from heading frame to local frame.
156  * @param vec is an arbitrary vector in heading frame
157  * @param yaw is the vehicle yaw
158  */
159  matrix::Vector2f _getHeadingToWorldFrame(const matrix::Vector2f &vec, const float &yaw);
160 
162  float _acc_state_dependent; /**< velocity change limit that depends on Intention */
163  float _jerk_state_dependent; /**< acceleration change limit during brake */
164  float _vel_max{10.0f}; /**< maximum horizontal speed allowed */
165  matrix::Vector2f _vel_sp_prev; /**< previous velocity setpoint */
166 
167  DEFINE_PARAMETERS(
168  (ParamFloat<px4::params::MPC_ACC_HOR_MAX>) _param_mpc_acc_hor_max, /**< acceleration in hover */
169  (ParamFloat<px4::params::MPC_ACC_HOR>) _param_mpc_acc_hor, /**< acceleration in flight */
170  (ParamFloat<px4::params::MPC_DEC_HOR_SLOW>) _param_mpc_dec_hor_slow, /**< deceleration in flight */
171  (ParamFloat<px4::params::MPC_JERK_MIN>) _param_mpc_jerk_min, /**< jerk min during brake */
172  (ParamFloat<px4::params::MPC_JERK_MAX>) _param_mpc_jerk_max, /**< jerk max during brake */
173  (ParamFloat<px4::params::MPC_VEL_MANUAL>) _param_mpc_vel_manual /**< maximum velocity in manual controlled mode */
174  )
175 };
matrix::Vector2f _getWorldToHeadingFrame(const matrix::Vector2f &vec, const float &yaw)
Rotate vector from local frame into heading frame.
void overwriteHoverAcceleration(float acc)
Overwrite methods: Needed if different parameter values than default required.
ManualSmoothingXY(ModuleParams *parent, const matrix::Vector2f &vel)
void overwriteDecelerationMin(float dec)
void updateMaxVelocity(const float &vel_max)
Maximum velocity is required to detect user intention.
float _acc_state_dependent
velocity change limit that depends on Intention
matrix::Vector2f _getHeadingToWorldFrame(const matrix::Vector2f &vec, const float &yaw)
Rotate vector from heading frame to local frame.
void overwriteJerkMin(float jerk)
float _jerk_state_dependent
acceleration change limit during brake
Intention _getIntention(const matrix::Vector2f &vel_sp, const matrix::Vector2f &vel, const float &yaw, const float &yawrate_sp)
Gets user intention.
matrix::Vector2f _vel_sp_prev
previous velocity setpoint
void _updateAcceleration(matrix::Vector2f &vel_sp, const matrix::Vector2f &vel, const float &yaw, const float &yawrate_sp, const float dt)
Sets velocity change limits (=acceleration).
float _vel_max
maximum horizontal speed allowed
void overwriteJerkMax(float jerk)
void overwriteMaxAcceleration(float acc)
float dt
Definition: px4io.c:73
void smoothVelocity(matrix::Vector2f &vel_sp, const matrix::Vector2f &vel, const float &yaw, const float &yawrate_sp, const float dt)
Smoothing of velocity setpoint horizontally based on flight direction.
void _velocitySlewRate(matrix::Vector2f &vel_sp, const float dt)
Limits the velocity setpoint change.
Intention getIntention()
Get user intention.
Intention _intention
user intention
void _setStateAcceleration(const matrix::Vector2f &vel_sp, const matrix::Vector2f &vel, const Intention &intention, const float dt)
Set acceleration depending on Intention.
~ManualSmoothingXY()=default