to Main Page

Code example

PLAIN TEXT VIEW
  1. /*
  2. Copyright (c) 2008 Dmitry Pyryeskin
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10.  
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13.  
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. */
  22. package org.MARS.robot.control
  23. {
  24.     import org.MARS.util.IOUtil;
  25.  
  26.     /**
  27.     * The Line Following Brain is a programmable controller device.
  28.     * It is programmed to read the inputs from the two line sensors
  29.     * and follow the edge of the line.<br>
  30.     * The brain uses the following algorithm to follow a line:
  31.     * <ul>
  32.     *   <li>whenever neither of the sensors detects a line
  33.     *       <ul>
  34.     *           <li>if the robot had never detected a line, go straight</li>
  35.     *           <li>if the left sensor was following the line before, turn left</li>
  36.     *           <li>if the right sensor was following the line before, turn right</li>
  37.     *       </ul>
  38.     *   </li>
  39.     *   <li>whenever both sensors detect a line
  40.     *       <ul>
  41.     *           <li>if it is the first time the robot found the line, turn left</li>
  42.     *           <li>if the left sensor was following the line before, turn right</li>
  43.     *           <li>if the right sensor was following the line before, turn left</li>
  44.     *       </ul>
  45.     *   </li>
  46.     *   <li>whenever on of the sensors detects a line and another doesn't move straight</li>
  47.     * </il>
  48.     *
  49.     * <p>
  50.     * IO Port configuration:<br>
  51.     * 0 and 1 - inputs from sensors (0 - left, 1 - right)<br>
  52.     * 2 through 5 - outputs (must be connected directly to the locomotion)
  53.     * </p>
  54.     * @see IOPort
  55.     * @see Robot
  56.     * @see ILocomotion
  57.     */
  58.     public class LineFollowingBrain extends AbstractElectronicDevice implements IController
  59.     {
  60.         public static const LEFT_SENSOR_PORT:Number = 0;
  61.         public static const RIGHT_SENSOR_PORT:Number = 1;
  62.        
  63.         private static const NONE:Number = 0;
  64.         private static const LEFT:Number = 1;
  65.         private static const RIGHT:Number = 2;
  66.        
  67.         /** The direction along which the robot is following the line **/
  68.         private var lineDirection:Number = NONE;
  69.        
  70.         /**
  71.          * Create a Line Following Brain controller
  72.          */
  73.         public function LineFollowingBrain()
  74.         {
  75.             super(6, IOPort.OUTPUT);
  76.             getPort(LEFT_SENSOR_PORT).direction = IOPort.INPUT;
  77.             getPort(RIGHT_SENSOR_PORT).direction = IOPort.INPUT;
  78.            
  79.             IOUtil.output("Line Following Brain added");
  80.         }
  81.        
  82.         protected function leftWheelForward():void
  83.         {
  84.             getPort(2).high();
  85.             getPort(3).low();
  86.         }
  87.        
  88.         protected function leftWheelReverse():void
  89.         {
  90.             getPort(2).low();
  91.             getPort(3).high();
  92.         }
  93.        
  94.         protected function rightWheelForward():void
  95.         {
  96.             getPort(4).high();
  97.             getPort(5).low();
  98.         }
  99.        
  100.         protected function rightWheelReverse():void
  101.         {
  102.             getPort(4).low();
  103.             getPort(5).high();
  104.         }
  105.        
  106.         /**
  107.          * A function that is called on every step of the physics engine.
  108.          * It reads the sensors' input and reverses the motors whenever needed.
  109.          */
  110.         public function step():void
  111.         {
  112.             var leftValue:Boolean = getPort(LEFT_SENSOR_PORT).value;
  113.             var rightValue:Boolean = getPort(RIGHT_SENSOR_PORT).value;
  114.            
  115.             if (leftValue == IOPort.HIGH && rightValue == IOPort.HIGH) {
  116.                 //both sensors are on a line: look for an edge
  117.                 if (lineDirection == NONE){
  118.                     //move left
  119.                     rightWheelForward();
  120.                     leftWheelReverse();
  121.                 } else if (lineDirection == LEFT){
  122.                     //move right
  123.                     rightWheelReverse();
  124.                     leftWheelForward();
  125.                 } else if (lineDirection == RIGHT){
  126.                     //move left
  127.                     rightWheelForward();
  128.                     leftWheelReverse();
  129.                 }
  130.             } else if (leftValue == IOPort.LOW && rightValue == IOPort.LOW) {
  131.                 //both sensors are off a line: look for the line
  132.                 if (lineDirection == NONE){
  133.                     //move forward
  134.                     rightWheelForward();
  135.                     leftWheelForward();
  136.                 } else if (lineDirection == LEFT){
  137.                     //move left
  138.                     rightWheelForward();
  139.                     leftWheelReverse();
  140.                 } else if (lineDirection == RIGHT){
  141.                     //move right
  142.                     rightWheelReverse();
  143.                     leftWheelForward();
  144.                 }
  145.             } else if (leftValue != rightValue) {
  146.                 //robot is on the edge of a line, move straight
  147.                 rightWheelForward();
  148.                 leftWheelForward();
  149.                 //save line direction in case it ends
  150.                 if (lineDirection == NONE) {
  151.                     if (rightValue == IOPort.HIGH) lineDirection = RIGHT;
  152.                     else lineDirection = RIGHT;
  153.                 }
  154.             }
  155.         }
  156.     }
  157. }