BoeBot.java 8.5 KB

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