BoeBot.java 6.8 KB

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