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.

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