Added presentations from EVI 2017

This commit is contained in:
Miguel Angel Astor Romero
2017-10-23 15:25:36 -04:00
parent 68ae52b86a
commit d60fc66a77
52 changed files with 3409 additions and 0 deletions

View File

@@ -0,0 +1,140 @@
package lejos.robotics.subsumption;
/**
* Arbitrator controls which Behavior object will become active in
* a behavior control system. Make sure to call start() after the
* Arbitrator is instantiated.<br>
* This class has three major responsibilities: <br>
* 1. Determine the highest priority behavior that returns <b> true </b> to takeControl()<br>
* 2. Suppress the active behavior if its priority is less than highest
* priority. <br>
* 3. When the action() method exits, call action() on the Behavior of highest priority.
* <br> The Arbitrator assumes that a Behavior is no longer active when action() exits,
* <br> therefore it will only call suppress() on the Behavior whose action() method is running.
* <br> It can make consecutive calls of action() on the same Behavior.
* <br> Requirements for a Behavior:
* <br> When suppress() is called, terminate action() immediately.
* <br> When action() exits, the robot is in a safe state (e.g. motors stopped)
* @see Behavior
* @author Roger Glassey
*/
public class Arbitrator
{
private final int NONE = -1;
private Behavior[] _behavior;
// highest priority behavior that wants control ; set by start() usec by monitor
private int _highestPriority = NONE;
private int _active = NONE; // active behavior; set by montior, used by start();
private boolean _returnWhenInactive;
/**
* Monitor is an inner class. It polls the behavior array to find the behavior of hightst
* priority. If higher than the active behavior, it calls active.suppress()
*/
private Monitor monitor;
/**
* Allocates an Arbitrator object and initializes it with an array of
* Behavior objects. The index of a behavior in this array is its priority level, so
* the behavior of the largest index has the highest the priority level.
* The behaviors in an Arbitrator can not
* be changed once the arbitrator is initialized.<BR>
* <B>NOTE:</B> Once the Arbitrator is initialized, the method start() must be
* called to begin the arbitration.
* @param behaviorList an array of Behavior objects.
* @param returnWhenInactive if <B>true</B>, the <B>start()</B> method returns when no Behavior is active.
*/
public Arbitrator(Behavior[] behaviorList, boolean returnWhenInactive)
{
_behavior = behaviorList;
_returnWhenInactive = returnWhenInactive;
monitor = new Monitor();
monitor.setDaemon(true);
}
/**
* Same as Arbitrator(behaviorList, false) Arbitrator start() never exits
* @param behaviorList An array of Behavior objects.
*/
public Arbitrator(Behavior[] behaviorList)
{
this(behaviorList, false);
}
/**
* This method starts the arbitration of Behaviors and runs an endless loop. <BR>
* Note: Arbitrator does not run in a separate thread. The start()
* method will never return unless <br>1. no action() method is running and
* <br>2. no behavior takeControl()
* returns <B> true </B> and <br> 3. the <i>returnWhenInacative </i> flag is true,
*/
public void start()
{
monitor.start();
while (_highestPriority == NONE)
{
Thread.yield();//wait for some behavior to take contro
}
while (true)
{
synchronized (monitor)
{
if (_highestPriority != NONE)
{
_active = _highestPriority;
} else if (_returnWhenInactive)
{// no behavior wants to run
monitor.more = false;//9 shut down monitor thread
return;
}
}// monotor released before action is called
if (_active != NONE) //_highestPrioirty could be NONE
{
_behavior[_active].action();
_active = NONE; // no active behavior at the moment
}
Thread.yield();
}
}
/**
* Finds the highest priority behavior that returns <B>true </B> to takeControl();
* If this priority is higher than the active behavior, it calls active.suppress().
* If there is no active behavior, calls suppress() on the most recently acrive behavior.
*/
private class Monitor extends Thread
{
boolean more = true;
int maxPriority = _behavior.length - 1;
public void run()
{
while (more)
{
//FIND HIGHEST PRIORITY BEHAVIOR THAT WANTS CONTROL
synchronized (this)
{
_highestPriority = NONE;
for (int i = maxPriority; i >= 0; i--)
{
if (_behavior[i].takeControl())
{
_highestPriority = i;
break;
}
}
int active = _active;// local copy: avoid out of bounds error in 134
if (active != NONE && _highestPriority > active)
{
_behavior[active].suppress();
}
}// end synchronize block - main thread can run now
Thread.yield();
}
}
}
}

View File

@@ -0,0 +1,55 @@
package lejos.robotics.subsumption;
/**
* The Behavior interface represents an object embodying a specific
* behavior belonging to a robot. Each behavior must define three things: <BR>
* 1) The circumstances to make this behavior seize control of the robot.
* e.g. When the touch sensor determines the robot has collided with an object.<BR>
* 2) The action to perform when this behavior takes control.
* e.g. Back up and turn.<BR>
* 3) A way to quickly exit from the action when the Arbitrator selects a higher
* priority behavior to take control.
* These are represented by defining the methods takeControl(), action(),
* and suppress() respectively. <BR>
* A behavior control system has one or more Behavior objects. When you have defined
* these objects, create an array of them and use that array to initialize an
* Arbitrator object.
*
* @see Arbitrator
* @version 0.9 May 2011
*/
public interface Behavior {
/**
* The boolean return indicates if this behavior should seize control of the robot.
* For example, a robot that reacts if a touch sensor is pressed: <BR>
* public boolean takeControl() { <BR>
* return touch.isPressed(); <BR>
* } <BR>
* @return boolean Indicates if this Behavior should seize control.
*/
public boolean takeControl();
/**
* The code in action() represents the tasks the robot performs when this
* behavior becomes active. It can be as complex as navigating around a
* room, or as simple as playing a tune.<BR>
* <B>The contract for implementing this method is:</B><BR>
* If its task is is complete, the method returns.
* It also <B> must </B> return promptly when the suppress() method
* is called, for example by testing the boolean suppress flag. <br>
* When this method exits, the robot is in a safe state for another behavior
* to run its action() method
*/
public void action();
/**
* The code in suppress() should cause the current behavior to exit. <BR>
* <B>The contract for implementing this method is:</B><BR>
* Exit quickly, for example, just set boolean flag.
*/
public void suppress();
}

View File

@@ -0,0 +1,120 @@
package miky;
import robocode.*;
import java.util.List;
import java.util.LinkedList;
/**
* SubsumpRobot - a robot by (your name here)
*/
public class SubsumpRobot extends Robot {
abstract class SubsumptionBehavior {
public abstract boolean takeControl();
public abstract void action();
}
class WanderBehavior extends SubsumptionBehavior {
public boolean takeControl() {
return true;
}
public void action() {
ahead(200);
if (Math.random() > 0.5)
turnLeft(30.0);
else
turnRight(30.0);
scan();
}
}
class MoveToRobotBehavior extends SubsumptionBehavior {
public boolean takeControl() {
return scannedRobot != null;
}
public void action() {
turnRight(scannedRobot.getBearing());
ahead(50);
}
}
class FireBehavior extends SubsumptionBehavior {
public boolean takeControl() {
return scannedRobot != null && scannedRobot.getDistance() < 200;
}
public void action() {
fire(3);
}
}
class AvoidWallBehavior extends SubsumptionBehavior {
public boolean takeControl() {
return hitEvent != null;
}
public void action() {
hitEvent = null;
scannedRobot = null;
back(100);
turnLeft(180.0);
}
}
private List<SubsumptionBehavior> behaviors;
private ScannedRobotEvent scannedRobot;
private Event hitEvent;
public SubsumpRobot() {
super();
hitEvent = null;
scannedRobot = null;
behaviors = new LinkedList<SubsumptionBehavior>();
}
public void addBehavior(SubsumptionBehavior behavior) {
behaviors.add(behavior);
}
public void removeBehavior(SubsumptionBehavior behavior) {
behaviors.remove(behavior);
}
/**
* run: SubsumpRobot's default behavior
*/
public void run() {
int behavior_index = 0;
// Add behaviors.
behaviors.add(new AvoidWallBehavior());
behaviors.add(new FireBehavior());
behaviors.add(new MoveToRobotBehavior());
behaviors.add(new WanderBehavior());
while(true) {
for (SubsumptionBehavior b : behaviors) {
if (b.takeControl()) {
b.action();
break;
}
}
}
}
public void onScannedRobot(ScannedRobotEvent e) {
scannedRobot = e;
}
public void onHitWall(HitWallEvent e) {
hitEvent = e;
}
public void onHitRobot(HitRobotEvent e) {
hitEvent = e;
}
public void onRobotDeath(RobotDeathEvent e) {
scannedRobot = null;
}
}