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.

159 lines
5.1 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. #define EMON_DEBUG 0
  9. class EmonSensor : public BaseSensor {
  10. public:
  11. // ---------------------------------------------------------------------
  12. // Public
  13. // ---------------------------------------------------------------------
  14. EmonSensor(): BaseSensor() {
  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. _voltage = voltage;
  27. }
  28. void setReference(double ref) {
  29. _reference = ref;
  30. }
  31. void setCurrentRatio(double ratio) {
  32. _current_ratio = ratio;
  33. }
  34. // ---------------------------------------------------------------------
  35. // Sensor API
  36. // ---------------------------------------------------------------------
  37. void begin() {
  38. // Resolution
  39. _adc_counts = 1 << _resolution;
  40. // Calculate factor
  41. _current_factor = _current_ratio * _reference / _adc_counts;
  42. // Calculate multiplier
  43. unsigned int s = 1;
  44. unsigned int i = 1;
  45. unsigned int m = s * i;
  46. while (m * _current_factor < 1) {
  47. _multiplier = m;
  48. i = (i == 1) ? 2 : (i == 2) ? 5 : 1;
  49. if (i == 1) s *= 10;
  50. m = s * i;
  51. }
  52. #if EMON_DEBUG
  53. Serial.print("[EMON] Current ratio: "); Serial.println(ratio);
  54. Serial.print("[EMON] Ref. Voltage: "); Serial.println(ref);
  55. Serial.print("[EMON] ADC Counts: "); Serial.println(_adc_counts);
  56. Serial.print("[EMON] Current factor: "); Serial.println(_current_factor);
  57. Serial.print("[EMON] Multiplier: "); Serial.println(_multiplier);
  58. #endif
  59. }
  60. protected:
  61. // ---------------------------------------------------------------------
  62. // Protected
  63. // ---------------------------------------------------------------------
  64. virtual unsigned int readADC(unsigned char channel) {}
  65. double read(unsigned char channel, double &pivot) {
  66. int sample;
  67. int max = 0;
  68. int min = _adc_counts;
  69. double filtered;
  70. double sum = 0;
  71. unsigned long time_span = millis();
  72. for (unsigned long i=0; i<_samples; i++) {
  73. // Read analog value
  74. sample = readADC(channel);
  75. if (sample > max) max = sample;
  76. if (sample < min) min = sample;
  77. // Digital low pass filter extracts the VDC offset
  78. pivot = (pivot + (sample - pivot) / EMON_FILTER_SPEED);
  79. filtered = sample - pivot;
  80. // Root-mean-square method
  81. sum += (filtered * filtered);
  82. }
  83. time_span = millis() - time_span;
  84. // Quick fix
  85. if (pivot < min || max < pivot) {
  86. pivot = (max + min) / 2.0;
  87. }
  88. // Calculate current
  89. double rms = _samples > 0 ? sqrt(sum / _samples) : 0;
  90. double current = _current_factor * rms;
  91. current = (double) (int(current * _multiplier) - 1) / _multiplier;
  92. if (current < 0) current = 0;
  93. #if EMON_DEBUG
  94. Serial.print("[EMON] Total samples: "); Serial.println(_samples);
  95. Serial.print("[EMON] Total time (ms): "); Serial.println(time_span);
  96. Serial.print("[EMON] Sample frequency (Hz): "); Serial.println(1000 * _samples / time_span);
  97. Serial.print("[EMON] Max value: "); Serial.println(max);
  98. Serial.print("[EMON] Min value: "); Serial.println(min);
  99. Serial.print("[EMON] Midpoint value: "); Serial.println(pivot);
  100. Serial.print("[EMON] RMS value: "); Serial.println(rms);
  101. Serial.print("[EMON] Current: "); Serial.println(current);
  102. #endif
  103. // Check timing
  104. if ((time_span > EMON_MAX_TIME)
  105. || ((time_span < EMON_MAX_TIME) && (_samples < EMON_MAX_SAMPLES))) {
  106. _samples = (_samples * EMON_MAX_TIME) / time_span;
  107. }
  108. return current;
  109. }
  110. unsigned char _magnitudes = 0;
  111. unsigned long _samples = EMON_MAX_SAMPLES;
  112. unsigned int _multiplier = 1;
  113. unsigned char _resolution = 10;
  114. double _voltage = EMON_MAINS_VOLTAGE;
  115. double _reference = EMON_REFERENCE_VOLTAGE;
  116. double _current_ratio = EMON_CURRENT_RATIO;
  117. unsigned long _adc_counts;
  118. double _current_factor;
  119. };