PX4 Firmware
PX4 Autopilot Software http://px4.io
FlightTaskManualPositionSmoothVel.cpp
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 
35 
36 #include <mathlib/mathlib.h>
37 #include <float.h>
38 
39 using namespace matrix;
40 
42 {
43  bool ret = FlightTaskManualPosition::activate(last_setpoint);
44 
45  // Check if the previous FlightTask provided setpoints
46  checkSetpoints(last_setpoint);
47  const Vector2f accel_prev(last_setpoint.acceleration[0], last_setpoint.acceleration[1]);
48  const Vector2f vel_prev(last_setpoint.vx, last_setpoint.vy);
49  const Vector2f pos_prev(last_setpoint.x, last_setpoint.y);
50 
51  _smoothing_xy.reset(accel_prev, vel_prev, pos_prev);
52  _smoothing_z.reset(last_setpoint.acceleration[2], last_setpoint.vz, last_setpoint.z);
53 
54  return ret;
55 }
56 
58 {
59  // The task is reacivated while the vehicle is on the ground. To detect takeoff in mc_pos_control_main properly
60  // using the generated jerk, reset the z derivatives to zero
61  _smoothing_xy.reset(Vector2f(), Vector2f(_velocity), Vector2f(_position));
62  _smoothing_z.reset(0.f, 0.f, _position(2));
63 }
64 
65 void FlightTaskManualPositionSmoothVel::checkSetpoints(vehicle_local_position_setpoint_s &setpoints)
66 {
67  // If the position setpoint is unknown, set to the current postion
68  if (!PX4_ISFINITE(setpoints.x)) { setpoints.x = _position(0); }
69 
70  if (!PX4_ISFINITE(setpoints.y)) { setpoints.y = _position(1); }
71 
72  if (!PX4_ISFINITE(setpoints.z)) { setpoints.z = _position(2); }
73 
74  // If the velocity setpoint is unknown, set to the current velocity
75  if (!PX4_ISFINITE(setpoints.vx)) { setpoints.vx = _velocity(0); }
76 
77  if (!PX4_ISFINITE(setpoints.vy)) { setpoints.vy = _velocity(1); }
78 
79  if (!PX4_ISFINITE(setpoints.vz)) { setpoints.vz = _velocity(2); }
80 
81  // No acceleration estimate available, set to zero if the setpoint is NAN
82  for (int i = 0; i < 3; i++) {
83  if (!PX4_ISFINITE(setpoints.acceleration[i])) { setpoints.acceleration[i] = 0.f; }
84  }
85 }
86 
88 {
89  _smoothing_xy.setCurrentPosition(_position.xy());
90 }
91 
93 {
94  _smoothing_xy.setCurrentVelocity(_velocity.xy());
95 }
96 
98 {
99  _smoothing_z.setCurrentPosition(_position(2));
100 }
101 
103 {
104  _smoothing_z.setCurrentVelocity(_velocity(2));
105 }
106 
108 {
109  // Set max accel/vel/jerk
110  // Has to be done before _updateTrajectories()
111  _updateTrajConstraints();
112  _updateTrajVelFeedback();
113  _updateTrajCurrentPositionEstimate();
114 
115  // Get yaw setpoint, un-smoothed position setpoints
117 
118  _updateTrajectories(_velocity_setpoint);
119 
120  // Fill the jerk, acceleration, velocity and position setpoint vectors
121  _setOutputState();
122 }
123 
125 {
126  _updateTrajConstraintsXY();
127  _updateTrajConstraintsZ();
128 }
129 
131 {
132  _smoothing_xy.setMaxJerk(_param_mpc_jerk_max.get());
133  _smoothing_xy.setMaxAccel(_param_mpc_acc_hor_max.get());
134  _smoothing_xy.setMaxVel(_constraints.speed_xy);
135 }
136 
138 {
139  _smoothing_z.setMaxJerk(_param_mpc_jerk_max.get());
140 
141  _smoothing_z.setMaxAccelUp(_param_mpc_acc_up_max.get());
142  _smoothing_z.setMaxVelUp(_constraints.speed_up);
143 
144  _smoothing_z.setMaxAccelDown(_param_mpc_acc_down_max.get());
145  _smoothing_z.setMaxVelDown(_constraints.speed_down);
146 }
147 
149 {
150  _smoothing_xy.setVelSpFeedback(Vector2f(_velocity_setpoint_feedback));
151  _smoothing_z.setVelSpFeedback(_velocity_setpoint_feedback(2));
152 }
153 
155 {
156  _smoothing_xy.setCurrentPositionEstimate(Vector2f(_position));
157  _smoothing_z.setCurrentPositionEstimate(_position(2));
158 }
159 
161 {
162  _smoothing_xy.update(_deltatime, Vector2f(vel_target));
163  _smoothing_z.update(_deltatime, vel_target(2));
164 }
165 
167 {
168  _setOutputStateXY();
169  _setOutputStateZ();
170 }
171 
173 {
174  _jerk_setpoint.xy() = _smoothing_xy.getCurrentJerk();
175  _acceleration_setpoint.xy() = _smoothing_xy.getCurrentAcceleration();
176  _velocity_setpoint.xy() = _smoothing_xy.getCurrentVelocity();
177  _position_setpoint.xy() = _smoothing_xy.getCurrentPosition();
178 }
179 
181 {
182  _jerk_setpoint(2) = _smoothing_z.getCurrentJerk();
183  _acceleration_setpoint(2) = _smoothing_z.getCurrentAcceleration();
184  _velocity_setpoint(2) = _smoothing_z.getCurrentVelocity();
185  _position_setpoint(2) = _smoothing_z.getCurrentPosition();
186 }
void _updateSetpoints() override
updates all setpoints
void reActivate() override
Call this to reset an active Flight Task.
Flight task for smooth manual controlled position.
bool activate(vehicle_local_position_setpoint_s last_setpoint) override
Call once on the event where you switch to the task.
virtual void _updateSetpoints() override
updates all setpoints
bool activate(vehicle_local_position_setpoint_s last_setpoint) override
Call once on the event where you switch to the task.
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
Vector2< float > Vector2f
Definition: Vector2.hpp:69
DEFINE_PARAMETERS_CUSTOM_PARENT(FlightTaskManualPosition,(ParamFloat< px4::params::MPC_JERK_MAX >) _param_mpc_jerk_max,(ParamFloat< px4::params::MPC_ACC_UP_MAX >) _param_mpc_acc_up_max,(ParamFloat< px4::params::MPC_ACC_DOWN_MAX >) _param_mpc_acc_down_max) private void _updateTrajConstraints()
void _ekfResetHandlerPositionXY() override
Reset position or velocity setpoints in case of EKF reset event.