BoeBot.java 6.7 KB

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