Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. #include "simpletools.h" // Library includes
  2. #include "abdrive.h"
  3. #include <stdbool.h>
  4.  
  5. #define DIM_SQUARES 16
  6. #define DIS_INITIAL 146
  7. #define DIS_UNIT 125
  8. #define STACK_SIZE DIM_SQUARES * DIM_SQUARES
  9. #define IR_DISTANCE_THRESHOLD 39
  10.  
  11. typedef enum {UP, DOWN, LEFT, RIGHT, NONE} DIRECTION;
  12. typedef struct STACK {
  13. int stk[STACK_SIZE];
  14. int top;
  15. } STACK;
  16.  
  17. STACK s;
  18. bool c[DIM_SQUARES][DIM_SQUARES];
  19. bool v[DIM_SQUARES];
  20.  
  21. int distance_ir_left() {
  22. int left = 0;
  23.  
  24. for(int dacVal = 0; dacVal < 160; dacVal += 4) {
  25. dac_ctr(26, 0, dacVal);
  26. freqout(11, 1, 38000);
  27. left += input(10);
  28. }
  29.  
  30. return left;
  31. }
  32.  
  33. int distance_ir_right() {
  34. int right;
  35.  
  36. for(int dacVal = 0; dacVal < 160; dacVal += 4) {
  37. dac_ctr(27, 1, dacVal);
  38. freqout(1, 1, 38000);
  39. right += input(2);
  40. }
  41.  
  42. return right;
  43. }
  44.  
  45. int direction_from(int current, DIRECTION direction) { // je suis dans une case , dependant de ma direction elle me donne le numero de la case
  46. // Value decomposition.
  47. int x = current % 4;
  48. int y = (current - x) / 4;
  49.  
  50. if(direction == UP) y += 1;
  51. if(direction == DOWN) y -= 1;
  52. if(direction == LEFT) x -= 1;
  53. if(direction == RIGHT) x += 1;
  54.  
  55. current = y * 4 + x; // Value recomposition.
  56.  
  57. if(current >= DIM_SQUARES || current < 0) return -1;
  58. return current;
  59. }
  60.  
  61. DIRECTION direction_between(int source, int destination) {
  62. int source_x = source % 4;
  63. int source_y = (source - source_x) / 4;
  64.  
  65. int destination_x = destination % 4;
  66. int destination_y = (destination - destination_x) / 4;
  67.  
  68. int delta_x = source_x - destination_x;
  69. int delta_y = source_y - destination_y;
  70.  
  71. if(delta_x == 1) return LEFT;
  72. if(delta_x == -1) return RIGHT;
  73. if(delta_y == 1) return DOWN;
  74. if(delta_y == -1) return UP;
  75. }
  76.  
  77. void stack_push(int num) {
  78. if (s.top == (STACK_SIZE - 1)) {
  79. printf ("Stack is full.\n");
  80. return;
  81. }
  82.  
  83. s.top = s.top + 1;
  84. s.stk[s.top] = num;
  85. }
  86.  
  87. int stack_pop() {
  88. if (s.top == - 1) {
  89. printf ("Stack is empty.\n");
  90. return 0;
  91. }
  92.  
  93. int num = s.stk[s.top];
  94. s.top -= 1;
  95. return num;
  96. }
  97.  
  98. int stack_top() {
  99. if (s.top == - 1) {
  100. printf ("Stack is empty.\n");
  101. return 0;
  102. }
  103.  
  104. int num = s.stk[s.top];
  105. return num;
  106. }
  107.  
  108. void move_initial() {
  109. drive_goto(DIS_INITIAL, DIS_INITIAL);
  110. }
  111.  
  112. void move_up() {
  113. drive_goto(DIS_UNIT, DIS_UNIT);
  114. }
  115.  
  116. void move_down() {
  117. drive_goto(-DIS_UNIT, -DIS_UNIT);
  118. }
  119.  
  120. void move_left() {
  121. drive_goto(-26, 25);
  122. drive_goto(DIS_UNIT, DIS_UNIT);
  123. drive_goto(25, -26);
  124. }
  125.  
  126. void move_right() {
  127. drive_goto(25, -26);
  128. drive_goto(DIS_UNIT, DIS_UNIT);
  129. drive_goto(-26, 25);
  130. }
  131.  
  132. int traverse() {
  133. int current = stack_top();
  134. if(current == 0 && v[current]) return true;
  135.  
  136. int id_left = direction_from(current, LEFT);
  137. int id_right = direction_from(current, RIGHT);
  138. int id_up = direction_from(current, UP);
  139. int id_down = direction_from(current, DOWN);
  140.  
  141. int distance_left = distance_ir_left();
  142. int distance_right = distance_ir_right();
  143. drive_goto(25, -26);
  144.  
  145. int distance_up = distance_ir_left();
  146. int distance_down = distance_ir_right();
  147. drive_goto(-26, 25);
  148.  
  149. bool direction_left = distance_left > IR_DISTANCE_THRESHOLD;
  150. bool direction_right = distance_right > IR_DISTANCE_THRESHOLD;
  151. bool direction_up = distance_up > IR_DISTANCE_THRESHOLD;
  152. bool direction_down = distance_down > IR_DISTANCE_THRESHOLD;
  153.  
  154. printf("U: %i, D: %i, L: %i, R: %i.\n", distance_up, distance_down, distance_left, distance_right);
  155. pause(1000);
  156.  
  157. v[current] = true;
  158.  
  159. int idx_dominant;
  160. DIRECTION direction_dominant;
  161.  
  162. c[current][id_left] = direction_left;
  163. c[current][id_right] = direction_right;
  164. c[current][id_up] = direction_up;
  165. c[current][id_down] = direction_down;
  166.  
  167. c[id_left][current] = direction_left;
  168. c[id_right][current] = direction_right;
  169. c[id_up][current] = direction_up;
  170. c[id_down][current] = direction_down;
  171.  
  172. if(direction_left && id_left != -1 && !v[id_left]) {
  173. idx_dominant = id_left;
  174. direction_dominant = LEFT;
  175. } else if(direction_right && id_right != -1 && !v[id_right]) {
  176. idx_dominant = id_right;
  177. direction_dominant = RIGHT;
  178. } else if(direction_up && id_up != -1 && !v[id_up]) {
  179. idx_dominant = id_up;
  180. direction_dominant = UP;
  181. } else if(direction_down && id_down != -1 && !v[id_down]) {
  182. idx_dominant = id_right;
  183. direction_dominant = DOWN;
  184. } else {
  185. idx_dominant = -1;
  186. direction_dominant = NONE;
  187. }
  188.  
  189. printf("Direction: %i, ID: %i.\n", direction_dominant, idx_dominant);
  190.  
  191. if(idx_dominant != -1) {
  192. if(direction_dominant == UP) move_up();
  193. if(direction_dominant == LEFT) move_left();
  194. if(direction_dominant == RIGHT) move_right();
  195. if(direction_dominant == DOWN) move_down();
  196. stack_push(idx_dominant);
  197. } else {
  198. stack_pop();
  199. int previous = stack_top();
  200. direction_dominant = direction_between(current, previous);
  201.  
  202. if(direction_dominant == UP) move_up();
  203. if(direction_dominant == LEFT) move_left();
  204. if(direction_dominant == RIGHT) move_right();
  205. if(direction_dominant == DOWN) move_down();
  206. }
  207.  
  208. return traverse();
  209. }
  210.  
  211. int main() {
  212. // Initialization
  213. s.top = -1;
  214.  
  215. for(int i = 0; i < DIM_SQUARES; ++i) {
  216. v[i] = false;
  217.  
  218. for(int j = 0; j < DIM_SQUARES; ++j) c[i][j] = i == j ? true : false;
  219. }
  220.  
  221. stack_push(0);
  222.  
  223. // Step 0: Move to the first square.
  224. move_initial();
  225.  
  226. // Step 1: Build a connection matrix by traversing the maze and return back.
  227. traverse();
  228.  
  229. // Step 2:
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement