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.
|
2018-10-25 15:29:45 +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
|
|
|
|
2018-11-01 17:43:49 +01:00
|
|
|
const vehicle_constraints_s FlightTask::empty_constraints = {0, NAN, NAN, NAN, NAN, NAN, NAN, NAN, {}};
|
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
|
|
|
|
2017-11-20 21:55:28 +01:00
|
|
|
bool FlightTask::initializeSubscriptions(SubscriptionArray &subscription_array)
|
|
|
|
|
{
|
|
|
|
|
if (!subscription_array.get(ORB_ID(vehicle_local_position), _sub_vehicle_local_position)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-28 16:08:58 +02:00
|
|
|
if (!subscription_array.get(ORB_ID(vehicle_attitude), _sub_attitude)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-20 21:55:28 +01:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-28 22:00:46 +01:00
|
|
|
bool FlightTask::activate()
|
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();
|
2017-11-27 21:31:47 +01:00
|
|
|
_time_stamp_activate = hrt_absolute_time();
|
2018-07-28 16:08:58 +02:00
|
|
|
_heading_reset_counter = _sub_attitude->get().quat_reset_counter;
|
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()
|
|
|
|
|
{
|
|
|
|
|
activate();
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
2018-10-10 15:07:15 +02:00
|
|
|
_evaluateVehicleLocalPosition();
|
|
|
|
|
return true;
|
2017-11-08 10:57:56 +01:00
|
|
|
}
|
|
|
|
|
|
2018-02-28 09:30:20 +01:00
|
|
|
const vehicle_local_position_setpoint_s FlightTask::getPositionSetpoint()
|
|
|
|
|
{
|
|
|
|
|
/* fill position setpoint message */
|
|
|
|
|
vehicle_local_position_setpoint_s vehicle_local_position_setpoint;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
vehicle_local_position_setpoint.acc_x = _acceleration_setpoint(0);
|
|
|
|
|
vehicle_local_position_setpoint.acc_y = _acceleration_setpoint(1);
|
|
|
|
|
vehicle_local_position_setpoint.acc_z = _acceleration_setpoint(2);
|
|
|
|
|
|
2018-10-25 15:29:45 +02:00
|
|
|
vehicle_local_position_setpoint.jerk_x = _jerk_setpoint(0);
|
|
|
|
|
vehicle_local_position_setpoint.jerk_y = _jerk_setpoint(1);
|
|
|
|
|
vehicle_local_position_setpoint.jerk_z = _jerk_setpoint(2);
|
|
|
|
|
|
2018-02-28 09:30:20 +01:00
|
|
|
_thrust_setpoint.copyTo(vehicle_local_position_setpoint.thrust);
|
|
|
|
|
vehicle_local_position_setpoint.yaw = _yaw_setpoint;
|
|
|
|
|
vehicle_local_position_setpoint.yawspeed = _yawspeed_setpoint;
|
2018-08-01 22:09:56 -04:00
|
|
|
|
2018-02-28 09:30:20 +01:00
|
|
|
return vehicle_local_position_setpoint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FlightTask::_resetSetpoints()
|
|
|
|
|
{
|
2018-09-24 08:17:32 +02:00
|
|
|
_position_setpoint.setAll(NAN);
|
|
|
|
|
_velocity_setpoint.setAll(NAN);
|
|
|
|
|
_acceleration_setpoint.setAll(NAN);
|
2018-10-25 15:29:45 +02:00
|
|
|
_jerk_setpoint.setAll(NAN);
|
2018-09-24 08:17:32 +02:00
|
|
|
_thrust_setpoint.setAll(NAN);
|
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;
|
|
|
|
|
|
2018-11-06 12:03:13 -05:00
|
|
|
if ((_time_stamp_current - _sub_attitude->get().timestamp) < _timeout) {
|
|
|
|
|
// yaw
|
|
|
|
|
_yaw = matrix::Eulerf(matrix::Quatf(_sub_attitude->get().q)).psi();
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-10 15:07:15 +02:00
|
|
|
// Only use vehicle-local-position topic fields if the topic is received within a certain timestamp
|
2017-11-20 21:55:28 +01:00
|
|
|
if ((_time_stamp_current - _sub_vehicle_local_position->get().timestamp) < _timeout) {
|
2018-05-24 08:49:18 +02:00
|
|
|
|
|
|
|
|
// position
|
|
|
|
|
if (_sub_vehicle_local_position->get().xy_valid) {
|
|
|
|
|
_position(0) = _sub_vehicle_local_position->get().x;
|
|
|
|
|
_position(1) = _sub_vehicle_local_position->get().y;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_sub_vehicle_local_position->get().z_valid) {
|
|
|
|
|
_position(2) = _sub_vehicle_local_position->get().z;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// velocity
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_sub_vehicle_local_position->get().v_z_valid) {
|
|
|
|
|
_velocity(2) = _sub_vehicle_local_position->get().vz;
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-21 20:05:10 +02:00
|
|
|
// distance to bottom
|
2018-06-25 07:48:19 +02:00
|
|
|
if (_sub_vehicle_local_position->get().dist_bottom_valid
|
|
|
|
|
&& PX4_ISFINITE(_sub_vehicle_local_position->get().dist_bottom)) {
|
2018-04-18 11:17:53 +02:00
|
|
|
_dist_to_bottom = _sub_vehicle_local_position->get().dist_bottom;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-12 10:33:36 +02:00
|
|
|
// global frame reference coordinates to enable conversions
|
|
|
|
|
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);
|
|
|
|
|
}
|
2017-11-08 10:57:56 +01:00
|
|
|
}
|
|
|
|
|
}
|
2018-04-18 11:20:51 +02:00
|
|
|
|
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();
|
|
|
|
|
_constraints.tilt = math::radians(_param_mpc_tiltmax_air.get());
|
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;
|
2018-04-18 11:20:51 +02:00
|
|
|
}
|