-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathACS7xx_Allegro.cpp
243 lines (200 loc) · 5.69 KB
/
ACS7xx_Allegro.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/**************************************************************************/
/*!
@file CurrentIC_ACS7xx_Allegro.cpp
@author SOSAndroid (E. Ha.)
@license BSD (see license.txt)
Set of methods to use measurements from ACS7xx sensors from Allegro Micro
All those methods are made to be called periodically
@section HISTORY
v1.0 - First release
V1.1 - Moving exponential average + robustness
v1.2 - Modify the constructor call to avoid some errors
v1.2.1 - Fix issue #1
*/
/**************************************************************************/
#include <stdlib.h>
#include "ACS7xx_Allegro.h"
/*========================================================================*/
/* CONSTRUCTORS */
/*========================================================================*/
/**************************************************************************/
/*!
Constructor
*/
/**************************************************************************/
ACS7XX_ALLEGRO::ACS7XX_ALLEGRO(void)
{
_bidir = ACS7XX_BIDIR_DEFAULT;
_pintoread = ACS7XX_PIN_DEFAULT;
_sensitivity = ACS7XX_SENSITIVITY_DEFAULT;
_voltage = BOARD_VOLTAGE_DEFAULT;
}
ACS7XX_ALLEGRO::ACS7XX_ALLEGRO(boolean bidir, int pintoread, double voltage, double sensitivity)
{
_bidir = bidir;
_pintoread = pintoread;
_sensitivity = sensitivity;
_voltage = voltage;
}
/*========================================================================*/
/* PUBLIC FUNCTIONS */
/*========================================================================*/
void ACS7XX_ALLEGRO::begin(void) {
if (_bidir) {
_voltage_offset = _voltage / 2.0;
}
else {
_voltage_offset = 0.0;
}
_resolution = (double) BOARD_ADC_DEPTH;
_adc_offset = (int) BOARD_MEASURED_OFFSET;
_factor_value = (double) ACS7XX_FACTOR_VALUE;
_lastMillis = 0; //need to add a function to start the counting by initializing lastmillis when starting a discharge / charge cycle
_lastCurrent = 0.0;
_AHCounter = 0.0;
_CoulombCounter = 0.0;
_movavgexp_alpha = 2.0 / (EXP_MOVAVG_N + 1.0);
_movavgexp_loop = 0;
_movavgexp_val = 0.0;
#ifdef SERIAL_DEBUG
if(!Serial) {
Serial.begin(9600);
}
#endif
return;
}
void ACS7XX_ALLEGRO::instantCurrent(double *current)
{
int readvalue = analogRead(_pintoread) + _adc_offset;
double readvolt = (((double) readvalue / _resolution) * _voltage) - _voltage_offset;
double readcur = readvolt * _factor_value / _sensitivity;
*current = readcur;
_lastCurrent = readcur;
ACS7XX_ALLEGRO::movingAvgExp(readcur);
#ifdef SERIAL_DEBUG
if (Serial){
Serial.print("Read value on pin including offset: ");
Serial.println(readvalue, DEC);
Serial.print("Current: ");
Serial.print(readcur, DEC);
Serial.println(" mA");
Serial.print("Moving average: ");
Serial.print(ACS7XX_ALLEGRO::getMovingAvgExp(), DEC);
Serial.println(" mA");
}
#endif
return;
}
void ACS7XX_ALLEGRO::ampereHourCount(double *mamperehc)
{
unsigned long currentmillis = millis();
double timeframehour = (double)(currentmillis - _lastMillis) / 3600000.0;
double readcurrent;
ACS7XX_ALLEGRO::instantCurrent(&readcurrent);
*mamperehc = readcurrent * timeframehour;
_lastMillis = currentmillis;
#ifdef SERIAL_DEBUG
if (Serial){
Serial.print("AmpHour: ");
Serial.print(*mamperehc, DEC);
Serial.println(" mAh");
Serial.print("timeframe ");
Serial.print(timeframehour, DEC);
Serial.println(" hour");
}
#endif
return;
}
void ACS7XX_ALLEGRO::updateCounters(void)
{
double amperehcTemp;
ACS7XX_ALLEGRO::ampereHourCount(&erehcTemp);
_lastAmperehour = amperehcTemp;
_AHCounter += amperehcTemp;
_lastCoulomb = amperehcTemp * 3.6;
_CoulombCounter += _lastCoulomb;
#ifdef SERIAL_DEBUG
if (Serial){
Serial.print("mAH counter ");
Serial.print(_AHCounter, DEC);
Serial.println(" mAH");
Serial.print("Coulomb counter ");
Serial.print(_CoulombCounter, DEC);
Serial.println(" C");
Serial.println("Counters updated");
}
#endif
return;
}
void ACS7XX_ALLEGRO::resetCounters(void)
{
_lastAmperehour = 0.0;
_AHCounter = 0.0;
_lastCoulomb = 0.0;
_CoulombCounter = 0.0;
#ifdef SERIAL_DEBUG
if (Serial){
Serial.println("Counters reseted");
}
#endif
return;
}
void ACS7XX_ALLEGRO::updateMillis(void)
{
_lastMillis = millis();
#ifdef SERIAL_DEBUG
if (Serial){
Serial.println("Millis updated");
}
#endif
return;
}
void ACS7XX_ALLEGRO::getAHCount(double *ahcount)
{
*ahcount = _AHCounter;
return;
}
void ACS7XX_ALLEGRO::getCoulombCount(double *ccount)
{
*ccount = _CoulombCounter;
return;
}
void ACS7XX_ALLEGRO::printDebugDeviceInit(void)
{
#ifdef SERIAL_DEBUG
if (Serial){
Serial.println("ACS7XX sensor object initialized");
Serial.print("PIN: ");
Serial.println(_pintoread, DEC);
Serial.print("Sensitivity: ");
Serial.print(_sensitivity, DEC);
Serial.println(" V/A");
Serial.print("device is ");
if(!_bidir) Serial.print("not ");
Serial.println("bidirectional");
Serial.println("...... ...... ......");
}
#endif
return;
}
void ACS7XX_ALLEGRO::movingAvgExp(double current) {
//init moving average exponetial with simple average of
if (_movavgexp_loop < EXP_MOVAVG_LOOP) {
_movavgexp_val += current;
if (_movavgexp_loop == (EXP_MOVAVG_LOOP - 1)) _movavgexp_val = _movavgexp_val / (double) EXP_MOVAVG_LOOP;
_movavgexp_loop ++;
}
else {
double movavgexp = _movavgexp_val + _movavgexp_alpha * (current - _movavgexp_val);
_movavgexp_val = movavgexp;
}
return;
}
double ACS7XX_ALLEGRO::getMovingAvgExp(void) {
return _movavgexp_val;
}
void ACS7XX_ALLEGRO::resetMovingAvgExp(void) {
_movavgexp_val = 0;
_movavgexp_loop = 0;
return;
}