/*
Copyright (c) 2008 Dmitry Pyryeskin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package org.MARS.robot.control
{
import org.MARS.util.IOUtil;
/**
* The Brain Two is a basic analogue controller device. It has
* an electrical circuit which reverses the directions of the both
* connected motors for a small period of time, whenever any
* connected sensors is triggered.
*
* <p>
* IO Port configuration:<br>
* 0 and 1 - inputs from sensors (0 - left, 1 - right)<br>
* 2 through 5 - outputs (must be connected directly to the locomotion)
* </p>
* @see IOPort
* @see Robot
* @see ILocomotion
* @see BarinOne
*/
public class BrainTwo extends BrainOne implements IController
{
/**
* Creates a Brain Two with the timers connected to the
* left and right motors set to the number of seconds provided.
*
* <p>NOTE: using timers of slightly different values will help
* the robot to avoid getting stuck in on place whenever it's
* movement is perpendicular to the edge of the surface.</p>
*
* @param ld - left timer (in seconds)
* @param rd - right timer (in seconds)
*/
public function BrainTwo(ld:Number, rd:Number)
{
super(ld, rd);
IOUtil.output("Brain Two extends Brain One");
}
/**
* A function that is called on every step of the physics engine.
* It advances the timers and reverses the motors whenever needed.
*/
override public function step():void
{
if (leftPort.value == IOPort.HIGH || rightPort.value == IOPort.HIGH){
leftWheelReverse();
leftTimer = leftDelay;
rightWheelReverse();
rightTimer = rightDelay;
}
//check timers
if (rightTimer == 0){ //countdown ended
rightWheelForward();
rightTimer = -1;
} else if (rightTimer > 0) {
//reduce timer
rightTimer--;
}
if (leftTimer == 0){ //countdown ended
leftWheelForward();
leftTimer = -1;
} else if (leftTimer > 0) {
//reduce timer
leftTimer--;
}
}
}
}