Download the file
  1. #include "current.h"
  2.  
  3. #define ALPHA 0.996
  4. #define NB_SAMPLES 3000
  5. #define BURDEN 68
  6. #define NB_TURNS 1012
  7. #define CALIBRATION 1.11
  8. #define RATIO (NB_TURNS / BURDEN * 3.3 / 1024)
  9.  
  10. Current::Current():
  11. port(SENSORS_PORT) { }
  12.  
  13. uint16_t Current::get_value() {
  14. float x=0.0, y=0.0, old_x, old_y, sum=0;
  15. // First, let the filter stabilize
  16. for (uint16_t i=0; i<NB_SAMPLES; i++) {
  17. old_y = y;
  18. old_x = x;
  19. x = port.anaRead();
  20. y = ALPHA * (old_y + x - old_x);
  21. }
  22. // Then take a sample
  23. for (uint16_t i=0; i<NB_SAMPLES; i++) {
  24. old_y = y;
  25. old_x = x;
  26. x = port.anaRead();
  27. y = ALPHA * (old_y + x - old_x);
  28. sum += y*y;
  29. }
  30. float Irms = CALIBRATION * RATIO * sqrt(sum/NB_SAMPLES)*1000;
  31. uint16_t Irms_int = (uint16_t)Irms;
  32. return Irms_int;
  33. }
  34.  
  35. // vim:ft=arduino
  36.