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.

245 lines
8.3 KiB

7 years ago
  1. // -----------------------------------------------------------------------------
  2. // Abstract Energy Monitor Sensor (other EMON sensors extend this class)
  3. // Copyright (C) 2017-2019 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 "../debug.h"
  9. #include "BaseEmonSensor.h"
  10. #include "I2CSensor.h"
  11. extern "C" {
  12. #include "../libs/fs_math.h"
  13. }
  14. class EmonSensor : public I2CSensor<BaseEmonSensor> {
  15. public:
  16. // ---------------------------------------------------------------------
  17. // Public
  18. // ---------------------------------------------------------------------
  19. EmonSensor() {
  20. // Calculate # of magnitudes
  21. #if EMON_REPORT_CURRENT
  22. ++_magnitudes;
  23. #endif
  24. #if EMON_REPORT_POWER
  25. ++_magnitudes;
  26. #endif
  27. #if EMON_REPORT_ENERGY
  28. ++_magnitudes;
  29. #endif
  30. }
  31. void expectedPower(unsigned char channel, unsigned int expected) {
  32. if (channel >= _channels) return;
  33. unsigned int actual = _current[channel] * _voltage;
  34. if (actual == 0) return;
  35. if (expected == actual) return;
  36. _current_ratio[channel] = _current_ratio[channel] * ((double) expected / (double) actual);
  37. calculateFactors(channel);
  38. _dirty = true;
  39. }
  40. // ---------------------------------------------------------------------
  41. void setVoltage(double voltage) {
  42. if (_voltage == voltage) return;
  43. _voltage = voltage;
  44. _dirty = true;
  45. }
  46. void setReference(double reference) {
  47. if (_reference == reference) return;
  48. _reference = reference;
  49. _dirty = true;
  50. }
  51. void setCurrentRatio(unsigned char channel, double current_ratio) {
  52. if (channel >= _channels) return;
  53. if (_current_ratio[channel] == current_ratio) return;
  54. _current_ratio[channel] = current_ratio;
  55. calculateFactors(channel);
  56. _dirty = true;
  57. }
  58. void resetRatios() {
  59. setCurrentRatio(0, EMON_CURRENT_RATIO);
  60. }
  61. // ---------------------------------------------------------------------
  62. double getVoltage() {
  63. return _voltage;
  64. }
  65. double getReference() {
  66. return _reference;
  67. }
  68. double getCurrentRatio(unsigned char channel) {
  69. if (channel >= _channels) return 0;
  70. return _current_ratio[channel];
  71. }
  72. unsigned char getChannels() {
  73. return _channels;
  74. }
  75. // ---------------------------------------------------------------------
  76. // Sensor API
  77. // ---------------------------------------------------------------------
  78. void begin() {
  79. // Resolution
  80. _adc_counts = 1 << _resolution;
  81. // Calculations
  82. for (unsigned char i=0; i<_channels; i++) {
  83. _energy[i] = _current[i] = 0.0;
  84. _pivot[i] = _adc_counts >> 1;
  85. calculateFactors(i);
  86. }
  87. #if SENSOR_DEBUG
  88. DEBUG_MSG("[EMON] Reference (mV): %d\n", int(1000 * _reference));
  89. DEBUG_MSG("[EMON] ADC counts: %d\n", _adc_counts);
  90. for (unsigned char i=0; i<_channels; i++) {
  91. DEBUG_MSG("[EMON] Channel #%d current ratio (mA/V): %d\n", i, int(1000 * _current_ratio[i]));
  92. DEBUG_MSG("[EMON] Channel #%d current factor (mA/bit): %d\n", i, int(1000 * _current_factor[i]));
  93. DEBUG_MSG("[EMON] Channel #%d Multiplier: %d\n", i, int(_multiplier[i]));
  94. }
  95. #endif
  96. _ready = true;
  97. _dirty = false;
  98. }
  99. protected:
  100. // ---------------------------------------------------------------------
  101. // Protected
  102. // ---------------------------------------------------------------------
  103. // Initializes internal variables
  104. void init() {
  105. _current_ratio = new double[_channels];
  106. _current_factor = new double[_channels];
  107. _multiplier = new uint16_t[_channels];
  108. _pivot = new double[_channels];
  109. _current = new double[_channels];
  110. }
  111. virtual unsigned int readADC(unsigned char channel) = 0;
  112. void calculateFactors(unsigned char channel) {
  113. _current_factor[channel] = _current_ratio[channel] * _reference / _adc_counts;
  114. unsigned int s = 1;
  115. unsigned int i = 1;
  116. unsigned int m = 1;
  117. unsigned int multiplier = 1;
  118. while (m * _current_factor[channel] < 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. _multiplier[channel] = 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 ? fs_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(1000 * 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. };
  185. #endif // SENSOR_SUPPORT