PositionControl.hpp
8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/****************************************************************************
*
* Copyright (c) 2018 - 2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/**
* @file PositionControl.hpp
*
* A cascaded position controller for position/velocity control only.
*/
#pragma once
#include <lib/mathlib/mathlib.h>
#include <matrix/matrix/math.hpp>
#include <uORB/topics/vehicle_attitude_setpoint.h>
#include <uORB/topics/vehicle_local_position_setpoint.h>
struct PositionControlStates {
matrix::Vector3f position;
matrix::Vector3f velocity;
matrix::Vector3f acceleration;
float yaw;
};
/**
* Core Position-Control for MC.
* This class contains P-controller for position and
* PID-controller for velocity.
* Inputs:
* vehicle position/velocity/yaw
* desired set-point position/velocity/thrust/yaw/yaw-speed
* constraints that are stricter than global limits
* Output
* thrust vector and a yaw-setpoint
*
* If there is a position and a velocity set-point present, then
* the velocity set-point is used as feed-forward. If feed-forward is
* active, then the velocity component of the P-controller output has
* priority over the feed-forward component.
*
* A setpoint that is NAN is considered as not set.
* If there is a position/velocity- and thrust-setpoint present, then
* the thrust-setpoint is ommitted and recomputed from position-velocity-PID-loop.
*/
class PositionControl
{
public:
PositionControl() = default;
~PositionControl() = default;
/**
* Set the position control gains
* @param P 3D vector of proportional gains for x,y,z axis
*/
void setPositionGains(const matrix::Vector3f &P) { _gain_pos_p = P; }
/**
* Set the velocity control gains
* @param P 3D vector of proportional gains for x,y,z axis
* @param I 3D vector of integral gains
* @param D 3D vector of derivative gains
*/
void setVelocityGains(const matrix::Vector3f &P, const matrix::Vector3f &I, const matrix::Vector3f &D);
/**
* Set the maximum velocity to execute with feed forward and position control
* @param vel_horizontal horizontal velocity limit
* @param vel_up upwards velocity limit
* @param vel_down downwards velocity limit
*/
void setVelocityLimits(const float vel_horizontal, const float vel_up, float vel_down);
/**
* Set the minimum and maximum collective normalized thrust [0,1] that can be output by the controller
* @param min minimum thrust e.g. 0.1 or 0
* @param max maximum thrust e.g. 0.9 or 1
*/
void setThrustLimits(const float min, const float max);
/**
* Set the maximum tilt angle in radians the output attitude is allowed to have
* @param tilt angle in radians from level orientation
*/
void setTiltLimit(const float tilt) { _lim_tilt = tilt; }
/**
* Set the normalized hover thrust
* @param thrust [0.1, 0.9] with which the vehicle hovers not acelerating down or up with level orientation
*/
void setHoverThrust(const float hover_thrust) { _hover_thrust = math::constrain(hover_thrust, 0.1f, 0.9f); }
/**
* Update the hover thrust without immediately affecting the output
* by adjusting the integrator. This prevents propagating the dynamics
* of the hover thrust signal directly to the output of the controller.
*/
void updateHoverThrust(const float hover_thrust_new);
/**
* Pass the current vehicle state to the controller
* @param PositionControlStates structure
*/
void setState(const PositionControlStates &states);
/**
* Pass the desired setpoints
* Note: NAN value means no feed forward/leave state uncontrolled if there's no higher order setpoint.
* @param setpoint a vehicle_local_position_setpoint_s structure
*/
void setInputSetpoint(const vehicle_local_position_setpoint_s &setpoint);
/**
* Apply P-position and PID-velocity controller that updates the member
* thrust, yaw- and yawspeed-setpoints.
* @see _thr_sp
* @see _yaw_sp
* @see _yawspeed_sp
* @param dt time in seconds since last iteration
* @return true if update succeeded and output setpoint is executable, false if not
*/
bool update(const float dt);
/**
* Set the integral term in xy to 0.
* @see _vel_int
*/
void resetIntegral() { _vel_int.setZero(); }
/**
* Get the controllers output local position setpoint
* These setpoints are the ones which were executed on including PID output and feed-forward.
* The acceleration or thrust setpoints can be used for attitude control.
* @param local_position_setpoint reference to struct to fill up
*/
void getLocalPositionSetpoint(vehicle_local_position_setpoint_s &local_position_setpoint) const;
/**
* Get the controllers output attitude setpoint
* This attitude setpoint was generated from the resulting acceleration setpoint after position and velocity control.
* It needs to be executed by the attitude controller to achieve velocity and position tracking.
* @param attitude_setpoint reference to struct to fill up
*/
void getAttitudeSetpoint(vehicle_attitude_setpoint_s &attitude_setpoint) const;
private:
bool _updateSuccessful();
void _positionControl(); ///< Position proportional control
void _velocityControl(const float dt); ///< Velocity PID control
void _accelerationControl(); ///< Acceleration setpoint processing
// Gains
matrix::Vector3f _gain_pos_p; ///< Position control proportional gain
matrix::Vector3f _gain_vel_p; ///< Velocity control proportional gain
matrix::Vector3f _gain_vel_i; ///< Velocity control integral gain
matrix::Vector3f _gain_vel_d; ///< Velocity control derivative gain
// Limits
float _lim_vel_horizontal{}; ///< Horizontal velocity limit with feed forward and position control
float _lim_vel_up{}; ///< Upwards velocity limit with feed forward and position control
float _lim_vel_down{}; ///< Downwards velocity limit with feed forward and position control
float _lim_thr_min{}; ///< Minimum collective thrust allowed as output [-1,0] e.g. -0.9
float _lim_thr_max{}; ///< Maximum collective thrust allowed as output [-1,0] e.g. -0.1
float _lim_tilt{}; ///< Maximum tilt from level the output attitude is allowed to have
float _hover_thrust{}; ///< Thrust [0.1, 0.9] with which the vehicle hovers not accelerating down or up with level orientation
// States
matrix::Vector3f _pos; /**< current position */
matrix::Vector3f _vel; /**< current velocity */
matrix::Vector3f _vel_dot; /**< velocity derivative (replacement for acceleration estimate) */
matrix::Vector3f _vel_int; /**< integral term of the velocity controller */
float _yaw{}; /**< current heading */
// Setpoints
matrix::Vector3f _pos_sp; /**< desired position */
matrix::Vector3f _vel_sp; /**< desired velocity */
matrix::Vector3f _acc_sp; /**< desired acceleration */
matrix::Vector3f _thr_sp; /**< desired thrust */
float _yaw_sp{}; /**< desired heading */
float _yawspeed_sp{}; /** desired yaw-speed */
};