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.

125 lines
3.9 KiB

  1. // -----------------------------------------------------------------------------
  2. // Eergy monitor sensor
  3. // -----------------------------------------------------------------------------
  4. #pragma once
  5. #include "Arduino.h"
  6. #include "BaseSensor.h"
  7. #define EMON_DEBUG 1
  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. _pivot = _adc_counts >> 1;
  15. _count = 2;
  16. // Calculate factor
  17. _current_factor = ratio * ref / _adc_counts;
  18. // Calculate multiplier
  19. calculateMultiplier();
  20. #if EMON_DEBUG
  21. Serial.print("[EMON] Current ratio: "); Serial.println(ratio);
  22. Serial.print("[EMON] Ref. Voltage: "); Serial.println(_voltage);
  23. Serial.print("[EMON] ADC Couns: "); Serial.println(_adc_counts);
  24. Serial.print("[EMON] Current factor: "); Serial.println(_current_factor);
  25. Serial.print("[EMON] Multiplier: "); Serial.println(_multiplier);
  26. #endif
  27. }
  28. protected:
  29. virtual unsigned int readADC(unsigned char port) {}
  30. void calculateMultiplier() {
  31. unsigned int s = 1;
  32. unsigned int i = 1;
  33. unsigned int m = s * i;
  34. while (m * _current_factor < 1) {
  35. _multiplier = m;
  36. i = (i == 1) ? 2 : (i == 2) ? 5 : 1;
  37. if (i == 1) s *= 10;
  38. m = s * i;
  39. }
  40. }
  41. double read(unsigned long value, unsigned char mode, unsigned char port) {
  42. int sample;
  43. int max = 0;
  44. int min = _adc_counts;
  45. double filtered;
  46. double sum = 0;
  47. unsigned long start = millis();
  48. unsigned long samples = 0;
  49. while (true) {
  50. // Read analog value
  51. sample = readADC(port);
  52. if (sample > max) max = sample;
  53. if (sample < min) min = sample;
  54. // Digital low pass filter extracts the VDC offset
  55. _pivot = (_pivot + (sample - _pivot) / EMON_FILTER_SPEED);
  56. filtered = sample - _pivot;
  57. // Root-mean-square method
  58. sum += (filtered * filtered);
  59. ++samples;
  60. // Exit condition
  61. if (mode == EMON_MODE_SAMPLES) {
  62. if (samples >= value) break;
  63. } else {
  64. if (millis() - start >= value) break;
  65. }
  66. yield();
  67. }
  68. // Quick fix
  69. if (_pivot < min || max < _pivot) {
  70. _pivot = (max + min) / 2.0;
  71. }
  72. double rms = samples > 0 ? sqrt(sum / samples) : 0;
  73. double current = _current_factor * rms;
  74. current = (double) (round(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(millis() - start);
  79. Serial.print("[EMON] Sample frequency (1/s): "); Serial.println(1000 * samples / (millis() - start));
  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. return current;
  87. }
  88. double _voltage;
  89. unsigned int _adc_counts;
  90. unsigned int _multiplier = 1;
  91. double _current_factor;
  92. double _pivot;
  93. };