Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. #include <ctime>
  2. #include <cmath>
  3. #include <deque>
  4. #include <kovan/motor.hpp>
  5.  
  6. #define LMOTOR 0
  7. #define RMOTOR 1
  8. #define TOPHAT 6
  9. #define GRAYLOW 256
  10. #define GRAYHIGH 767
  11. #define WIDTH 250
  12. //#define OFFSET .5
  13. #define BIAS 5
  14. #define ACC 3
  15. #define PREC 5
  16. #define DEBUG_LINEFOLLOW 1
  17.  
  18. bool lineFollow(int distance, int speed)
  19. {
  20. Motor lmotor(LMOTOR);
  21. Motor rmotor(RMOTOR);
  22. Analog irsens(TOPHAT);
  23. int timeorig=std::time(NULL), counter=0, turn=0, tmptime=seconds() * 1000;
  24. std::deque<double> alpha(ACC);
  25. double theta, omega, timetotal=distance/speed, tmpa=0;
  26. lmotor.MoveAtVelocity(speed);
  27. while(seconds()-timeorig <= timetotal)
  28. {
  29. if(counter%PREC==0)
  30. {
  31. counter=0;
  32. if(irsens.value()>GRAYHIGH)
  33. {
  34. for(std::deque<double>::iterator i=alpha.begin(); i!=alpha.end() && i<alpha.end(); i++)
  35. {
  36. tmpa += *i;
  37. }
  38. alpha.pop_front();
  39. alpha.push_back(tmpa/ACC+0.25);
  40. tmpa=0;
  41. }
  42. else if(irsens.value()<GRAYLOW)
  43. {
  44. for(std::deque<double>::iterator i=alpha.begin(); i!=alpha.end() && i<alpha.end(); i++)
  45. {
  46. tmpa += *i;
  47. }
  48. alpha.pop_front();
  49. alpha.push_back(tmpa/ACC-0.25);
  50. tmpa=0;
  51. }
  52. else
  53. {
  54. theta=0;
  55. turn=0;
  56. }
  57. omega += alpha.back();
  58. if (omega>0)
  59. {
  60. rmotor.MoveAtVelocity(speed-WIDTH*tan(omega));
  61. lmotor.MoveAtVelocity(speed+BIAS);
  62. }
  63. else
  64. {
  65. lmotor.MoveAtVelocity(speed-WIDTH*tan(-1*omega)
  66. rmotor.MoveAtVelocity(speed+BIAS);
  67. }
  68. omega>0 ? rmotor.MoveAtVelocity(speed-WIDTH*tan(omega)) : lmotor.MoveAtVelocity(speed-WIDTH*tan(-1*omega));
  69. theta += omega.back()*(seconds() * 1000-tmptime);
  70. tmptime=seconds() * 1000.0;
  71. }
  72. switch(turn)
  73. {
  74. case 0:
  75. if(theta > 15)
  76. {
  77. alpha.pop_front();
  78. alpha.push_back(alpha.back()*-1);
  79. turn++;
  80. }
  81. break;
  82. case 1:
  83. if(theta < -15)
  84. {
  85. alpha.pop_front();
  86. alpha.push_back(alpha.back()+2);
  87. turn++;
  88. }
  89. break;
  90. case 2:
  91. if (theta < -170)
  92. {
  93. alpha.pop_front();
  94. alpha.push_back(alpha.back()*-1);
  95. turn++;
  96. }
  97. break;
  98. case 3:
  99. if (theta > 170)
  100. {
  101. return 0;
  102. }
  103. }
  104. counter++;
  105. if (seconds() * 1000.0 >= tmptime)
  106. {
  107. theta+=omega.back();
  108. }
  109. msleep(5);
  110. }
  111. return 1;
  112. }
  113. #ifdef DEBUG_LINEFOLLOW
  114. int main()
  115. {
  116. Motor lmotor(LMOTOR);
  117. Motor rmotor(RMOTOR);
  118. lmotor.MoveRelativePosition(100, 200);
  119. rmotor.MoveRelativePosition(500, 1000);
  120. lmotor.BlockMotorDone();
  121. return lineFollow(2000, 500);
  122. }
  123. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement