| 1 | // FlightHistory |
| 2 | // |
| 3 | // Written by James Turner, started December 2012. |
| 4 | // |
| 5 | // Copyright (C) 2012 James Turner - zakalawe (at) mac com |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | /////////////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | #ifdef HAVE_CONFIG_H |
| 24 | # include "config.h" |
| 25 | #endif |
| 26 | |
| 27 | #include "FlightHistory.hxx" |
| 28 | |
| 29 | #include <algorithm> |
| 30 | |
| 31 | #include <chrono> |
| 32 | #include <cstdint> |
| 33 | #include <simgear/debug/logstream.hxx> |
| 34 | #include <simgear/math/SGMath.hxx> |
| 35 | #include <simgear/misc/strutils.hxx> |
| 36 | #include <simgear/props/props_io.hxx> |
| 37 | #include <simgear/sg_inlines.h> |
| 38 | #include <simgear/structure/commands.hxx> |
| 39 | #include <simgear/structure/exception.hxx> |
| 40 | |
| 41 | #include <Main/fg_props.hxx> |
| 42 | #include <Main/globals.hxx> |
| 43 | |
| 44 | using namespace std::chrono; |
| 45 | |
| 46 | FGFlightHistory::FGFlightHistory() = default; |
| 47 | |
| 48 | FGFlightHistory::~FGFlightHistory() = default; |
| 49 | |
| 50 | void FGFlightHistory::init() |
| 51 | { |
| 52 | m_enabled = fgGetNode("/sim/history/enabled", true); |
| 53 | m_sampleInterval = fgGetDouble("/sim/history/sample-interval-sec", 1.0); |
| 54 | if (m_sampleInterval <= 0.0) { // would be bad |
| 55 | SG_LOG(SG_FLIGHT, SG_INFO, "invalid flight-history sample interval:" << m_sampleInterval |
| 56 | << ", defaulting to " << m_sampleInterval); |
| 57 | m_sampleInterval = 1.0; |
| 58 | } |
| 59 | |
| 60 | // cap memory use at 4MB |
| 61 | m_maxMemoryUseBytes = fgGetInt("/sim/history/max-memory-use-bytes", 1024 * 1024 * 4); |
| 62 | m_weightOnWheels = NULL; |
| 63 | // reset the history when we detect a take-off |
| 64 | if (fgGetBool("/sim/history/clear-on-takeoff", true)) { |
| 65 | m_weightOnWheels = fgGetNode("/gear/gear[1]/wow", 0, true); |
| 66 | m_lastWoW = m_weightOnWheels->getBoolValue(); |
| 67 | } |
| 68 | |
| 69 | // force bucket re-allocation |
| 70 | m_lastCaptureTime = globals->get_sim_time_sec(); |
| 71 | |
| 72 | globals->get_commands()->addCommand("clear-flight-history", this, &FGFlightHistory::clearHistoryCommand); |
| 73 | } |
| 74 | |
| 75 | void FGFlightHistory::shutdown() |
| 76 | { |
| 77 | clear(); |
| 78 | globals->get_commands()->removeCommand("clear-flight-history"); |
| 79 | } |
| 80 | |
| 81 | void FGFlightHistory::reinit() |
| 82 | { |
| 83 | shutdown(); |
| 84 | init(); |
| 85 | } |
| 86 | |
| 87 | void FGFlightHistory::update(double dt) |
| 88 | { |
| 89 | if ((dt == 0.0) || !m_enabled->getBoolValue()) { |
| 90 | return; // paused or disabled |
| 91 | } |
| 92 | |
| 93 | if (m_weightOnWheels) { |
| 94 | |
| 95 | if (m_lastWoW && !m_weightOnWheels->getBoolValue()) { |
| 96 | SG_LOG(SG_FLIGHT, SG_INFO, "history: detected main-gear takeoff, clearing history"); |
| 97 | clear(); |
| 98 | } |
| 99 | } // of rest-on-takeoff enabled |
| 100 | |
| 101 | // spatial check - moved at least 1m since last capture |
| 102 | if (!m_buckets.empty()) { |
| 103 | const auto& cb = currentBucket(); |
| 104 | if (!cb.isEmpty()) { |
| 105 | SGVec3d lastCaptureCart(SGVec3d::fromGeod(cb.lastSample().position)); |
| 106 | double d2 = distSqr(lastCaptureCart, globals->get_aircraft_position_cart()); |
| 107 | if (d2 <= 1.0) { |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | double elapsed = globals->get_sim_time_sec() - m_lastCaptureTime; |
| 114 | if (elapsed > m_sampleInterval) { |
| 115 | capture(); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void FGFlightHistory::allocateNewBucket() |
| 120 | { |
| 121 | if (!m_buckets.empty() && (currentMemoryUseBytes() > m_maxMemoryUseBytes)) { |
| 122 | m_buckets.pop_front(); |
| 123 | } |
| 124 | |
| 125 | m_buckets.emplace_back(); |
| 126 | } |
| 127 | |
| 128 | FGFlightHistory::SampleBucket& FGFlightHistory::currentBucket() |
| 129 | { |
| 130 | assert(!m_buckets.empty()); |
| 131 | return m_buckets.back(); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | void FGFlightHistory::capture() |
| 136 | { |
| 137 | if (m_buckets.empty() || currentBucket().isComplete()) { |
| 138 | allocateNewBucket(); |
| 139 | } |
| 140 | |
| 141 | m_lastCaptureTime = globals->get_sim_time_sec(); |
| 142 | auto& cb = currentBucket(); |
| 143 | auto& sample = cb.samples[cb.validSamples]; |
| 144 | |
| 145 | sample.simTimeMSec = static_cast<size_t>(m_lastCaptureTime * 1000.0); |
| 146 | sample.position = globals->get_aircraft_position(); |
| 147 | |
| 148 | double heading, pitch, roll; |
| 149 | globals->get_aircraft_orientation(heading, pitch, roll); |
| 150 | sample.heading = static_cast<float>(heading); |
| 151 | sample.pitch = static_cast<float>(pitch); |
| 152 | sample.roll = static_cast<float>(roll); |
| 153 | |
| 154 | cb.validSamples++; |
| 155 | } |
| 156 | |
| 157 | PagedPathForHistory_ptr FGFlightHistory::pagedPathForHistory(size_t max_entries, size_t newerThan ) const |
| 158 | { |
| 159 | PagedPathForHistory_ptr result = new PagedPathForHistory(); |
| 160 | if (m_buckets.empty()) { |
| 161 | return result; |
| 162 | } |
| 163 | |
| 164 | for (const auto& bucket : m_buckets) { |
| 165 | // iterate over all the valid samples in the bucket |
| 166 | for (unsigned int index = 0; index < bucket.validSamples; ++index) { |
| 167 | // skip older entries |
| 168 | // TODO: bisect! |
| 169 | if (bucket.samples[index].simTimeMSec <= newerThan) |
| 170 | continue; |
| 171 | |
| 172 | if( max_entries ) { |
| 173 | max_entries--; |
| 174 | SGGeod g = bucket.samples[index].position; |
| 175 | result->path.push_back(g); |
| 176 | result->last_seen = bucket.samples[index].simTimeMSec; |
| 177 | } else { |
| 178 | goto exit; |
| 179 | } |
| 180 | |
| 181 | } // of samples iteration |
| 182 | } // of buckets iteration |
| 183 | |
| 184 | exit: |
| 185 | return result; |
| 186 | } |
| 187 | |
| 188 | |
| 189 | SGGeodVec FGFlightHistory::pathForHistory(double minEdgeLengthM) const |
| 190 | { |
| 191 | SGGeodVec result; |
| 192 | if (m_buckets.empty()) { |
| 193 | return result; |
| 194 | } |
| 195 | |
| 196 | result.push_back(m_buckets.front().samples[0].position); |
| 197 | SGVec3d lastOutputCart = SGVec3d::fromGeod(result.back()); |
| 198 | double minLengthSqr = minEdgeLengthM * minEdgeLengthM; |
| 199 | |
| 200 | for (const auto& bucket : m_buckets) { |
| 201 | const unsigned int count = bucket.validSamples; |
| 202 | |
| 203 | // iterate over all the valid samples in the bucket |
| 204 | for (unsigned int index = 0; index < count; ++index) { |
| 205 | SGGeod g = bucket.samples[index].position; |
| 206 | SGVec3d cart(SGVec3d::fromGeod(g)); |
| 207 | if (distSqr(cart, lastOutputCart) > minLengthSqr) { |
| 208 | lastOutputCart = cart; |
| 209 | result.push_back(g); |
| 210 | } |
| 211 | } // of samples iteration |
| 212 | } // of buckets iteration |
| 213 | |
| 214 | return result; |
| 215 | } |
| 216 | |
| 217 | void FGFlightHistory::clear() |
| 218 | { |
| 219 | m_buckets.clear(); |
| 220 | } |
| 221 | |
| 222 | size_t FGFlightHistory::SampleBucket::bucketMinAge() const |
| 223 | { |
| 224 | // youngest / most recent (minimum) age in the bucket is the last value |
| 225 | if (validSamples == 0) { |
| 226 | return {}; |
| 227 | } |
| 228 | |
| 229 | return samples[validSamples - 1].simTimeMSec; |
| 230 | } |
| 231 | |
| 232 | const FGFlightHistory::Sample& FGFlightHistory::SampleBucket::lastSample() const |
| 233 | { |
| 234 | if (validSamples == 0) { |
| 235 | return samples.front(); |
| 236 | } |
| 237 | |
| 238 | return samples.at(validSamples - 1); |
| 239 | } |
| 240 | |
| 241 | void FGFlightHistory::clearOlderThan(std::chrono::seconds keepMostRecent) |
| 242 | { |
| 243 | if (keepMostRecent.count() == 0) { |
| 244 | clear(); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | const auto cutoff = globals->get_sim_time_sec() - keepMostRecent.count(); |
| 249 | const auto cutoffMSec = static_cast<size_t>(cutoff * 1000.0); |
| 250 | while (!m_buckets.empty()) { |
| 251 | if (m_buckets.front().bucketMinAge() >= cutoffMSec) { |
| 252 | break; |
| 253 | } |
| 254 | |
| 255 | m_buckets.pop_front(); // clear oldest bucket |
| 256 | } |
| 257 | |
| 258 | // we don't worry about doing a partial clear of front-most bucket |
| 259 | } |
| 260 | |
| 261 | size_t FGFlightHistory::currentMemoryUseBytes() const |
| 262 | { |
| 263 | return sizeof(SampleBucket) * m_buckets.size(); |
| 264 | } |
| 265 | |
| 266 | bool FGFlightHistory::clearHistoryCommand(const SGPropertyNode* args, SGPropertyNode*) |
| 267 | { |
| 268 | const auto keepSeconds = args->getDoubleValue("keep-most-recent-secs", 0.0); |
| 269 | clearOlderThan(std::chrono::seconds(static_cast<int64_t>(keepSeconds))); |
| 270 | return true; |
| 271 | } |
| 272 | |
| 273 | |
| 274 | // Register the subsystem. |
| 275 | SGSubsystemMgr::Registrant<FGFlightHistory> registrantFGFlightHistory; |