Firmware
PositionControl.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 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 
40 #include <matrix/matrix/math.hpp>
41 
42 #include <uORB/topics/vehicle_local_position.h>
43 #include <uORB/topics/vehicle_local_position_setpoint.h>
44 #include <uORB/topics/vehicle_constraints.h>
45 #include <px4_module_params.h>
46 #pragma once
47 
49  matrix::Vector3f position;
50  matrix::Vector3f velocity;
51  matrix::Vector3f acceleration;
52  float yaw;
53 };
54 
76 {
77 public:
78 
80  ~PositionControl() = default;
81 
88  void overwriteParams();
89 
94  void updateState(const PositionControlStates &states);
95 
101  bool updateSetpoint(const vehicle_local_position_setpoint_s &setpoint);
102 
107  void updateConstraints(const vehicle_constraints_s &constraints);
108 
117  void generateThrustYawSetpoint(const float dt);
118 
123  void resetIntegralXY() { _thr_int(0) = _thr_int(1) = 0.0f; }
124 
129  void resetIntegralZ() { _thr_int(2) = 0.0f; }
130 
136  const matrix::Vector3f &getThrustSetpoint() { return _thr_sp; }
137 
143  const float &getYawSetpoint() { return _yaw_sp; }
144 
150  const float &getYawspeedSetpoint() { return _yawspeed_sp; }
151 
157  const matrix::Vector3f getVelSp()
158  {
159  matrix::Vector3f vel_sp{};
160 
161  for (int i = 0; i <= 2; i++) {
162  if (_ctrl_vel[i]) {
163  vel_sp(i) = _vel_sp(i);
164 
165  } else {
166  vel_sp(i) = NAN;
167  }
168  }
169 
170  return vel_sp;
171  }
172 
178  const matrix::Vector3f getPosSp()
179  {
180  matrix::Vector3f pos_sp{};
181 
182  for (int i = 0; i <= 2; i++) {
183  if (_ctrl_pos[i]) {
184  pos_sp(i) = _pos_sp(i);
185 
186  } else {
187  pos_sp(i) = NAN;
188  }
189  }
190 
191  return pos_sp;
192  }
193 
194 protected:
195 
196  void updateParams() override;
197 
198 private:
203  bool _interfaceMapping();
204 
205  void _positionController();
206  void _velocityController(const float &dt);
207  void _setCtrlFlag(bool value);
209  matrix::Vector3f _pos{};
210  matrix::Vector3f _vel{};
211  matrix::Vector3f _vel_dot{};
212  matrix::Vector3f _acc{};
213  float _yaw{0.0f};
214  matrix::Vector3f _pos_sp{};
215  matrix::Vector3f _vel_sp{};
216  matrix::Vector3f _acc_sp{};
217  matrix::Vector3f _thr_sp{};
218  float _yaw_sp{};
219  float _yawspeed_sp{};
220  matrix::Vector3f _thr_int{};
221  vehicle_constraints_s _constraints{};
222  bool _skip_controller{false};
223  bool _ctrl_pos[3] = {true, true, true};
224  bool _ctrl_vel[3] = {true, true, true};
226  DEFINE_PARAMETERS(
227  (ParamFloat<px4::params::MPC_THR_MAX>) _param_mpc_thr_max,
228  (ParamFloat<px4::params::MPC_THR_HOVER>) _param_mpc_thr_hover,
229  (ParamFloat<px4::params::MPC_THR_MIN>) _param_mpc_thr_min,
230  (ParamFloat<px4::params::MPC_MANTHR_MIN>) _param_mpc_manthr_min,
231  (ParamFloat<px4::params::MPC_XY_VEL_MAX>) _param_mpc_xy_vel_max,
232  (ParamFloat<px4::params::MPC_Z_VEL_MAX_DN>) _param_mpc_z_vel_max_dn,
233  (ParamFloat<px4::params::MPC_Z_VEL_MAX_UP>) _param_mpc_z_vel_max_up,
235  _param_mpc_tiltmax_air, // maximum tilt for any position controlled mode in radians
237  _param_mpc_man_tilt_max, // maximum til for stabilized/altitude mode in radians
238  (ParamFloat<px4::params::MPC_Z_P>) _param_mpc_z_p,
239  (ParamFloat<px4::params::MPC_Z_VEL_P>) _param_mpc_z_vel_p,
240  (ParamFloat<px4::params::MPC_Z_VEL_I>) _param_mpc_z_vel_i,
241  (ParamFloat<px4::params::MPC_Z_VEL_D>) _param_mpc_z_vel_d,
242  (ParamFloat<px4::params::MPC_XY_P>) _param_mpc_xy_p,
243  (ParamFloat<px4::params::MPC_XY_VEL_P>) _param_mpc_xy_vel_p,
244  (ParamFloat<px4::params::MPC_XY_VEL_I>) _param_mpc_xy_vel_i,
245  (ParamFloat<px4::params::MPC_XY_VEL_D>) _param_mpc_xy_vel_d
246  )
247 };
void resetIntegralZ()
Set the integral term in z to 0.
Definition: PositionControl.hpp:129
Core Position-Control for MC.
Definition: PositionControl.hpp:75
void resetIntegralXY()
Set the integral term in xy to 0.
Definition: PositionControl.hpp:123
const float & getYawspeedSetpoint()
Get the.
Definition: PositionControl.hpp:150
const matrix::Vector3f & getThrustSetpoint()
Get the.
Definition: PositionControl.hpp:136
const matrix::Vector3f getVelSp()
Get the.
Definition: PositionControl.hpp:157
Definition: px4_param.h:318
C++ base class for modules/classes using configuration parameters.
Definition: px4_module_params.h:46
const matrix::Vector3f getPosSp()
Get the.
Definition: PositionControl.hpp:178
const float & getYawSetpoint()
Get the.
Definition: PositionControl.hpp:143
Definition: PositionControl.hpp:48