2021-02-26 14:10:36 +01:00
|
|
|
// The MIT License (MIT)
|
|
|
|
|
//
|
|
|
|
|
// Copyright (c) 2018 Mateusz Pusz
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
// copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
// SOFTWARE.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "geographic.h"
|
2022-12-29 20:18:48 +01:00
|
|
|
#include <mp_units/chrono.h>
|
|
|
|
|
#include <mp_units/format.h>
|
|
|
|
|
#include <mp_units/math.h> // IWYU pragma: keep
|
|
|
|
|
#include <mp_units/quantity_point.h>
|
|
|
|
|
#include <mp_units/systems/isq/space_and_time.h>
|
2021-03-30 13:21:05 +02:00
|
|
|
#include <algorithm>
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <initializer_list>
|
|
|
|
|
#include <iterator>
|
|
|
|
|
#include <ostream>
|
|
|
|
|
#include <ranges>
|
2022-04-02 21:36:42 +02:00
|
|
|
#include <string> // IWYU pragma: keep
|
2021-03-30 13:21:05 +02:00
|
|
|
#include <vector>
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
// An example of a really simplified tactical glide computer
|
|
|
|
|
// Simplifications:
|
|
|
|
|
// - glider 100% clean and with full factory performance (brand new painting)
|
|
|
|
|
// - no influence of the ballast (pilot weight, water, etc) to glider performance
|
|
|
|
|
// - only one point on a glider polar curve
|
|
|
|
|
// - no influence of bank angle (during circling) on a glider performance
|
|
|
|
|
// - no wind
|
|
|
|
|
// - constant thermals strength
|
|
|
|
|
// - thermals exactly where and when we need them ;-)
|
|
|
|
|
// - no airspaces
|
|
|
|
|
// - ground level changes linearly between waypoints
|
|
|
|
|
// - no ground obstacles (i.e. mountains) to pass
|
|
|
|
|
// - flight path exactly on a shortest possible line to destination
|
|
|
|
|
|
|
|
|
|
namespace glide_computer {
|
|
|
|
|
|
|
|
|
|
// https://en.wikipedia.org/wiki/Flight_planning#Units_of_measurement
|
2022-12-29 20:18:48 +01:00
|
|
|
inline constexpr struct mean_sea_level : mp_units::absolute_point_origin<mp_units::isq::height> {
|
2022-12-23 19:24:56 +01:00
|
|
|
} mean_sea_level;
|
2023-02-14 12:58:54 +01:00
|
|
|
QUANTITY_SPEC(rate_of_climb_speed, mp_units::isq::speed, mp_units::isq::height / mp_units::isq::time);
|
2022-12-23 19:24:56 +01:00
|
|
|
|
2021-02-26 14:10:36 +01:00
|
|
|
// length
|
2022-12-29 20:18:48 +01:00
|
|
|
using distance = mp_units::quantity<mp_units::isq::distance[mp_units::si::kilo<mp_units::si::metre>]>;
|
|
|
|
|
using height = mp_units::quantity<mp_units::isq::height[mp_units::si::metre]>;
|
|
|
|
|
using altitude = mp_units::quantity_point<mp_units::isq::altitude[mp_units::si::metre], mean_sea_level>;
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
// time
|
2022-12-29 20:18:48 +01:00
|
|
|
using duration = mp_units::quantity<mp_units::isq::duration[mp_units::si::second]>;
|
|
|
|
|
using timestamp = mp_units::quantity_point<mp_units::isq::time[mp_units::si::second],
|
2023-04-03 19:23:08 +02:00
|
|
|
mp_units::chrono_point_origin<std::chrono::system_clock>>;
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
// speed
|
2022-12-29 20:18:48 +01:00
|
|
|
using velocity = mp_units::quantity<mp_units::isq::speed[mp_units::si::kilo<mp_units::si::metre> / mp_units::si::hour]>;
|
|
|
|
|
using rate_of_climb = mp_units::quantity<rate_of_climb_speed[mp_units::si::metre / mp_units::si::second]>;
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
// text output
|
|
|
|
|
template<class CharT, class Traits>
|
|
|
|
|
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const altitude& a)
|
|
|
|
|
{
|
2022-12-23 19:24:56 +01:00
|
|
|
return os << a.absolute() << " AMSL";
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace glide_computer
|
|
|
|
|
|
|
|
|
|
template<>
|
2022-12-23 19:24:56 +01:00
|
|
|
struct STD_FMT::formatter<glide_computer::altitude> :
|
2022-12-29 20:18:48 +01:00
|
|
|
formatter<mp_units::quantity<mp_units::isq::altitude[mp_units::si::metre]>> {
|
2021-02-26 14:10:36 +01:00
|
|
|
template<typename FormatContext>
|
2022-12-23 19:24:56 +01:00
|
|
|
auto format(const glide_computer::altitude& a, FormatContext& ctx)
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
2022-12-29 20:18:48 +01:00
|
|
|
formatter<mp_units::quantity<mp_units::isq::altitude[mp_units::si::metre]>>::format(a.absolute(), ctx);
|
2021-09-20 15:01:58 +02:00
|
|
|
return STD_FMT::format_to(ctx.out(), " AMSL");
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// definition of glide computer databases and utilities
|
|
|
|
|
namespace glide_computer {
|
|
|
|
|
|
|
|
|
|
struct glider {
|
|
|
|
|
struct polar_point {
|
|
|
|
|
velocity v;
|
|
|
|
|
rate_of_climb climb;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
|
std::array<polar_point, 1> polar;
|
|
|
|
|
};
|
|
|
|
|
|
2023-02-08 21:47:48 -08:00
|
|
|
constexpr mp_units::QuantityOf<mp_units::dimensionless> auto glide_ratio(const glider::polar_point& polar)
|
2022-12-23 19:24:56 +01:00
|
|
|
{
|
|
|
|
|
return polar.v / -polar.climb;
|
|
|
|
|
}
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
struct weather {
|
|
|
|
|
height cloud_base;
|
|
|
|
|
rate_of_climb thermal_strength;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct waypoint {
|
|
|
|
|
std::string name;
|
2022-04-21 21:25:54 +02:00
|
|
|
geographic::position<long double> pos;
|
2021-02-26 14:10:36 +01:00
|
|
|
altitude alt;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class task {
|
|
|
|
|
public:
|
|
|
|
|
using waypoints = std::vector<waypoint>;
|
|
|
|
|
|
|
|
|
|
class leg {
|
|
|
|
|
const waypoint* begin_;
|
|
|
|
|
const waypoint* end_;
|
|
|
|
|
distance length_ = geographic::spherical_distance(begin().pos, end().pos);
|
|
|
|
|
public:
|
2022-04-02 21:36:42 +02:00
|
|
|
leg(const waypoint& b, const waypoint& e) noexcept : begin_(&b), end_(&e) {}
|
2021-02-26 14:10:36 +01:00
|
|
|
constexpr const waypoint& begin() const { return *begin_; };
|
|
|
|
|
constexpr const waypoint& end() const { return *end_; }
|
2023-04-11 09:12:48 +02:00
|
|
|
constexpr distance get_distance() const { return length_; }
|
2021-02-26 14:10:36 +01:00
|
|
|
};
|
|
|
|
|
using legs = std::vector<leg>;
|
|
|
|
|
|
|
|
|
|
template<std::ranges::input_range R>
|
2022-08-03 12:29:49 +02:00
|
|
|
requires std::same_as<std::ranges::range_value_t<R>, waypoint>
|
|
|
|
|
explicit task(const R& r) : waypoints_(std::ranges::begin(r), std::ranges::end(r))
|
2022-04-02 21:36:42 +02:00
|
|
|
{
|
|
|
|
|
}
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
task(std::initializer_list<waypoint> wpts) : waypoints_(wpts) {}
|
|
|
|
|
|
|
|
|
|
const waypoints& get_waypoints() const { return waypoints_; }
|
|
|
|
|
const legs& get_legs() const { return legs_; }
|
|
|
|
|
|
|
|
|
|
const waypoint& get_start() const { return waypoints_.front(); }
|
|
|
|
|
const waypoint& get_finish() const { return waypoints_.back(); }
|
|
|
|
|
|
2023-04-11 09:12:48 +02:00
|
|
|
distance get_distance() const { return length_; }
|
2021-02-26 14:10:36 +01:00
|
|
|
|
2022-04-02 21:36:42 +02:00
|
|
|
distance get_leg_dist_offset(std::size_t leg_index) const
|
|
|
|
|
{
|
|
|
|
|
return leg_index == 0 ? distance{} : leg_total_distances_[leg_index - 1];
|
|
|
|
|
}
|
2021-03-30 13:21:05 +02:00
|
|
|
std::size_t get_leg_index(distance dist) const
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
2022-04-02 21:36:42 +02:00
|
|
|
return static_cast<std::size_t>(
|
|
|
|
|
std::ranges::distance(leg_total_distances_.cbegin(), std::ranges::lower_bound(leg_total_distances_, dist)));
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
waypoints waypoints_;
|
|
|
|
|
legs legs_ = make_legs(waypoints_);
|
|
|
|
|
std::vector<distance> leg_total_distances_ = make_leg_total_distances(legs_);
|
|
|
|
|
distance length_ = leg_total_distances_.back();
|
|
|
|
|
|
|
|
|
|
static legs make_legs(const task::waypoints& wpts);
|
|
|
|
|
static std::vector<distance> make_leg_total_distances(const legs& legs);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct safety {
|
|
|
|
|
height min_agl_height;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct aircraft_tow {
|
|
|
|
|
height height_agl;
|
|
|
|
|
rate_of_climb performance;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct flight_point {
|
|
|
|
|
timestamp ts;
|
|
|
|
|
altitude alt;
|
2021-03-30 13:21:05 +02:00
|
|
|
std::size_t leg_idx = 0;
|
2021-03-12 23:06:11 +01:00
|
|
|
distance dist{};
|
2021-02-26 14:10:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
altitude terrain_level_alt(const task& t, const flight_point& pos);
|
|
|
|
|
|
|
|
|
|
constexpr height agl(altitude glider_alt, altitude terrain_level) { return glider_alt - terrain_level; }
|
|
|
|
|
|
2022-12-29 20:18:48 +01:00
|
|
|
inline mp_units::quantity<mp_units::isq::length[mp_units::si::kilo<mp_units::si::metre>]> length_3d(distance dist,
|
|
|
|
|
height h)
|
2021-02-26 14:10:36 +01:00
|
|
|
{
|
2023-02-14 12:58:54 +01:00
|
|
|
return hypot(dist, h);
|
2021-02-26 14:10:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s, altitude ground_alt);
|
|
|
|
|
|
2022-04-02 21:36:42 +02:00
|
|
|
void estimate(timestamp start_ts, const glider& g, const weather& w, const task& t, const safety& s,
|
|
|
|
|
const aircraft_tow& at);
|
2021-02-26 14:10:36 +01:00
|
|
|
|
|
|
|
|
} // namespace glide_computer
|