BoeBot.java 9.1 KB

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