Advertisement
Guest User

Untitled

a guest
Jan 29th, 2018
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. // This is a component for EMC2 HAL
  2. // Copyright 2006 Jeff Epler <jepler@unpythonic.net>
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; either version 2 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. component rollingavg "calculate simple rolling average of up to 200 samples";
  18.  
  19. pin in float in;
  20. pin out float out;
  21. pin in unsigned depth=1;
  22.  
  23. variable double avgarray[199];
  24. variable int placeholder;
  25.  
  26.  
  27. function _;
  28. license "GPL";
  29. ;;
  30. FUNCTION(_) {
  31.  
  32.  
  33. double avgtotal = 0 ;
  34. int i = 0;
  35. int idepth = 0;
  36. idepth=depth;
  37.  
  38. if (idepth > 200) idepth=200;
  39.  
  40. avgarray[placeholder++]=in;
  41.  
  42. if (placeholder >= idepth) placeholder=0;
  43.  
  44. for(i=0;i<idepth;i++){
  45. avgtotal=avgtotal + avgarray[i];
  46. }
  47.  
  48. if (idepth!=0) out = avgtotal/idepth;
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement