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.robot.Robot;
  25.     import org.MARS.util.IOUtil;
  26.    
  27.     /**
  28.     * The Brain One is a basic analogue controller device. It has
  29.     * an electrical circuit which reverses the directions of
  30.     * the connected motors for a small period of time whenever one of the
  31.     * sensors connected to the motor is triggered.
  32.     *
  33.     * <p>
  34.     * IO Port configuration:<br>
  35.     * 0 and 1 - inputs from sensors (0 - left, 1 - right)<br>
  36.     * 2 through 5 - outputs (must be connected directly to the locomotion)
  37.     * </p>
  38.     * @see IOPort
  39.     * @see Robot
  40.     * @see ILocomotion
  41.     */
  42.     public class BrainOne extends AbstractElectronicDevice implements IController
  43.     {
  44.         public static const LEFT_SENSOR_PORT:Number = 0;
  45.         public static const RIGHT_SENSOR_PORT:Number = 1;
  46.        
  47.         protected var leftPort:IOPort;
  48.         protected var rightPort:IOPort;
  49.         protected var leftDelay:Number;
  50.         protected var rightDelay:Number;
  51.         protected var leftTimer:Number = 0;
  52.         protected var rightTimer:Number = 0;
  53.        
  54.         /**
  55.          * Creates a Brain One with the timers connected to the
  56.          * left and right motors set to the number of seconds provided.
  57.          *
  58.          * <p>NOTE: using timers of slightly different values will help
  59.          * the robot to avoid getting stuck in on place whenever it's
  60.          * movement is perpendicular to the edge of the surface.</p>
  61.          *
  62.          * @param   ld - left timer (in seconds)
  63.          * @param   rd - right timer (in seconds)
  64.          */
  65.         public function BrainOne(ld:Number, rd:Number)
  66.         {
  67.             super(6, IOPort.OUTPUT);
  68.             getPort(LEFT_SENSOR_PORT).direction = IOPort.INPUT;
  69.             getPort(RIGHT_SENSOR_PORT).direction = IOPort.INPUT;
  70.             leftPort = getPort(LEFT_SENSOR_PORT);
  71.             rightPort = getPort(RIGHT_SENSOR_PORT);
  72.            
  73.             //set timers
  74.             setTimers(ld, rd);
  75.             IOUtil.output("Brain One added");
  76.         }
  77.        
  78.         protected function leftWheelForward():void
  79.         {
  80.             getPort(2).high();
  81.             getPort(3).low();
  82.         }
  83.        
  84.         protected function leftWheelReverse():void
  85.         {
  86.             getPort(2).low();
  87.             getPort(3).high();
  88.         }
  89.        
  90.         protected function rightWheelForward():void
  91.         {
  92.             getPort(4).high();
  93.             getPort(5).low();
  94.         }
  95.        
  96.         protected function rightWheelReverse():void
  97.         {
  98.             getPort(4).low();
  99.             getPort(5).high();
  100.         }
  101.        
  102.         /**
  103.          * Set the values of the Brain's timers to the provided ones
  104.          * @param   ld - left timer (in seconds)
  105.          * @param   rd - right timer (in seconds)
  106.          */
  107.         public function setTimers(ld:Number, rd:Number):void {
  108.             if (ld != -1) leftDelay = ld * IOUtil.FRAMERATE;
  109.             if (rd != -1) rightDelay = rd * IOUtil.FRAMERATE;
  110.         }
  111.        
  112.         /**
  113.          * A function that is called on every step of the physics engine.
  114.          * It advances the timers and reverses the motors whenever needed.
  115.          */
  116.         public function step():void
  117.         {          
  118.             if (leftPort.value == IOPort.HIGH){
  119.                 leftWheelReverse();
  120.                 leftTimer = leftDelay;
  121.             }
  122.            
  123.             if (rightPort.value == IOPort.HIGH){
  124.                 rightWheelReverse();
  125.                 rightTimer = rightDelay;
  126.             }
  127.            
  128.             //check timers
  129.             if (rightTimer == 0){ //countdown ended
  130.                  rightWheelForward();
  131.                  rightTimer = -1;
  132.             } else if (rightTimer > 0) {
  133.                 //reduce timer
  134.                 rightTimer--;
  135.             }
  136.            
  137.             if (leftTimer == 0){ //countdown ended
  138.                  leftWheelForward();
  139.                  leftTimer = -1;
  140.             } else if (leftTimer > 0) {
  141.                 //reduce timer
  142.                 leftTimer--;
  143.             }
  144.         }
  145.     }
  146. }