Advertisement
Guest User

vizsga

a guest
Oct 28th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.85 KB | None | 0 0
  1. //#include <stdio.h>
  2.  
  3.  
  4. const int BLACK = 1;
  5. const int  BLUE = 2;
  6. const int  GREEN = 3;
  7. const int  YELLOW = 4;
  8. const int  RED = 5;
  9. const int  WHITE = 6;
  10.  
  11. const int  COLOR_FOLLOW = WHITE;
  12. const int  COLOR_WALL = RED;
  13. const int  COLOR_INTERSECTION = YELLOW;
  14. const int  COLOR_BLOCKMARK = GREEN;
  15. const int  COLOR_MAZE_END = YELLOW;
  16.  
  17. const int BLOCK_WIDTH_CM = 20;
  18. const int BLOCK_LENGTH_CM = 20;
  19.  
  20. int getColor() {
  21.   int color;
  22.   //printf("What color are you seeing?\n");
  23.   //scanf("%d", &color);
  24.   color = Sensor(IN_3);
  25.   NumOut(LCD_LINE2, LCD_LINE2, color, false);
  26.   return color;
  27. }
  28.  
  29. void turnLeft() {
  30.   //printf("Turning left 90°\n");
  31.   OnFwdSync(OUT_AC, 32, 90);
  32.   Wait(1000);
  33. }
  34.  
  35. void turnRight() {
  36.   //printf("Turning left 90°\n");
  37.   OnFwdSync(OUT_AC, 32, -90);
  38.   Wait(1000);
  39. }
  40.  
  41. void turnBack() {
  42.   //printf(“Turning back 180°\n”);
  43.   OnFwdSync(OUT_AC, 32, 180);
  44.   Wait(1000);
  45. }
  46.  
  47. void goForward() {
  48.   //printf("Going forward\n");
  49.   OnFwdSync(OUT_AC, 70, 0);
  50.  
  51. }
  52.  
  53.  
  54. void goForwardCm(int cm) {
  55.   //printf("Going forward %d cms\n", cm);
  56.   int fordulat = 0;
  57.   int b = 0;
  58.   ResetTachoCount(OUT_A);
  59.   OnFwdSync(OUT_AC, 70, 0);
  60.   while(b == 0) {
  61.     fordulat += MotorTachoCount(OUT_A);
  62.     if (fordulat >= cm * 360) {
  63.       b = 1;
  64.     }
  65.   }
  66.   OnFwdSync(OUT_AC, 0, 0);
  67. }
  68.  
  69. void goBackCm(int cm) {
  70.   //printf("Going forward %d cms\n", cm);
  71.   int fordulat = 0;
  72.   int b = 0;
  73.   ResetTachoCount(OUT_A);
  74.   OnFwdSync(OUT_AC, -70, 0);
  75.   while(b == 0) {
  76.     fordulat += MotorTachoCount(OUT_A);
  77.     if (fordulat >= cm * 360) {
  78.       b = 1;
  79.     }
  80.   }
  81.   OnFwdSync(OUT_AC, 0, 0);
  82. }
  83.  
  84. void backOff() {
  85.   goBackCm(5);
  86. }
  87.  
  88.  
  89. void followLine() {
  90.   //printf("Following line\n");
  91.   goForward();
  92. }
  93.  
  94.  
  95. void crossIntersection() {
  96.   //printf("Crossing an intersection\n");
  97.   while (getColor() != COLOR_FOLLOW) {
  98.     goForward();
  99.     Wait(10);
  100.   }
  101. }
  102.  
  103.  
  104. void avoidBlock() {
  105.   //printf("Avoiding a block\n");
  106.   int distance = SensorUS(IN_2);
  107.   while (distance > 10) {
  108.     goForward();
  109.     distance = SensorUS(IN_2);
  110.   }
  111.   OnFwdSync(OUT_AC, 0, 0);
  112.   turnRight();
  113.   goForwardCm(BLOCK_WIDTH_CM / 2);
  114.   turnLeft();
  115.   goForwardCm(BLOCK_LENGTH_CM);
  116.   turnLeft();
  117.   goForwardCm(BLOCK_WIDTH_CM / 2);
  118.   turnRight();
  119.   goForward();
  120. }
  121.  
  122. void turnSequence() {
  123.   turnRight();
  124.   backOff();
  125.   if(getColor() == COLOR_WALL) {
  126.     turnBack();
  127.     backOff();
  128.     if(getColor() == COLOR_WALL) {
  129.       turnLeft();
  130.       backOff();
  131.     }
  132.   }
  133. }
  134.  
  135. void solveMaze() {
  136.   //printf("Solving a maze\n");
  137.   //TODO detection of entering the maze
  138.   int b = 1;
  139.   while(b) {
  140.     goForward();
  141.     Wait(100);
  142.     if(getColor() == COLOR_WALL) {
  143.       turnSequence();
  144.     }
  145.   //TODO detection of the end of the maze
  146.   }
  147. }
  148.  
  149. void searchLine() {
  150.   //printf("Searching for a line\n");
  151.  
  152.  
  153.   unsigned int turn = 0; // fordulasi erosseg
  154.   int turnway = 1; // fordulasi irany
  155.   int tpower = 0; // fordulasi erosseg az irannyal "sulyozva"
  156.   while (getColor() != COLOR_FOLLOW) {
  157.       if (turn % 30 == 0) {
  158.         turnway *= -2;
  159.       }
  160.       tpower = ((turn % 50) + 50) * turnway;
  161.       OnFwdSync(OUT_AC, 30, tpower);
  162.       Wait(100);
  163.       turn++;
  164.   }
  165. }
  166.  
  167. task main() {
  168.   int color;
  169.   string test;
  170.  
  171.   SetSensorColorFull(IN_3);
  172.   SetSensorLowspeed(IN_2);
  173.  
  174.   while (1) {
  175.     color = getColor();
  176.  
  177.     switch (color) {
  178.       case COLOR_FOLLOW: strcpy(test, "follow"); TextOut(0, LCD_LINE1, test); followLine(); break;
  179.       case COLOR_INTERSECTION: strcpy(test, "cross"); TextOut(0, LCD_LINE1, test); crossIntersection(); break;
  180.       case COLOR_BLOCKMARK: strcpy(test, "block"); TextOut(0, LCD_LINE1, test); avoidBlock(); break;
  181.       case COLOR_WALL: strcpy(test, "maze"); TextOut(0, LCD_LINE1, test); solveMaze(); break;
  182.       default: strcpy(test, "searching"); TextOut(0, LCD_LINE1, test); searchLine(); break;
  183.     }
  184.     Wait(10);
  185.   }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement