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.

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