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 Brain Two is a basic analogue controller device. It has
  28.     * an electrical circuit which reverses the directions of the both
  29.     * connected motors for a small period of time, whenever any
  30.     * connected sensors is triggered.
  31.     *
  32.     * <p>
  33.     * IO Port configuration:<br>
  34.     * 0 and 1 - inputs from sensors (0 - left, 1 - right)<br>
  35.     * 2 through 5 - outputs (must be connected directly to the locomotion)
  36.     * </p>
  37.     * @see IOPort
  38.     * @see Robot
  39.     * @see ILocomotion
  40.     * @see BarinOne
  41.     */
  42.     public class BrainTwo extends BrainOne implements IController
  43.     {
  44.         /**
  45.          * Creates a Brain Two with the timers connected to the
  46.          * left and right motors set to the number of seconds provided.
  47.          *
  48.          * <p>NOTE: using timers of slightly different values will help
  49.          * the robot to avoid getting stuck in on place whenever it's
  50.          * movement is perpendicular to the edge of the surface.</p>
  51.          *
  52.          * @param   ld - left timer (in seconds)
  53.          * @param   rd - right timer (in seconds)
  54.          */
  55.         public function BrainTwo(ld:Number, rd:Number)
  56.         {
  57.             super(ld, rd);
  58.             IOUtil.output("Brain Two extends Brain One");
  59.         }
  60.        
  61.         /**
  62.          * A function that is called on every step of the physics engine.
  63.          * It advances the timers and reverses the motors whenever needed.
  64.          */
  65.         override public function step():void
  66.         {
  67.             if (leftPort.value == IOPort.HIGH || rightPort.value == IOPort.HIGH){
  68.                 leftWheelReverse();
  69.                 leftTimer = leftDelay;
  70.                 rightWheelReverse();
  71.                 rightTimer = rightDelay;
  72.             }
  73.            
  74.             //check timers
  75.             if (rightTimer == 0){ //countdown ended
  76.                  rightWheelForward();
  77.                  rightTimer = -1;
  78.             } else if (rightTimer > 0) {
  79.                 //reduce timer
  80.                 rightTimer--;
  81.             }
  82.            
  83.             if (leftTimer == 0){ //countdown ended
  84.                  leftWheelForward();
  85.                  leftTimer = -1;
  86.             } else if (leftTimer > 0) {
  87.                 //reduce timer
  88.                 leftTimer--;
  89.             }
  90.         }
  91.     }
  92. }