-
Notifications
You must be signed in to change notification settings - Fork 16
/
emonLibCM.cpp
1520 lines (1282 loc) · 58.4 KB
/
emonLibCM.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
emonLibCM.cpp - Library for openenergymonitor
GNU GPL
*/
// This library provides continuous single-phase monitoring of real power on up to five CT channels.
// All of the time-critical code is now contained within the ISR, only the slower activities
// are done within the main code. These slower activities include RF transmissions,
// and all Serial statements (not part of the library).
//
// This library is suitable for either 50 or 60 Hz operation.
//
// Original Author: Robin Emley (calypso_rae on Open Energy Monitor Forum)
// Addition of Wh totals by: Trystan Lea
// Heavily modified to improve performance and calibration; temperature measurement
// and pulse counting incorporated into the library, by Robert Wall
// Release for testing 4/1/2017
//
// Version 2.0 21/11/2018
// Version 2.01 3/12/2018 Calculation error in phase error correction - const.'360' missing, 'x' & 'y' coefficients swapped.
// Version 2.02 13/07/2019 Temperature measurement: Added "BAD_TEMPERATURE" return value when reporting period < 0.2 s,
// getLogicalChannel( ), ReCalibrate_VChannel( ), ReCalibrate_IChannel( ) added, setPulsePin( ) interrupt no. was obligatory,
// pulse & temperatures were set/enabled only at startup, setTemperatureDataPin was ineffective, preloaded sensor addresses
// not handled properly.
// 18/10/2019 Sketch using this became the default in emonTx V3.4
// Version 2.03 25/10/2019 Mains Frequency reporting [getLineFrequency( )]added,
// ADC reference source was AVcc and not selectable - ability to select [setADC_VRef( )] added,
// sampleSetsDuringThisDatalogPeriod (and derivatives) was samplesDuringThisDatalogPeriod etc,
// Energy calculation changed to use internal clock rather than mains time by addition of "frequencyDeviation".
// Version 2.04 1/8/2020 In examples, 'else' added to "if (EmonLibCM_Ready())" to keep JeeLib alive. Example for RFM69CW only ('Classic' format) and
// not using JeeLib added.
// getDatalog_period( ) added. Temperature array is now ignored if first device is not a DS18B20. 'BAD_TEMPERATURE' now returned if
// sensor address is made invalid during operation. Array above is sensor count is filled with UNUSED_TEMPERATURE. Superfluous 'if'
// removed at end of retrieveTemperatures() - power pin is now set low regardless. 'else' added to 'if' in
// (if (temperatureEnabled = _enable)) in TemperatureEnable( ) to ensure power is off if not required, delay after retrieving
// a temperature was 5 ms.
// unsigned long missing_VoltageSamples (was "missing_Voltage"), bool firstcycle were not volatile, unnecessary copies of
// 'protected' variables removed, datalog period was set only at startup & minimum limit added. cycleCountForDatalogging,
// min_startup_cycles were signed.
// (Plus some cosmetic changes)
// Version 2.1.0 9/7/2021 2nd pulse input added, array of structs was individual variables. N.B. The definition setPulsePin(byte channel, int _pin)
// is incompatible with the old definition of setPulsePin(int _pin, int _interrupt). Solution for 85 °C problem added,
// special print format for emonPi added. 'Setters' to initialise Wh counters & pulse count added. If no a.c. voltage,
// now uses assumed Vrms to calculate power, VA & energy, p.f. and frequency both report zero. Error in phase shift calculation
// meant wrong correction was applied when ct's were sampled out of sequence.
// Version 2.1.1 26/7/2021 Typo in Include file name - was emonLibCM2P.h
// Version 2.1.2 7/8/2021 'assumedACVoltage' was 'assumedVrms' (name conflict in some sketches).
// Version 2.2.0 14/9/2021 Pulse counting could count double on switch bounce. Changed: PulseMinPeriod, default was 110 ms;
// 'laststate', 'timing' & 'edge' added to track input state; 'edge' added to setters; in init(), interrupt was attached to RISING edge;
// most of the code in the pulse ISR removed, replaced by countPulses( ) called from main loop.
// Version 2.2.1 5/12/2012 Repackaged 30/10/2021 release: Debugging statements accidentally left in pulse ISR removed.
// Version 2.2.2 15/9/2022 If temperature sensor pin had not been set but relied on default, no power was applied for initial search.
// #include "WProgram.h" un-comment for use on older versions of Arduino IDE
// #define SAMPPIN 5 // EmonTx: Preferred pin for testing. This MUST be commented out if the temperature sensor power is connected here. Only include for testing.
// #define SAMPPIN 19 // EmonTx: Alternative pin for testing. This MUST be commented out if the temperature sensor power is connected here. Only include for testing.
// #define INTPINS // Debugging print of interrupt allocations
#include "emonLibCM.h"
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
unsigned int cycles_per_second = 50; // mains frequency in Hz (i.e. 50 or 60)
float datalog_period_in_seconds = 10.0;
unsigned int min_startup_cycles = 10;
// Maximum number of Current (I) channels used to create arrays
static const int max_no_of_channels = 5;
// User set number of Current (I) channels used by 'for' loops
int no_of_channels = 4;
// number of Current (I) channels that have been set
byte no_of_Iinputs = 0;
// for general interaction between the main code and the ISR
volatile boolean datalogEventPending;
volatile unsigned long missing_VoltageSamples = 0; // provides a timebase mechanism for current-only use
// - uses the ADC free-running rate as a clock.
double line_frequency; // Timed from sample rate & cycle count
// Arrays for current channels (zero-based)
int realPower_CT[max_no_of_channels];
int apparentPower_CT[max_no_of_channels];
double Irms_CT[max_no_of_channels];
long wh_CT[max_no_of_channels] = {0, 0, 0, 0, 0};
double pf[max_no_of_channels];
double Vrms;
volatile boolean ChannelInUse[max_no_of_channels];
static byte lChannel[max_no_of_channels+1]; // logical current channel no. (0-based)
// analogue ports
static byte ADC_Sequence[max_no_of_channels+1] = {0,1,2,3,4,5}; // <-- Sequence in which the analogue ports are scanned, first is Voltage, remainder are currents
// ADC data
int ADCBits = 10; // 10 for the emonTx and most of the Arduino range, 12 for the Arduino Due.
double Vref = 3.3; // ADC Reference Voltage = 3.3 for emonTX, 5.0 for most of the Arduino range.
int ADCDuration = 104; // Time in microseconds for one ADC conversion = 104 for 16 MHz clock
byte ADCRef = VREF_NORMAL << 6; // ADC Reference: VREF_EXTERNAL, VREF_NORMAL = AVcc, VREF_INTERNAL = Internal 1.1 V
// Pulse Counting
#define PULSEINPUTS 2 // No of available interrupts for pulse counting (2 is the maximum, add more "onPulse..." functions for more)
struct pulse {
byte PulsePin = 3; // default to DI3 for the emonTx V3
byte PulseInterrupt = 1; // default to int1 for the emonTx V3
unsigned long PulseMinPeriod = 20; // default to 20 ms
byte edge = FALLING; // edge to increment count
unsigned long pulseCount = 0; // Total number of pulses from switch-on
unsigned long pulseIncrement = 0; // Incremental number of pulses between readings
bool PulseEnabled = false;
bool PulseChange = false; // track change of state of counting
bool laststate = HIGH; // Last state of interrupt pin
volatile bool timing = false; // 'debounce' period running
volatile unsigned long pulseTime; // Instant of the last interrupt - used for debounce logic
} pulses[PULSEINPUTS];
void onPulse(byte channel); // General pulse handler
void onPulse0(), onPulse1(); // Individual pulse handlers - one per interrupt
// Set-up values
//--------------
// These set up the library for different hardware configurations
//
// setADC Sets the ADC resolution and the conversion time
// cycles_per_second Defines the mains frequency
// _min_startup_cycles The period of inactivity whilst the system settles at start-up
// _datalog_period The rate at which data is reported for logging
// SetADC_Channel Defines the channels input pin and calibration constants
//
//
// Calibration values
//-------------------
// Many calibration values are used in this sketch:
//
// ADCCal This sets up the ADC reference voltage
// voltageCal This is the principal calibration for the ac adapter.
// currentCal A per-channel amplitude calibration for each current transformer.
// phaseCal A per-channel calibration for the phase error correction
// With most hardware, the default values are likely to work fine without
// need for change. A compact explanation of each of these values now follows:
// Voltage calibration constant. This is the mains voltage that would give 1 V
// at the ADC input:
// AC-AC Voltage adapter is designed to step down the voltage from 240V to 9V
// but the AC Voltage adapter is running open circuit and so output voltage is
// likely to be about 20% higher than 9V, actually 11.6 V for the UK Ideal adapter
// (from the data sheet).
// Open circuit step down = 240 / 11.6 = 20.69
// The output voltage is then stepped down further with the voltage divider which has
// values Rb = 10k, Rt = 120k which will reduce the voltage by 13 times.
// The combined step down is therefore 20.69 x 13 = 268.97 which is the
// theoretical calibration constant. The actual constant for a given
// unit and ac adapter is likely to be different by a few percent.
// Other adapters may be different by more.
// Current calibration constant. This is the mains current that would give 1 V
// at the ADC input:
// Current calibration constant channels 1 - 3 = 100 A / 50 mA / 22 Ohms burden resistor = 90.9
// (The default CT sensor is 100 A : 50 mA)
// for channel 4 is 100 A / 50 mA / 120R burden resistor = 16.67
// The actual constant for a given unit and CT is likely to be different by a few percent.
// phaseCal is used to alter the phase of the voltage waveform relative to the
// current waveform. The algorithm interpolates between the most recent pair
// of voltage samples according to the value of phaseCal.
//
// The value of phaseCal entered is difference between the phase lead of the voltage transformer and
// the phase lead of the current transformer, in degrees (changes of less than 0.1 deg are
// unlikely to make a detectable difference).
/**************************************************************************************************
*
* General variables
*
*
***************************************************************************************************/
// -------------- general global variables -----------------
// Some of these variables are used in multiple blocks so cannot be static.
// For integer maths, many variables need to be 'long' or in extreme cases 'int64_t'
double currentCal[max_no_of_channels] = {90.91, 90.91, 90.91, 16.67, 90.91};
double phaseCal_CT[max_no_of_channels] ={4.2, 4.2, 4.2, 1.0, 4.2};
double voltageCal = 268.97;
unsigned int ADC_Counts = 1 << ADCBits;
bool stop = false;
volatile bool firstcycle = true;
unsigned int samplesDuringThisCycle;
bool acPresent = false; // true when ac voltage input is detected.
unsigned int acDetectedThreshold = ADC_Counts >> 5; // ac voltage detection threshold, ~10% of nominal voltage (given large amount of ripple)
double assumedACVoltage = 240.0;
unsigned int datalogPeriodInMainsCycles;
unsigned long ADCsamples_per_datalog_period;
// accumulators & counters for use by the ISR
long cumV_deltas; // <--- for offset removal (V)
int64_t sumPA_CT[max_no_of_channels]; // 'partial' power for real power calculation
int64_t sumPB_CT[max_no_of_channels]; // 'partial' power for real power calculation
uint64_t sumIsquared_CT[max_no_of_channels];
long cumI_deltas_CT[max_no_of_channels]; // <--- for offset removal (I)
uint64_t sum_Vsquared; // for Vrms datalogging
long sampleSetsDuringThisDatalogPeriod;
// Copies of ISR data for use by the main code
// These are filled by the ADC helper routine at the end of the datalogging period
volatile int64_t copyOf_sumPA_CT[max_no_of_channels];
volatile int64_t copyOf_sumPB_CT[max_no_of_channels];
volatile uint64_t copyOf_sumIsquared_CT[max_no_of_channels];
volatile uint64_t copyOf_sum_Vsquared;
volatile long copyOf_sampleSetsDuringThisDatalogPeriod;
volatile int64_t copyOf_cumI_deltas[max_no_of_channels];
volatile int64_t copyOf_cumV_deltas;
// For mechanisms to check the integrity of this code structure
#ifdef INTEGRITY
int sampleSetsDuringThisMainsCycle;
int lowestNoOfSampleSetsPerMainsCycle;
volatile int copyOf_lowestNoOfSampleSetsPerMainsCycle;
#endif
enum polarities {NEGATIVE, POSITIVE};
// For an enhanced polarity detection mechanism, which includes a persistence check
#define POLARITY_CHECK_MAXCOUNT 3 // 1
polarities polarityUnconfirmed;
polarities polarityConfirmed; // for improved zero-crossing detection
polarities polarityConfirmedOfLastSampleV; // for zero-crossing detection
float residualEnergy_CT[max_no_of_channels];
double x[max_no_of_channels], y[max_no_of_channels]; // coefficients for real power interpolation
// Temperature measurement
//
// Hardware Configuration
byte W1Pin = 5; // 1-Wire pin for temperature = 5 for emonTx V3, 4 for emonTx V2 & emonTx Shield
char DS18B20_PWR = -1; // Power pin for DS18B20 temperature sensors. Default -1 - power off
// Global variables used only inside the library
OneWire oneWire(W1Pin);
bool temperatureEnabled = false;
byte numSensors = 0;
byte temperatureResolution = TEMPRES_11;
byte temperatureMaxCount = 1;
DeviceAddress *temperatureSensors = NULL;
bool keepAddresses = false;
int *temperatures = NULL;
unsigned int temperatureConversionDelayTime;
unsigned long temperatureConversionDelaySamples;
volatile bool startConvertTemperatures = false;
volatile bool convertingTemperaturesNoAC = false; // Only used when not using mains for timing.
/**************************************************************************************************
*
* APPLICATION INTERFACE - Getters & Setters
*
*
***************************************************************************************************/
void EmonLibCM_SetADC_VChannel(byte ADC_Input, double _amplitudeCal)
{
ADC_Sequence[0] = ADC_Input;
voltageCal = _amplitudeCal;
}
void EmonLibCM_SetADC_IChannel(byte ADC_Input, double _amplitudeCal, double _phaseCal)
{
currentCal[no_of_Iinputs] = _amplitudeCal;
phaseCal_CT[no_of_Iinputs] = _phaseCal;
ChannelInUse[no_of_Iinputs] = true;
lChannel[ADC_Input] = no_of_Iinputs;
ADC_Sequence[++no_of_Iinputs] = ADC_Input;
}
void EmonLibCM_ReCalibrate_VChannel(double _amplitudeCal)
{
voltageCal = _amplitudeCal * Vref / ADC_Counts;
}
void EmonLibCM_ReCalibrate_IChannel(byte ADC_Input, double _amplitudeCal, double _phaseCal)
{
byte lChannel = EmonLibCM_getLogicalChannel(ADC_Input);
currentCal[lChannel] = _amplitudeCal * Vref / ADC_Counts;
phaseCal_CT[lChannel] = _phaseCal;
calcPhaseShift(lChannel);
}
void EmonLibCM_cycles_per_second(unsigned int _cycles_per_second)
{
cycles_per_second = _cycles_per_second;
datalogPeriodInMainsCycles = datalog_period_in_seconds * cycles_per_second;
calcTemperatureLead();
for (byte i = 0; i<no_of_channels; i++)
calcPhaseShift(i);
}
void EmonLibCM_min_startup_cycles(unsigned int _min_startup_cycles)
{
min_startup_cycles = _min_startup_cycles;
}
void EmonLibCM_datalog_period(float _datalog_period_in_seconds)
{
if (datalog_period_in_seconds < 0.1)
datalog_period_in_seconds = 0.1;
datalog_period_in_seconds = _datalog_period_in_seconds;
datalogPeriodInMainsCycles = datalog_period_in_seconds * cycles_per_second;
ADCsamples_per_datalog_period = datalog_period_in_seconds * MICROSPERSEC / ADCDuration;
// Set lead time to start temperature conversion
calcTemperatureLead();
}
void EmonLibCM_setADC(int _ADCBits, int _ADCDuration)
{
ADCBits = _ADCBits;
ADCDuration = _ADCDuration;
}
void EmonLibCM_setADC_VRef(byte _ADCRef)
{
ADCRef = _ADCRef << 6;
}
void EmonLibCM_setPulseEnable(bool _enable)
{
pulses[0].PulseEnabled = _enable;
pulses[0].PulseChange = true;
}
void EmonLibCM_setPulseEnable(byte channel, bool _enable)
{
pulses[channel].PulseEnabled = _enable;
pulses[channel].PulseChange = true;
}
void EmonLibCM_setPulsePin(int _pin)
{
pulses[0].PulsePin = _pin;
pulses[0].PulseInterrupt = digitalPinToInterrupt(_pin);
}
void EmonLibCM_setPulsePin(byte channel, int _pin, int _interrupt)
{
pulses[channel].PulsePin = _pin;
pulses[channel].PulseInterrupt = _interrupt;
}
void EmonLibCM_setPulsePin(byte channel, int _pin)
{
if (channel != 0 || channel != 1) // Attempt to deal with incompatibility with prior versions
{
_pin = channel;
channel = 0;
}
pulses[channel].PulsePin = _pin;
pulses[channel].PulseInterrupt = digitalPinToInterrupt(_pin);
}
void EmonLibCM_setPulseMinPeriod(int _period, byte _edge)
{
pulses[0].PulseMinPeriod = _period;
pulses[0].edge = _edge;
}
void EmonLibCM_setPulseMinPeriod(byte channel, int _period, byte _edge)
{
pulses[channel].PulseMinPeriod = _period;
pulses[channel].edge = _edge;
}
void EmonLibCM_setPulseCount(long _pulseCount)
{
pulses[0].pulseCount = _pulseCount;
}
void EmonLibCM_setPulseCount(byte channel, long _pulseCount)
{
pulses[channel].pulseCount = _pulseCount;
}
void EmonLibCM_setAssumedVrms(double _assumedVrms)
{
assumedACVoltage = _assumedVrms;
}
void EmonLibCM_setWattHour(byte channel, long _wh)
{
wh_CT[channel] = _wh;
}
void EmonLibCM_ADCCal(double _Vref)
{
Vref = _Vref;
}
bool EmonLibCM_acPresent(void)
{
return(acPresent);
}
int EmonLibCM_getRealPower(int channel)
{
return realPower_CT[channel];
}
int EmonLibCM_getApparentPower(int channel)
{
return apparentPower_CT[channel];
}
double EmonLibCM_getPF(int channel)
{
return pf[channel];
}
double EmonLibCM_getIrms(int channel)
{
return Irms_CT[channel];
}
double EmonLibCM_getVrms(void)
{
return Vrms;
}
double EmonLibCM_getAssumedVrms(void)
{
return assumedACVoltage;
}
double EmonLibCM_getDatalog_period(void)
{
return datalog_period_in_seconds;
}
double EmonLibCM_getLineFrequency(void)
{
if (acPresent)
return line_frequency;
else
return 0;
}
long EmonLibCM_getWattHour(int channel)
{
return wh_CT[channel];
}
unsigned long EmonLibCM_getPulseCount(void)
{
return pulses[0].pulseCount;
}
unsigned long EmonLibCM_getPulseCount(byte channel)
{
return pulses[channel].pulseCount;
}
int EmonLibCM_getLogicalChannel(byte ADC_Input)
{
// Look up logical channel associated with physical pin
// N.B. Returns 255 for an unused input
return lChannel[ADC_Input];
}
#ifdef INTEGRITY
int EmonLibCM_minSampleSetsDuringThisMainsCycle(void)
{
return copyOf_lowestNoOfSampleSetsPerMainsCycle;
// The answer should be 192 (50 Hz) or 160 (60 Hz) divided by
// 2 for 1 CT in use, 3 for 2 CTs in use, etc.
// Returns 999 if no mains is detected.
}
#endif
void EmonLibCM_Init(void)
{
// Set number of channels to the number defined, else use the defaults
if (no_of_Iinputs)
{
no_of_channels = no_of_Iinputs;
for (byte i = no_of_Iinputs+1; i < max_no_of_channels; i++)
ChannelInUse[i] = false;
}
// Set up voltage calibration to take account of ADC width etc
voltageCal = voltageCal * Vref / ADC_Counts;
// Likewise each current channel
for (int i=0; i<no_of_channels; i++)
{
currentCal[i] = currentCal[i] * Vref / ADC_Counts;
calcPhaseShift(i);
residualEnergy_CT[i] = 0;
}
EmonLibCM_Start();
datalogPeriodInMainsCycles = datalog_period_in_seconds * cycles_per_second;
datalogEventPending = false;
ADCsamples_per_datalog_period = datalog_period_in_seconds * MICROSPERSEC / ADCDuration;
// nominally a truncation error of 0.16% at 1s, or 0.004% at 10 s by having this as an integer - insignificant
#ifdef SAMPPIN
pinMode(SAMPPIN, OUTPUT);
digitalWrite(SAMPPIN, LOW);
#endif
for (byte channel = 0; channel < PULSEINPUTS; channel++)
{
if (pulses[channel].PulseEnabled)
{
pinMode(pulses[channel].PulsePin, INPUT_PULLUP); // Set interrupt pulse counting pin as input & attach interrupt
if (channel == 0)
attachInterrupt(pulses[0].PulseInterrupt, onPulse0, CHANGE);
if (channel == 1)
attachInterrupt(pulses[1].PulseInterrupt, onPulse1, CHANGE);
}
#ifdef INTPINS
Serial.print("Ch: ");Serial.println(channel);
Serial.print(" en: ");Serial.println(pulses[channel].PulseEnabled);
Serial.print(" pin: ");Serial.println(pulses[channel].PulsePin);
Serial.print(" int: ");Serial.println(pulses[channel].PulseInterrupt);
Serial.print(" pin3, int: ");Serial.println(digitalPinToInterrupt(3));
Serial.print(" pin2, int: ");Serial.println(digitalPinToInterrupt(2));
#endif
}
}
/**************************************************************************************************
*
* START
*
*
***************************************************************************************************/
void EmonLibCM_Start(void)
{
firstcycle = true;
missing_VoltageSamples = 0;
// Set up the ADC to be free-running
//
// BIT: 7, 6, 5, 4, 3, 2, 1, 0
// ADCSRA: ADEN, ADSC, ADFR, ADIF, ADIE, ADPS2, ADPS1, ADPS0
//
// ADEN: ADC Enable
// ADSC: ADC Start Conversion
// ADFR: ADC Free Running Select, or ADATE (ADC Auto Trigger Enable)
// ADIF: ADC Interrupt Flag
// ADIE: ADC Interrupt Enable
// ADPS2, ADPS1, ADPS0: ADC Prescaler Select Bits (CLOCK FREQUENCY)
//
// The default value of ADCSRA before we change it with the following is 135
// ADCSRA: ADEN, ADSC, ADFR, ADIF, ADIE, ADPS2, ADPS1, ADPS0
// 128 64 32 16 8 4 2 1
// 1 0 0 0 0 1 1 1
// The ADC is enabled and the ADC clock is set to system clock / 128
//
// The following sets ADCSRA to a value of 239
// 1 1 1 0 1 1 1 1
ADCSRA = (1<<ADPS0)+(1<<ADPS1)+(1<<ADPS2); // Set the ADC's clock to system clock / 128
ADCSRA |= (1 << ADEN); // Enable the ADC
ADCSRA |= (1<<ADATE); // set the Auto Trigger Enable bit in the ADCSRA register. Because
// bits ADTS0-2 have not been set (i.e. they are all zero), the
// ADC's trigger source is set to "free running mode".
ADCSRA |=(1<<ADIE); // set the ADC interrupt enable bit. When this bit is written
// to one and the I-bit in SREG is set, the
// ADC Conversion Complete Interrupt is activated.
ADCSRA |= (1<<ADSC); // start ADC manually first time
sei(); // Enable Global Interrupts
}
void EmonLibCM_StopADC(void)
{
// This stop function returns the ADC to default state
ADCSRA = (1<<ADPS0)+(1<<ADPS1)+(1<<ADPS2); // Set the ADC's clock to system clock / 128
ADCSRA |= (1<<ADEN); // Enable the ADC
ADCSRA |= (0<<ADATE);
ADCSRA |= (0<<ADIE);
ADCSRA |= (0<<ADSC);
stop = false;
}
/**************************************************************************************************
*
* Retrieve and apply final processing of data ready for reporting
*
*
***************************************************************************************************/
void EmonLibCM_get_readings()
{
// Use the 'volatile' variables passed from the ISR.
double frequencyDeviation;
cli();
for (int i=0; i<no_of_channels; i++)
{
if (!ChannelInUse[i])
{
copyOf_sumPA_CT[i] = 0;
copyOf_sumPB_CT[i] = 0;
copyOf_sumIsquared_CT[i] = 0;
copyOf_cumI_deltas[i] = 0;
}
}
for (byte channel = 0; channel < PULSEINPUTS; channel++)
{
if (pulses[channel].PulseChange)
{
if (pulses[channel].PulseEnabled)
{
pinMode(pulses[channel].PulsePin, INPUT_PULLUP); // Set interrupt pulse counting pin as input & attach interrupt
if (channel == 0)
attachInterrupt(pulses[channel].PulseInterrupt, onPulse0, CHANGE);
if (channel == 1)
attachInterrupt(pulses[channel].PulseInterrupt, onPulse1, CHANGE);
}
else
{
detachInterrupt(pulses[channel].PulseInterrupt); // Detach pulse counting interrupt
pulses[channel].PulseChange = false;
}
}
if (pulses[channel].pulseIncrement) // if the ISR has counted some pulses, update the total count
{
pulses[channel].pulseCount += pulses[channel].pulseIncrement;
pulses[channel].pulseIncrement = 0;
}
}
sei();
// Calculate the final values, scaling for the number of samples and applying calibration coefficients.
// The final values are deposited in global variables for extraction by the 'getter' functions.
// The rms of a signal plus an offset is sqrt( signal^2 + offset^2).
// Vrms still contains the fine voltage offset. Correct this by subtracting the "Offset V^2" before the sq. root.
// Real Power is calculated by interpolating between the 'partial power' values, applying "trigonometric" coefficients to
// preserve the amplitude of the interpolated value.
Vrms = sqrt(((double)copyOf_sum_Vsquared / copyOf_sampleSetsDuringThisDatalogPeriod)
- ((double)copyOf_cumV_deltas * copyOf_cumV_deltas / copyOf_sampleSetsDuringThisDatalogPeriod / copyOf_sampleSetsDuringThisDatalogPeriod));
Vrms *= voltageCal;
frequencyDeviation = (double)ADCsamples_per_datalog_period / (copyOf_sampleSetsDuringThisDatalogPeriod * (no_of_channels + 1)); // nominal value / actual value
line_frequency = cycles_per_second * frequencyDeviation;
for (int i=0; i<no_of_channels; i++) // Current channels
{
double powerNow;
double energyNow;
double VA;
int wattHoursRecent;
double sumRealPower;
// Apply combined phase & timing correction
sumRealPower = (copyOf_sumPA_CT[i] * x[i] + copyOf_sumPB_CT[i] * y[i]);
// sumRealPower still contains the fine offsets of both V & I. Correct this by subtracting the "Offset Power": cumV_deltas * cumI_deltas
powerNow = (sumRealPower / copyOf_sampleSetsDuringThisDatalogPeriod - (double)copyOf_cumV_deltas * copyOf_cumI_deltas[i]
/ copyOf_sampleSetsDuringThisDatalogPeriod / copyOf_sampleSetsDuringThisDatalogPeriod) * voltageCal * currentCal[i];
// root of mean squares, removing fine offset
// The rms of a signal plus an offset is sqrt( signal^2 + offset^2).
// Here (signal+offset)^2 = copyOf_sumIsquared_CT / no of samples
// offset = cumI_deltas / no of samples
Irms_CT[i] = sqrt(((double)copyOf_sumIsquared_CT[i] / copyOf_sampleSetsDuringThisDatalogPeriod) - ((double)copyOf_cumI_deltas[i] * copyOf_cumI_deltas[i] / copyOf_sampleSetsDuringThisDatalogPeriod / copyOf_sampleSetsDuringThisDatalogPeriod));
Irms_CT[i] *= currentCal[i];
if (acPresent)
{
VA = Irms_CT[i] * Vrms;
pf[i] = powerNow / VA;
if (pf[i] > 1.05 || pf[i] < -1.05 || isnan(pf[i]))
pf[i] = 0.0;
realPower_CT[i] = powerNow + 0.5; // rounded to nearest Watt
apparentPower_CT[i] = VA + 0.5; // rounded to nearest VA
energyNow = (powerNow * datalog_period_in_seconds / frequencyDeviation) // correct for mains time != clock time
+ residualEnergy_CT[i]; // fp for accuracy
}
else
{
VA = Irms_CT[i] * assumedACVoltage;
pf[i] = 0.0;
realPower_CT[i] = VA + 0.5; // rounded to nearest Watt
apparentPower_CT[i] = VA + 0.5; // rounded to nearest VA
energyNow = (VA * datalog_period_in_seconds / frequencyDeviation) // correct for mains time != clock time
+ residualEnergy_CT[i]; // fp for accuracy
}
wattHoursRecent = energyNow / 3600; // integer assignment to extract whole Wh
wh_CT[i]+= wattHoursRecent; // accumulated WattHours since start-up
residualEnergy_CT[i] = energyNow - (wattHoursRecent * 3600.0); // fp for accuracy
}
// Retrieve the temperatures, which should be stored inside each sensor
if (temperatureEnabled)
{
retrieveTemperatures();
}
}
bool EmonLibCM_Ready()
{
countPulses();
if (startConvertTemperatures)
{
startConvertTemperatures = false;
convertTemperatures();
}
if (datalogEventPending)
{
datalogEventPending = false;
EmonLibCM_get_readings();
return true;
}
return false;
}
void EmonLibCM_confirmPolarity()
{
/* This routine prevents a zero-crossing point from being declared until
* a certain number of consecutive samples in the 'other' half of the
* waveform have been encountered. It forms part of the ISR.
*/
static byte count = 0;
if (polarityUnconfirmed != polarityConfirmedOfLastSampleV)
{
count++;
}
else
{
count = 0;
}
if (count >= POLARITY_CHECK_MAXCOUNT) {
count = 0;
polarityConfirmed = polarityUnconfirmed;
}
}
void calcPhaseShift(byte lChannel)
{
/* Calculate the 'X' & 'Y' coefficients of phase shift for the c.t.
* phaseCal value supplied is the difference between VT lead and CT lead in degrees
* Add the delay due to the time taken by the ADC to convert one sample (ADCDuration),
* knowing the position of the current sample with respect to
* the voltage, then convert to radians.
* x & y are the constants used in the power interpolation. (Sanity check: x + y ≈ 1)
*/
const double two_pi = 6.2831853;
double sampleRate = ADCDuration * (no_of_channels + 1) * two_pi * cycles_per_second / MICROSPERSEC; // in radians
double phase_shift = (phaseCal_CT[lChannel] / 360.0 + (lChannel+1) * ADCDuration
* (double)cycles_per_second/MICROSPERSEC) * two_pi; // Total phase shift in radians
// (lChannel+1) was ADC_Sequence[lChannel+1]
y[lChannel] = sin(phase_shift) / sin(sampleRate);
x[lChannel] = cos(phase_shift) - y[lChannel] * cos(sampleRate);
}
void calcTemperatureLead(void)
{
/* Set lead time to start temperature conversion
* The temparature sensors are instructed to 'convert' the temperature just in time for the result
* to be available to be retrieved and reported along with the other data.
* The lead time is the number of cycles (or in the absence of ac, no. of samples) to allow after
* datalogEventPending has been set to true, so that temperature conversion will complete just before
* the next datalog event. Adjust the resolution if necessary so that conversion within one
* datalogging period is possible.
*/
int conversionLeadTime = (CONVERSION_LEAD_TIME >> (3 - ((temperatureResolution & 0x70) >> 5)));
// Should give 95 - 760 ms lead time, now convert to cycles (for a.c. present) or samples (for a.c. not present).
temperatureConversionDelayTime = datalogPeriodInMainsCycles - (long)conversionLeadTime * cycles_per_second / 1000 - 1;
// '-1' to counter the effect of integer truncation, and make sure there is some spare time
temperatureConversionDelaySamples = ((unsigned long)(datalog_period_in_seconds * 1000.0)
- (unsigned long)conversionLeadTime - 5) * 1000 / ADCDuration;
// '- 5' extra 5 ms to make sure there is some spare time
}
/**************************************************************************************************
*
* ADC Interrupt Handling
*
*
***************************************************************************************************/
void EmonLibCM_allGeneralProcessing_withinISR()
{
/* This routine deals with activities that are only required at specific points
* within each mains cycle. It forms part of the ISR.
*/
if (stop)
EmonLibCM_StopADC();
static unsigned int cycleCountForDatalogging = 0;
// a simple routine for checking the performance of this new ISR structure
if (acPresent)
{
if (polarityConfirmed == POSITIVE)
{
if (polarityConfirmedOfLastSampleV != POSITIVE)
{
/* Instantaneous power contributions are summed in accumulators during each
* datalogging period. At the end of each period, copies are made of their
* content for use by the main code. The accumulators, and any associated
* counters are then reset for use during the next period.
*/
cycleCountForDatalogging++;
#ifdef INTEGRITY
if (sampleSetsDuringThisMainsCycle < lowestNoOfSampleSetsPerMainsCycle)
{
lowestNoOfSampleSetsPerMainsCycle = sampleSetsDuringThisMainsCycle;
}
sampleSetsDuringThisMainsCycle = 0;
#endif
// Used in stop start operation, discards the first partial cycle
if (firstcycle==true && cycleCountForDatalogging >= min_startup_cycles)
{
firstcycle = false;
cycleCountForDatalogging = 0;
for (int i=0; i<no_of_channels; i++)
{
sumPA_CT[i] = 0;
sumPB_CT[i] = 0;
sumIsquared_CT[i] = 0;
cumI_deltas_CT[i] = 0;
}
sum_Vsquared = 0;
cumV_deltas = 0;
#ifdef INTEGRITY
lowestNoOfSampleSetsPerMainsCycle = 999;
#endif
sampleSetsDuringThisDatalogPeriod = 0;
}
// Start temperature conversion
if (cycleCountForDatalogging == temperatureConversionDelayTime && firstcycle==false)
{
// Only do it on this one cycle
startConvertTemperatures = true;
}
if (cycleCountForDatalogging >= datalogPeriodInMainsCycles && firstcycle==false)
{
cycleCountForDatalogging = 0;
for (int i=0; i<no_of_channels; i++)
{
copyOf_sumPA_CT[i] = sumPA_CT[i];
copyOf_sumPB_CT[i] = sumPB_CT[i];
sumPA_CT[i] = 0;
sumPB_CT[i] = 0;
copyOf_sumIsquared_CT[i] = sumIsquared_CT[i];
sumIsquared_CT[i] = 0;
copyOf_cumI_deltas[i] = cumI_deltas_CT[i];
cumI_deltas_CT[i] = 0;
}
copyOf_cumV_deltas = cumV_deltas;
copyOf_sum_Vsquared = sum_Vsquared;
sum_Vsquared = 0;
cumV_deltas = 0;
copyOf_sampleSetsDuringThisDatalogPeriod = sampleSetsDuringThisDatalogPeriod;
sampleSetsDuringThisDatalogPeriod = 0;
#ifdef INTEGRITY
copyOf_lowestNoOfSampleSetsPerMainsCycle = lowestNoOfSampleSetsPerMainsCycle; // (for diags only)
lowestNoOfSampleSetsPerMainsCycle = 999;
#endif
datalogEventPending = true;
}
} // end of processing that is specific to the first Vsample in each +ve half cycle
} // end of processing that is specific to samples where the voltage is positive
else // the polarity of this sample is negative
{
if (polarityConfirmedOfLastSampleV != NEGATIVE)
{
// This is the start of a new -ve half cycle (just after the zero-crossing point)
//
samplesDuringThisCycle = 0;
// check_RF_LED_status();
} // end of processing that is specific to the first Vsample in each -ve half cycle
} // end of processing that is specific to samples where the voltage is positive
}
else
{
// In the case where the voltage signal is missing this part counts ADC samples up to the
// duration of the datalog period, at which point it will make the readings available.
// The reporting interval is now dependent on the processor's internal clock
// Start temperature conversion
if (missing_VoltageSamples > temperatureConversionDelaySamples && convertingTemperaturesNoAC == false)
{
// Only do it once per report
startConvertTemperatures = true;
convertingTemperaturesNoAC = true;
}
if (missing_VoltageSamples > ADCsamples_per_datalog_period)
{
missing_VoltageSamples = 0; // reset the missing samples count here.
firstcycle = true; // firstcycle reset to true so that next reading
// with voltage signal starts from the right place
#ifdef INTEGRITY
lowestNoOfSampleSetsPerMainsCycle = 999;
#endif
cycleCountForDatalogging = 0;
for (int i=0; i<no_of_channels; i++)
{
copyOf_sumPA_CT[i] = 0;
copyOf_sumPB_CT[i] = 0;
sumPA_CT[i] = 0;
sumPB_CT[i] = 0;
copyOf_sumIsquared_CT[i] = sumIsquared_CT[i];
sumIsquared_CT[i] = 0;
copyOf_cumI_deltas[i] = cumI_deltas_CT[i];
cumI_deltas_CT[i] = 0;
}
copyOf_sum_Vsquared = sum_Vsquared;
sum_Vsquared = 0;
copyOf_cumV_deltas = cumV_deltas;
cumV_deltas = 0;
copyOf_sampleSetsDuringThisDatalogPeriod = sampleSetsDuringThisDatalogPeriod;
sampleSetsDuringThisDatalogPeriod = 0;
#ifdef INTEGRITY
copyOf_lowestNoOfSampleSetsPerMainsCycle = lowestNoOfSampleSetsPerMainsCycle; // (for diags only)
// lowestNoOfSampleSetsPerMainsCycle = 999;
#endif
datalogEventPending = true;
// Stops the sampling at the end of the cycle if EmonLibCM_Stop() has been called
// if (stop) EmonLibCM_StopADC();
}
}
}