-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatteryMonitor.cpp
130 lines (102 loc) · 2.23 KB
/
BatteryMonitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* BatteryMonitor.cpp
*
* Created: 12/20/2019 7:42:52 PM
* Author: Tristan Callahan
*/
#include <Arduino.h>
#include "BatteryMonitor.h"
void BatteryMonitor::checkBattery()
{
struct Samples {
float volts[5];
float amps [5];
float temp[5];
};
Samples sample;
for (int i = 0; i < 5; ++i)
{
sample.volts[i] = voltmeter.getVolts();
sample.amps[i] = ammeter.getAmps();
sample.temp[i] = thermometer.getTempFahrenheit();
}
float Vtemp;
float Atemp;
float Ttemp;
for (int i = 0; i < 5; ++i)
{
Vtemp = Vtemp + sample.volts[i];
Atemp = Atemp + sample.amps[i];
Ttemp = Ttemp + sample.temp[i];
}
Vtemp = Vtemp / 5;
Atemp = Atemp / 5;
Ttemp = Ttemp / 5;
status.temp = Vtemp;
status.amps = Atemp;
status.temp = Ttemp;
}
float BatteryMonitor::getAmps()
{
return status.amps;
}
float BatteryMonitor::getVolts()
{
return status.volts;
}
float BatteryMonitor::getTemp()
{
return status.temp;
}
void BatteryMonitor::openBatteryRelay()
{
digitalWrite(batteryRelayPin,HIGH);
isBatteryRelayPinHigh = true;
timeRelayOpened = AutoFarm::Clock::currentTimeStamp();
}
void BatteryMonitor::closeBatteryRelay()
{
if ( isBatteryRelayPinHigh == true)
{
digitalWrite(batteryRelayPin,LOW);
isBatteryRelayPinHigh = false;
}
}
bool BatteryMonitor::isBatteryChargingCorrectly()
{
checkBattery();
AutoFarm::TimeStamp ts = AutoFarm::Clock::currentTimeStamp();
if ((ts.unixtime - chargeHistory.lastBatteryCheck.unixtime) > 3600 ) //1 hour in seconds
{
chargeHistory.lastBatteryCheck = ts;
chargeHistory.hoursCharged = chargeHistory.hoursCharged + 1;
chargeHistory.ampHours = chargeHistory.ampHours + status.amps;
if (chargeHistory.hoursCharged > 23)
{
chargeHistory.hoursCharged = 0;
chargeHistory.ampHours = 0;
}
}
if (status.volts > 14.8 ||
status.amps > 10 ||
status.temp > 90 ||
chargeHistory.ampHours > 11
)
{
shouldWeOpenBatteryRelay = true;
return false;
}
shouldWeOpenBatteryRelay = false;
return true;
}
void BatteryMonitor::execute()
{
if (shouldWeOpenBatteryRelay == true)
{
openBatteryRelay();
}
else
{
closeBatteryRelay();
}
}