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.

206 lines
7.6 KiB

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