Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. // Zawartosc pliku main.c
  2. #include "sumolib.h"
  3.  
  4. // deklaracje funkcji
  5. void decide();
  6. void forward();
  7. void forward_atack();
  8. void forward_left();
  9. void forward_right();
  10. void backward();
  11. void backward_atack();
  12. void backward_left();
  13. void backward_right();
  14. void stop();
  15. void rotate_left();
  16. void rotate_right();
  17. void countdown();
  18. void timer1();
  19.  
  20. // zmienne globalne
  21. volatile int dist_treshold = 0;
  22. volatile int normal_power = 0;
  23. volatile int dist1_detected = 0;
  24. volatile int atack_power = 0;
  25. volatile int dist2_detected = 0;
  26. volatile int dist3_detected = 0;
  27. volatile int started = 0;
  28.  
  29. int main() {
  30. init();
  31. // Wartości stałe
  32. dist_treshold = 300;
  33. normal_power = 40;
  34. atack_power = 100;
  35. // Planowanie działania timera
  36. timerSchedule(timer1, 1);
  37. // Oczekiwanie na naciśnięcie przycisku i odliczanie
  38. countdown();
  39. // Pętla główna programu
  40. for (;;) {
  41. // Decyzja o podejmowanej akcji
  42. decide();
  43. // Opóźnienie
  44. waitMs(20);
  45. }
  46. }
  47.  
  48. void decide() {
  49. // Funkcja podejmująca decyzję o akcji - algorytm właściwy
  50. // Zmienne pomocnicze
  51. dist1_detected = distValue(DIST1) > dist_treshold;
  52. dist2_detected = distValue(DIST2) > dist_treshold;
  53. dist3_detected = distValue(DIST3) > dist_treshold;
  54. // Najwyższy priorytet mają czujniki podłoża, sprawdzanie czy którykolwiek z nich widzi linię
  55.  
  56. // Żaden czujnik podłoża nie wykrył linii
  57. // Sprawdzanie, czy któryś z czujników odległości widzi przeciwnika
  58. if (dist1_detected || dist2_detected || dist3_detected) {
  59. // Którykolwiek czujnik odległości widzi przeciwnika, sprawdzanie dalej
  60. if (dist1_detected && dist2_detected) {
  61. // Przeciwnik jest na wprost
  62. forward_atack();
  63. }
  64. } else {
  65. // Żaden czujnik nie wykrył przeciwnika ani linii
  66. // Sprawdzanie czy robot wystartował
  67. if (!started) {
  68. // W zależności od stanu przełącznika wybór pierwszego ruchu
  69. if (switchIsOn(SWITCH2)) {
  70. forward_left();
  71. } else {
  72. forward_right();
  73. }
  74. // Zaznaczanie, że robot wystartował
  75. started = 1;
  76. }
  77. }
  78.  
  79. }
  80.  
  81. void forward() {
  82. // Do przodu
  83. motorSetPower(MOTOR1, normal_power);
  84. motorSetPower(MOTOR2, normal_power);
  85. motorForward(MOTOR1);
  86. motorForward(MOTOR2);
  87. ledOn(LED9);
  88. ledOff(LED10);
  89. ledOn(LED11);
  90. }
  91.  
  92. void forward_atack() {
  93. // Do przodu - atak
  94. motorSetPower(MOTOR1, atack_power);
  95. motorSetPower(MOTOR2, atack_power);
  96. motorForward(MOTOR1);
  97. motorForward(MOTOR2);
  98. ledOn(LED9);
  99. ledOff(LED10);
  100. ledOn(LED11);
  101. }
  102.  
  103. void forward_left() {
  104. // Do przodu na lewo
  105. motorSetPower(MOTOR1, normal_power / 3);
  106. motorSetPower(MOTOR2, normal_power);
  107. motorForward(MOTOR1);
  108. motorForward(MOTOR2);
  109. ledOn(LED9);
  110. ledOff(LED10);
  111. ledOff(LED11);
  112. }
  113.  
  114. void forward_right() {
  115. // Do przodu na prawo
  116. motorSetPower(MOTOR1, normal_power);
  117. motorSetPower(MOTOR2, normal_power / 3);
  118. motorForward(MOTOR1);
  119. motorForward(MOTOR2);
  120. ledOff(LED9);
  121. ledOff(LED10);
  122. ledOn(LED11);
  123. }
  124.  
  125. void backward() {
  126. // Do tyłu
  127. motorSetPower(MOTOR1, normal_power);
  128. motorSetPower(MOTOR2, normal_power);
  129. motorBackward(MOTOR1);
  130. motorBackward(MOTOR2);
  131. ledOff(LED9);
  132. ledOn(LED10);
  133. ledOff(LED11);
  134. }
  135.  
  136. void backward_atack() {
  137. // Atak do tyłu
  138. motorSetPower(MOTOR1, atack_power);
  139. motorSetPower(MOTOR2, atack_power);
  140. motorBackward(MOTOR1);
  141. motorBackward(MOTOR2);
  142. ledOff(LED9);
  143. ledOn(LED10);
  144. ledOff(LED11);
  145. }
  146.  
  147. void backward_left() {
  148. // Do tyłu na lewo
  149. motorSetPower(MOTOR1, normal_power / 3);
  150. motorSetPower(MOTOR2, normal_power);
  151. motorBackward(MOTOR1);
  152. motorBackward(MOTOR2);
  153. ledOn(LED9);
  154. ledOff(LED10);
  155. ledOff(LED11);
  156. }
  157.  
  158. void backward_right() {
  159. // Do tyłu na prawo
  160. motorSetPower(MOTOR1, normal_power);
  161. motorSetPower(MOTOR2, normal_power / 3);
  162. motorBackward(MOTOR1);
  163. motorBackward(MOTOR2);
  164. ledOff(LED9);
  165. ledOff(LED10);
  166. ledOn(LED11);
  167. }
  168.  
  169. void stop() {
  170. // Stop
  171. motorStop(MOTOR1);
  172. motorStop(MOTOR2);
  173. ledOff(LED9);
  174. ledOff(LED10);
  175. ledOff(LED11);
  176. }
  177.  
  178. void rotate_left() {
  179. // Obrót w lewo
  180. motorSetPower(MOTOR1, normal_power);
  181. motorSetPower(MOTOR2, normal_power);
  182. motorBackward(MOTOR1);
  183. motorForward(MOTOR2);
  184. ledOn(LED9);
  185. ledOff(LED10);
  186. ledOff(LED11);
  187. }
  188.  
  189. void rotate_right() {
  190. // Obrót w prawo
  191. motorSetPower(MOTOR1, normal_power);
  192. motorSetPower(MOTOR2, normal_power);
  193. motorForward(MOTOR1);
  194. motorBackward(MOTOR2);
  195. ledOff(LED9);
  196. ledOff(LED10);
  197. ledOn(LED11);
  198. }
  199.  
  200. void countdown() {
  201. // Oczekiwanie na naciśnięcie któregoś przycisku
  202. while (!buttonPressed(BUTTON1) && !buttonPressed(BUTTON2)) {
  203. }
  204. // 5...
  205. ledOn(LED9);
  206. ledOff(LED10);
  207. ledOn(LED11);
  208. wait(1);
  209. // 4...
  210. ledOff(LED11);
  211. wait(1);
  212. // 3...
  213. ledOff(LED9);
  214. ledOn(LED10);
  215. ledOn(LED11);
  216. wait(1);
  217. // 2...
  218. ledOff(LED11);
  219. wait(1);
  220. // 1...
  221. ledOff(LED10);
  222. ledOn(LED11);
  223. wait(1);
  224. // Start
  225. ledOn(LED9);
  226. ledOn(LED10);
  227. }
  228.  
  229. void timer1() {
  230. // Miganie diodą co 500 ms
  231. ledSet(LED8, timeInMilis() / 500 % 2);
  232. // Wyświetlenie n diodach stanów czujników
  233. ledSet(LED1, grdDetected(GRD1));
  234. ledSet(LED2, grdDetected(GRD2));
  235. ledSet(LED3, grdDetected(GRD3));
  236. ledSet(LED4, grdDetected(GRD4));
  237. ledSet(LED5, distValue(DIST1) > dist_treshold);
  238. ledSet(LED6, distValue(DIST2) > dist_treshold);
  239. ledSet(LED7, distValue(DIST3) > dist_treshold);
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement