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.

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