PX4 Firmware
PX4 Autopilot Software http://px4.io
FlightTaskManualAltitudeSmoothVel.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 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 FlightManualAltitude.cpp
36  */
37 
39 
40 #include <float.h>
41 
42 using namespace matrix;
43 
45 {
46  bool ret = FlightTaskManualAltitude::activate(last_setpoint);
47 
48  // Check if the previous FlightTask provided setpoints
49  checkSetpoints(last_setpoint);
50 
51  _smoothing.reset(last_setpoint.acceleration[2], last_setpoint.vz, last_setpoint.z);
52 
53  return ret;
54 }
55 
57 {
58  // The task is reacivated while the vehicle is on the ground. To detect takeoff in mc_pos_control_main properly
59  // using the generated jerk, reset the z derivatives to zero
60  _smoothing.reset(0.f, 0.f, _position(2));
61 }
62 
63 void FlightTaskManualAltitudeSmoothVel::checkSetpoints(vehicle_local_position_setpoint_s &setpoints)
64 {
65  // If the position setpoint is unknown, set to the current postion
66  if (!PX4_ISFINITE(setpoints.z)) { setpoints.z = _position(2); }
67 
68  // If the velocity setpoint is unknown, set to the current velocity
69  if (!PX4_ISFINITE(setpoints.vz)) { setpoints.vz = _velocity(2); }
70 
71  // No acceleration estimate available, set to zero if the setpoint is NAN
72  if (!PX4_ISFINITE(setpoints.acceleration[2])) { setpoints.acceleration[2] = 0.f; }
73 }
74 
76 {
77  _smoothing.setCurrentPosition(_position(2));
78 }
79 
81 {
82  _smoothing.setCurrentVelocity(_velocity(2));
83 }
84 
86 {
87  // Set max accel/vel/jerk
88  // Has to be done before _updateTrajectories()
89  _updateTrajConstraints();
90 
91  _smoothing.setVelSpFeedback(_velocity_setpoint_feedback(2));
92  _smoothing.setCurrentPositionEstimate(_position(2));
93 
94  // Get yaw setpoint, un-smoothed position setpoints
96 
97  _smoothing.update(_deltatime, _velocity_setpoint(2));
98 
99  // Fill the jerk, acceleration, velocity and position setpoint vectors
100  _setOutputState();
101 }
102 
103 void FlightTaskManualAltitudeSmoothVel::_updateTrajConstraints()
104 {
105  _smoothing.setMaxJerk(_param_mpc_jerk_max.get());
106 
107  _smoothing.setMaxAccelUp(_param_mpc_acc_up_max.get());
108  _smoothing.setMaxVelUp(_constraints.speed_up);
109 
110  _smoothing.setMaxAccelDown(_param_mpc_acc_down_max.get());
111  _smoothing.setMaxVelDown(_constraints.speed_down);
112 }
113 
114 
116 {
117  _jerk_setpoint(2) = _smoothing.getCurrentJerk();
118  _acceleration_setpoint(2) = _smoothing.getCurrentAcceleration();
119  _velocity_setpoint(2) = _smoothing.getCurrentVelocity();
120  _position_setpoint(2) = _smoothing.getCurrentPosition();
121 }
void reActivate() override
Call this to reset an active Flight Task.
void _ekfResetHandlerPositionZ() override
Reset position or velocity setpoints in case of EKF reset event.
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
virtual void _updateSetpoints() override
updates all setpoints
virtual void _updateSetpoints()
updates all setpoints
bool activate(vehicle_local_position_setpoint_s last_setpoint) override
Call once on the event where you switch to the task.