BoeBot.java 8.8 KB

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