40 #include <mathlib/mathlib.h> 42 #include <px4_platform_common/defines.h> 55 _lim_vel_horizontal = vel_horizontal;
57 _lim_vel_down = vel_down;
76 for (
int i = 0; i <= 2; i++) {
77 _ctrl_pos[i] = _ctrl_vel[i] = value;
86 _pos_sp =
Vector3f(setpoint.
x, setpoint.
y, setpoint.
z);
90 _yaw_sp = setpoint.
yaw;
92 bool mapping_succeeded = _interfaceMapping();
96 _skip_controller = PX4_ISFINITE(_thr_sp(0)) && PX4_ISFINITE(_thr_sp(1))
97 && PX4_ISFINITE(_thr_sp(2));
99 return mapping_succeeded;
104 if (_skip_controller) {
108 float thr_mag = _thr_sp.length();
110 if (thr_mag > _lim_thr_max) {
111 _thr_sp = _thr_sp.normalized() * _lim_thr_max;
113 }
else if (thr_mag < _lim_thr_min && thr_mag >
FLT_EPSILON) {
114 _thr_sp = _thr_sp.normalized() * _lim_thr_min;
123 _positionController();
124 _velocityController(dt);
131 bool failsafe =
false;
139 for (
int i = 0; i <= 2; i++) {
141 if (PX4_ISFINITE(_pos_sp(i))) {
144 if (!PX4_ISFINITE(_vel_sp(i))) {
153 if (!PX4_ISFINITE(_pos(i)) || !PX4_ISFINITE(_vel(i))) {
157 }
else if (PX4_ISFINITE(_vel_sp(i))) {
161 _pos_sp(i) = _pos(i) = 0.0f;
162 _ctrl_pos[i] =
false;
168 if (!PX4_ISFINITE(_vel(i))) {
172 }
else if (PX4_ISFINITE(_thr_sp(i))) {
176 _pos_sp(i) = _pos(i) = 0.0f;
177 _vel_sp(i) = _vel(i) = 0.0f;
178 _ctrl_pos[i] = _ctrl_vel[i] =
false;
192 if (!PX4_ISFINITE(_vel_dot(0)) || !PX4_ISFINITE(_vel_dot(1))) {
193 _vel_dot(0) = _vel_dot(1) = 0.0f;
196 if (!PX4_ISFINITE(_vel_dot(2))) {
200 if (!PX4_ISFINITE(_yawspeed_sp)) {
205 if (!PX4_ISFINITE(_yaw_sp)) {
209 if (PX4_ISFINITE(_yaw)) {
220 _thr_sp(0) = _thr_sp(1) = 0.0f;
223 _thr_sp(2) = -(_lim_thr_min + (_hover_thrust - _lim_thr_min) * 0.7
f);
234 const Vector3f vel_sp_position = (_pos_sp - _pos).emult(_gain_pos_p);
235 _vel_sp = vel_sp_position + _vel_sp;
240 Vector2f(_vel_sp - vel_sp_position), _lim_vel_horizontal);
241 _vel_sp(0) = vel_sp_xy(0);
242 _vel_sp(1) = vel_sp_xy(1);
244 _vel_sp(2) =
math::constrain(_vel_sp(2), -_constraints.speed_up, _constraints.speed_down);
273 const Vector3f vel_err = _vel_sp - _vel;
276 float thrust_desired_D = _gain_vel_p(2) * vel_err(2) + _gain_vel_d(2) * _vel_dot(2) + _thr_int(
280 float uMax = -_lim_thr_min;
281 float uMin = -_lim_thr_max;
287 bool stop_integral_D = (thrust_desired_D >= uMax && vel_err(2) >= 0.0f) ||
288 (thrust_desired_D <= uMin && vel_err(2) <= 0.0f);
290 if (!stop_integral_D) {
291 _thr_int(2) += vel_err(2) * _gain_vel_i(2) *
dt;
300 if (PX4_ISFINITE(_thr_sp(0)) && PX4_ISFINITE(_thr_sp(1))) {
303 float thr_xy_max = fabsf(_thr_sp(2)) * tanf(_constraints.tilt);
304 _thr_sp(0) *= thr_xy_max;
305 _thr_sp(1) *= thr_xy_max;
310 thrust_desired_NE(0) = _gain_vel_p(0) * vel_err(0) + _gain_vel_d(0) * _vel_dot(0) + _thr_int(0);
311 thrust_desired_NE(1) = _gain_vel_p(1) * vel_err(1) + _gain_vel_d(1) * _vel_dot(1) + _thr_int(1);
314 float thrust_max_NE_tilt = fabsf(_thr_sp(2)) * tanf(_constraints.tilt);
315 float thrust_max_NE = sqrtf(_lim_thr_max * _lim_thr_max - _thr_sp(2) * _thr_sp(2));
316 thrust_max_NE =
math::min(thrust_max_NE_tilt, thrust_max_NE);
319 _thr_sp(0) = thrust_desired_NE(0);
320 _thr_sp(1) = thrust_desired_NE(1);
322 if (thrust_desired_NE * thrust_desired_NE > thrust_max_NE * thrust_max_NE) {
323 float mag = thrust_desired_NE.
length();
324 _thr_sp(0) = thrust_desired_NE(0) / mag * thrust_max_NE;
325 _thr_sp(1) = thrust_desired_NE(1) / mag * thrust_max_NE;
330 float arw_gain = 2.f / _gain_vel_p(0);
333 vel_err_lim(0) = vel_err(0) - (thrust_desired_NE(0) - _thr_sp(0)) * arw_gain;
334 vel_err_lim(1) = vel_err(1) - (thrust_desired_NE(1) - _thr_sp(1)) * arw_gain;
337 _thr_int(0) += _gain_vel_i(0) * vel_err_lim(0) *
dt;
338 _thr_int(1) += _gain_vel_i(1) * vel_err_lim(1) *
dt;
344 _constraints = constraints;
349 if (!PX4_ISFINITE(constraints.
tilt)
350 || !(constraints.
tilt < _lim_tilt)) {
351 _constraints.
tilt = _lim_tilt;
354 if (!PX4_ISFINITE(constraints.
speed_up) || !(constraints.
speed_up < _lim_vel_up)) {
355 _constraints.speed_up = _lim_vel_up;
359 _constraints.speed_down = _lim_vel_down;
362 if (!PX4_ISFINITE(constraints.
speed_xy) || !(constraints.
speed_xy < _lim_vel_horizontal)) {
363 _constraints.speed_xy = _lim_vel_horizontal;
369 local_position_setpoint.
x = _pos_sp(0);
370 local_position_setpoint.
y = _pos_sp(1);
371 local_position_setpoint.
z = _pos_sp(2);
372 local_position_setpoint.
yaw = _yaw_sp;
373 local_position_setpoint.
yawspeed = _yawspeed_sp;
374 local_position_setpoint.
vx = _vel_sp(0);
375 local_position_setpoint.
vy = _vel_sp(1);
376 local_position_setpoint.
vz = _vel_sp(2);
378 _thr_sp.copyTo(local_position_setpoint.
thrust);
constexpr _Tp constrain(_Tp val, _Tp min_val, _Tp max_val)
void _positionController()
void updateConstraints(const vehicle_constraints_s &constraints)
Set constraints that are stricter than the global limits.
matrix::Vector3f acceleration
void generateThrustYawSetpoint(const float dt)
Apply P-position and PID-velocity controller that updates the member thrust, yaw- and yawspeed-setpoi...
A cascaded position controller for position/velocity control only.
matrix::Vector3f position
void getAttitudeSetpoint(vehicle_attitude_setpoint_s &attitude_setpoint) const
Get the controllers output attitude setpoint This attitude setpoint was generated from the resulting ...
void updateState(const PositionControlStates &states)
Update the current vehicle state.
void setThrustLimits(const float min, const float max)
Set the minimum and maximum collective normalized thrust [0,1] that can be output by the controller...
void setVelocityLimits(const float vel_horizontal, const float vel_up, float vel_down)
Set the maximum velocity to execute with feed forward and position control.
void _velocityController(const float &dt)
applies the P-position-controller
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Vector2< float > Vector2f
matrix::Vector3f velocity
void setVelocityGains(const matrix::Vector3f &P, const matrix::Vector3f &I, const matrix::Vector3f &D)
Set the velocity control gains.
Dual< Scalar, N > min(const Dual< Scalar, N > &a, const Dual< Scalar, N > &b)
void _setCtrlFlag(bool value)
applies the PID-velocity-controller
constexpr _Tp min(_Tp a, _Tp b)
void thrustToAttitude(const Vector3f &thr_sp, const float yaw_sp, vehicle_attitude_setpoint_s &att_sp)
Converts thrust vector and yaw set-point to a desired attitude.
Vector3< float > Vector3f
Vector2f constrainXY(const Vector2f &v0, const Vector2f &v1, const float &max)
Outputs the sum of two vectors but respecting the limits and priority.
bool _interfaceMapping()
Maps setpoints to internal-setpoints.
void getLocalPositionSetpoint(vehicle_local_position_setpoint_s &local_position_setpoint) const
Get the controllers output local position setpoint These setpoints are the ones which were executed o...
bool updateSetpoint(const vehicle_local_position_setpoint_s &setpoint)
Update the desired setpoints.
Dual< Scalar, N > max(const Dual< Scalar, N > &a, const Dual< Scalar, N > &b)
Simple functions for vector manipulation that do not fit into matrix lib.