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.

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