3 // Written by James Turner, started December 2012.
5 // Copyright (C) 2012 James Turner - zakalawe (at) mac com
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.
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.
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.
21 ///////////////////////////////////////////////////////////////////////////////
27 #include "FlightHistory.hxx"
30 #include <boost/foreach.hpp>
32 #include <simgear/sg_inlines.h>
33 #include <simgear/debug/logstream.hxx>
34 #include <simgear/props/props_io.hxx>
35 #include <simgear/misc/strutils.hxx>
36 #include <simgear/structure/exception.hxx>
37 #include <simgear/math/SGMath.hxx>
39 #include <Main/fg_props.hxx>
40 #include <Main/globals.hxx>
42 FGFlightHistory::FGFlightHistory() :
43 m_sampleInterval(5.0),
44 m_validSampleCount(SAMPLE_BUCKET_WIDTH)
48 FGFlightHistory::~FGFlightHistory()
52 void FGFlightHistory::init()
54 m_enabled = fgGetNode("/sim/history/enabled", true);
55 m_sampleInterval = fgGetDouble("/sim/history/sample-interval-sec", 1.0);
56 if (m_sampleInterval <= 0.0) { // would be bad
57 SG_LOG(SG_FLIGHT, SG_INFO, "invalid flight-history sample interval:" << m_sampleInterval
58 << ", defaulting to " << m_sampleInterval);
59 m_sampleInterval = 1.0;
62 // cap memory use at 4MB
63 m_maxMemoryUseBytes = fgGetInt("/sim/history/max-memory-use-bytes", 1024 * 1024 * 4);
64 m_weightOnWheels = NULL;
65 // reset the history when we detect a take-off
66 if (fgGetBool("/sim/history/clear-on-takeoff", true)) {
67 m_weightOnWheels = fgGetNode("/gear/gear[1]/wow", 0, true);
68 m_lastWoW = m_weightOnWheels->getBoolValue();
71 // force bucket re-allocation
72 m_validSampleCount = SAMPLE_BUCKET_WIDTH;
75 void FGFlightHistory::shutdown()
80 void FGFlightHistory::reinit()
86 void FGFlightHistory::update(double dt)
88 if ((dt == 0.0) || !m_enabled->getBoolValue()) {
89 return; // paused or disabled
92 if (m_weightOnWheels) {
94 if (m_lastWoW && !m_weightOnWheels->getBoolValue()) {
95 SG_LOG(SG_FLIGHT, SG_INFO, "history: detected main-gear takeoff, clearing history");
98 } // of rest-on-takeoff enabled
100 double elapsed = globals->get_sim_time_sec() - m_lastCaptureTime;
101 if (elapsed > m_sampleInterval) {
106 void FGFlightHistory::allocateNewBucket()
108 SampleBucket* bucket = NULL;
109 if (!m_buckets.empty() && (currentMemoryUseBytes() > m_maxMemoryUseBytes)) {
110 bucket = m_buckets.front();
111 m_buckets.erase(m_buckets.begin());
113 bucket = new SampleBucket;
116 m_buckets.push_back(bucket);
117 m_validSampleCount = 0;
120 void FGFlightHistory::capture()
122 if (m_validSampleCount == SAMPLE_BUCKET_WIDTH) {
123 // bucket is full, allocate a new one
127 m_lastCaptureTime = globals->get_sim_time_sec();
128 Sample* sample = m_buckets.back()->samples + m_validSampleCount;
130 sample->simTimeMSec = static_cast<int>(m_lastCaptureTime * 1000.0);
131 sample->position = globals->get_aircraft_position();
133 double heading, pitch, roll;
134 globals->get_aircraft_orientation(heading, pitch, roll);
135 sample->heading = static_cast<float>(heading);
136 sample->pitch = static_cast<float>(pitch);
137 sample->roll = static_cast<float>(roll);
139 ++m_validSampleCount;
142 SGGeodVec FGFlightHistory::pathForHistory(double minEdgeLengthM) const
145 if (m_buckets.empty()) {
149 result.push_back(m_buckets.front()->samples[0].position);
150 SGVec3d lastOutputCart = SGVec3d::fromGeod(result.back());
151 double minLengthSqr = minEdgeLengthM * minEdgeLengthM;
153 BOOST_FOREACH(SampleBucket* bucket, m_buckets) {
154 unsigned int count = (bucket == m_buckets.back() ? m_validSampleCount : SAMPLE_BUCKET_WIDTH);
156 // iterate over all the valid samples in the bucket
157 for (unsigned int index = 0; index < count; ++index) {
158 SGGeod g = bucket->samples[index].position;
159 SGVec3d cart(SGVec3d::fromGeod(g));
160 if (distSqr(cart, lastOutputCart) > minLengthSqr) {
161 lastOutputCart = cart;
164 } // of samples iteration
165 } // of buckets iteration
170 void FGFlightHistory::clear()
172 BOOST_FOREACH(SampleBucket* ptr, m_buckets) {
176 m_validSampleCount = SAMPLE_BUCKET_WIDTH;
179 size_t FGFlightHistory::currentMemoryUseBytes() const
181 return sizeof(SampleBucket) * m_buckets.size();