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 Squid Test Brain is a programmable controller designed for a
  28.      * specific purpose and a specific robot. The robot must find it's
  29.      * way to the goal on the other side of a table by following the lines
  30.      * and avoiding the moving 'Squid' robot.
  31.      *
  32.      * <p>
  33.      * IO Port configuration:<br>
  34.      * 0 through 3 - outputs (must be connected directly to the locomotion)<br>
  35.      * 4 and 5 - inputs from line sensors (4 - left, 5 - right)<br>
  36.      * 6 and 7 - inputs from IR sensors (6 - left, 7 - right)<br>
  37.      * 8 and 9 - inputs from edge sensors (8 - left, 9 - right)<br>
  38.      * 10 - output to an LED
  39.      * </p>
  40.      *
  41.      * @see IOPort
  42.      * @see Robot
  43.      * @see ILocomotion
  44.      */
  45.     public class SquidTestBrain extends AbstractElectronicDevice implements IController
  46.     {
  47.         public static const LEFT_MOTOR_PORT_A:Number = 0;
  48.         public static const LEFT_MOTOR_PORT_B:Number = 1;
  49.         public static const RIGHT_MOTOR_PORT_A:Number = 2;
  50.         public static const RIGHT_MOTOR_PORT_B:Number = 3;
  51.        
  52.         public static const LEFT_LINE_SENSOR_PORT:Number = 4;
  53.         public static const RIGHT_LINE_SENSOR_PORT:Number = 5;
  54.         public static const LEFT_IR_SENSOR_PORT:Number = 6;
  55.         public static const RIGHT_IR_SENSOR_PORT:Number = 7;
  56.         public static const LEFT_EDGE_SENSOR_PORT:Number = 8;
  57.         public static const RIGHT_EDGE_SENSOR_PORT:Number = 9;
  58.         public static const LED_PORT:Number = 10;
  59.        
  60.         private static const NONE:Number = 0;
  61.         private static const LEFT:Number = 1;
  62.         private static const RIGHT:Number = 2;
  63.         private static const SHARP_LEFT:Number = 3;
  64.         private static const SHARP_RIGHT:Number = 4;
  65.         private static const STRAIGHT:Number = 5;
  66.         private static const REVERSE:Number = 6;
  67.        
  68.         /** Flag used to stop the sensor check if an immidiate threat is detected **/
  69.         private var threat:Boolean = false;
  70.        
  71.         /** Counter used by the main loop **/
  72.         private var counter:Number = 0;
  73.        
  74.         /** Counter used by the main loop **/
  75.         private var reverseCounter:Number = 0;
  76.        
  77.         /** Counter used by the main loop **/
  78.         private var lostLineCounter:Number = 0;
  79.        
  80.         /** Direction which the robot will follow **/
  81.         private var movementDirection:Number = STRAIGHT;
  82.        
  83.         /** The direction along which the robot is following the line **/
  84.         private var lineDirection:Number = NONE;
  85.        
  86.         /**
  87.          * Create a Line Following Brain controller
  88.          */
  89.         public function SquidTestBrain()
  90.         {
  91.             super(16, IOPort.INPUT);
  92.             getPort(LEFT_MOTOR_PORT_A).direction = IOPort.OUTPUT;
  93.             getPort(LEFT_MOTOR_PORT_B).direction = IOPort.OUTPUT;
  94.             getPort(RIGHT_MOTOR_PORT_A).direction = IOPort.OUTPUT;
  95.             getPort(RIGHT_MOTOR_PORT_B).direction = IOPort.OUTPUT;
  96.             getPort(LED_PORT).direction = IOPort.OUTPUT;
  97.            
  98.             IOUtil.output("Squid Test Brain added");
  99.         }
  100.        
  101.         protected function leftWheelForward():void
  102.         {
  103.             getPort(LEFT_MOTOR_PORT_A).high();
  104.             getPort(LEFT_MOTOR_PORT_B).low();
  105.         }
  106.        
  107.         protected function leftWheelReverse():void
  108.         {
  109.             getPort(LEFT_MOTOR_PORT_A).low();
  110.             getPort(LEFT_MOTOR_PORT_B).high();
  111.         }
  112.        
  113.         protected function rightWheelForward():void
  114.         {
  115.             getPort(RIGHT_MOTOR_PORT_A).high();
  116.             getPort(RIGHT_MOTOR_PORT_B).low();
  117.         }
  118.        
  119.         protected function rightWheelReverse():void
  120.         {
  121.             getPort(RIGHT_MOTOR_PORT_A).low();
  122.             getPort(RIGHT_MOTOR_PORT_B).high();
  123.         }
  124.        
  125.         protected function leftWheelStop():void
  126.         {
  127.             getPort(LEFT_MOTOR_PORT_A).low();
  128.             getPort(LEFT_MOTOR_PORT_B).low();
  129.         }
  130.        
  131.         protected function rightWheelStop():void
  132.         {
  133.             getPort(RIGHT_MOTOR_PORT_A).low();
  134.             getPort(RIGHT_MOTOR_PORT_B).low();
  135.         }
  136.        
  137.        
  138.         /**
  139.          * A function that is called on every step of the physics engine.
  140.          * It reads the sensors' input and reverses the motors whenever needed.
  141.          */
  142.         public function step():void
  143.         {
  144.             //step 1: check the edge sensors
  145.             var leftValue:Boolean = getPort(LEFT_EDGE_SENSOR_PORT).value;
  146.             var rightValue:Boolean = getPort(RIGHT_EDGE_SENSOR_PORT).value;
  147.             var edgeFound:Boolean = false;
  148.            
  149.             if (leftValue == IOPort.HIGH && rightValue == IOPort.HIGH) {
  150.                 //both edge sensors detect an edge, reverse for 0.6 seconds
  151.                 movementDirection = SHARP_LEFT;
  152.                 counter = 0.8 * IOUtil.FRAMERATE;
  153.                 reverseCounter = 0.6 * IOUtil.FRAMERATE;
  154.                 threat = true;
  155.                 edgeFound = true;
  156.             } else if (leftValue == IOPort.HIGH) {
  157.                 //left edge sensor detects an edge, move right for 1 second
  158.                 movementDirection = SHARP_RIGHT;
  159.                 counter = 1 * IOUtil.FRAMERATE;
  160.                 threat = true;
  161.                 edgeFound = true;
  162.             } else if (rightValue == IOPort.HIGH) {
  163.                 //right edge sensor detects an edge, move left for 1 second
  164.                 movementDirection = SHARP_LEFT;
  165.                 counter = 1 * IOUtil.FRAMERATE;
  166.                 threat = true;
  167.                 edgeFound = true;
  168.             }
  169.            
  170.             if (edgeFound || threat) {
  171.                 move();
  172.                 return;
  173.             }
  174.            
  175.             //step 3: check the IR sensors
  176.             leftValue = getPort(LEFT_IR_SENSOR_PORT).value;
  177.             rightValue = getPort(RIGHT_IR_SENSOR_PORT).value;
  178.             var objectFound:Boolean = false;
  179.            
  180.             if (leftValue == IOPort.HIGH && rightValue == IOPort.HIGH) {
  181.                 //both IRs detect an object, reverse for 0.5 seconds
  182.                 movementDirection = LEFT;
  183.                 counter = 10;
  184.                 reverseCounter = 0.5 * IOUtil.FRAMERATE;
  185.                 objectFound = true;
  186.             } else if (leftValue == IOPort.HIGH) {
  187.                 //left IR detects an object, move right for 0.5 seconds
  188.                 movementDirection = RIGHT;
  189.                 counter = 0.5 * IOUtil.FRAMERATE;
  190.                 objectFound = true;
  191.             } else if (rightValue == IOPort.HIGH) {
  192.                 //right IR detects an object, move left for 0.5 seconds
  193.                 movementDirection = LEFT;
  194.                 counter = 0.5 * IOUtil.FRAMERATE;
  195.                 objectFound = true;
  196.             }
  197.            
  198.             if (objectFound) {
  199.                 move();
  200.                 return;
  201.             }
  202.            
  203.             leftValue = getPort(LEFT_LINE_SENSOR_PORT).value;
  204.             rightValue = getPort(RIGHT_LINE_SENSOR_PORT).value;
  205.            
  206.             if (leftValue == IOPort.HIGH && rightValue == IOPort.HIGH) {
  207.                 //both sensors are on a line: look for an edge
  208.                 if (lineDirection == NONE){
  209.                     //turn left
  210.                     movementDirection = LEFT;
  211.                     counter = 5 * IOUtil.FRAMERATE;
  212.                 } else if (lineDirection == LEFT){
  213.                     //move right
  214.                     movementDirection = SHARP_RIGHT;
  215.                     counter = 5 * IOUtil.FRAMERATE;
  216.                 } else if (lineDirection == RIGHT){
  217.                     //move left
  218.                     movementDirection = SHARP_LEFT;
  219.                     counter = 5 * IOUtil.FRAMERATE;
  220.                 }
  221.             } else if (leftValue == IOPort.LOW && rightValue == IOPort.LOW) {
  222.                 //both sensors are off a line: look for the line
  223.                 if (lineDirection == NONE){
  224.                     //move forward
  225.                     movementDirection = STRAIGHT;
  226.                     counter = 0;
  227.                 } else {
  228.                     if (lostLineCounter >= 0) {
  229.                         //line was lost recently
  230.                         if (lineDirection == LEFT){
  231.                             //move left
  232.                             movementDirection = SHARP_LEFT;
  233.                             counter = 5 * IOUtil.FRAMERATE;
  234.                         } else if (lineDirection == RIGHT){
  235.                             //move right
  236.                             movementDirection = SHARP_RIGHT;
  237.                             counter = 5 * IOUtil.FRAMERATE;
  238.                         }
  239.                         lostLineCounter ++;
  240.                         if (lostLineCounter > 100) lostLineCounter = -1;
  241.                         getPort(LED_PORT).high();
  242.                     } else {
  243.                         //line lost for over 100 cycles, move forward
  244.                         movementDirection = STRAIGHT;
  245.                         counter = 0;
  246.                         lineDirection = NONE;
  247.                         getPort(LED_PORT).low();
  248.                     }
  249.                 }
  250.             } else if (leftValue != rightValue) {
  251.                 //robot is on the edge of a line, move straight
  252.                 movementDirection = STRAIGHT;
  253.                 counter = 0;
  254.                 lostLineCounter = 0;
  255.                 getPort(LED_PORT).low();
  256.                 //save line direction in case it ends
  257.                 if (lineDirection == NONE) {
  258.                     if (leftValue == IOPort.HIGH) lineDirection = LEFT;
  259.                     else lineDirection = RIGHT;
  260.                 }
  261.             }
  262.             move();
  263.         }
  264.        
  265.         /**
  266.          * A function that sets the direction of the motors
  267.          * based on the <code>counter</code> and <code>movementDirection</code>
  268.          * variables.
  269.          */
  270.         private function move():void {
  271.             if (reverseCounter != 0) {
  272.                 leftWheelReverse();
  273.                 rightWheelReverse();
  274.                 reverseCounter --;
  275.                 return;
  276.             }
  277.            
  278.              if (movementDirection == LEFT) {
  279.                 leftWheelStop();
  280.                 rightWheelForward();
  281.                 counter --;
  282.             } else if (movementDirection == RIGHT) {
  283.                 leftWheelForward();
  284.                 rightWheelStop();
  285.                 counter --;
  286.             } else if (movementDirection == SHARP_LEFT) {
  287.                 leftWheelReverse();
  288.                 rightWheelForward();
  289.                 counter --;
  290.             } else if (movementDirection == SHARP_RIGHT) {
  291.                 leftWheelForward();
  292.                 rightWheelReverse();
  293.                 counter --;
  294.             } else if (counter == 0 && movementDirection == STRAIGHT) {
  295.                 leftWheelForward();
  296.                 rightWheelForward();
  297.             }
  298.            
  299.             if (counter == 0) {
  300.                 movementDirection = STRAIGHT;
  301.                 //remote the threat status
  302.                 if (threat) threat = false;
  303.             }
  304.         }
  305.     }
  306. }