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.

131 lines
4.2 KiB

  1. // -----------------------------------------------------------------------------
  2. // Eergy monitor sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. #define EMON_DEBUG 0
  8. class EmonSensor : public BaseSensor {
  9. public:
  10. EmonSensor(double voltage, unsigned char bits, double ref, double ratio): BaseSensor() {
  11. // Cache
  12. _voltage = voltage;
  13. _adc_counts = 1 << bits;
  14. #if EMON_REPORT_CURRENT
  15. ++_magnitudes;
  16. #endif
  17. #if EMON_REPORT_POWER
  18. ++_magnitudes;
  19. #endif
  20. #if EMON_REPORT_ENERGY
  21. ++_magnitudes;
  22. #endif
  23. // Calculate factor
  24. _current_factor = ratio * ref / _adc_counts;
  25. // Calculate multiplier
  26. calculateMultiplier();
  27. #if EMON_DEBUG
  28. Serial.print("[EMON] Current ratio: "); Serial.println(ratio);
  29. Serial.print("[EMON] Ref. Voltage: "); Serial.println(ref);
  30. Serial.print("[EMON] ADC Counts: "); Serial.println(_adc_counts);
  31. Serial.print("[EMON] Current factor: "); Serial.println(_current_factor);
  32. Serial.print("[EMON] Multiplier: "); Serial.println(_multiplier);
  33. #endif
  34. }
  35. protected:
  36. virtual unsigned int readADC(unsigned char channel) {}
  37. void calculateMultiplier() {
  38. unsigned int s = 1;
  39. unsigned int i = 1;
  40. unsigned int m = s * i;
  41. while (m * _current_factor < 1) {
  42. _multiplier = m;
  43. i = (i == 1) ? 2 : (i == 2) ? 5 : 1;
  44. if (i == 1) s *= 10;
  45. m = s * i;
  46. }
  47. }
  48. double read(unsigned char channel, double &pivot) {
  49. int sample;
  50. int max = 0;
  51. int min = _adc_counts;
  52. double filtered;
  53. double sum = 0;
  54. unsigned long time_span = millis();
  55. for (unsigned long i=0; i<_samples; i++) {
  56. // Read analog value
  57. sample = readADC(channel);
  58. if (sample > max) max = sample;
  59. if (sample < min) min = sample;
  60. // Digital low pass filter extracts the VDC offset
  61. pivot = (pivot + (sample - pivot) / EMON_FILTER_SPEED);
  62. filtered = sample - pivot;
  63. // Root-mean-square method
  64. sum += (filtered * filtered);
  65. }
  66. time_span = millis() - time_span;
  67. // Quick fix
  68. if (pivot < min || max < pivot) {
  69. pivot = (max + min) / 2.0;
  70. }
  71. // Calculate current
  72. double rms = _samples > 0 ? sqrt(sum / _samples) : 0;
  73. double current = _current_factor * rms;
  74. current = (double) (int(current * _multiplier) - 1) / _multiplier;
  75. if (current < 0) current = 0;
  76. #if EMON_DEBUG
  77. Serial.print("[EMON] Total samples: "); Serial.println(_samples);
  78. Serial.print("[EMON] Total time (ms): "); Serial.println(time_span);
  79. Serial.print("[EMON] Sample frequency (Hz): "); Serial.println(1000 * _samples / time_span);
  80. Serial.print("[EMON] Max value: "); Serial.println(max);
  81. Serial.print("[EMON] Min value: "); Serial.println(min);
  82. Serial.print("[EMON] Midpoint value: "); Serial.println(pivot);
  83. Serial.print("[EMON] RMS value: "); Serial.println(rms);
  84. Serial.print("[EMON] Current: "); Serial.println(current);
  85. #endif
  86. // Check timing
  87. if ((time_span > EMON_MAX_TIME)
  88. || ((time_span < EMON_MAX_TIME) && (_samples < EMON_MAX_SAMPLES))) {
  89. _samples = (_samples * EMON_MAX_TIME) / time_span;
  90. }
  91. return current;
  92. }
  93. double _voltage;
  94. unsigned char _magnitudes = 0;
  95. unsigned long _adc_counts;
  96. unsigned int _multiplier = 1;
  97. double _current_factor;
  98. unsigned long _samples = EMON_MAX_SAMPLES;
  99. };