/*
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 Line Following Brain is a programmable controller device.
* It is programmed to read the inputs from the two line sensors
* and follow the edge of the line.<br>
* The brain uses the following algorithm to follow a line:
* <ul>
* <li>whenever neither of the sensors detects a line
* <ul>
* <li>if the robot had never detected a line, go straight</li>
* <li>if the left sensor was following the line before, turn left</li>
* <li>if the right sensor was following the line before, turn right</li>
* </ul>
* </li>
* <li>whenever both sensors detect a line
* <ul>
* <li>if it is the first time the robot found the line, turn left</li>
* <li>if the left sensor was following the line before, turn right</li>
* <li>if the right sensor was following the line before, turn left</li>
* </ul>
* </li>
* <li>whenever on of the sensors detects a line and another doesn't move straight</li>
* </il>
*
* <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 LineFollowingBrain extends AbstractElectronicDevice implements IController
{
public static const LEFT_SENSOR_PORT:Number = 0;
public static const RIGHT_SENSOR_PORT:Number = 1;
private static const NONE:Number = 0;
private static const LEFT:Number = 1;
private static const RIGHT:Number = 2;
/** The direction along which the robot is following the line **/
private var lineDirection:Number = NONE;
/**
* Create a Line Following Brain controller
*/
public function LineFollowingBrain()
{
super(6, IOPort.OUTPUT);
getPort(LEFT_SENSOR_PORT).direction = IOPort.INPUT;
getPort(RIGHT_SENSOR_PORT).direction = IOPort.INPUT;
IOUtil.output("Line Following Brain 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();
}
/**
* A function that is called on every step of the physics engine.
* It reads the sensors' input and reverses the motors whenever needed.
*/
public function step():void
{
var leftValue:Boolean = getPort(LEFT_SENSOR_PORT).value;
var rightValue:Boolean = getPort(RIGHT_SENSOR_PORT).value;
if (leftValue == IOPort.HIGH && rightValue == IOPort.HIGH) {
//both sensors are on a line: look for an edge
if (lineDirection == NONE){
//move left
rightWheelForward();
leftWheelReverse();
} else if (lineDirection == LEFT){
//move right
rightWheelReverse();
leftWheelForward();
} else if (lineDirection == RIGHT){
//move left
rightWheelForward();
leftWheelReverse();
}
} else if (leftValue == IOPort.LOW && rightValue == IOPort.LOW) {
//both sensors are off a line: look for the line
if (lineDirection == NONE){
//move forward
rightWheelForward();
leftWheelForward();
} else if (lineDirection == LEFT){
//move left
rightWheelForward();
leftWheelReverse();
} else if (lineDirection == RIGHT){
//move right
rightWheelReverse();
leftWheelForward();
}
} else if (leftValue != rightValue) {
//robot is on the edge of a line, move straight
rightWheelForward();
leftWheelForward();
//save line direction in case it ends
if (lineDirection == NONE) {
if (rightValue == IOPort.HIGH) lineDirection = RIGHT;
else lineDirection = RIGHT;
}
}
}
}
}