2019-07-18 12:25:28 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the "bluetoothheater" distribution
|
|
|
|
* (https://gitlab.com/mrjones.id.au/bluetoothheater)
|
|
|
|
*
|
2019-09-08 00:14:36 +00:00
|
|
|
* Copyright (C) 2019 Ray Jones <ray@mrjones.id.au>
|
2019-07-18 12:25:28 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "FuelGauge.h"
|
|
|
|
#include "NVStorage.h"
|
|
|
|
#include "DebugPort.h"
|
2019-07-24 09:25:07 +00:00
|
|
|
#include "../RTC/RTCStore.h"
|
2019-07-18 12:25:28 +00:00
|
|
|
|
|
|
|
CFuelGauge::CFuelGauge()
|
|
|
|
{
|
2019-07-19 20:53:12 +00:00
|
|
|
_pumpStrokes = 0;
|
2019-07-18 12:25:28 +00:00
|
|
|
_pumpCal = 0.02;
|
2019-07-19 20:53:12 +00:00
|
|
|
_lastStoredVal = _pumpStrokes;
|
2019-07-18 12:25:28 +00:00
|
|
|
DebugPort.println("CFuelGauge::CFuelGauge");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-07-19 20:53:12 +00:00
|
|
|
CFuelGauge::init(float fuelUsed)
|
2019-07-18 12:25:28 +00:00
|
|
|
{
|
|
|
|
_pumpCal = NVstore.getHeaterTuning().pumpCal;
|
2019-07-19 20:53:12 +00:00
|
|
|
|
|
|
|
_pumpStrokes = fuelUsed;
|
|
|
|
DebugPort.printf("Initialising fuel gauge with %.2f strokes\r\n", _pumpStrokes);
|
|
|
|
_lastStoredVal = _pumpStrokes;
|
2020-03-31 08:46:25 +00:00
|
|
|
RTC_Store.setFuelGauge(_pumpStrokes); // uses RTC registers to store this
|
2019-07-18 12:25:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-20 06:08:43 +00:00
|
|
|
void
|
|
|
|
CFuelGauge::reset()
|
|
|
|
{
|
2020-03-31 08:46:25 +00:00
|
|
|
DebugPort.println("resetting fuel gauge");
|
2019-07-20 06:08:43 +00:00
|
|
|
_pumpStrokes = 0;
|
|
|
|
_lastStoredVal = _pumpStrokes;
|
|
|
|
RTC_Store.setFuelGauge(_pumpStrokes); // uses RTC registers to store this
|
|
|
|
}
|
2019-07-18 12:25:28 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
CFuelGauge::Integrate(float Hz)
|
|
|
|
{
|
|
|
|
unsigned long timenow = millis();
|
|
|
|
long tSample = timenow - _lasttime;
|
|
|
|
_lasttime = timenow;
|
|
|
|
|
2019-07-19 20:53:12 +00:00
|
|
|
_pumpStrokes += Hz * tSample * 0.001; // Hz * seconds
|
2019-07-18 12:25:28 +00:00
|
|
|
|
2019-07-19 20:53:12 +00:00
|
|
|
float fuelDelta = _pumpStrokes - _lastStoredVal;
|
|
|
|
bool bStoppedSave = (Hz == 0) && (_pumpStrokes != _lastStoredVal);
|
|
|
|
if(fuelDelta > 10 || bStoppedSave) { // record fuel usage every 10 minutes, or every 10 strokes
|
2019-07-28 07:40:12 +00:00
|
|
|
// DebugPort.printf("Storing fuel gauge: %.2f strokes\r\n", _pumpStrokes);
|
2019-07-19 20:53:12 +00:00
|
|
|
RTC_Store.setFuelGauge(_pumpStrokes); // uses RTC registers to store this
|
|
|
|
_lastStoredVal = _pumpStrokes;
|
2019-07-18 12:25:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
2019-07-21 11:17:54 +00:00
|
|
|
CFuelGauge::Used_mL()
|
2019-07-20 06:08:43 +00:00
|
|
|
{
|
|
|
|
return _pumpStrokes * _pumpCal; // strokes * millilitre / stroke
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
|
|
|
CFuelGauge::Used_strokes()
|
2019-07-18 12:25:28 +00:00
|
|
|
{
|
2019-07-20 06:08:43 +00:00
|
|
|
return _pumpStrokes;
|
2019-07-18 12:25:28 +00:00
|
|
|
}
|