Advertisement
unCoderSc

ccc

Nov 20th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. global key
  2. InitKeyboard();
  3. state = 0;
  4. timerVal = 0;
  5. turnCount = 1;
  6. rtSpeed = 0.78;
  7. control = false;
  8. speed = 45;
  9. lastDist = 0;
  10. incDec = 0;
  11.  
  12. % Placeholder for timer.
  13. while 1
  14. pause(0.1);
  15.  
  16.  
  17. if(key == 'q')
  18. brick.StopAllMotors();
  19. break;
  20. end
  21.  
  22. if(key == 'c')
  23. brick.MoveMotorAngleRel('AD', 0, 0, 'Brake');
  24. brick.MoveMotorAngleRel('AD', 0, 0, 'Coast');
  25. control = true;
  26. end
  27.  
  28. if control == true
  29. if(key == '1') % white claw B
  30. brick.MoveMotorAngleRel('B', -40, 30, 'Coast');
  31. end
  32.  
  33.  
  34. if(key == '3') % other claw C
  35. brick.MoveMotorAngleRel('C', -40, 30, 'Coast');
  36. end
  37.  
  38. if(key == '2') % white claw B
  39. brick.MoveMotorAngleRel('B', 40, 30, 'Coast');
  40. end
  41.  
  42.  
  43. if(key == '4') % other claw C
  44. brick.MoveMotorAngleRel('C', 40, 30, 'Coast');
  45. end
  46.  
  47. if(key == 'x') % end control
  48. control = false;
  49. end
  50.  
  51.  
  52. switch key
  53. case 'uparrow'
  54. brick.MoveMotor('AD', speed);
  55.  
  56. case 'downarrow'
  57. brick.MoveMotor('AD', -speed-15);
  58.  
  59. case 'leftarrow'
  60. brick.MoveMotor('A', -speed);
  61. brick.MoveMotor('D', speed);
  62.  
  63.  
  64. case 'rightarrow'
  65. brick.MoveMotor('A', speed);
  66. brick.MoveMotor('D', -speed);
  67. case 0
  68. brick.MoveMotorAngleRel('AD', 0, 0, 'Brake');
  69. brick.MoveMotorAngleRel('AD', 0, 0, 'Coast');
  70. end
  71.  
  72.  
  73.  
  74. else
  75.  
  76. switch state
  77. case 0
  78. % Move Forward until Bumped
  79. curr = brick.UltrasonicDist(1);
  80. if curr < lastDist
  81. incDec = 7.5;
  82. else
  83. incDec = -7.5;
  84. end
  85.  
  86. brick.ResetMotorAngle('AD');
  87. brick.MoveMotor('A', 85);
  88. brick.MoveMotor('D', 85+(incDec));
  89.  
  90. % color
  91. % color = brick.ColorColor(3);
  92. % if color == 4 % yellow
  93. % brick.MoveMotorAngleRel('AD', 0, 0, 'Brake');
  94. % brick.MoveMotorAngleRel('AD', 0, 0, 'Coast');
  95. % pause(3);
  96. % end
  97.  
  98.  
  99. % Output Action
  100. if brick.TouchPressed(4)
  101. % State Transition
  102. brick.MoveMotor('AD', 0);
  103. state = 1;
  104. timerVal = tic;
  105. % start timer.
  106. end
  107.  
  108. lastDist = brick.UltrasonicDist(1);
  109. if lastDist > 60.0
  110. state = 3;
  111. timerVal = tic;
  112. end
  113.  
  114.  
  115. case 1
  116. %Bumped, Reverse for a time.
  117. % Output Action
  118. brick.MoveMotor('AD', -100);
  119. if(toc(timerVal) > 0.50) % 0.57
  120. % State Transition after 1 seconds
  121. state = 2;
  122. timerVal = tic;
  123. % Reset Timer
  124. end
  125. case 2
  126. % Done Reversing, turn left
  127. brick.MoveMotor('A', -50);
  128. brick.MoveMotor('D', 50);
  129. if(toc(timerVal) > 0.8) % 0.42
  130. % State Transition after 0.5 seconds
  131. state = 0;
  132. end
  133.  
  134. case 3
  135.  
  136. % Done detecting, turn right
  137. brick.MoveMotor('A', 50);
  138. brick.MoveMotor('D', -50);
  139. % if turnCount == 3
  140. % rtSpeed = 0.55;
  141. % end
  142. if(toc(timerVal) > 0.8)
  143. % State Transition after 0.5 seconds
  144. turnCount = turnCount +1;
  145. state = 4;
  146. end
  147. case 4
  148. % move for few seconds after turning right
  149. brick.MoveMotor('AD', 85);
  150. if(toc(timerVal) > 2)
  151. % State Transition after 0.5 seconds
  152. state = 0;
  153. end
  154.  
  155.  
  156. end
  157.  
  158. end
  159.  
  160. end
  161. CloseKeyboard();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement