| 1 | /* |
| 2 | * SPDX-FileName: FlightHistory.hxx |
| 3 | * SPDX-FileCopyrightText: Copyright (C) 2012 James Turner - zakalawe (at) mac com |
| 4 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #pragma once |
| 8 | |
| 9 | #include <chrono> |
| 10 | #include <vector> |
| 11 | #include <deque> |
| 12 | #include <array> |
| 13 | |
| 14 | #include <simgear/math/SGMath.hxx> |
| 15 | #include <simgear/props/props.hxx> |
| 16 | #include <simgear/structure/subsystem_mgr.hxx> |
| 17 | |
| 18 | typedef std::vector<SGGeod> SGGeodVec; |
| 19 | |
| 20 | class PagedPathForHistory : public SGReferenced |
| 21 | { |
| 22 | public: |
| 23 | PagedPathForHistory() : last_seen(0) {} |
| 24 | virtual ~PagedPathForHistory() {} |
| 25 | SGGeodVec path; |
| 26 | time_t last_seen; |
| 27 | }; |
| 28 | |
| 29 | typedef SGSharedPtr<PagedPathForHistory> PagedPathForHistory_ptr; |
| 30 | |
| 31 | const unsigned int SAMPLE_BUCKET_WIDTH = 1024; |
| 32 | |
| 33 | /** |
| 34 | * record the history of the aircraft's movements, making it available |
| 35 | * as a contiguous block. This can be used to show the historical flight-path |
| 36 | * over a long period of time (unlike the replay system), but only a small, |
| 37 | * fixed set of properties are recorded. (Positioned and orientation, but |
| 38 | * not velocity, acceleration, control inputs, or so on) |
| 39 | */ |
| 40 | class FGFlightHistory : public SGSubsystem |
| 41 | { |
| 42 | public: |
| 43 | FGFlightHistory(); |
| 44 | virtual ~FGFlightHistory(); |
| 45 | |
| 46 | // Subsystem API. |
| 47 | void init() override; |
| 48 | void reinit() override; |
| 49 | void shutdown() override; |
| 50 | void update(double dt) override; |
| 51 | |
| 52 | // Subsystem identification. |
| 53 | static const char* staticSubsystemClassId() { return "history"; } |
| 54 | |
| 55 | PagedPathForHistory_ptr pagedPathForHistory(size_t max_entries, size_t newerThan = 0) const; |
| 56 | /** |
| 57 | * retrieve the path, collapsing segments shorter than |
| 58 | * the specified minimum length |
| 59 | */ |
| 60 | SGGeodVec pathForHistory(double minEdgeLengthM = 50.0) const; |
| 61 | |
| 62 | /** |
| 63 | * clear the history |
| 64 | */ |
| 65 | void clear(); |
| 66 | |
| 67 | /** |
| 68 | * clear all samples older than a cutoff time |
| 69 | */ |
| 70 | void clearOlderThan(std::chrono::seconds keepMostRecent); |
| 71 | |
| 72 | private: |
| 73 | bool clearHistoryCommand(const SGPropertyNode* args, SGPropertyNode*); |
| 74 | |
| 75 | /** |
| 76 | * @class A single data sample in the history system. |
| 77 | */ |
| 78 | class Sample |
| 79 | { |
| 80 | public: |
| 81 | SGGeod position; |
| 82 | /// heading, pitch and roll can be recorded at lower precision |
| 83 | /// than a double - actually 16 bits might be sufficient |
| 84 | float heading, pitch, roll; |
| 85 | size_t simTimeMSec; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | /** |
| 90 | * Bucket is a fixed-size container of samples. This is a crude slab |
| 91 | * allocation of samples, in chunks defined by the width constant above. |
| 92 | * Keep in mind that even with a 1Hz sample frequency, we use less than |
| 93 | * 200 KB per hour - avoiding continuous malloc traffic, or expensive |
| 94 | * std::vector reallocations, is the key factor here. |
| 95 | */ |
| 96 | class SampleBucket |
| 97 | { |
| 98 | public: |
| 99 | const Sample& lastSample() const; |
| 100 | |
| 101 | std::array<Sample, SAMPLE_BUCKET_WIDTH> samples; |
| 102 | size_t validSamples = 0; // mustbe between 0 and SAMPLE_BUCKET_WIDTH |
| 103 | |
| 104 | size_t bucketMinAge() const; |
| 105 | |
| 106 | bool isComplete() const |
| 107 | { |
| 108 | return validSamples == SAMPLE_BUCKET_WIDTH; |
| 109 | } |
| 110 | |
| 111 | bool isEmpty() const |
| 112 | { |
| 113 | return validSamples == 0; |
| 114 | } |
| 115 | }; |
| 116 | |
| 117 | SampleBucket& currentBucket(); |
| 118 | |
| 119 | double m_lastCaptureTime{0.0}; |
| 120 | double m_sampleInterval = 5.0; ///< sample interval in seconds |
| 121 | /// our store of samples (in buckets). The last bucket is partially full, |
| 122 | /// with the number of valid samples indicated by m_validSampleCount |
| 123 | std::deque<SampleBucket> m_buckets; |
| 124 | |
| 125 | SGPropertyNode_ptr m_weightOnWheels; |
| 126 | SGPropertyNode_ptr m_enabled; |
| 127 | |
| 128 | bool m_lastWoW{false}; |
| 129 | size_t m_maxMemoryUseBytes{0}; |
| 130 | |
| 131 | void allocateNewBucket(); |
| 132 | |
| 133 | void capture(); |
| 134 | |
| 135 | size_t currentMemoryUseBytes() const; |
| 136 | }; |