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.

231 lines
8.2 KiB

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