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.

242 lines
8.4 KiB

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