refactor: glide_computer renamed to glide_computer_lib

This commit is contained in:
Mateusz Pusz
2023-08-24 19:19:55 +02:00
parent af4615e466
commit 9f1a42bc7f
5 changed files with 4 additions and 4 deletions

View File

@@ -0,0 +1,27 @@
# 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.
cmake_minimum_required(VERSION 3.2)
add_library(glide_computer_lib STATIC glide_computer_lib.cpp include/glide_computer_lib.h)
target_link_libraries(glide_computer_lib PRIVATE mp-units::core-fmt PUBLIC mp-units::si mp-units::utility example_utils)
target_include_directories(glide_computer_lib PUBLIC include)

View File

@@ -0,0 +1,174 @@
// 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.
#include "glide_computer_lib.h"
#include <mp-units/format.h>
#include <iostream>
#include <numeric>
#include <string_view>
namespace glide_computer {
using namespace mp_units;
task::legs task::make_legs(const waypoints& wpts)
{
task::legs res;
res.reserve(wpts.size() - 1);
auto to_leg = [](const waypoint& w1, const waypoint& w2) { return task::leg(w1, w2); };
std::ranges::transform(wpts.cbegin(), prev(wpts.cend()), next(wpts.cbegin()), wpts.cend(), std::back_inserter(res),
to_leg);
return res;
}
std::vector<distance> task::make_leg_total_distances(const legs& legs)
{
std::vector<distance> res;
res.reserve(legs.size());
auto to_length = [](const leg& l) { return l.get_distance(); };
std::transform_inclusive_scan(legs.cbegin(), legs.cend(), std::back_inserter(res), std::plus(), to_length);
return res;
}
geographic::msl_altitude terrain_level_alt(const task& t, const flight_point& pos)
{
const task::leg& l = t.get_legs()[pos.leg_idx];
const height alt_diff = l.end().alt - l.begin().alt;
return l.begin().alt + alt_diff * ((pos.dist - t.get_leg_dist_offset(pos.leg_idx)) / l.get_distance());
}
// Returns `x` of the intersection of a glide line and a terrain line.
// y = -x / glide_ratio + pos.alt;
// y = (finish_alt - ground_alt) / dist_to_finish * x + ground_alt + min_agl_height;
distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s,
geographic::msl_altitude ground_alt)
{
const auto dist_to_finish = t.get_distance() - pos.dist;
return quantity_cast<isq::distance>(ground_alt + s.min_agl_height - pos.alt) /
((ground_alt - t.get_finish().alt) / dist_to_finish - 1 / glide_ratio(g.polar[0]));
}
} // namespace glide_computer
namespace {
using namespace glide_computer;
void print(std::string_view phase_name, timestamp start_ts, const glide_computer::flight_point& point,
const glide_computer::flight_point& new_point)
{
std::cout << MP_UNITS_STD_FMT::format(
"| {:<12} | {:>9%.1Q %q} (Total: {:>9%.1Q %q}) | {:>8%.1Q %q} (Total: {:>8%.1Q %q}) | {:>7%.0Q %q} ({:>6%.0Q %q}) "
"|\n",
phase_name, value_cast<si::minute>(new_point.ts - point.ts), value_cast<si::minute>(new_point.ts - start_ts),
new_point.dist - point.dist, new_point.dist, new_point.alt - point.alt, new_point.alt);
}
flight_point takeoff(timestamp start_ts, const task& t) { return {start_ts, t.get_start().alt}; }
flight_point tow(timestamp start_ts, const flight_point& pos, const aircraft_tow& at)
{
const duration d = (at.height_agl / at.performance);
const flight_point new_pos{pos.ts + d, pos.alt + at.height_agl, pos.leg_idx, pos.dist};
print("Tow", start_ts, pos, new_pos);
return new_pos;
}
flight_point circle(timestamp start_ts, const flight_point& pos, const glider& g, const weather& w, const task& t,
height& height_to_gain)
{
const height h_agl = agl(pos.alt, terrain_level_alt(t, pos));
const height circling_height = std::min(w.cloud_base - h_agl, height_to_gain);
const rate_of_climb circling_rate = w.thermal_strength + g.polar[0].climb;
const duration d = (circling_height / circling_rate);
const flight_point new_pos{pos.ts + d, pos.alt + circling_height, pos.leg_idx, pos.dist};
height_to_gain -= circling_height;
print("Circle", start_ts, pos, new_pos);
return new_pos;
}
flight_point glide(timestamp start_ts, const flight_point& pos, const glider& g, const task& t, const safety& s)
{
const auto ground_alt = terrain_level_alt(t, pos);
const auto dist = glide_distance(pos, g, t, s, ground_alt);
const auto new_distance = pos.dist + dist;
const auto alt = ground_alt + s.min_agl_height;
const auto l3d = length_3d(dist, pos.alt - alt);
const duration d = l3d / g.polar[0].v;
const flight_point new_pos{pos.ts + d, terrain_level_alt(t, pos) + s.min_agl_height, t.get_leg_index(new_distance),
new_distance};
print("Glide", start_ts, pos, new_pos);
return new_pos;
}
flight_point final_glide(timestamp start_ts, const flight_point& pos, const glider& g, const task& t)
{
const auto dist = t.get_distance() - pos.dist;
const auto l3d = length_3d(dist, pos.alt - t.get_finish().alt);
const duration d = l3d / g.polar[0].v;
const flight_point new_pos{pos.ts + d, t.get_finish().alt, t.get_legs().size() - 1, pos.dist + dist};
print("Final Glide", start_ts, pos, new_pos);
return new_pos;
}
} // namespace
namespace glide_computer {
void estimate(timestamp start_ts, const glider& g, const weather& w, const task& t, const safety& s,
const aircraft_tow& at)
{
std::cout << MP_UNITS_STD_FMT::format("| {:<12} | {:^28} | {:^26} | {:^21} |\n", "Flight phase", "Duration",
"Distance", "Height");
std::cout << MP_UNITS_STD_FMT::format("|{0:-^14}|{0:-^30}|{0:-^28}|{0:-^23}|\n", "");
// ready to takeoff
flight_point pos = takeoff(start_ts, t);
// estimate aircraft towing
pos = tow(start_ts, pos, at);
// estimate the msl_altitude needed to reach the finish line from this place
const geographic::msl_altitude final_glide_alt =
t.get_finish().alt + quantity_cast<isq::height>(t.get_distance() / glide_ratio(g.polar[0]));
// how much height we still need to gain in the thermalls to reach the destination?
height height_to_gain = final_glide_alt - pos.alt;
do {
// glide to the next thermall
pos = glide(start_ts, pos, g, t, s);
// circle in a thermall to gain height
pos = circle(start_ts, pos, g, w, t, height_to_gain);
} while (height_to_gain > height{});
// final glide
pos = final_glide(start_ts, pos, g, t);
}
} // namespace glide_computer

View File

@@ -0,0 +1,185 @@
// 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"
#include <mp-units/chrono.h>
#include <mp-units/math.h> // IWYU pragma: keep
#include <mp-units/quantity_point.h>
#include <mp-units/systems/isq/space_and_time.h>
#include <algorithm>
#include <array>
#include <initializer_list>
#include <iterator>
#include <ostream>
#include <ranges>
#include <string> // IWYU pragma: keep
#include <vector>
// 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
QUANTITY_SPEC(rate_of_climb_speed, mp_units::isq::speed, mp_units::isq::height / mp_units::isq::time);
// length
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]>;
// time
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],
mp_units::chrono_point_origin<std::chrono::system_clock>>;
// speed
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]>;
// definition of glide computer databases and utilities
struct glider {
struct polar_point {
velocity v;
rate_of_climb climb;
};
std::string name;
std::array<polar_point, 1> polar;
};
constexpr mp_units::QuantityOf<mp_units::dimensionless> auto glide_ratio(const glider::polar_point& polar)
{
return polar.v / -polar.climb;
}
struct weather {
height cloud_base;
rate_of_climb thermal_strength;
};
struct waypoint {
std::string name;
geographic::position<long double> pos;
geographic::msl_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:
leg(const waypoint& b, const waypoint& e) noexcept : begin_(&b), end_(&e) {}
constexpr const waypoint& begin() const { return *begin_; };
constexpr const waypoint& end() const { return *end_; }
constexpr distance get_distance() const { return length_; }
};
using legs = std::vector<leg>;
template<std::ranges::input_range R>
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))
{
}
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(); }
distance get_distance() const { return length_; }
distance get_leg_dist_offset(std::size_t leg_index) const
{
return leg_index == 0 ? distance{} : leg_total_distances_[leg_index - 1];
}
std::size_t get_leg_index(distance dist) const
{
return static_cast<std::size_t>(
std::ranges::distance(leg_total_distances_.cbegin(), std::ranges::lower_bound(leg_total_distances_, dist)));
}
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;
geographic::msl_altitude alt;
std::size_t leg_idx = 0;
distance dist{};
};
geographic::msl_altitude terrain_level_alt(const task& t, const flight_point& pos);
constexpr height agl(geographic::msl_altitude glider_alt, geographic::msl_altitude terrain_level)
{
return glider_alt - terrain_level;
}
inline mp_units::quantity<mp_units::isq::length[mp_units::si::kilo<mp_units::si::metre>]> length_3d(distance dist,
height h)
{
return hypot(dist, h);
}
distance glide_distance(const flight_point& pos, const glider& g, const task& t, const safety& s,
geographic::msl_altitude ground_alt);
void estimate(timestamp start_ts, const glider& g, const weather& w, const task& t, const safety& s,
const aircraft_tow& at);
} // namespace glide_computer