Advertisement
sebyvs

Median filter

Mar 23rd, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. #include "asw_lf.h"
  2. #include "rte.h"
  3.  
  4. #define AVERAGE_WINDOW 4
  5.  
  6. void ASW_vfollowLine(){
  7. // Meadian filter on last N elements
  8. // Hope this shit works, otherwise I'm gonna cut my penis
  9. static T_F16 values[AVERAGE_WINDOW] = { [0 ... (AVERAGE_WINDOW - 1)] = 3.5 };
  10. static int pos = 0;
  11.  
  12. RTE_vMotorSetDir(FORWARD);
  13. RTE_vMotorSetSpeed(30);
  14. RTE_vMotorSetSpeed(90);
  15. T_U16 lf = RTE_vGetLineFollower();
  16. T_F16 position = 3.0 - getDirectionFromLF(lf);
  17. T_F16 dir = getDirectionFromLF(lf);
  18. // If we lose signal, keep last known position
  19. if(lf != -1){
  20. RTE_vServoSetPosition(90 + position * 10.0);
  21. if(dir != -1){
  22. T_F16 position = (T_F16)3.5 - dir;
  23. pos++;
  24. pos %= AVERAGE_WINDOW;
  25. values[pos] = position;
  26.  
  27. T_F16 mean = 0;
  28. int i = 0;
  29. for(i = 0; i < AVERAGE_WINDOW; ++i){
  30. mean += values[i];
  31. }
  32. mean /= AVERAGE_WINDOW;
  33.  
  34. RTE_vServoSetPosition(90 + mean * 20.0);
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement