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.

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