Android release v6.7_preview1
[xcsoar.git] / src / FlightStatistics.cpp
blobe90bc065a6d39f2e83a3cc8688919fdbec903a07
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "FlightStatistics.hpp"
26 void FlightStatistics::Reset() {
27 ScopeLock lock(mutex);
29 thermal_average.Reset();
30 altitude.Reset();
31 altitude_base.Reset();
32 altitude_ceiling.Reset();
33 task_speed.Reset();
34 altitude_terrain.Reset();
37 void
38 FlightStatistics::StartTask()
40 ScopeLock lock(mutex);
41 // JMW clear thermal climb average on task start
42 thermal_average.Reset();
43 task_speed.Reset();
46 void
47 FlightStatistics::AddAltitudeTerrain(const fixed tflight, const fixed terrainalt)
49 ScopeLock lock(mutex);
50 altitude_terrain.LeastSquaresUpdate(std::max(fixed(0), tflight / 3600),
51 terrainalt);
54 void
55 FlightStatistics::AddAltitude(const fixed tflight, const fixed alt)
57 ScopeLock lock(mutex);
58 altitude.LeastSquaresUpdate(std::max(fixed(0), tflight / 3600), alt);
61 fixed
62 FlightStatistics::AverageThermalAdjusted(const fixed mc_current,
63 const bool circling)
65 ScopeLock lock(mutex);
67 fixed mc_stats;
68 if (thermal_average.y_ave > fixed(0)) {
69 if (mc_current > fixed(0) && circling)
70 mc_stats = (thermal_average.sum_n * thermal_average.y_ave + mc_current) /
71 (thermal_average.sum_n + 1);
72 else
73 mc_stats = thermal_average.y_ave;
74 } else {
75 mc_stats = mc_current;
78 return mc_stats;
81 void
82 FlightStatistics::AddTaskSpeed(const fixed tflight, const fixed val)
84 ScopeLock lock(mutex);
85 task_speed.LeastSquaresUpdate(tflight / 3600, std::max(fixed(0),val));
88 void
89 FlightStatistics::AddClimbBase(const fixed tflight, const fixed alt)
91 ScopeLock lock(mutex);
93 if (!altitude_ceiling.IsEmpty())
94 // only update base if have already climbed, otherwise
95 // we will catch the takeoff height as the base.
96 altitude_base.LeastSquaresUpdate(std::max(fixed(0), tflight) / 3600, alt);
99 void
100 FlightStatistics::AddClimbCeiling(const fixed tflight, const fixed alt)
102 ScopeLock lock(mutex);
103 altitude_ceiling.LeastSquaresUpdate(std::max(fixed(0), tflight) / 3600, alt);
107 * Adds a thermal to the ThermalAverage calculator
108 * @param v Average climb speed of the last thermal
110 void
111 FlightStatistics::AddThermalAverage(const fixed v)
113 ScopeLock lock(mutex);
114 thermal_average.LeastSquaresUpdate(v);