/*
* File name: FehlerAgent.java (package eas.simulation.users.lukas.fehlerSchaetzung)
* Author(s): aifb
* Java version: 6.0
* Generation date: 04.03.2011 (13:36:55)
*
* (c) This file and the EAS (Easy Agent Simulation) framework containing it
* is protected by Creative Commons by-nc-sa license. Any altered or
* further developed versions of this file have to meet the agreements
* stated by the license conditions.
*
* In a nutshell
* -------------
* You are free:
* - to Share -- to copy, distribute and transmit the work
* - to Remix -- to adapt the work
*
* Under the following conditions:
* - Attribution -- You must attribute the work in the manner specified by the
* author or licensor (but not in any way that suggests that they endorse
* you or your use of the work).
* - Noncommercial -- You may not use this work for commercial purposes.
* - Share Alike -- If you alter, transform, or build upon this work, you may
* distribute the resulting work only under the same or a similar license to
* this one.
*
* + Detailed license conditions (Germany):
* http://creativecommons.org/licenses/by-nc-sa/3.0/de/
* + Detailed license conditions (unported):
* http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en
*
* This header must be placed in the beginning of any version of this file.
*/
package eas.users.lukas.fehlerSchaetzung;
import java.util.List;
import eas.math.geometry.Rectangle2D;
import eas.math.geometry.Vector2D;
import eas.simulation.Wink;
import eas.simulation.agent.GenericActuator;
import eas.simulation.agent.GenericSensor;
import eas.simulation.spatial.sim2D.standardAgents.AbstractAgent2D;
import eas.simulation.spatial.sim2D.standardEnvironments.AbstractEnvironment2DFast;
import eas.startSetup.ParCollection;
/**
* @author aifb
*
*/
public class FehlerAgent extends AbstractAgent2D<AbstractEnvironment2DFast<?>> {
/**
*
*/
private static final long serialVersionUID = -2753694386979412488L;
/**
* @param id
* @param env
*/
public FehlerAgent(int id, AbstractEnvironment2DFast<?> env, ParCollection params) {
super(id, env, params);
this.addActuator(new GenericActuator<FehlerEnv, FehlerAgent>() {
/**
*
*/
private static final long serialVersionUID = -6606929720799171109L;
@Override
public void actuate(FehlerEnv env, FehlerAgent agent) {
env.differentialDriveForward(agent.id());
}
@Override
public String id() {
return "Drive";
}
});
this.addSensor(new GenericSensor<Double, FehlerEnv, FehlerAgent>() {
/**
*
*/
private static final long serialVersionUID = -3134093964045770627L;
@Override
public Double sense(FehlerEnv env, FehlerAgent agent) {
Vector2D pos = env.getAgentPosition(agent.id());
Vector2D liob = new Vector2D(pos);
Vector2D reun = new Vector2D(pos);
liob.sub(quadratRadius, quadratRadius);
reun.add(quadratRadius, quadratRadius);
Rectangle2D box = new Rectangle2D(liob, reun);
List<Integer> neighbors = env.calculateAgentsInBox(box);
double anzahl = 0;
double winkelSumme = 0;
for (int i : neighbors) {
if (i != agent.id()) {
winkelSumme += env.getAgentAngle(i);
anzahl++;
}
}
if (anzahl == 0) {
return env.getAgentAngle(agent.id());
}
return winkelSumme / anzahl;
}
@Override
public String id() {
return "NeighborAngle";
}
});
}
public final static double quadratRadius = 25;
@Override
public void step(Wink simTime) {
super.step(simTime);
if (simTime.getLastTick() == 0) {
this.getEnvironment().setWheelErrorLeft(this.id(), 0.03, 0.03);
this.getEnvironment().setWheelErrorRight(this.id(), 0.03, 0);
this.getEnvironment().setWheelSpeedLeft(this.id(), 3);
this.getEnvironment().setWheelSpeedRight(this.id(), 3);
}
double ownAngle = this.getEnvironment().getAgentAngle(this.id());
double neighborAngle = (Double) this.sense("NeighborAngle");
double buffer = 14;
double left = this.getEnvironment().getWheelSpeedLeft(this.id());
double right = this.getEnvironment().getWheelSpeedRight(this.id());
double correction = 1;
correction = 1.005;
if (ownAngle < neighborAngle - buffer) {
this.getEnvironment().setWheelSpeedLeft(this.id(), left * correction);
this.getEnvironment().setWheelSpeedRight(this.id(), right / correction);
} else if (ownAngle > neighborAngle + buffer) {
this.getEnvironment().setWheelSpeedLeft(this.id(), left / correction);
this.getEnvironment().setWheelSpeedRight(this.id(), right * correction);
}
this.actuate("Drive");
}
}