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.

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