2017-08-14 19:23:24 +02:00
|
|
|
/****************************************************************************
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2017 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.
|
|
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/**
|
2017-08-14 15:16:05 +02:00
|
|
|
* @file FlightTask.hpp
|
2017-08-14 19:23:24 +02:00
|
|
|
*
|
|
|
|
|
* Abstract base class for different advanced flight tasks like orbit, follow me, ...
|
|
|
|
|
*
|
|
|
|
|
* @author Matthias Grob <maetugr@gmail.com>
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-10-25 10:56:32 +02:00
|
|
|
#include <px4_platform_common/module_params.h>
|
2017-11-08 10:57:56 +01:00
|
|
|
#include <drivers/drv_hrt.h>
|
2017-11-07 19:36:35 +01:00
|
|
|
#include <matrix/matrix/math.hpp>
|
2019-09-30 18:16:36 -04:00
|
|
|
#include <uORB/Subscription.hpp>
|
2018-11-01 17:43:49 +01:00
|
|
|
#include <uORB/topics/landing_gear.h>
|
2017-11-08 10:57:56 +01:00
|
|
|
#include <uORB/topics/vehicle_local_position.h>
|
|
|
|
|
#include <uORB/topics/vehicle_local_position_setpoint.h>
|
2017-12-14 07:51:39 +01:00
|
|
|
#include <uORB/topics/vehicle_command.h>
|
2018-04-26 11:21:26 +02:00
|
|
|
#include <uORB/topics/vehicle_constraints.h>
|
2018-07-28 16:08:58 +02:00
|
|
|
#include <uORB/topics/vehicle_attitude.h>
|
2018-07-09 11:53:35 +02:00
|
|
|
#include <uORB/topics/vehicle_trajectory_waypoint.h>
|
2019-11-21 18:20:13 +01:00
|
|
|
#include <uORB/topics/home_position.h>
|
2019-11-12 13:54:00 -05:00
|
|
|
#include <lib/weather_vane/WeatherVane.hpp>
|
2017-11-20 21:55:28 +01:00
|
|
|
|
2020-04-22 13:18:35 +02:00
|
|
|
struct ekf_reset_counters_s {
|
|
|
|
|
uint8_t xy;
|
|
|
|
|
uint8_t vxy;
|
|
|
|
|
uint8_t z;
|
|
|
|
|
uint8_t vz;
|
2020-08-07 14:06:38 -04:00
|
|
|
uint8_t heading;
|
2020-04-22 13:18:35 +02:00
|
|
|
};
|
|
|
|
|
|
2018-03-26 08:14:36 +02:00
|
|
|
class FlightTask : public ModuleParams
|
2017-08-14 19:23:24 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2018-03-26 08:14:36 +02:00
|
|
|
FlightTask() :
|
|
|
|
|
ModuleParams(nullptr)
|
2018-04-26 11:22:18 +02:00
|
|
|
{
|
|
|
|
|
_resetSetpoints();
|
|
|
|
|
_constraints = empty_constraints;
|
|
|
|
|
}
|
2017-08-14 19:23:24 +02:00
|
|
|
|
2017-11-28 22:00:46 +01:00
|
|
|
virtual ~FlightTask() = default;
|
|
|
|
|
|
2017-11-27 21:31:47 +01:00
|
|
|
/**
|
2017-11-28 18:17:39 +01:00
|
|
|
* Call once on the event where you switch to the task
|
2020-07-04 11:54:27 +02:00
|
|
|
* @param last_setpoint last output of the previous task
|
2017-11-28 22:00:46 +01:00
|
|
|
* @return true on success, false on error
|
2017-11-28 18:17:39 +01:00
|
|
|
*/
|
2020-08-04 12:42:36 -04:00
|
|
|
virtual bool activate(const vehicle_local_position_setpoint_s &last_setpoint);
|
2017-11-27 21:31:47 +01:00
|
|
|
|
2018-10-23 15:24:57 +02:00
|
|
|
/**
|
|
|
|
|
* Call this to reset an active Flight Task
|
|
|
|
|
*/
|
|
|
|
|
virtual void reActivate();
|
|
|
|
|
|
2017-12-14 07:51:39 +01:00
|
|
|
/**
|
|
|
|
|
* To be called to adopt parameters from an arrived vehicle command
|
2019-01-27 09:44:53 +01:00
|
|
|
* @param command received command message containing the parameters
|
2017-12-14 07:51:39 +01:00
|
|
|
* @return true if accepted, false if declined
|
|
|
|
|
*/
|
2019-01-27 09:44:53 +01:00
|
|
|
virtual bool applyCommandParameters(const vehicle_command_s &command) { return false; }
|
2017-12-14 07:51:39 +01:00
|
|
|
|
2017-11-28 22:00:46 +01:00
|
|
|
/**
|
|
|
|
|
* Call before activate() or update()
|
|
|
|
|
* to initialize time and input data
|
|
|
|
|
* @return true on success, false on error
|
|
|
|
|
*/
|
|
|
|
|
virtual bool updateInitialize();
|
2017-08-14 19:23:24 +02:00
|
|
|
|
|
|
|
|
/**
|
2017-05-22 13:26:08 +02:00
|
|
|
* To be called regularly in the control loop cycle to execute the task
|
2017-11-28 22:00:46 +01:00
|
|
|
* @return true on success, false on error
|
2017-08-14 19:23:24 +02:00
|
|
|
*/
|
2020-04-22 13:18:35 +02:00
|
|
|
virtual bool update();
|
2017-08-14 19:23:24 +02:00
|
|
|
|
2019-04-24 14:34:00 +02:00
|
|
|
/**
|
|
|
|
|
* Call after update()
|
|
|
|
|
* to constrain the generated setpoints in order to comply
|
|
|
|
|
* with the constraints of the current mode
|
|
|
|
|
* @return true on success, false on error
|
|
|
|
|
*/
|
|
|
|
|
virtual bool updateFinalize() { return true; };
|
|
|
|
|
|
2018-02-26 13:30:23 +01:00
|
|
|
/**
|
2017-11-05 19:48:20 +01:00
|
|
|
* Get the output data
|
2019-01-27 09:44:53 +01:00
|
|
|
* @return task output setpoints that get executed by the positon controller
|
2018-02-26 13:30:23 +01:00
|
|
|
*/
|
2018-02-28 09:30:20 +01:00
|
|
|
const vehicle_local_position_setpoint_s getPositionSetpoint();
|
2018-02-26 13:30:23 +01:00
|
|
|
|
2020-04-22 13:18:35 +02:00
|
|
|
const ekf_reset_counters_s getResetCounters() const { return _reset_counters; }
|
|
|
|
|
void setResetCounters(const ekf_reset_counters_s &counters) { _reset_counters = counters; }
|
|
|
|
|
|
2018-04-26 11:21:26 +02:00
|
|
|
/**
|
|
|
|
|
* Get vehicle constraints.
|
|
|
|
|
* The constraints can vary with task.
|
|
|
|
|
* @return constraints
|
|
|
|
|
*/
|
2018-08-28 14:57:47 -04:00
|
|
|
const vehicle_constraints_s &getConstraints() { return _constraints; }
|
2018-04-26 11:21:26 +02:00
|
|
|
|
2018-11-01 17:43:49 +01:00
|
|
|
/**
|
|
|
|
|
* Get landing gear position.
|
|
|
|
|
* The constraints can vary with task.
|
|
|
|
|
* @return landing gear
|
|
|
|
|
*/
|
|
|
|
|
const landing_gear_s &getGear() { return _gear; }
|
|
|
|
|
|
2018-07-18 08:53:39 +02:00
|
|
|
/**
|
|
|
|
|
* Get avoidance desired waypoint
|
|
|
|
|
* @return desired waypoints
|
|
|
|
|
*/
|
2018-08-28 14:57:47 -04:00
|
|
|
const vehicle_trajectory_waypoint_s &getAvoidanceWaypoint() { return _desired_waypoint; }
|
2018-07-09 11:53:35 +02:00
|
|
|
|
2018-04-26 11:21:26 +02:00
|
|
|
/**
|
|
|
|
|
* Empty setpoint.
|
|
|
|
|
* All setpoints are set to NAN.
|
|
|
|
|
*/
|
2018-01-05 17:58:06 +01:00
|
|
|
static const vehicle_local_position_setpoint_s empty_setpoint;
|
2017-12-15 01:59:55 +01:00
|
|
|
|
2020-04-22 13:18:35 +02:00
|
|
|
/**.
|
|
|
|
|
* All counters are set to 0.
|
|
|
|
|
*/
|
|
|
|
|
static const ekf_reset_counters_s zero_reset_counters;
|
|
|
|
|
|
2018-04-26 11:21:26 +02:00
|
|
|
/**
|
|
|
|
|
* Empty constraints.
|
|
|
|
|
* All constraints are set to NAN.
|
|
|
|
|
*/
|
|
|
|
|
static const vehicle_constraints_s empty_constraints;
|
|
|
|
|
|
2018-11-01 17:43:49 +01:00
|
|
|
/**
|
|
|
|
|
* default landing gear state
|
|
|
|
|
*/
|
2018-11-13 16:53:36 +01:00
|
|
|
static const landing_gear_s empty_landing_gear_default_keep;
|
2018-11-01 17:43:49 +01:00
|
|
|
|
2018-03-26 08:14:36 +02:00
|
|
|
/**
|
|
|
|
|
* Call this whenever a parameter update notification is received (parameter_update uORB message)
|
|
|
|
|
*/
|
|
|
|
|
void handleParameterUpdate()
|
|
|
|
|
{
|
|
|
|
|
updateParams();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-04 17:30:30 +02:00
|
|
|
/**
|
|
|
|
|
* Sets an external yaw handler which can be used by any flight task to implement a different yaw control strategy.
|
|
|
|
|
* This method does nothing, each flighttask which wants to use the yaw handler needs to override this method.
|
|
|
|
|
*/
|
2018-11-05 10:41:06 +01:00
|
|
|
virtual void setYawHandler(WeatherVane *ext_yaw_handler) {}
|
2018-09-04 17:30:30 +02:00
|
|
|
|
2018-10-17 16:33:03 +02:00
|
|
|
void updateVelocityControllerIO(const matrix::Vector3f &vel_sp,
|
2020-01-27 17:17:43 +01:00
|
|
|
const matrix::Vector3f &acc_sp)
|
|
|
|
|
{
|
|
|
|
|
_velocity_setpoint_feedback = vel_sp;
|
|
|
|
|
_acceleration_setpoint_feedback = acc_sp;
|
|
|
|
|
}
|
2018-10-10 15:59:09 +02:00
|
|
|
|
2017-05-19 18:44:46 +02:00
|
|
|
protected:
|
2019-12-07 15:13:27 +01:00
|
|
|
uORB::SubscriptionData<vehicle_local_position_s> _sub_vehicle_local_position{ORB_ID(vehicle_local_position)};
|
|
|
|
|
uORB::SubscriptionData<home_position_s> _sub_home_position{ORB_ID(home_position)};
|
2018-04-18 11:20:51 +02:00
|
|
|
|
2019-05-14 07:57:13 +01:00
|
|
|
/** Reset all setpoints to NAN */
|
2018-04-18 11:20:51 +02:00
|
|
|
void _resetSetpoints();
|
|
|
|
|
|
2019-05-14 07:57:13 +01:00
|
|
|
/** Check and update local position */
|
2018-10-10 15:07:15 +02:00
|
|
|
void _evaluateVehicleLocalPosition();
|
2018-04-18 11:20:51 +02:00
|
|
|
|
2019-11-21 18:20:13 +01:00
|
|
|
void _evaluateDistanceToGround();
|
|
|
|
|
|
2019-05-14 07:57:13 +01:00
|
|
|
/** Set constraints to default values */
|
2019-05-13 21:07:05 +02:00
|
|
|
virtual void _setDefaultConstraints();
|
2018-04-18 11:20:51 +02:00
|
|
|
|
2019-09-23 13:42:06 +02:00
|
|
|
/** Determine when to trigger a takeoff (ignored in flight) */
|
2019-05-14 07:57:13 +01:00
|
|
|
virtual bool _checkTakeoff();
|
|
|
|
|
|
2019-09-23 13:42:06 +02:00
|
|
|
/**
|
|
|
|
|
* Monitor the EKF reset counters and
|
|
|
|
|
* call the appropriate handling functions in case of a reset event
|
2020-04-22 13:18:35 +02:00
|
|
|
* TODO: add the delta values to all the handlers
|
2019-09-23 13:42:06 +02:00
|
|
|
*/
|
|
|
|
|
void _checkEkfResetCounters();
|
|
|
|
|
virtual void _ekfResetHandlerPositionXY() {};
|
|
|
|
|
virtual void _ekfResetHandlerVelocityXY() {};
|
|
|
|
|
virtual void _ekfResetHandlerPositionZ() {};
|
|
|
|
|
virtual void _ekfResetHandlerVelocityZ() {};
|
|
|
|
|
virtual void _ekfResetHandlerHeading(float delta_psi) {};
|
|
|
|
|
|
2017-11-28 21:59:31 +01:00
|
|
|
/* Time abstraction */
|
2017-11-16 05:29:22 +01:00
|
|
|
static constexpr uint64_t _timeout = 500000; /**< maximal time in us before a loop or data times out */
|
2019-12-07 15:13:27 +01:00
|
|
|
float _time{}; /**< passed time in seconds since the task was activated */
|
|
|
|
|
float _deltatime{}; /**< passed time in seconds since the task was last updated */
|
|
|
|
|
hrt_abstime _time_stamp_activate{}; /**< time stamp when task was activated */
|
|
|
|
|
hrt_abstime _time_stamp_current{}; /**< time stamp at the beginning of the current task update */
|
|
|
|
|
hrt_abstime _time_stamp_last{}; /**< time stamp when task was last updated */
|
2017-05-19 19:15:01 +02:00
|
|
|
|
2018-02-28 09:30:20 +01:00
|
|
|
/* Current vehicle state */
|
2017-11-08 18:30:16 +01:00
|
|
|
matrix::Vector3f _position; /**< current vehicle position */
|
|
|
|
|
matrix::Vector3f _velocity; /**< current vehicle velocity */
|
2019-12-07 15:13:27 +01:00
|
|
|
float _yaw{}; /**< current vehicle yaw heading */
|
|
|
|
|
float _dist_to_bottom{}; /**< current height above ground level */
|
|
|
|
|
float _dist_to_ground{}; /**< equals _dist_to_bottom if valid, height above home otherwise */
|
2018-02-26 13:34:07 +01:00
|
|
|
|
2018-04-18 11:21:07 +02:00
|
|
|
/**
|
|
|
|
|
* Setpoints which the position controller has to execute.
|
|
|
|
|
* Setpoints that are set to NAN are not controlled. Not all setpoints can be set at the same time.
|
|
|
|
|
* If more than one type of setpoint is set, then order of control is a as follow: position, velocity,
|
|
|
|
|
* acceleration, thrust. The exception is _position_setpoint together with _velocity_setpoint, where the
|
|
|
|
|
* _velocity_setpoint is used as feedforward.
|
2018-10-25 15:29:45 +02:00
|
|
|
* _acceleration_setpoint and _jerk_setpoint are currently not supported.
|
2018-04-18 11:21:07 +02:00
|
|
|
*/
|
2018-02-28 09:30:20 +01:00
|
|
|
matrix::Vector3f _position_setpoint;
|
|
|
|
|
matrix::Vector3f _velocity_setpoint;
|
|
|
|
|
matrix::Vector3f _acceleration_setpoint;
|
2018-10-25 15:29:45 +02:00
|
|
|
matrix::Vector3f _jerk_setpoint;
|
2019-12-07 15:13:27 +01:00
|
|
|
float _yaw_setpoint{};
|
|
|
|
|
float _yawspeed_setpoint{};
|
2018-10-10 15:59:09 +02:00
|
|
|
matrix::Vector3f _velocity_setpoint_feedback;
|
2020-01-27 17:17:43 +01:00
|
|
|
matrix::Vector3f _acceleration_setpoint_feedback;
|
2018-10-10 15:59:09 +02:00
|
|
|
|
2020-04-22 13:18:35 +02:00
|
|
|
ekf_reset_counters_s _reset_counters{}; ///< Counters for estimator local position resets
|
2019-09-23 13:42:06 +02:00
|
|
|
|
2018-04-26 11:21:26 +02:00
|
|
|
/**
|
|
|
|
|
* Vehicle constraints.
|
|
|
|
|
* The constraints can vary with tasks.
|
|
|
|
|
*/
|
2018-08-30 09:21:13 -04:00
|
|
|
vehicle_constraints_s _constraints{};
|
2018-04-26 11:21:26 +02:00
|
|
|
|
2018-11-01 17:43:49 +01:00
|
|
|
landing_gear_s _gear{};
|
|
|
|
|
|
2018-07-09 11:53:35 +02:00
|
|
|
/**
|
|
|
|
|
* Desired waypoints.
|
|
|
|
|
* Goals set by the FCU to be sent to the obstacle avoidance system.
|
|
|
|
|
*/
|
2018-08-30 09:21:13 -04:00
|
|
|
vehicle_trajectory_waypoint_s _desired_waypoint{};
|
2018-07-09 11:53:35 +02:00
|
|
|
|
2018-04-18 11:20:51 +02:00
|
|
|
DEFINE_PARAMETERS_CUSTOM_PARENT(ModuleParams,
|
2019-03-20 10:28:17 +01:00
|
|
|
(ParamFloat<px4::params::MPC_XY_VEL_MAX>) _param_mpc_xy_vel_max,
|
|
|
|
|
(ParamFloat<px4::params::MPC_Z_VEL_MAX_DN>) _param_mpc_z_vel_max_dn,
|
2020-04-19 13:01:57 +02:00
|
|
|
(ParamFloat<px4::params::MPC_Z_VEL_MAX_UP>) _param_mpc_z_vel_max_up
|
2018-04-26 11:22:18 +02:00
|
|
|
)
|
2017-08-14 19:23:24 +02:00
|
|
|
};
|