BoeBot.java 8.6 KB

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