/*
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 Squid Test Brain is a programmable controller designed for a
* specific purpose and a specific robot. The robot must find it's
* way to the goal on the other side of a table by following the lines
* and avoiding the moving 'Squid' robot.
*
* <p>
* IO Port configuration:<br>
* 0 through 3 - outputs (must be connected directly to the locomotion)<br>
* 4 and 5 - inputs from line sensors (4 - left, 5 - right)<br>
* 6 and 7 - inputs from IR sensors (6 - left, 7 - right)<br>
* 8 and 9 - inputs from edge sensors (8 - left, 9 - right)<br>
* 10 - output to an LED
* </p>
*
* @see IOPort
* @see Robot
* @see ILocomotion
*/
public class SquidTestBrain extends AbstractElectronicDevice implements IController
{
public static const LEFT_MOTOR_PORT_A:Number = 0;
public static const LEFT_MOTOR_PORT_B:Number = 1;
public static const RIGHT_MOTOR_PORT_A:Number = 2;
public static const RIGHT_MOTOR_PORT_B:Number = 3;
public static const LEFT_LINE_SENSOR_PORT:Number = 4;
public static const RIGHT_LINE_SENSOR_PORT:Number = 5;
public static const LEFT_IR_SENSOR_PORT:Number = 6;
public static const RIGHT_IR_SENSOR_PORT:Number = 7;
public static const LEFT_EDGE_SENSOR_PORT:Number = 8;
public static const RIGHT_EDGE_SENSOR_PORT:Number = 9;
public static const LED_PORT:Number = 10;
private static const NONE:Number = 0;
private static const LEFT:Number = 1;
private static const RIGHT:Number = 2;
private static const SHARP_LEFT:Number = 3;
private static const SHARP_RIGHT:Number = 4;
private static const STRAIGHT:Number = 5;
private static const REVERSE:Number = 6;
/** Flag used to stop the sensor check if an immidiate threat is detected **/
private var threat:Boolean = false;
/** Counter used by the main loop **/
private var counter:Number = 0;
/** Counter used by the main loop **/
private var reverseCounter:Number = 0;
/** Counter used by the main loop **/
private var lostLineCounter:Number = 0;
/** Direction which the robot will follow **/
private var movementDirection:Number = STRAIGHT;
/** The direction along which the robot is following the line **/
private var lineDirection:Number = NONE;
/**
* Create a Line Following Brain controller
*/
public function SquidTestBrain()
{
super(16, IOPort.INPUT);
getPort(LEFT_MOTOR_PORT_A).direction = IOPort.OUTPUT;
getPort(LEFT_MOTOR_PORT_B).direction = IOPort.OUTPUT;
getPort(RIGHT_MOTOR_PORT_A).direction = IOPort.OUTPUT;
getPort(RIGHT_MOTOR_PORT_B).direction = IOPort.OUTPUT;
getPort(LED_PORT).direction = IOPort.OUTPUT;
IOUtil.output("Squid Test Brain added");
}
protected function leftWheelForward():void
{
getPort(LEFT_MOTOR_PORT_A).high();
getPort(LEFT_MOTOR_PORT_B).low();
}
protected function leftWheelReverse():void
{
getPort(LEFT_MOTOR_PORT_A).low();
getPort(LEFT_MOTOR_PORT_B).high();
}
protected function rightWheelForward():void
{
getPort(RIGHT_MOTOR_PORT_A).high();
getPort(RIGHT_MOTOR_PORT_B).low();
}
protected function rightWheelReverse():void
{
getPort(RIGHT_MOTOR_PORT_A).low();
getPort(RIGHT_MOTOR_PORT_B).high();
}
protected function leftWheelStop():void
{
getPort(LEFT_MOTOR_PORT_A).low();
getPort(LEFT_MOTOR_PORT_B).low();
}
protected function rightWheelStop():void
{
getPort(RIGHT_MOTOR_PORT_A).low();
getPort(RIGHT_MOTOR_PORT_B).low();
}
/**
* 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
{
//step 1: check the edge sensors
var leftValue:Boolean = getPort(LEFT_EDGE_SENSOR_PORT).value;
var rightValue:Boolean = getPort(RIGHT_EDGE_SENSOR_PORT).value;
var edgeFound:Boolean = false;
if (leftValue == IOPort.HIGH && rightValue == IOPort.HIGH) {
//both edge sensors detect an edge, reverse for 0.6 seconds
movementDirection = SHARP_LEFT;
counter = 0.8 * IOUtil.FRAMERATE;
reverseCounter = 0.6 * IOUtil.FRAMERATE;
threat = true;
edgeFound = true;
} else if (leftValue == IOPort.HIGH) {
//left edge sensor detects an edge, move right for 1 second
movementDirection = SHARP_RIGHT;
counter = 1 * IOUtil.FRAMERATE;
threat = true;
edgeFound = true;
} else if (rightValue == IOPort.HIGH) {
//right edge sensor detects an edge, move left for 1 second
movementDirection = SHARP_LEFT;
counter = 1 * IOUtil.FRAMERATE;
threat = true;
edgeFound = true;
}
if (edgeFound || threat) {
move();
return;
}
//step 3: check the IR sensors
leftValue = getPort(LEFT_IR_SENSOR_PORT).value;
rightValue = getPort(RIGHT_IR_SENSOR_PORT).value;
var objectFound:Boolean = false;
if (leftValue == IOPort.HIGH && rightValue == IOPort.HIGH) {
//both IRs detect an object, reverse for 0.5 seconds
movementDirection = LEFT;
counter = 10;
reverseCounter = 0.5 * IOUtil.FRAMERATE;
objectFound = true;
} else if (leftValue == IOPort.HIGH) {
//left IR detects an object, move right for 0.5 seconds
movementDirection = RIGHT;
counter = 0.5 * IOUtil.FRAMERATE;
objectFound = true;
} else if (rightValue == IOPort.HIGH) {
//right IR detects an object, move left for 0.5 seconds
movementDirection = LEFT;
counter = 0.5 * IOUtil.FRAMERATE;
objectFound = true;
}
if (objectFound) {
move();
return;
}
leftValue = getPort(LEFT_LINE_SENSOR_PORT).value;
rightValue = getPort(RIGHT_LINE_SENSOR_PORT).value;
if (leftValue == IOPort.HIGH && rightValue == IOPort.HIGH) {
//both sensors are on a line: look for an edge
if (lineDirection == NONE){
//turn left
movementDirection = LEFT;
counter = 5 * IOUtil.FRAMERATE;
} else if (lineDirection == LEFT){
//move right
movementDirection = SHARP_RIGHT;
counter = 5 * IOUtil.FRAMERATE;
} else if (lineDirection == RIGHT){
//move left
movementDirection = SHARP_LEFT;
counter = 5 * IOUtil.FRAMERATE;
}
} else if (leftValue == IOPort.LOW && rightValue == IOPort.LOW) {
//both sensors are off a line: look for the line
if (lineDirection == NONE){
//move forward
movementDirection = STRAIGHT;
counter = 0;
} else {
if (lostLineCounter >= 0) {
//line was lost recently
if (lineDirection == LEFT){
//move left
movementDirection = SHARP_LEFT;
counter = 5 * IOUtil.FRAMERATE;
} else if (lineDirection == RIGHT){
//move right
movementDirection = SHARP_RIGHT;
counter = 5 * IOUtil.FRAMERATE;
}
lostLineCounter ++;
if (lostLineCounter > 100) lostLineCounter = -1;
getPort(LED_PORT).high();
} else {
//line lost for over 100 cycles, move forward
movementDirection = STRAIGHT;
counter = 0;
lineDirection = NONE;
getPort(LED_PORT).low();
}
}
} else if (leftValue != rightValue) {
//robot is on the edge of a line, move straight
movementDirection = STRAIGHT;
counter = 0;
lostLineCounter = 0;
getPort(LED_PORT).low();
//save line direction in case it ends
if (lineDirection == NONE) {
if (leftValue == IOPort.HIGH) lineDirection = LEFT;
else lineDirection = RIGHT;
}
}
move();
}
/**
* A function that sets the direction of the motors
* based on the <code>counter</code> and <code>movementDirection</code>
* variables.
*/
private function move():void {
if (reverseCounter != 0) {
leftWheelReverse();
rightWheelReverse();
reverseCounter --;
return;
}
if (movementDirection == LEFT) {
leftWheelStop();
rightWheelForward();
counter --;
} else if (movementDirection == RIGHT) {
leftWheelForward();
rightWheelStop();
counter --;
} else if (movementDirection == SHARP_LEFT) {
leftWheelReverse();
rightWheelForward();
counter --;
} else if (movementDirection == SHARP_RIGHT) {
leftWheelForward();
rightWheelReverse();
counter --;
} else if (counter == 0 && movementDirection == STRAIGHT) {
leftWheelForward();
rightWheelForward();
}
if (counter == 0) {
movementDirection = STRAIGHT;
//remote the threat status
if (threat) threat = false;
}
}
}
}