BoeBot.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. package boebot;
  2. import stamp.core.*;
  3. public class BoeBot{
  4. MotorAansturing motor;
  5. Botsingdetectie botsingdetectie;
  6. FysiekeIndicator fysiekeindicator;
  7. Aansturing aansturing;
  8. Lijnvolger lijnvolger;
  9. char route[];
  10. static int currentCase = 4;
  11. static int currentStep = 0;
  12. public BoeBot(){
  13. motor = new MotorAansturing();
  14. botsingdetectie = new Botsingdetectie();
  15. aansturing = new Aansturing(4,4);
  16. lijnvolger = new Lijnvolger();
  17. fysiekeindicator = new FysiekeIndicator();
  18. motor.setSnelheid(100);
  19. motor.setRichting(true);
  20. route = new char[]{'v','v','v','l','l','r','r','v','r','v', ' '};
  21. for(int i = 0; i < route.length; i++){
  22. System.out.println(route[i]);
  23. }
  24. System.out.println("de huidige stappen:" );
  25. //variabele om functies in en uit te schakelen.
  26. boolean afstandsbediening = true;
  27. boolean obstakelDetectie = false;
  28. boolean routeplanner = true;
  29. boolean sparcours = false;
  30. boolean routeafgerond = true;
  31. while(true){
  32. if(obstakelDetectie){
  33. obstakelDetectie();
  34. }
  35. if(afstandsbediening){
  36. }
  37. if(routeplanner){
  38. if(routeafgerond){
  39. routeafgerond = routeplanner();
  40. }
  41. }
  42. if(sparcours){
  43. }
  44. }
  45. }
  46. public void obstakelDetectie(){
  47. if(botsingdetectie.detectEdge()){
  48. CPU.writePin(CPU.pin1,true);
  49. motor.noodStop();
  50. motor.setRichting(false);
  51. motor.start();
  52. CPU.delay(2000);
  53. motor.noodStop();
  54. motor.turn(180);
  55. motor.setRichting(true);
  56. motor.start();
  57. }else if(botsingdetectie.detectObject() == 1){
  58. motor.noodStop();
  59. motor.setRichting(false);
  60. motor.start();
  61. CPU.delay(2000);
  62. motor.noodStop();
  63. motor.turn(90);
  64. motor.setRichting(true);
  65. motor.start();
  66. }else if(botsingdetectie.detectObject() == 2){
  67. motor.noodStop();
  68. motor.setRichting(false);
  69. motor.start();
  70. CPU.delay(2000);
  71. motor.noodStop();
  72. motor.turn(270);
  73. motor.setRichting(true);
  74. motor.start();
  75. }else if(botsingdetectie.detectObject() == 3){
  76. motor.noodStop();
  77. motor.setRichting(false);
  78. motor.start();
  79. CPU.delay(2000);
  80. motor.noodStop();
  81. motor.turn(180);
  82. motor.setRichting(true);
  83. motor.start();
  84. }
  85. }
  86. public boolean routeplanner(){
  87. if(currentStep == 0){
  88. verwerkAansturing(route[currentStep]);
  89. }
  90. int getalLijnvolger = lijnvolger.readSensor();
  91. switch(getalLijnvolger){
  92. case 0:
  93. if(currentCase != 0){
  94. motor.setSnelheid(100);
  95. motor.setRichting(true);
  96. motor.rijden();
  97. currentCase = 0;
  98. }
  99. break;
  100. case 1:
  101. if(currentCase != 1){
  102. motor.setSnelheidL(10);
  103. motor.setSnelheidR(100);
  104. motor.setRichting(true);
  105. motor.rijden();
  106. currentCase = 1;
  107. }
  108. break;
  109. case 2:
  110. if(currentCase != 2){
  111. motor.setSnelheidL(100);
  112. motor.setSnelheidR(10);
  113. motor.setRichting(true);
  114. motor.rijden();
  115. currentCase = 2;
  116. }
  117. break;
  118. case 4:
  119. if(currentCase != 4){
  120. if(route[currentStep] != ' '){
  121. verwerkAansturing(route[currentStep]);
  122. System.out.println(route[currentStep-1]);
  123. CPU.delay(1000);
  124. }else{
  125. motor.noodStop();
  126. System.out.println("klaar");
  127. return false;
  128. }
  129. currentCase = 4;
  130. }
  131. break;
  132. case 5:
  133. if(currentCase != 5){
  134. motor.noodStop();
  135. System.out.println("Gat!!");
  136. currentCase = 5;
  137. return false;
  138. }
  139. default:
  140. // motor.noodStop();
  141. }
  142. return true;
  143. }
  144. public void verwerkAansturing(char opdracht){
  145. switch(opdracht){
  146. case 'v':
  147. motor.rijden();
  148. CPU.delay(200);
  149. break;
  150. case 'a':
  151. CPU.delay(1200);
  152. motor.noodStop();
  153. CPU.delay(200);
  154. motor.turn(180);
  155. break;
  156. case 'r':
  157. motor.setSnelheid(100);
  158. motor.setRichting(true);
  159. motor.rijden();
  160. CPU.delay(1200);
  161. motor.noodStop();
  162. CPU.delay(200);
  163. motor.turn(90);
  164. break;
  165. case 'l':
  166. motor.setSnelheid(100);
  167. motor.setRichting(true);
  168. motor.rijden();
  169. CPU.delay(1200);
  170. motor.noodStop();
  171. CPU.delay(200);
  172. motor.turn(270);
  173. break;
  174. default:
  175. motor.noodStop();
  176. break;
  177. }
  178. currentStep++;
  179. }
  180. }