/*
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.robot.Robot;
import org.MARS.util.IOUtil;
/**
* The Brain One is a basic analogue controller device. It has
* an electrical circuit which reverses the directions of
* the connected motors for a small period of time whenever one of the
* sensors connected to the motor 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
*/
public class BrainOne extends AbstractElectronicDevice implements IController
{
public static const LEFT_SENSOR_PORT:Number = 0;
public static const RIGHT_SENSOR_PORT:Number = 1;
protected var leftPort:IOPort;
protected var rightPort:IOPort;
protected var leftDelay:Number;
protected var rightDelay:Number;
protected var leftTimer:Number = 0;
protected var rightTimer:Number = 0;
/**
* Creates a Brain One 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 BrainOne(ld:Number, rd:Number)
{
super(6, IOPort.OUTPUT);
getPort(LEFT_SENSOR_PORT).direction = IOPort.INPUT;
getPort(RIGHT_SENSOR_PORT).direction = IOPort.INPUT;
leftPort = getPort(LEFT_SENSOR_PORT);
rightPort = getPort(RIGHT_SENSOR_PORT);
//set timers
setTimers(ld, rd);
IOUtil.output("Brain One added");
}
protected function leftWheelForward():void
{
getPort(2).high();
getPort(3).low();
}
protected function leftWheelReverse():void
{
getPort(2).low();
getPort(3).high();
}
protected function rightWheelForward():void
{
getPort(4).high();
getPort(5).low();
}
protected function rightWheelReverse():void
{
getPort(4).low();
getPort(5).high();
}
/**
* Set the values of the Brain's timers to the provided ones
* @param ld - left timer (in seconds)
* @param rd - right timer (in seconds)
*/
public function setTimers(ld:Number, rd:Number):void {
if (ld != -1) leftDelay = ld * IOUtil.FRAMERATE;
if (rd != -1) rightDelay = rd * IOUtil.FRAMERATE;
}
/**
* A function that is called on every step of the physics engine.
* It advances the timers and reverses the motors whenever needed.
*/
public function step():void
{
if (leftPort.value == IOPort.HIGH){
leftWheelReverse();
leftTimer = leftDelay;
}
if (rightPort.value == IOPort.HIGH){
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--;
}
}
}
}