Maze path search robot : mazeFunction.ino

Please copy and paste it into Arduino IDE.
  1. void mazeSolve(void) {
  2.   while (!status) {
  3.     readLFSsensors();
  4.     switch (mode) {
  5.       case NO_LINE:
  6.         Stop();
  7.         Turnright(178);
  8.         Forward(0);
  9.         delay(100);
  10.         if (EndCount == 0) {
  11.           recIntersection('B');
  12.         }
  13.         Serial.println("U_TURN");
  14.         break;
  15.       case CONT_LINE:
  16.         Stop();
  17.         Forward(38);
  18.         readLFSsensors();
  19.         if (mode != CONT_LINE) {
  20.           Stop();
  21.           Turnleft(88); // or it is a "T" or "Cross"). In both cases, goes to LEFT
  22.           if (EndCount == 0) {
  23.             recIntersection('L');
  24.           }
  25.           Serial.println("T_LEFT_TURN");
  26.         }
  27.         else mazeEnd();
  28.         Serial.println("mazeEnd");
  29.         break;
  30.       case RIGHT_TURN:
  31.         Stop();
  32.         Forward(5);
  33.         readLFSsensors();
  34.         if (mode == RIGHT_TURN) {
  35.           Stop();
  36.           Forward(33);
  37.           readLFSsensors();
  38.           if (mode == NO_LINE) {
  39.             Stop();
  40.             Turnright(88);
  41.             if (EndCount == 0) {
  42.               recIntersection('R');
  43.             }
  44.             Serial.println("RIGHT_TURN");
  45.           } else {
  46.             if (EndCount == 0) {
  47.               recIntersection('S');
  48.             }
  49.           }
  50.           Serial.println("Straight");
  51.         }
  52.         break;
  53.       case LEFT_TURN:
  54.         Stop();
  55.         Forward(5);
  56.         readLFSsensors();
  57.         if (mode == LEFT_TURN) {
  58.           Stop();
  59.           Forward(33);
  60.           Stop();
  61.           Turnleft(88);
  62.           if (EndCount == 0) {
  63.             recIntersection('L');
  64.           }
  65.           Serial.println("LEFT_TURN");
  66.         }
  67.         break;
  68.       case FOLLOWING_LINE:
  69.         Straight();
  70.         break;
  71.     }
  72.   }
  73. }
  74. //---------------------------------------------
  75. void recIntersection(char direction) {
  76.   path[pathLength] = direction; // Store the intersection in the path variable.
  77.   pathLength ++;
  78.   simplifyPath(); // Simplify the learned path.
  79. }
  80. //----------------------------------------------
  81. void mazeEnd(void) {
  82.   Stop();
  83.   EndCount++;
  84.   Serial.print(" EndCount=");
  85.   Serial.println(EndCount);
  86.   Stop();
  87.   for (int i = 0; i < pathLength; i++)
  88.     Serial.print(path[i]);
  89.   Serial.print(" pathLenght ==> ");
  90.   Serial.print(pathLength);
  91.   Serial.println(" mazeEnd");
  92.   status = 1;
  93.   mode = STOPPED;
  94.   if (EndCount == 1) { //Start位置へ戻る
  95.     ledBlink(2);
  96.     Serial.println("Start位置へ戻る");
  97.     Stop();
  98.     delay(1000);
  99.     Turnright(178);
  100.     Forward(0);
  101.     delay(1000);
  102.     Stop();
  103.     status = 0;
  104.     mazeSolve();
  105.     Stop();
  106.     delay(1000);
  107.     Turnright(178);
  108.     Forward(0);
  109.     delay(1000);
  110.     EndCount = 0;
  111.     Stop();
  112.   }
  113. }
  114. //---------------------------------------------------------------------
  115. // Path simplification. The strategy is that whenever we enEndCounter a
  116. // sequence xBx, we can simplify it by cutting out the dead end. For
  117. // example, LBL -> S, because a single S bypasses the dead end
  118. // represented by LBL.
  119. void simplifyPath() {
  120.   // only simplify the path if the second-to-last turn was a 'B'
  121.   if (pathLength < 3 || path[pathLength - 2] != 'B')
  122.     return;
  123.   int totalAngle = 0;
  124.   int i;
  125.   for (i = 1; i <= 3; i++)
  126.   {
  127.     switch (path[pathLength - i])
  128.     {
  129.       case 'R':
  130.         totalAngle += 90;
  131.         break;
  132.       case 'L':
  133.         totalAngle += 270;
  134.         break;
  135.       case 'B':
  136.         totalAngle += 180;
  137.         break;
  138.     }
  139.   }
  140.   // Get the angle as a number between 0 and 360 degrees.
  141.   totalAngle = totalAngle % 360;
  142.   // Replace all of those turns with a single one.
  143.   switch (totalAngle)
  144.   {
  145.     case 0:
  146.       path[pathLength - 3] = 'S';
  147.       break;
  148.     case 90:
  149.       path[pathLength - 3] = 'R';
  150.       break;
  151.     case 180:
  152.       path[pathLength - 3] = 'B';
  153.       break;
  154.     case 270:
  155.       path[pathLength - 3] = 'L';
  156.       break;
  157.   }
  158.   // The path is now two steps shorter.
  159.   pathLength -= 2;
  160. }
  161. //------------------------------------------------------------
  162. void mazeOptimization (void)
  163. {
  164.   while (!status) {
  165.     readLFSsensors();
  166.     switch (mode)
  167.     {
  168.       case FOLLOWING_LINE:
  169.         Straight();
  170.         break;
  171.       case CONT_LINE:
  172.         if (pathIndex >= pathLength) mazeEnd ();
  173.         else {
  174.           mazeTurn (path[pathIndex]);
  175.           pathIndex++;
  176.         }
  177.         break;
  178.       case LEFT_TURN:
  179.         if (pathIndex >= pathLength) mazeEnd ();
  180.         else {
  181.           mazeTurn (path[pathIndex]);
  182.           pathIndex++;
  183.         }
  184.         break;
  185.       case RIGHT_TURN:
  186.         if (pathIndex >= pathLength) mazeEnd ();
  187.         else {
  188.           mazeTurn (path[pathIndex]);
  189.           pathIndex++;
  190.         }
  191.         break;
  192.     }
  193.   }
  194. }
  195. //-----------------------------------------------------
  196. void mazeTurn (char dir) {
  197.   switch (dir) {
  198.     case 'L': // Turn Left
  199.       Forward(38);
  200.       Turnleft(88);
  201.       break;
  202.     case 'R': // Turn Right
  203.       Forward(38);
  204.       Turnright(88);
  205.       break;
  206.     case 'B': // Turn Back
  207.       Turnright(178);
  208.       break;
  209.     case 'S': // Go Straight
  210.       Forward(40);
  211.       Straight();
  212.       break;
  213.   }
  214. }
  215. //-----------------------------------------------------
  216. void ledBlink(int times) {
  217.   for (int i = 0; i < times; i++) {
  218.     digitalWrite (ledPin, HIGH);
  219.     delay (500);
  220.     digitalWrite (ledPin, LOW);
  221.     delay (500);
  222.   }
  223. }

by Paradise
元のページヘ戻る