forked from openenergymonitor/EmonLib
-
Notifications
You must be signed in to change notification settings - Fork 5
/
EmonLib.h
108 lines (79 loc) · 3.15 KB
/
EmonLib.h
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
/*
Emon.h - Library for openenergymonitor
Created by Trystan Lea, April 27 2010
GNU GPL
modified to use up to 12 bits ADC resolution (ex. Arduino Due)
by [email protected] 26.12.2013
Low Pass filter for offset removal replaces HP filter 1/1/2015 - RW
*/
#ifndef EmonLib_h
#define EmonLib_h
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
// define theoretical vref calibration constant for use in readvcc()
// 1100mV*1024 ADC steps http://openenergymonitor.org/emon/node/1186
// override in your code with value for your specific AVR chip
// determined by procedure described under "Calibrating the internal reference voltage" at
// http://openenergymonitor.org/emon/buildingblocks/calibration
#ifndef READVCC_CALIBRATION_CONST
#define READVCC_CALIBRATION_CONST 1126400L
#endif
// to enable 15-bit ADC resolution on the ADS1115
// or to enable 12-bit ADC resolution on Arduino Due,
// otherwise will default to 10 bits, as in regular Arduino-based boards.
#if defined(ADS1115_CONVERSIONDELAY)
#define ADC_BITS 15
#elif defined(__arm__)
#define ADC_BITS 12
#else
#define ADC_BITS 10
#endif
#define ADC_COUNTS (1<<ADC_BITS)
class EnergyMonitor
{
public:
EnergyMonitor();
typedef int (*inputPinReaderMethod) (int _pin);
inputPinReaderMethod inputPinReader;
static int defaultInputPinReader(int _pin);
void voltage(unsigned int _inPinV, double _VCAL, double _PHASECAL);
void current(unsigned int _inPinI, double _ICAL);
void voltageTX(double _VCAL, double _PHASECAL);
void currentTX(unsigned int _channel, double _ICAL);
void calcVI(unsigned int crossings, unsigned int timeout);
double calcIrms(unsigned int NUMBER_OF_SAMPLES);
void serialprint();
long readVcc();
//Useful value variables
double realPower,
apparentPower,
powerFactor,
Vrms,
Irms;
private:
//Set Voltage and current input pins
unsigned int inPinV;
unsigned int inPinI;
//Calibration coefficients
//These need to be set in order to obtain accurate results
double VCAL;
double ICAL;
double PHASECAL;
//--------------------------------------------------------------------------------------
// Variable declaration for emon_calc procedure
//--------------------------------------------------------------------------------------
int sampleV; //sample_ holds the raw analog read value
int sampleI;
double lastFilteredV,filteredV; //Filtered_ is the raw analog value minus the DC offset
double filteredI;
double offsetV; //Low-pass filter output
double offsetI; //Low-pass filter output
double phaseShiftedV; //Holds the calibrated phase shifted voltage.
double sqV,sumV,sqI,sumI,instP,sumP; //sq = squared, sum = Sum, inst = instantaneous
int startV; //Instantaneous voltage at start of sample window.
boolean lastVCross, checkVCross; //Used to measure number of times threshold is crossed.
};
#endif