2017-11-08 10:57:56 +01:00
|
|
|
#include "FlightTask.hpp"
|
|
|
|
|
#include <mathlib/mathlib.h>
|
2018-07-12 10:33:36 +02:00
|
|
|
#include <lib/ecl/geo/geo.h>
|
2017-11-08 10:57:56 +01:00
|
|
|
|
2017-11-08 22:31:14 +01:00
|
|
|
constexpr uint64_t FlightTask::_timeout;
|
2018-04-26 11:21:26 +02:00
|
|
|
// First index of empty_setpoint corresponds to time-stamp and requires a finite number.
|
2019-10-22 16:41:52 +02:00
|
|
|
const vehicle_local_position_setpoint_s FlightTask::empty_setpoint = {0, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, {NAN, NAN, NAN}, {NAN, NAN, NAN}, {NAN, NAN, NAN}, {}};
|
2018-07-09 11:53:35 +02:00
|
|
|
|
2019-05-14 07:57:13 +01:00
|
|
|
const vehicle_constraints_s FlightTask::empty_constraints = {0, NAN, NAN, NAN, NAN, NAN, NAN, NAN, false, {}};
|
2018-11-13 16:53:36 +01:00
|
|
|
const landing_gear_s FlightTask::empty_landing_gear_default_keep = {0, landing_gear_s::GEAR_KEEP, {}};
|
2018-07-09 11:53:35 +02:00
|
|
|
|
2019-07-15 17:57:40 +02:00
|
|
|
bool FlightTask::activate(vehicle_local_position_setpoint_s last_setpoint)
|
2017-11-27 21:31:47 +01:00
|
|
|
{
|
2018-02-28 09:30:20 +01:00
|
|
|
_resetSetpoints();
|
2018-04-26 11:22:18 +02:00
|
|
|
_setDefaultConstraints();
|
2019-09-23 13:42:06 +02:00
|
|
|
_initEkfResetCounters();
|
2017-11-27 21:31:47 +01:00
|
|
|
_time_stamp_activate = hrt_absolute_time();
|
2018-11-13 16:53:36 +01:00
|
|
|
_gear = empty_landing_gear_default_keep;
|
2017-11-28 22:00:46 +01:00
|
|
|
return true;
|
2017-11-27 21:31:47 +01:00
|
|
|
}
|
|
|
|
|
|
2018-10-23 15:24:57 +02:00
|
|
|
void FlightTask::reActivate()
|
|
|
|
|
{
|
2019-07-02 16:11:44 +02:00
|
|
|
activate(getPositionSetpoint());
|
2018-10-23 15:24:57 +02:00
|
|
|
}
|
|
|
|
|
|
2017-11-28 22:00:46 +01:00
|
|
|
bool FlightTask::updateInitialize()
|
2017-11-08 10:57:56 +01:00
|
|
|
{
|
2017-11-20 14:27:56 +01:00
|
|
|
_time_stamp_current = hrt_absolute_time();
|
|
|
|
|
_time = (_time_stamp_current - _time_stamp_activate) / 1e6f;
|
|
|
|
|
_deltatime = math::min((_time_stamp_current - _time_stamp_last), _timeout) / 1e6f;
|
|
|
|
|
_time_stamp_last = _time_stamp_current;
|
2019-09-30 18:16:36 -04:00
|
|
|
|
|
|
|
|
_sub_vehicle_local_position.update();
|
|
|
|
|
_sub_attitude.update();
|
2019-11-21 18:20:13 +01:00
|
|
|
_sub_home_position.update();
|
2019-09-30 18:16:36 -04:00
|
|
|
|
2018-10-10 15:07:15 +02:00
|
|
|
_evaluateVehicleLocalPosition();
|
2019-11-21 18:20:13 +01:00
|
|
|
_evaluateDistanceToGround();
|
2019-09-23 13:42:06 +02:00
|
|
|
_checkEkfResetCounters();
|
2018-10-10 15:07:15 +02:00
|
|
|
return true;
|
2017-11-08 10:57:56 +01:00
|
|
|
}
|
|
|
|
|
|
2019-09-23 13:42:06 +02:00
|
|
|
void FlightTask::_initEkfResetCounters()
|
|
|
|
|
{
|
2019-09-30 18:16:36 -04:00
|
|
|
_reset_counters.xy = _sub_vehicle_local_position.get().xy_reset_counter;
|
|
|
|
|
_reset_counters.vxy = _sub_vehicle_local_position.get().vxy_reset_counter;
|
|
|
|
|
_reset_counters.z = _sub_vehicle_local_position.get().z_reset_counter;
|
|
|
|
|
_reset_counters.vz = _sub_vehicle_local_position.get().vz_reset_counter;
|
|
|
|
|
_reset_counters.quat = _sub_attitude.get().quat_reset_counter;
|
2019-09-23 13:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlightTask::_checkEkfResetCounters()
|
|
|
|
|
{
|
|
|
|
|
// Check if a reset event has happened
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().xy_reset_counter != _reset_counters.xy) {
|
2019-09-23 13:42:06 +02:00
|
|
|
_ekfResetHandlerPositionXY();
|
2019-09-30 18:16:36 -04:00
|
|
|
_reset_counters.xy = _sub_vehicle_local_position.get().xy_reset_counter;
|
2019-09-23 13:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().vxy_reset_counter != _reset_counters.vxy) {
|
2019-09-23 13:42:06 +02:00
|
|
|
_ekfResetHandlerVelocityXY();
|
2019-09-30 18:16:36 -04:00
|
|
|
_reset_counters.vxy = _sub_vehicle_local_position.get().vxy_reset_counter;
|
2019-09-23 13:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().z_reset_counter != _reset_counters.z) {
|
2019-09-23 13:42:06 +02:00
|
|
|
_ekfResetHandlerPositionZ();
|
2019-09-30 18:16:36 -04:00
|
|
|
_reset_counters.z = _sub_vehicle_local_position.get().z_reset_counter;
|
2019-09-23 13:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().vz_reset_counter != _reset_counters.vz) {
|
2019-09-23 13:42:06 +02:00
|
|
|
_ekfResetHandlerVelocityZ();
|
2019-09-30 18:16:36 -04:00
|
|
|
_reset_counters.vz = _sub_vehicle_local_position.get().vz_reset_counter;
|
2019-09-23 13:42:06 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_attitude.get().quat_reset_counter != _reset_counters.quat) {
|
|
|
|
|
float delta_psi = matrix::Eulerf(matrix::Quatf(_sub_attitude.get().delta_q_reset)).psi();
|
2019-09-23 13:42:06 +02:00
|
|
|
_ekfResetHandlerHeading(delta_psi);
|
2019-09-30 18:16:36 -04:00
|
|
|
_reset_counters.quat = _sub_attitude.get().quat_reset_counter;
|
2019-09-23 13:42:06 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 09:30:20 +01:00
|
|
|
const vehicle_local_position_setpoint_s FlightTask::getPositionSetpoint()
|
|
|
|
|
{
|
|
|
|
|
/* fill position setpoint message */
|
2020-01-05 21:03:54 -05:00
|
|
|
vehicle_local_position_setpoint_s vehicle_local_position_setpoint{};
|
2018-02-28 09:30:20 +01:00
|
|
|
vehicle_local_position_setpoint.timestamp = hrt_absolute_time();
|
2018-08-01 22:09:56 -04:00
|
|
|
|
|
|
|
|
vehicle_local_position_setpoint.x = _position_setpoint(0);
|
|
|
|
|
vehicle_local_position_setpoint.y = _position_setpoint(1);
|
|
|
|
|
vehicle_local_position_setpoint.z = _position_setpoint(2);
|
|
|
|
|
|
|
|
|
|
vehicle_local_position_setpoint.vx = _velocity_setpoint(0);
|
|
|
|
|
vehicle_local_position_setpoint.vy = _velocity_setpoint(1);
|
|
|
|
|
vehicle_local_position_setpoint.vz = _velocity_setpoint(2);
|
|
|
|
|
|
2019-10-22 16:41:52 +02:00
|
|
|
_acceleration_setpoint.copyTo(vehicle_local_position_setpoint.acceleration);
|
|
|
|
|
_jerk_setpoint.copyTo(vehicle_local_position_setpoint.jerk);
|
2018-02-28 09:30:20 +01:00
|
|
|
vehicle_local_position_setpoint.yaw = _yaw_setpoint;
|
|
|
|
|
vehicle_local_position_setpoint.yawspeed = _yawspeed_setpoint;
|
2018-08-01 22:09:56 -04:00
|
|
|
|
2020-01-27 17:17:43 +01:00
|
|
|
// deprecated, only kept for output logging
|
|
|
|
|
matrix::Vector3f(NAN, NAN, NAN).copyTo(vehicle_local_position_setpoint.thrust);
|
|
|
|
|
|
2018-02-28 09:30:20 +01:00
|
|
|
return vehicle_local_position_setpoint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlightTask::_resetSetpoints()
|
|
|
|
|
{
|
2019-12-07 15:13:27 +01:00
|
|
|
_position_setpoint.setNaN();
|
|
|
|
|
_velocity_setpoint.setNaN();
|
|
|
|
|
_acceleration_setpoint.setNaN();
|
|
|
|
|
_jerk_setpoint.setNaN();
|
2018-02-28 09:30:20 +01:00
|
|
|
_yaw_setpoint = _yawspeed_setpoint = NAN;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 15:07:15 +02:00
|
|
|
void FlightTask::_evaluateVehicleLocalPosition()
|
2017-11-08 10:57:56 +01:00
|
|
|
{
|
2018-10-10 15:07:15 +02:00
|
|
|
_position.setAll(NAN);
|
|
|
|
|
_velocity.setAll(NAN);
|
|
|
|
|
_yaw = NAN;
|
|
|
|
|
_dist_to_bottom = NAN;
|
|
|
|
|
|
2019-09-30 18:16:36 -04:00
|
|
|
if ((_time_stamp_current - _sub_attitude.get().timestamp) < _timeout) {
|
2018-11-06 12:03:13 -05:00
|
|
|
// yaw
|
2019-09-30 18:16:36 -04:00
|
|
|
_yaw = matrix::Eulerf(matrix::Quatf(_sub_attitude.get().q)).psi();
|
2018-11-06 12:03:13 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-10 15:07:15 +02:00
|
|
|
// Only use vehicle-local-position topic fields if the topic is received within a certain timestamp
|
2019-09-30 18:16:36 -04:00
|
|
|
if ((_time_stamp_current - _sub_vehicle_local_position.get().timestamp) < _timeout) {
|
2018-05-24 08:49:18 +02:00
|
|
|
|
|
|
|
|
// position
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().xy_valid) {
|
|
|
|
|
_position(0) = _sub_vehicle_local_position.get().x;
|
|
|
|
|
_position(1) = _sub_vehicle_local_position.get().y;
|
2018-05-24 08:49:18 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().z_valid) {
|
|
|
|
|
_position(2) = _sub_vehicle_local_position.get().z;
|
2018-05-24 08:49:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// velocity
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().v_xy_valid) {
|
|
|
|
|
_velocity(0) = _sub_vehicle_local_position.get().vx;
|
|
|
|
|
_velocity(1) = _sub_vehicle_local_position.get().vy;
|
2018-05-24 08:49:18 +02:00
|
|
|
}
|
|
|
|
|
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().v_z_valid) {
|
|
|
|
|
_velocity(2) = _sub_vehicle_local_position.get().vz;
|
2018-05-24 08:49:18 +02:00
|
|
|
}
|
|
|
|
|
|
2018-06-21 20:05:10 +02:00
|
|
|
// distance to bottom
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().dist_bottom_valid
|
|
|
|
|
&& PX4_ISFINITE(_sub_vehicle_local_position.get().dist_bottom)) {
|
|
|
|
|
_dist_to_bottom = _sub_vehicle_local_position.get().dist_bottom;
|
2018-04-18 11:17:53 +02:00
|
|
|
}
|
|
|
|
|
|
2018-07-12 10:33:36 +02:00
|
|
|
// global frame reference coordinates to enable conversions
|
2019-09-30 18:16:36 -04:00
|
|
|
if (_sub_vehicle_local_position.get().xy_global && _sub_vehicle_local_position.get().z_global) {
|
|
|
|
|
globallocalconverter_init(_sub_vehicle_local_position.get().ref_lat, _sub_vehicle_local_position.get().ref_lon,
|
|
|
|
|
_sub_vehicle_local_position.get().ref_alt, _sub_vehicle_local_position.get().ref_timestamp);
|
2018-07-12 10:33:36 +02:00
|
|
|
}
|
2017-11-08 10:57:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-04-18 11:20:51 +02:00
|
|
|
|
2019-11-21 18:20:13 +01:00
|
|
|
void FlightTask::_evaluateDistanceToGround()
|
|
|
|
|
{
|
2019-12-03 11:36:43 +01:00
|
|
|
// Altitude above ground is local z-position or altitude above home or distance sensor altitude depending on what's available
|
2019-11-22 09:44:51 +01:00
|
|
|
_dist_to_ground = -_position(2);
|
2019-11-21 18:20:13 +01:00
|
|
|
|
|
|
|
|
if (PX4_ISFINITE(_dist_to_bottom)) {
|
|
|
|
|
_dist_to_ground = _dist_to_bottom;
|
|
|
|
|
|
|
|
|
|
} else if (_sub_home_position.get().valid_alt) {
|
|
|
|
|
_dist_to_ground = -(_position(2) - _sub_home_position.get().z);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-26 11:22:18 +02:00
|
|
|
void FlightTask::_setDefaultConstraints()
|
2018-04-18 11:20:51 +02:00
|
|
|
{
|
2019-03-20 10:28:17 +01:00
|
|
|
_constraints.speed_xy = _param_mpc_xy_vel_max.get();
|
|
|
|
|
_constraints.speed_up = _param_mpc_z_vel_max_up.get();
|
|
|
|
|
_constraints.speed_down = _param_mpc_z_vel_max_dn.get();
|
2020-04-19 13:01:57 +02:00
|
|
|
_constraints.tilt = NAN;
|
2018-05-24 17:59:48 +02:00
|
|
|
_constraints.min_distance_to_ground = NAN;
|
2018-06-25 10:00:08 +02:00
|
|
|
_constraints.max_distance_to_ground = NAN;
|
2019-05-13 21:07:05 +02:00
|
|
|
_constraints.want_takeoff = false;
|
2018-04-18 11:20:51 +02:00
|
|
|
}
|
2019-05-14 07:57:13 +01:00
|
|
|
|
|
|
|
|
bool FlightTask::_checkTakeoff()
|
|
|
|
|
{
|
|
|
|
|
// position setpoint above the minimum altitude
|
|
|
|
|
bool position_triggered_takeoff = false;
|
|
|
|
|
|
|
|
|
|
if (PX4_ISFINITE(_position_setpoint(2))) {
|
2019-05-22 15:38:00 +01:00
|
|
|
// minimal altitude either 20cm or what is necessary for correct estimation e.g. optical flow
|
2019-05-14 07:57:13 +01:00
|
|
|
float min_altitude = 0.2f;
|
2019-09-30 18:16:36 -04:00
|
|
|
const float min_distance_to_ground = _sub_vehicle_local_position.get().hagl_min;
|
2019-05-14 07:57:13 +01:00
|
|
|
|
|
|
|
|
if (PX4_ISFINITE(min_distance_to_ground)) {
|
|
|
|
|
min_altitude = min_distance_to_ground + 0.05f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
position_triggered_takeoff = _position_setpoint(2) < (_position(2) - min_altitude);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// upwards velocity setpoint
|
|
|
|
|
bool velocity_triggered_takeoff = false;
|
|
|
|
|
|
|
|
|
|
if (PX4_ISFINITE(_velocity_setpoint(2))) {
|
|
|
|
|
velocity_triggered_takeoff = _velocity_setpoint(2) < -0.3f;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return position_triggered_takeoff || velocity_triggered_takeoff;
|
|
|
|
|
}
|