Browse Source

done

master
Lennart Weller 7 years ago
parent
commit
ea0e508a86
  1. 3
      src/main/java/de/ring0/lhw/footballesp/FootballEventProcessor.java
  2. 32
      src/main/java/de/ring0/lhw/footballesp/GoalKeeperClearsBolt.java
  3. 2
      src/main/java/de/ring0/lhw/footballesp/StdoutBolt.java
  4. 1
      src/main/java/de/ring0/lhw/footballesp/beans/Ball.java
  5. 14
      src/main/java/de/ring0/lhw/footballesp/beans/Keeper.java
  6. 6
      src/main/java/de/ring0/lhw/footballesp/beans/SensorEvent.java
  7. 4
      src/main/java/de/ring0/lhw/footballesp/beans/SensorObject.java

3
src/main/java/de/ring0/lhw/footballesp/FootballEventProcessor.java

@ -1,6 +1,5 @@
package de.ring0.lhw.footballesp;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@ -30,7 +29,7 @@ public class FootballEventProcessor {
System.out.println("Press any key to exit");
try {
new BufferedReader(new InputStreamReader(System.in)).readLine();
new InputStreamReader(System.in).read();
} catch (IOException e) {
e.printStackTrace();
}

32
src/main/java/de/ring0/lhw/footballesp/GoalKeeperClearsBolt.java

@ -1,9 +1,5 @@
package de.ring0.lhw.footballesp;
import java.util.Map;
import backtype.storm.task.OutputCollector;
import backtype.storm.task.TopologyContext;
import backtype.storm.topology.BasicOutputCollector;
import backtype.storm.topology.OutputFieldsDeclarer;
import backtype.storm.topology.base.BaseBasicBolt;
@ -17,16 +13,11 @@ import de.ring0.lhw.footballesp.beans.SensorObject;
public class GoalKeeperClearsBolt extends BaseBasicBolt {
private static final long serialVersionUID = 5116836905856288660L;
private static final short DISTANCE_TO_BALL = 5000;
private OutputCollector collector;
private static final double DISTANCE_TO_BALL = 3.5 * 1000;
private Ball currentBall;
private Keeper keeperA;
private Keeper keeperB;
private Keeper lastKeeper;
public void prepare(@SuppressWarnings("rawtypes") Map conf, TopologyContext context, OutputCollector collector) {
this.collector = collector;
}
@Override
public void declareOutputFields(OutputFieldsDeclarer arg0) {
@ -34,8 +25,8 @@ public class GoalKeeperClearsBolt extends BaseBasicBolt {
}
@Override
public void execute(Tuple arg0, BasicOutputCollector arg1) {
SensorEvent e = (SensorEvent) arg0.getValueByField("SensorEvent");
public void execute(Tuple tuple, BasicOutputCollector collector) {
SensorEvent e = (SensorEvent) tuple.getValueByField("SensorEvent");
if (e == null || !inField(e)) return;
//Process current sensor information and update accordingly
@ -43,11 +34,11 @@ public class GoalKeeperClearsBolt extends BaseBasicBolt {
if(e.sid < 13) {
if(currentBall == null) currentBall = new Ball(e);
else currentBall.setCurrentEvent(e);
} else if(e.sid > 96 && e.sid < 99) {
} else if(e.sid == 97 || e.sid == 98) {
if(keeperA == null) keeperA = new Keeper(e);
else keeperA.setCurrentEvent(e);
thisKeeper = keeperA;
} else if(e.sid > 99 && e.sid < 100) {
} else if(e.sid == 99 || e.sid == 100) {
if(keeperB == null) keeperB = new Keeper(e);
else keeperB.setCurrentEvent(e);
thisKeeper = keeperB;
@ -57,19 +48,20 @@ public class GoalKeeperClearsBolt extends BaseBasicBolt {
if (lastKeeper != null && distance3D(lastKeeper, currentBall) > DISTANCE_TO_BALL)
lastKeeper = null;
//No keeper had the ball close to his hands before and now there is one
if(lastKeeper == null && thisKeeper != null) {
int distance = distance3D(thisKeeper, currentBall);
if (distance <= DISTANCE_TO_BALL) {
if (distance3D(thisKeeper, currentBall) <= DISTANCE_TO_BALL) {
lastKeeper = thisKeeper;
thisKeeper.incrClearances();
collector.emit(new Values(thisKeeper));
}
}
}
private int distance3D(SensorObject e1, SensorObject e2) {
if (e1 == null || e2 == null) return 999999;
return (int) Math.sqrt(Math.pow(e1.getX() - e2.getX(), 2) + Math.pow(e1.getY() - e2.getY(), 2) + Math.pow(e1.getZ() - e2.getZ(), 2));
private double distance3D(SensorObject e1, SensorObject e2) {
if (e1 == null || e2 == null) return Double.NaN;
double distance = Math.sqrt(Math.pow(e1.getX() - e2.getX(), 2) + Math.pow(e1.getY() - e2.getY(), 2) + Math.pow(e1.getZ() - e2.getZ(), 2));
return distance;
}
private boolean inField(SensorEvent e) {

2
src/main/java/de/ring0/lhw/footballesp/StdoutBolt.java

@ -12,7 +12,7 @@ public class StdoutBolt extends BaseBasicBolt {
@Override
public void execute(Tuple arg0, BasicOutputCollector arg1) {
Keeper so = (Keeper) arg0.getValueByField("Keeper");
System.out.printf("%ld,%d,%d\n", so.getTimestamp(), so.getSID(), so.getClearances());
System.out.printf("%d,%d,%d\n", so.getTimestamp(), so.getSID(), so.getClearances());
}
@Override

1
src/main/java/de/ring0/lhw/footballesp/beans/Ball.java

@ -1,6 +1,7 @@
package de.ring0.lhw.footballesp.beans;
public class Ball extends SensorObject {
private static final long serialVersionUID = 7965981514762473964L;
public static int[] BALL_IDS = {4,8,10,12};
public Ball(SensorEvent e) {

14
src/main/java/de/ring0/lhw/footballesp/beans/Keeper.java

@ -1,18 +1,21 @@
package de.ring0.lhw.footballesp.beans;
import java.util.concurrent.atomic.AtomicInteger;
public class Keeper extends SensorObject {
private short nrClearances = 0;
private static final long serialVersionUID = -5955652404426363141L;
private AtomicInteger nrClearances = new AtomicInteger(0);
public Keeper(SensorEvent e) {
super(e);
}
public short getClearances() {
return nrClearances;
return nrClearances.shortValue();
}
public void incrClearances() {
this.nrClearances++;
this.nrClearances.getAndIncrement();
}
public Team getTeam() {
@ -23,4 +26,9 @@ public class Keeper extends SensorObject {
public String toString() {
return String.format("Keeper %s", getTeam() == Team.TEAM_A? "A": "B");
}
@Override
public boolean equals(Object o) {
return o instanceof Keeper && this.getTeam().equals(((Keeper)o).getTeam());
}
}

6
src/main/java/de/ring0/lhw/footballesp/beans/SensorEvent.java

@ -1,6 +1,10 @@
package de.ring0.lhw.footballesp.beans;
public class SensorEvent {
import java.io.Serializable;
public class SensorEvent implements Serializable {
private static final long serialVersionUID = -4883393333510044787L;
//Sensor id and timestamp
public byte sid;
public long ts;
//position from (0,0) (mid-field)

4
src/main/java/de/ring0/lhw/footballesp/beans/SensorObject.java

@ -1,7 +1,9 @@
package de.ring0.lhw.footballesp.beans;
public class SensorObject {
import java.io.Serializable;
public class SensorObject implements Serializable {
private static final long serialVersionUID = -17160524598345607L;
private SensorEvent currentEvent;
public SensorObject(SensorEvent e) {
currentEvent = e;

Loading…
Cancel
Save