android/GlueIOIOPort: fix spurious errors after IOIO baud rate change
[xcsoar.git] / src / BallastDumpManager.cpp
bloba3a590ebd379b62c68633b040cdd852c2dbb2181
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 "BallastDumpManager.hpp"
25 #include "Engine/GlideSolvers/GlidePolar.hpp"
27 void
28 BallastDumpManager::Start()
30 assert(!IsEnabled());
31 enabled = true;
33 // Update ballast_clock for the next call to BallastDumpManager::Update()
34 ballast_clock.Update();
37 void
38 BallastDumpManager::Stop()
40 assert(IsEnabled());
41 enabled = false;
44 void
45 BallastDumpManager::SetEnabled(bool _enabled)
47 if (_enabled && !IsEnabled())
48 Start();
49 else if (!_enabled && IsEnabled())
50 Stop();
53 bool
54 BallastDumpManager::Update(GlidePolar &glide_polar, unsigned dump_time)
56 assert(IsEnabled());
58 // We don't know how fast the water is flowing so don't pretend that we do
59 if (dump_time == 0) {
60 Stop();
61 return false;
64 // Milliseconds since last ballast_clock.Update() call
65 int dt = ballast_clock.Elapsed();
67 // Update ballast_clock for the next call to BallastDumpManager::Update()
68 ballast_clock.Update();
70 // How many percent of the max. ballast do we dump in one millisecond
71 fixed percent_per_millisecond = fixed(1) / (1000 * dump_time);
73 // Calculate the new ballast percentage
74 fixed ballast = glide_polar.GetBallast() - dt * percent_per_millisecond;
76 // Check if the plane is dry now
77 if (negative(ballast)) {
78 Stop();
79 glide_polar.SetBallastLitres(fixed(0));
80 return false;
83 // Set new ballast
84 glide_polar.SetBallast(ballast);
85 return true;