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.

257 lines
8.8 KiB

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