PX4 Firmware
PX4 Autopilot Software http://px4.io
PositionControl.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 - 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 
34 /**
35  * @file PositionControl.hpp
36  *
37  * A cascaded position controller for position/velocity control only.
38  */
39 
40 #pragma once
41 
42 #include <matrix/matrix/math.hpp>
47 
52  float yaw;
53 };
54 
55 /**
56  * Core Position-Control for MC.
57  * This class contains P-controller for position and
58  * PID-controller for velocity.
59  * Inputs:
60  * vehicle position/velocity/yaw
61  * desired set-point position/velocity/thrust/yaw/yaw-speed
62  * constraints that are stricter than global limits
63  * Output
64  * thrust vector and a yaw-setpoint
65  *
66  * If there is a position and a velocity set-point present, then
67  * the velocity set-point is used as feed-forward. If feed-forward is
68  * active, then the velocity component of the P-controller output has
69  * priority over the feed-forward component.
70  *
71  * A setpoint that is NAN is considered as not set.
72  * If there is a position/velocity- and thrust-setpoint present, then
73  * the thrust-setpoint is ommitted and recomputed from position-velocity-PID-loop.
74  */
76 {
77 public:
78 
79  PositionControl() = default;
80  ~PositionControl() = default;
81 
82  /**
83  * Set the position control gains
84  * @param P 3D vector of proportional gains for x,y,z axis
85  */
86  void setPositionGains(const matrix::Vector3f &P) { _gain_pos_p = P; }
87 
88  /**
89  * Set the velocity control gains
90  * @param P 3D vector of proportional gains for x,y,z axis
91  * @param I 3D vector of integral gains
92  * @param D 3D vector of derivative gains
93  */
94  void setVelocityGains(const matrix::Vector3f &P, const matrix::Vector3f &I, const matrix::Vector3f &D);
95 
96  /**
97  * Set the maximum velocity to execute with feed forward and position control
98  * @param vel_horizontal horizontal velocity limit
99  * @param vel_up upwards velocity limit
100  * @param vel_down downwards velocity limit
101  */
102  void setVelocityLimits(const float vel_horizontal, const float vel_up, float vel_down);
103 
104  /**
105  * Set the minimum and maximum collective normalized thrust [0,1] that can be output by the controller
106  * @param min minimum thrust e.g. 0.1 or 0
107  * @param max maximum thrust e.g. 0.9 or 1
108  */
109  void setThrustLimits(const float min, const float max);
110 
111  /**
112  * Set the maximum tilt angle in radians the output attitude is allowed to have
113  * @param tilt angle from level orientation in radians
114  */
115  void setTiltLimit(const float tilt) { _lim_tilt = tilt; }
116 
117  /**
118  * Set the maximum tilt angle in radians the output attitude is allowed to have
119  * @param thrust [0,1] with which the vehicle hovers not aacelerating down or up with level orientation
120  */
121  void setHoverThrust(const float thrust) { _hover_thrust = thrust; }
122 
123  /**
124  * Update the current vehicle state.
125  * @param PositionControlStates structure
126  */
127  void updateState(const PositionControlStates &states);
128 
129  /**
130  * Update the desired setpoints.
131  * @param setpoint a vehicle_local_position_setpoint_s structure
132  * @return true if setpoint has updated correctly
133  */
134  bool updateSetpoint(const vehicle_local_position_setpoint_s &setpoint);
135 
136  /**
137  * Set constraints that are stricter than the global limits.
138  * @param constraints a PositionControl structure with supported constraints
139  */
140  void updateConstraints(const vehicle_constraints_s &constraints);
141 
142  /**
143  * Apply P-position and PID-velocity controller that updates the member
144  * thrust, yaw- and yawspeed-setpoints.
145  * @see _thr_sp
146  * @see _yaw_sp
147  * @see _yawspeed_sp
148  * @param dt the delta-time
149  */
150  void generateThrustYawSetpoint(const float dt);
151 
152  /**
153  * Set the integral term in xy to 0.
154  * @see _thr_int
155  */
156  void resetIntegralXY() { _thr_int(0) = _thr_int(1) = 0.f; }
157 
158  /**
159  * Set the integral term in z to 0.
160  * @see _thr_int
161  */
162  void resetIntegralZ() { _thr_int(2) = 0.f; }
163 
164  /**
165  * Get the
166  * @see _vel_sp
167  * @return The velocity set-point that was executed in the control-loop. Nan if velocity control-loop was skipped.
168  */
170  {
171  matrix::Vector3f vel_sp{};
172 
173  for (int i = 0; i <= 2; i++) {
174  if (_ctrl_vel[i]) {
175  vel_sp(i) = _vel_sp(i);
176 
177  } else {
178  vel_sp(i) = NAN;
179  }
180  }
181 
182  return vel_sp;
183  }
184 
185  /**
186  * Get the controllers output local position setpoint
187  * These setpoints are the ones which were executed on including PID output and feed-forward.
188  * The acceleration or thrust setpoints can be used for attitude control.
189  * @param local_position_setpoint reference to struct to fill up
190  */
191  void getLocalPositionSetpoint(vehicle_local_position_setpoint_s &local_position_setpoint) const;
192 
193  /**
194  * Get the controllers output attitude setpoint
195  * This attitude setpoint was generated from the resulting acceleration setpoint after position and velocity control.
196  * It needs to be executed by the attitude controller to achieve velocity and position tracking.
197  * @param attitude_setpoint reference to struct to fill up
198  */
199  void getAttitudeSetpoint(vehicle_attitude_setpoint_s &attitude_setpoint) const;
200 
201 private:
202  /**
203  * Maps setpoints to internal-setpoints.
204  * @return true if mapping succeeded.
205  */
206  bool _interfaceMapping();
207 
208  void _positionController(); /** applies the P-position-controller */
209  void _velocityController(const float &dt); /** applies the PID-velocity-controller */
210  void _setCtrlFlag(bool value); /**< set control-loop flags (only required for logging) */
211 
212  // Gains
213  matrix::Vector3f _gain_pos_p; ///< Position control proportional gain
214  matrix::Vector3f _gain_vel_p; ///< Velocity control proportional gain
215  matrix::Vector3f _gain_vel_i; ///< Velocity control integral gain
216  matrix::Vector3f _gain_vel_d; ///< Velocity control derivative gain
217 
218  // Limits
219  float _lim_vel_horizontal{}; ///< Horizontal velocity limit with feed forward and position control
220  float _lim_vel_up{}; ///< Upwards velocity limit with feed forward and position control
221  float _lim_vel_down{}; ///< Downwards velocity limit with feed forward and position control
222  float _lim_thr_min{}; ///< Minimum collective thrust allowed as output [-1,0] e.g. -0.9
223  float _lim_thr_max{}; ///< Maximum collective thrust allowed as output [-1,0] e.g. -0.1
224  float _lim_tilt{}; ///< Maximum tilt from level the output attitude is allowed to have
225 
226  float _hover_thrust{}; ///< Thrust [0,1] with which the vehicle hovers not aacelerating down or up with level orientation
227 
228  // States
229  matrix::Vector3f _pos; /**< current position */
230  matrix::Vector3f _vel; /**< current velocity */
231  matrix::Vector3f _vel_dot; /**< velocity derivative (replacement for acceleration estimate) */
232  matrix::Vector3f _thr_int; /**< integral term of the velocity controller */
233  float _yaw{}; /**< current heading */
234 
235  vehicle_constraints_s _constraints{}; /**< variable constraints */
236 
237  // Setpoints
238  matrix::Vector3f _pos_sp; /**< desired position */
239  matrix::Vector3f _vel_sp; /**< desired velocity */
240  matrix::Vector3f _acc_sp; /**< desired acceleration */
241  matrix::Vector3f _thr_sp; /**< desired thrust */
242  float _yaw_sp{}; /**< desired heading */
243  float _yawspeed_sp{}; /** desired yaw-speed */
244 
245  bool _skip_controller{false}; /**< skips position/velocity controller. true for stabilized mode */
246  bool _ctrl_pos[3] = {true, true, true}; /**< True if the control-loop for position was used */
247  bool _ctrl_vel[3] = {true, true, true}; /**< True if the control-loop for velocity was used */
248 };
void resetIntegralZ()
Set the integral term in z to 0.
void resetIntegralXY()
Set the integral term in xy to 0.
matrix::Vector3f acceleration
matrix::Vector3f _thr_int
integral term of the velocity controller
matrix::Vector3f _acc_sp
desired acceleration
matrix::Vector3f _pos
current position
matrix::Vector3f position
void setTiltLimit(const float tilt)
Set the maximum tilt angle in radians the output attitude is allowed to have.
Core Position-Control for MC.
matrix::Vector3f velocity
void setHoverThrust(const float thrust)
Set the maximum tilt angle in radians the output attitude is allowed to have.
matrix::Vector3f _vel_dot
velocity derivative (replacement for acceleration estimate)
const matrix::Vector3f getVelSp() const
Get the.
matrix::Vector3f _pos_sp
desired position
matrix::Vector3f _gain_pos_p
Position control proportional gain.
constexpr _Tp min(_Tp a, _Tp b)
Definition: Limits.hpp:54
void setPositionGains(const matrix::Vector3f &P)
Set the position control gains.
matrix::Vector3f _vel_sp
desired velocity
constexpr _Tp max(_Tp a, _Tp b)
Definition: Limits.hpp:60
float dt
Definition: px4io.c:73
matrix::Vector3f _thr_sp
desired thrust
P[0][0]
Definition: quatCovMat.c:44
matrix::Vector3f _gain_vel_p
Velocity control proportional gain.
matrix::Vector3f _vel
current velocity
matrix::Vector3f _gain_vel_i
Velocity control integral gain.
matrix::Vector3f _gain_vel_d
Velocity control derivative gain.