Commit 46b4892b authored by Tobias Meuser's avatar Tobias Meuser
Browse files

Added tracking functionality to controller

parent 97c0ed5e
...@@ -12,6 +12,7 @@ import java.util.HashMap; ...@@ -12,6 +12,7 @@ import java.util.HashMap;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry;
import java.util.Random; import java.util.Random;
import de.tud.kom.p2psim.api.simengine.SimulatorObserver; import de.tud.kom.p2psim.api.simengine.SimulatorObserver;
...@@ -19,6 +20,7 @@ import de.tud.kom.p2psim.impl.simengine.Simulator; ...@@ -19,6 +20,7 @@ import de.tud.kom.p2psim.impl.simengine.Simulator;
import de.tud.kom.p2psim.impl.topology.PositionVector; import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.VehicleInformationContainer; import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.VehicleInformationContainer;
import de.tudarmstadt.maki.simonstrator.api.Randoms; import de.tudarmstadt.maki.simonstrator.api.Randoms;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location; import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.EdgeController; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.EdgeController;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor;
...@@ -31,6 +33,8 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.Road ...@@ -31,6 +33,8 @@ import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.Road
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.SerializableRoadNetwork; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.SerializableRoadNetwork;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoAdditionalRouteAvailableException; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoAdditionalRouteAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoExitAvailableException; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.exception.NoExitAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.paths.VehiclePathTracker;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.paths.VehiclePathTrackerFactory;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.DijkstraAlgorithm; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.DijkstraAlgorithm;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.RoutingAlgorithm; import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.routing.RoutingAlgorithm;
import de.tudresden.sumo.cmd.Edge; import de.tudresden.sumo.cmd.Edge;
...@@ -56,6 +60,8 @@ import it.polito.appeal.traci.SumoTraciConnection; ...@@ -56,6 +60,8 @@ import it.polito.appeal.traci.SumoTraciConnection;
public class TraciSimulationController implements VehicleController, SimulationSetupExtractor, EdgeController, SimulatorObserver { public class TraciSimulationController implements VehicleController, SimulationSetupExtractor, EdgeController, SimulatorObserver {
private static final File TEMP_FILE = new File(new File(System.getProperty("java.io.tmpdir")), "road_network.tmp"); private static final File TEMP_FILE = new File(new File(System.getProperty("java.io.tmpdir")), "road_network.tmp");
private static final boolean TRAIN_PATH_PROBABILITIES = true;
private Random _random = Randoms.getRandom(getClass()); private Random _random = Randoms.getRandom(getClass());
private static final Map<String, TraciSimulationController> CONTROLLER = new HashMap<>(); private static final Map<String, TraciSimulationController> CONTROLLER = new HashMap<>();
...@@ -110,7 +116,7 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -110,7 +116,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
} }
@Override @Override
public synchronized void init() { public synchronized void init(long pTimeScale) {
if (!_initalized) { if (!_initalized) {
Random random = Randoms.getRandom("SUMO"); Random random = Randoms.getRandom("SUMO");
...@@ -136,12 +142,23 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -136,12 +142,23 @@ public class TraciSimulationController implements VehicleController, SimulationS
for (int i = 0; i < _startTime; i++) { for (int i = 0; i < _startTime; i++) {
System.out.println("Pre-Start Setup: " + i + " of " + _startTime + " steps done."); System.out.println("Pre-Start Setup: " + i + " of " + _startTime + " steps done.");
try {
_connection.do_timestep(); if (TRAIN_PATH_PROBABILITIES) {
} catch (Exception e) { Map<String, VehicleInformationContainer> positions = nextStep();
throw new AssertionError(e); for (Entry<String, VehicleInformationContainer> entry : positions.entrySet()) {
VehiclePathTrackerFactory.getVehiclePathTracker().setEdge(entry.getKey(), entry.getValue().getRoute().getStart(), i * Time.SECOND);
}
} else {
try {
_connection.do_timestep();
} catch (Exception e) {
throw new AssertionError(e);
}
} }
} }
if (TRAIN_PATH_PROBABILITIES) {
VehiclePathTrackerFactory.getVehiclePathTracker().disableTracking();
}
} }
} }
...@@ -161,6 +178,13 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -161,6 +178,13 @@ public class TraciSimulationController implements VehicleController, SimulationS
return _positons.get(pVehicleID).getPosition(); return _positons.get(pVehicleID).getPosition();
} }
@Override
public double getVehicleLength(String pVehicleID) {
SumoCommand lengthCommand = Vehicle.getLength(pVehicleID);
Object requestObject = requestObject(lengthCommand);
return (Double) requestObject;
}
@Override @Override
public double getVehicleHeading(String pVehicleID) { public double getVehicleHeading(String pVehicleID) {
return _positons.get(pVehicleID).getHeading(); return _positons.get(pVehicleID).getHeading();
...@@ -191,10 +215,10 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -191,10 +215,10 @@ public class TraciSimulationController implements VehicleController, SimulationS
if (_observedAreaSet) { if (_observedAreaSet) {
if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) { if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) {
result.add(new PositionVector(sumoPosition.x - _startX, sumoPosition.y - _startY, 0)); result.add(new PositionVector(sumoPosition.x - _startX, sumoPosition.y - _startY));
} }
} else { } else {
result.add(new PositionVector(sumoPosition.x, sumoPosition.y, 0)); result.add(new PositionVector(sumoPosition.x, sumoPosition.y));
} }
} }
...@@ -220,54 +244,61 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -220,54 +244,61 @@ public class TraciSimulationController implements VehicleController, SimulationS
return result; return result;
} }
@Override private Map<String, VehicleInformationContainer> nextStep() {
public boolean nextStep() { try {
if (Simulator.getEndTime() == Simulator.getCurrentTime()) { _connection.do_timestep();
return false;
}
try {
_connection.do_timestep();
try { try {
synchronized (_positons) { Map<String, VehicleInformationContainer> vehiclePositions = new HashMap<>();
_positons.clear();
int temp = (Integer) _connection.do_job_get(Simulation.getCurrentTime()); List<String> allVehicles = requestAllVehicles();
for (String vehicle : allVehicles) {
Location position = requestVehiclePosition(vehicle);
if (position != null) {
double heading = requestVehicleHeading(vehicle);
double speed = requestVehicleSpeed(vehicle);
RoadNetworkRoute route = requestVehicleRoute(vehicle);
_step = temp / 1000.0; VehicleInformationContainer informationContainer = new VehicleInformationContainer(position, heading, speed, route);
if (_start == -1) { vehiclePositions.put(vehicle, informationContainer);
_start = _step; }
} }
return vehiclePositions;
} catch (Exception e) {
throw new AssertionError(e);
}
} catch (Exception e) {
throw new AssertionError(e);
}
}
Map<String, VehicleInformationContainer> vehiclePositions = new HashMap<>(); @Override
public boolean nextStep(long pTimeScale) {
if (Simulator.getEndTime() == Simulator.getCurrentTime()) {
return false;
}
Map<String, VehicleInformationContainer> vehiclePositions = nextStep();
List<String> allVehicles = requestAllVehicles(); try {
for (String vehicle : allVehicles) { int temp = (Integer) _connection.do_job_get(Simulation.getCurrentTime());
Location position = requestVehiclePosition(vehicle);
if (position != null) {
double heading = requestVehicleHeading(vehicle);
double speed = requestVehicleSpeed(vehicle);
RoadNetworkRoute route = requestVehicleRoute(vehicle);
VehicleInformationContainer informationContainer = new VehicleInformationContainer(position, heading, speed, route); _step = temp / (pTimeScale / (double)Time.MILLISECOND);
vehiclePositions.put(vehicle, informationContainer); if (_start == -1) {
} _start = _step;
} }
_positons = vehiclePositions; } catch (Exception e) {
} throw new AssertionError(e);
} catch (Exception e) { }
e.printStackTrace();
}
return true; if (vehiclePositions != null) {
} catch (RuntimeException e) { _positons = vehiclePositions;
throw e; return true;
} catch (Exception e) { } else {
e.printStackTrace(); _positons.clear();
return false;
} }
return false;
} }
private Location requestVehiclePosition(String pVehicleID) { private Location requestVehiclePosition(String pVehicleID) {
...@@ -287,7 +318,7 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -287,7 +318,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
if (_observedAreaSet) { if (_observedAreaSet) {
if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) { if (_startX <= sumoPosition.x && sumoPosition.x <= _endX && _startY <= sumoPosition.y && sumoPosition.y <= _endY) {
return new PositionVector(sumoPosition.x - _startX, sumoPosition.y - _startY, 0); return new PositionVector(sumoPosition.x - _startX, sumoPosition.y - _startY);
} else { } else {
double diffX = _startX - sumoPosition.x; double diffX = _startX - sumoPosition.x;
if (diffX < 0 || sumoPosition.x - _endX < diffX) { if (diffX < 0 || sumoPosition.x - _endX < diffX) {
...@@ -312,7 +343,7 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -312,7 +343,7 @@ public class TraciSimulationController implements VehicleController, SimulationS
return null; return null;
} }
} else { } else {
return new PositionVector(sumoPosition.x, sumoPosition.y, 0); return new PositionVector(sumoPosition.x, sumoPosition.y);
} }
} }
...@@ -329,7 +360,6 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -329,7 +360,6 @@ public class TraciSimulationController implements VehicleController, SimulationS
Object angleObject = requestObject(angleCommand); Object angleObject = requestObject(angleCommand);
if (angleObject != null) { if (angleObject != null) {
return (Double) angleObject; return (Double) angleObject;
} }
...@@ -844,8 +874,11 @@ public class TraciSimulationController implements VehicleController, SimulationS ...@@ -844,8 +874,11 @@ public class TraciSimulationController implements VehicleController, SimulationS
length += (double) object; length += (double) object;
} }
if ((_roadNetwork.getEdge(pEdgeID).getLaneAmount()) != 0) {
return length / (_roadNetwork.getEdge(pEdgeID).getLaneAmount()); return length / (_roadNetwork.getEdge(pEdgeID).getLaneAmount());
} else {
return 0.0;
}
} }
@Override @Override
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment