Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
8.6 KiB

6 years ago
  1. // -----------------------------------------------------------------------------
  2. // Abstract Energy Monitor Sensor (other EMON sensors extend this class)
  3. // Copyright (C) 2017-2018 by Xose Pérez <xose dot perez at gmail dot com>
  4. // -----------------------------------------------------------------------------
  5. #if SENSOR_SUPPORT
  6. #pragma once
  7. #undef I2C_SUPPORT
  8. #define I2C_SUPPORT 1 // Explicitly request I2C support.
  9. #include "Arduino.h"
  10. #include "I2CSensor.h"
  11. class EmonSensor : public I2CSensor {
  12. public:
  13. // ---------------------------------------------------------------------
  14. // Public
  15. // ---------------------------------------------------------------------
  16. EmonSensor(): I2CSensor() {
  17. // Calculate # of magnitudes
  18. #if EMON_REPORT_CURRENT
  19. ++_magnitudes;
  20. #endif
  21. #if EMON_REPORT_POWER
  22. ++_magnitudes;
  23. #endif
  24. #if EMON_REPORT_ENERGY
  25. ++_magnitudes;
  26. #endif
  27. }
  28. void expectedPower(unsigned char channel, unsigned int expected) {
  29. if (channel >= _channels) return;
  30. unsigned int actual = _current[channel] * _voltage;
  31. if (actual == 0) return;
  32. if (expected == actual) return;
  33. _current_ratio[channel] = _current_ratio[channel] * ((double) expected / (double) actual);
  34. calculateFactors(channel);
  35. _dirty = true;
  36. }
  37. void resetEnergy() {
  38. for (unsigned char i=0; i<_channels; i++) {
  39. _energy[i] = 0;
  40. }
  41. }
  42. // ---------------------------------------------------------------------
  43. void setVoltage(double voltage) {
  44. if (_voltage == voltage) return;
  45. _voltage = voltage;
  46. _dirty = true;
  47. }
  48. void setReference(double reference) {
  49. if (_reference == reference) return;
  50. _reference = reference;
  51. _dirty = true;
  52. }
  53. void setCurrentRatio(unsigned char channel, double current_ratio) {
  54. if (channel >= _channels) return;
  55. if (_current_ratio[channel] == current_ratio) return;
  56. _current_ratio[channel] = current_ratio;
  57. calculateFactors(channel);
  58. _dirty = true;
  59. }
  60. // ---------------------------------------------------------------------
  61. double getVoltage() {
  62. return _voltage;
  63. }
  64. double getReference() {
  65. return _reference;
  66. }
  67. double getCurrentRatio(unsigned char channel) {
  68. if (channel >= _channels) return 0;
  69. return _current_ratio[channel];
  70. }
  71. unsigned char getChannels() {
  72. return _channels;
  73. }
  74. // ---------------------------------------------------------------------
  75. // Sensor API
  76. // ---------------------------------------------------------------------
  77. void begin() {
  78. // Resolution
  79. _adc_counts = 1 << _resolution;
  80. // Calculations
  81. for (unsigned char i=0; i<_channels; i++) {
  82. _energy[i] = _current[i] = 0;
  83. _pivot[i] = _adc_counts >> 1;
  84. calculateFactors(i);
  85. }
  86. #if SENSOR_DEBUG
  87. DEBUG_MSG("[EMON] Reference (mV): %d\n", int(1000 * _reference));
  88. DEBUG_MSG("[EMON] ADC counts: %d\n", _adc_counts);
  89. for (unsigned char i=0; i<_channels; i++) {
  90. DEBUG_MSG("[EMON] Channel #%d current ratio (mA/V): %d\n", i, int(1000 * _current_ratio[i]));
  91. DEBUG_MSG("[EMON] Channel #%d current factor (mA/bit): %d\n", i, int(1000 * _current_factor[i]));
  92. DEBUG_MSG("[EMON] Channel #%d Multiplier: %d\n", i, int(_multiplier[i]));
  93. }
  94. #endif
  95. _ready = true;
  96. _dirty = false;
  97. }
  98. protected:
  99. // ---------------------------------------------------------------------
  100. // Protected
  101. // ---------------------------------------------------------------------
  102. // Initializes internal variables
  103. void init() {
  104. _current_ratio = new double[_channels];
  105. _current_factor = new double[_channels];
  106. _multiplier = new uint16_t[_channels];
  107. _pivot = new double[_channels];
  108. _current = new double[_channels];
  109. #if EMON_REPORT_ENERGY
  110. _energy = new uint32_t[_channels];
  111. #endif
  112. }
  113. virtual unsigned int readADC(unsigned char channel) {}
  114. void calculateFactors(unsigned char channel) {
  115. _current_factor[channel] = _current_ratio[channel] * _reference / _adc_counts;
  116. unsigned int s = 1;
  117. unsigned int i = 1;
  118. unsigned int m = s * i;
  119. unsigned int multiplier;
  120. while (m * _current_factor[channel] < 1) {
  121. multiplier = m;
  122. i = (i == 1) ? 2 : (i == 2) ? 5 : 1;
  123. if (i == 1) s *= 10;
  124. m = s * i;
  125. }
  126. _multiplier[channel] = multiplier;
  127. }
  128. double read(unsigned char channel) {
  129. int max = 0;
  130. int min = _adc_counts;
  131. double sum = 0;
  132. unsigned long time_span = millis();
  133. for (unsigned long i=0; i<_samples; i++) {
  134. int sample;
  135. double filtered;
  136. // Read analog value
  137. sample = readADC(channel);
  138. if (sample > max) max = sample;
  139. if (sample < min) min = sample;
  140. // Digital low pass filter extracts the VDC offset
  141. _pivot[channel] = (_pivot[channel] + (sample - _pivot[channel]) / EMON_FILTER_SPEED);
  142. filtered = sample - _pivot[channel];
  143. // Root-mean-square method
  144. sum += (filtered * filtered);
  145. }
  146. time_span = millis() - time_span;
  147. // Quick fix
  148. if (_pivot[channel] < min || max < _pivot[channel]) {
  149. _pivot[channel] = (max + min) / 2.0;
  150. }
  151. // Calculate current
  152. double rms = _samples > 0 ? sqrt(sum / _samples) : 0;
  153. double current = _current_factor[channel] * rms;
  154. current = (double) (int(current * _multiplier[channel]) - 1) / _multiplier[channel];
  155. if (current < 0) current = 0;
  156. #if SENSOR_DEBUG
  157. DEBUG_MSG("[EMON] Channel: %d\n", channel);
  158. DEBUG_MSG("[EMON] Total samples: %d\n", _samples);
  159. DEBUG_MSG("[EMON] Total time (ms): %d\n", time_span);
  160. DEBUG_MSG("[EMON] Sample frequency (Hz): %d\n", int(1000 * _samples / time_span));
  161. DEBUG_MSG("[EMON] Max value: %d\n", max);
  162. DEBUG_MSG("[EMON] Min value: %d\n", min);
  163. DEBUG_MSG("[EMON] Midpoint value: %d\n", int(_pivot[channel]));
  164. DEBUG_MSG("[EMON] RMS value: %d\n", int(rms));
  165. DEBUG_MSG("[EMON] Current (mA): %d\n", int(1000 * current));
  166. #endif
  167. // Check timing
  168. if ((time_span > EMON_MAX_TIME)
  169. || ((time_span < EMON_MAX_TIME) && (_samples < EMON_MAX_SAMPLES))) {
  170. _samples = (_samples * EMON_MAX_TIME) / time_span;
  171. }
  172. return current;
  173. }
  174. unsigned char _channels = 0; // Number of ADC channels available
  175. unsigned char _magnitudes = 0; // Number of magnitudes per channel
  176. unsigned long _samples = EMON_MAX_SAMPLES; // Samples (dynamically modificable)
  177. unsigned char _resolution = 10; // ADC resolution in bits
  178. unsigned long _adc_counts; // Max count
  179. double _voltage = EMON_MAINS_VOLTAGE; // Mains voltage
  180. double _reference = EMON_REFERENCE_VOLTAGE; // ADC reference voltage (100%)
  181. double * _current_ratio; // Ratio ampers in main loop to voltage in secondary (per channel)
  182. double * _current_factor; // Calculated, reads (RMS) to current (per channel)
  183. uint16_t * _multiplier; // Calculated, error (per channel)
  184. double * _pivot; // Moving average mid point (per channel)
  185. double * _current; // Last current reading (per channel)
  186. #if EMON_REPORT_ENERGY
  187. uint32_t * _energy; // Aggregated energy (per channel)
  188. #endif
  189. };
  190. #endif // SENSOR_SUPPORT