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.

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