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.

236 lines
8.3 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. #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. _ready = true;
  88. _dirty = false;
  89. }
  90. protected:
  91. // ---------------------------------------------------------------------
  92. // Protected
  93. // ---------------------------------------------------------------------
  94. // Initializes internal variables
  95. void init() {
  96. _current_ratio = new double[_channels];
  97. _current_factor = new double[_channels];
  98. _multiplier = new uint16_t[_channels];
  99. _pivot = new double[_channels];
  100. _current = new double[_channels];
  101. #if EMON_REPORT_ENERGY
  102. _energy = new uint32_t[_channels];
  103. #endif
  104. }
  105. virtual unsigned int readADC(unsigned char channel) {}
  106. unsigned int calculateMultiplier(double current_factor) {
  107. unsigned int s = 1;
  108. unsigned int i = 1;
  109. unsigned int m = s * i;
  110. unsigned int multiplier;
  111. while (m * current_factor < 1) {
  112. multiplier = m;
  113. i = (i == 1) ? 2 : (i == 2) ? 5 : 1;
  114. if (i == 1) s *= 10;
  115. m = s * i;
  116. }
  117. return multiplier;
  118. }
  119. double read(unsigned char channel) {
  120. int max = 0;
  121. int min = _adc_counts;
  122. double sum = 0;
  123. unsigned long time_span = millis();
  124. for (unsigned long i=0; i<_samples; i++) {
  125. int sample;
  126. double filtered;
  127. // Read analog value
  128. sample = readADC(channel);
  129. if (sample > max) max = sample;
  130. if (sample < min) min = sample;
  131. // Digital low pass filter extracts the VDC offset
  132. _pivot[channel] = (_pivot[channel] + (sample - _pivot[channel]) / EMON_FILTER_SPEED);
  133. filtered = sample - _pivot[channel];
  134. // Root-mean-square method
  135. sum += (filtered * filtered);
  136. }
  137. time_span = millis() - time_span;
  138. // Quick fix
  139. if (_pivot[channel] < min || max < _pivot[channel]) {
  140. _pivot[channel] = (max + min) / 2.0;
  141. }
  142. // Calculate current
  143. double rms = _samples > 0 ? sqrt(sum / _samples) : 0;
  144. double current = _current_factor[channel] * rms;
  145. current = (double) (int(current * _multiplier[channel]) - 1) / _multiplier[channel];
  146. if (current < 0) current = 0;
  147. #if SENSOR_DEBUG
  148. DEBUG_MSG("[EMON] Channel: %d\n", channel);
  149. DEBUG_MSG("[EMON] Total samples: %d\n", _samples);
  150. DEBUG_MSG("[EMON] Total time (ms): %d\n", time_span);
  151. DEBUG_MSG("[EMON] Sample frequency (Hz): %d\n", int(1000 * _samples / time_span));
  152. DEBUG_MSG("[EMON] Max value: %d\n", max);
  153. DEBUG_MSG("[EMON] Min value: %d\n", min);
  154. DEBUG_MSG("[EMON] Midpoint value: %d\n", int(_pivot[channel]));
  155. DEBUG_MSG("[EMON] RMS value: %d\n", int(rms));
  156. DEBUG_MSG("[EMON] Current (mA): %d\n", int(current));
  157. #endif
  158. // Check timing
  159. if ((time_span > EMON_MAX_TIME)
  160. || ((time_span < EMON_MAX_TIME) && (_samples < EMON_MAX_SAMPLES))) {
  161. _samples = (_samples * EMON_MAX_TIME) / time_span;
  162. }
  163. return current;
  164. }
  165. unsigned char _channels = 0; // Number of ADC channels available
  166. unsigned char _magnitudes = 0; // Number of magnitudes per channel
  167. unsigned long _samples = EMON_MAX_SAMPLES; // Samples (dynamically modificable)
  168. unsigned char _resolution = 10; // ADC resolution in bits
  169. unsigned long _adc_counts; // Max count
  170. double _voltage = EMON_MAINS_VOLTAGE; // Mains voltage
  171. double _reference = EMON_REFERENCE_VOLTAGE; // ADC reference voltage (100%)
  172. double * _current_ratio; // Ratio ampers in main loop to voltage in secondary (per channel)
  173. double * _current_factor; // Calculated, reads (RMS) to current (per channel)
  174. uint16_t * _multiplier; // Calculated, error (per channel)
  175. double * _pivot; // Moving average mid point (per channel)
  176. double * _current; // Last current reading (per channel)
  177. #if EMON_REPORT_ENERGY
  178. uint32_t * _energy; // Aggregated energy (per channel)
  179. #endif
  180. };
  181. #endif // SENSOR_SUPPORT