Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1.  
  2.  
  3. int wallfound;
  4.  
  5.  
  6. wallfound=1;
  7.  
  8. configureCollisionDetection(1, 20.0, 50.0, 20.0, 50.0, 0.5);
  9.  
  10. float _sqrt(float value)
  11. {
  12. if (value <= 0)
  13. {
  14. return 0;
  15. }
  16. return value ^ 0.5;
  17. }
  18.  
  19. float asinInDegrees(float x)
  20. {
  21. float negate = 0;
  22. if (x < 0)
  23. {
  24. x = x*-1;
  25. negate = 1;
  26. }
  27. float pi = 3.14159265;
  28. float radians = -0.0187293;
  29. radians = radians * x;
  30. radians = radians + 0.0742610;
  31. radians = radians * x;
  32. radians = radians - 0.2121144;
  33. radians = radians * x;
  34. radians = radians + 1.5707288;
  35. radians = pi * 0.5 - ((1.0 - x) ^ 0.5) * radians;
  36. radians = radians - 2 * negate * radians;
  37. return radians * 180 / pi;
  38. }
  39.  
  40. void _doRoll(float duration, float speed, float direction)
  41. {
  42. if (duration > 0)
  43. {
  44. controlSystemTargetYaw = direction;
  45. controlSystemTargetSpeed = speed;
  46.  
  47. float finishTime = currentRobotTime + duration;
  48. while (currentRobotTime < finishTime)
  49. {
  50. wait;
  51. }
  52. }
  53. controlSystemTargetSpeed = 0;
  54. }
  55.  
  56. void find_wall()
  57. {
  58. //reset direction
  59.  
  60. controlSystemTargetYaw = 0;
  61. setCalibration = 0;
  62.  
  63. //move until collision
  64. controlSystemTargetSpeed = 100;
  65.  
  66. //find collision orientation
  67. //reset heading
  68. }
  69.  
  70. void find_orientation()
  71. {
  72. setRgbLed(0,255,0);
  73. //current position
  74. float startX = locatorPositionX;
  75. float startY = locatorPositionY;
  76.  
  77. //move heading 0
  78. _doRoll(1,20,0);
  79.  
  80. //new position
  81. float endX = locatorPositionX;
  82. float endY = locatorPositionY;
  83.  
  84. //calculate deviation from calibration
  85. float offsetAngle = 0;
  86. float deltaX = endX - startX;
  87. float deltaY = endY - startY;
  88.  
  89. float deltaHeading = asinInDegrees((deltaY/deltaX)/_sqrt((deltaX^2 + deltaY^2)));
  90. int TEMP = controlSystemTargetYaw - 90 + deltaHeading;
  91. setCalibration = TEMP;
  92. setRgbLed(0,255,255);
  93. delay(1);
  94.  
  95. //set calibration
  96. }
  97.  
  98. void change_direction(int change)
  99. {
  100. controlSystemTargetYaw = controlSystemTargetYaw - change;
  101. setCalibration = controlSystemTargetYaw;
  102. }
  103.  
  104. void turn_left()
  105. {
  106. //stop
  107. controlSystemTargetSpeed = 0;
  108. //turn direction
  109. change_direction(-90);
  110. //maybe find wall again
  111. }
  112.  
  113.  
  114. void turn_right() {
  115. //stop
  116. controlSystemTargetSpeed = 0;
  117. //turn direction
  118. change_direction(90);
  119. //find wall again
  120. }
  121.  
  122.  
  123. void handleCollision()
  124. {
  125. setRgbLed(255,0,0);
  126.  
  127. if (wallfound == 1)
  128. {
  129. controlSystemTargetSpeed = 0;
  130.  
  131. wallfound = 0;
  132. delay(3);
  133. find_orientation();
  134.  
  135.  
  136. }
  137. turn_right();
  138. }
  139.  
  140.  
  141. void follow_wall()
  142. {
  143. while (accelSensorXRight > -1)
  144. {
  145. //move on heading -10
  146. controlSystemTargetImuYaw = -10;
  147. controlSystemTargetSpeed = 80;
  148. }
  149.  
  150. turn_left();
  151.  
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158. onCollisionEvent = &handleCollision;
  159.  
  160. void startProgram()
  161. {
  162. find_wall();
  163. while (1)
  164. {
  165. follow_wall();
  166. }
  167.  
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement