Commit 26c7897f authored by jimonreal's avatar jimonreal Committed by Jose Ignacio Monreal Bailey
Browse files

Traffic lights using RSU model.

Changes to RSU Model for PoC, if it works we will create a TL Placement and Movement model.
parent b7c5c6a6
......@@ -20,10 +20,7 @@
package de.tud.kom.p2psim.impl.topology.movement;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.*;
import de.tud.kom.p2psim.api.network.SimNetInterface;
import de.tud.kom.p2psim.api.topology.movement.MovementModel;
......@@ -32,16 +29,27 @@ import de.tud.kom.p2psim.impl.network.routed.RoutedNetLayer;
import de.tud.kom.p2psim.impl.topology.PositionVector;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.traci.TraciSimulationController;
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.xml.XMLSimulationController;
import de.tud.kom.p2psim.impl.vehicular.trafficlights.DefaultTrafficLightInformationComponent;
import de.tudarmstadt.maki.simonstrator.api.Event;
import de.tudarmstadt.maki.simonstrator.api.EventHandler;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.Time;
import de.tudarmstadt.maki.simonstrator.api.common.graph.INodeID;
import de.tudarmstadt.maki.simonstrator.api.component.ComponentNotAvailableException;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.TrafficLightController;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.trafficlights.TrafficLight;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.trafficlights.TrafficLightInformationComponent;
import de.tudarmstadt.maki.simonstrator.api.util.XMLConfigurableConstructor;
public class RSUMovementModel implements MovementModel {
public class RSUMovementModel implements MovementModel, EventHandler {
private final List<SimLocationActuator> components;
private final Map<String, SimLocationActuator> idComponentMatcher;
private final Queue<SimLocationActuator> freeComponents;
private final Map<INodeID, String> hostTrafficLightIdMatching;
private final Map<INodeID, Integer> hostIntersectionMatching;
private final String sumoExe;
private final String sumoConfigFile;
......@@ -54,19 +62,29 @@ public class RSUMovementModel implements MovementModel {
private final int height;
private final long timestepConversion = Time.SECOND;
private long timeBetweenMoveOperations;
private boolean initialized = false;
private boolean _reuseComponents = true;
private TrafficLightController _controller;
private SimulationSetupExtractor _extractor;
private SimulationSetupExtractor _controller;
private List<Location> _intersections;
private int _currentIndex = 0;
private final Map<INodeID, Integer> hostIntersectionMatching = new HashMap<>();
private static final Location DEFAULT_LOCATION = new PositionVector(Double.NaN, Double.NaN);
@XMLConfigurableConstructor({ "sumoExe", "sumoConfigFile", "offsetX", "offsetY", "width", "height" })
public RSUMovementModel(String sumoExe, String sumoConfigFile, String offsetX, String offsetY, String width, String height) {
@XMLConfigurableConstructor({ "timeBetweenMoveOperations", "sumoExe", "sumoConfigFile", "offsetX", "offsetY", "width", "height" })
public RSUMovementModel(long timeBetweenMoveOperations, String sumoExe, String sumoConfigFile, String offsetX, String offsetY, String width, String height) {
this.components = new LinkedList<>();
this.idComponentMatcher = new HashMap<>();
this.freeComponents = new LinkedList<>();
this.hostTrafficLightIdMatching = new HashMap<>();
this.hostIntersectionMatching = new HashMap<>();
this.timeBetweenMoveOperations = timeBetweenMoveOperations;
this.sumoExe = sumoExe;
this.sumoConfigFile = sumoConfigFile;
......@@ -79,10 +97,15 @@ public class RSUMovementModel implements MovementModel {
this.height = Integer.parseInt(height);
}
@XMLConfigurableConstructor({ "sumoIntersections", "offsetX", "offsetY", "width", "height" })
public RSUMovementModel(String sumoIntersections, String offsetX, String offsetY, String width, String height) {
@XMLConfigurableConstructor({ "timeBetweenMoveOperations", "sumoIntersections", "offsetX", "offsetY", "width", "height" })
public RSUMovementModel(long timeBetweenMoveOperations, String sumoIntersections, String offsetX, String offsetY, String width, String height) {
this.components = new LinkedList<>();
this.idComponentMatcher = new HashMap<>();
this.freeComponents = new LinkedList<>();
this.hostTrafficLightIdMatching = new HashMap<>();
this.hostIntersectionMatching = new HashMap<>();
this.timeBetweenMoveOperations = timeBetweenMoveOperations;
this.sumoIntersections = sumoIntersections;
this.sumoExe = null;
......@@ -97,6 +120,7 @@ public class RSUMovementModel implements MovementModel {
@Override
public final void addComponent(SimLocationActuator comp) {
components.add(comp);
freeComponents.add(comp);
}
@Override
......@@ -110,6 +134,7 @@ public class RSUMovementModel implements MovementModel {
initializeModel();
initialized = true;
}
if (_currentIndex < _intersections.size()) {
// Initial placement
Location intersection = _intersections.get(_currentIndex);
......@@ -120,18 +145,8 @@ public class RSUMovementModel implements MovementModel {
_currentIndex++;
//Put interfaces online
try {
RoutedNetLayer routedNetLayer = actuator.getHost().getComponent(RoutedNetLayer.class);
for (SimNetInterface netInterface : routedNetLayer.getSimNetworkInterfaces()) {
if (netInterface.isOffline()) {
netInterface.goOnline();
}
}
} catch (ComponentNotAvailableException e) {
e.printStackTrace();
}
} else {
actuator.updateCurrentLocation(new PositionVector(Double.NaN, Double.NaN));
actuator.updateCurrentLocation(DEFAULT_LOCATION);
}
}
......@@ -140,6 +155,10 @@ public class RSUMovementModel implements MovementModel {
* resulting movement trace.
*/
protected void initializeModel() {
if (!initialized) {
Event.scheduleWithDelay(timeBetweenMoveOperations, this, null, 0);
if (this.sumoExe != null) {
_controller = TraciSimulationController.createSimulationController(sumoExe, sumoConfigFile);
_controller.init();
......@@ -151,7 +170,121 @@ public class RSUMovementModel implements MovementModel {
_controller.setObservedArea(offsetX, offsetY, offsetX + width, offsetY + height);
_intersections = _controller.getAllIntersections(true);
}
}
// System.out.println("Require " + _intersections.size() + " RSUs");
}
@Override
public void eventOccurred(Object content, int type) {
//Call everyTrafficLight!
long currentTime = Time.getCurrentTime() / timestepConversion;
while (_controller.getStep() - _controller.getStart() < currentTime) {
if (!_controller.nextStep()) {
return;
}
}
//Assumption grid, Intersections and traffic lights are the same number
//Fix for robust usage
List<Location> intersections = _controller.getAllIntersections(true);
List<TrafficLight> trafficLights = _controller.getAllTrafficLights();
for (int i = 0 ; i < trafficLights.size() ; i++) {
TrafficLight trafficLight = trafficLights.get(i);
Location trafficLightLocation = intersections.get(i);
if (trafficLightLocation == null) {
trafficLights.remove(i--);
continue;
}
SimLocationActuator component = requestSimActuator(trafficLight.getTrafficLightId());
try {
RoutedNetLayer routedNetLayer = component.getHost().getComponent(RoutedNetLayer.class);
for (SimNetInterface simNetInterface : routedNetLayer.getSimNetworkInterfaces()) {
//if (simNetInterface.isOffline()) {
simNetInterface.goOnline();
//}
}
} catch (ComponentNotAvailableException e) {
e.printStackTrace();
}
component.updateCurrentLocation(intersections.get(i));
component.setMovementSpeed(0.00);
}
if (trafficLights.size() != idComponentMatcher.size()) {
ArrayList<String> registeredTrafficLights = new ArrayList<>(idComponentMatcher.keySet());
for (int i = 0 ; i < registeredTrafficLights.size() ; i++) {
String trafficLightId = registeredTrafficLights.get(i);
trafficLights.forEach(trafficLight -> {
if (trafficLight.getTrafficLightId().equalsIgnoreCase(trafficLightId)) {
addFreeHost(trafficLightId);
}
});
}
}
Event.scheduleWithDelay(timeBetweenMoveOperations,this, null, 0);
}
private void addFreeHost(String trafficLightId) {
if (idComponentMatcher.containsKey(trafficLightId)) {
SimLocationActuator simLocationActuator = idComponentMatcher.remove(trafficLightId);
if (simLocationActuator != null) {
try {
TrafficLightInformationComponent trafficLightInformationComponent = simLocationActuator.getHost().getComponent(TrafficLightInformationComponent.class);
trafficLightInformationComponent.resetTrafficLightId();
} catch (ComponentNotAvailableException e) {
//e.printStackTrace();
}
hostTrafficLightIdMatching.remove(simLocationActuator.getHost().getId());
try {
RoutedNetLayer routedNetLayer = simLocationActuator.getHost().getComponent(RoutedNetLayer.class);
for (SimNetInterface simNetInterface : routedNetLayer.getSimNetworkInterfaces()) {
if (simNetInterface.isOnline()) {
simNetInterface.goOffline();
}
}
} catch (ComponentNotAvailableException e) {
e.printStackTrace();
}
}
if (_reuseComponents) {
freeComponents.add(simLocationActuator);
}
}
}
private SimLocationActuator requestSimActuator(String pTrafficLightId) {
if (!idComponentMatcher.containsKey(pTrafficLightId)) {
SimLocationActuator simLocationActuator = freeComponents.poll();
if (simLocationActuator != null) {
TrafficLightInformationComponent trafficLightInformationComponent;
try {
trafficLightInformationComponent = simLocationActuator.getHost().getComponent(TrafficLightInformationComponent.class);
} catch (ComponentNotAvailableException e) {
Host host = simLocationActuator.getHost();
trafficLightInformationComponent = new DefaultTrafficLightInformationComponent(host, _controller, _extractor);
host.registerComponent(trafficLightInformationComponent);
}
trafficLightInformationComponent.setTrafficLightId(pTrafficLightId);
idComponentMatcher.put(pTrafficLightId, simLocationActuator);
hostTrafficLightIdMatching.put(simLocationActuator.getHost().getId(), pTrafficLightId);
} else {
throw new RuntimeException("Unable to assign new components. Please increase node amount" + (_reuseComponents?".":" or enable the reuse of components."));
}
}
return idComponentMatcher.get(pTrafficLightId);
}
}
\ No newline at end of file
......@@ -200,7 +200,7 @@ public class VehicleMovementModel implements MovementModel, EventHandler {
if (!initialized) {
initializeModel();
VehicleMovementModel.getRoadNetwork();
//VehicleMovementModel.getRoadNetwork();
initialized = true;
}
......
......@@ -275,6 +275,12 @@ public class TraciSimulationController implements VehicleController, SimulationS
execute(typeCommand);
}
@Override
public void setVehicleMaxSpeed(String pVehicleID, double pVehicleSpeed) {
SumoCommand maxSpeedCommand = Vehicle.setMaxSpeed(pVehicleID, pVehicleSpeed);
execute(maxSpeedCommand);
}
private Location requestVehiclePosition(String pVehicleID) {
if (_vehiclesOutOfRange.containsKey(pVehicleID)) {
if (_vehiclesOutOfRange.get(pVehicleID) < _step) {
......
......@@ -14,14 +14,18 @@ import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.contro
import de.tud.kom.p2psim.impl.topology.movement.vehicular.sumo.simulation.controller.csv.RoadSideUnitInformationHandler;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.TrafficLightController;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.VehicleController;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetwork;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkEdge;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkLink;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.RoadNetworkRoute;
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.trafficlights.TrafficLight;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.trafficlights.TrafficLightState;
public class XMLSimulationController implements VehicleController, SimulationSetupExtractor, Runnable {
public class XMLSimulationController implements VehicleController, SimulationSetupExtractor, Runnable, TrafficLightController {
private String _vehicleDataPath;
private String _roadSideUnitDataPath;
......@@ -67,6 +71,31 @@ public class XMLSimulationController implements VehicleController, SimulationSet
}
}
@Override
public double getStartX() {
return 0;
}
@Override
public double getStartY() {
return 0;
}
@Override
public double getEndX() {
return 0;
}
@Override
public double getEndY() {
return 0;
}
@Override
public boolean isObservedAreaSet() {
return false;
}
@Override
public Location getVehiclePosition(String pVehicleID) {
return getVehiclePosition(_step, pVehicleID);
......@@ -76,6 +105,21 @@ public class XMLSimulationController implements VehicleController, SimulationSet
return _vehicleDataInformationHandler.getVehiclePositions().get(pVehicleID);
}
@Override
public List<TrafficLight> getAllTrafficLights() {
return null;
}
@Override
public List<String> getControlledLanes(String pTrafficLightID) {
return null;
}
@Override
public Map<RoadNetworkLink, TrafficLightState> getTrafficLightStateForEachLane(String pTrafficLightID) {
return null;
}
@Override
public boolean nextStep() {
_vehicleDataInformationHandler.readNext();
......@@ -220,6 +264,11 @@ public class XMLSimulationController implements VehicleController, SimulationSet
_vehicleDataInformationHandler.getVehiclePositions().get(pVehicleID).setGap(gap);
}
@Override
public void setVehicleMaxSpeed(String pVehicleID, double pVehicleSpeed) {
_vehicleDataInformationHandler.getVehiclePositions().get(pVehicleID).setSpeed(pVehicleSpeed);
}
@Override
public double getVehicleSpeed(String pVehicleID) {
return _vehicleDataInformationHandler.getVehiclePositions().get(pVehicleID).getSpeed();
......
......@@ -144,6 +144,11 @@ public class DefaultVehicleInformationComponent implements VehicleInformationCom
controller.setVehicleGap(vehicleID, vehicleGap);
}
@Override
public void setVehicleMaxSpeed(String vehicleID, double vehicleSpeed) {
controller.setVehicleMaxSpeed(vehicleID, vehicleSpeed);
}
@Override
public void resetVehicleID() {
vehicleID = null;
......
package de.tud.kom.p2psim.impl.vehicular.trafficlights;
import de.tudarmstadt.maki.simonstrator.api.Host;
import de.tudarmstadt.maki.simonstrator.api.component.sensor.location.Location;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.SimulationSetupExtractor;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.api.TrafficLightController;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.trafficlights.TrafficLight;
import de.tudarmstadt.maki.simonstrator.api.component.vehicular.roadnetwork.trafficlights.TrafficLightInformationComponent;
import java.util.List;
public class DefaultTrafficLightInformationComponent implements TrafficLightInformationComponent {
private Host host;
private TrafficLightController controller;
private SimulationSetupExtractor extractor;
private String trafficLightId;
public DefaultTrafficLightInformationComponent(Host host, TrafficLightController controller, SimulationSetupExtractor extractor) {
this.host = host;
this.controller = controller;
this.extractor = extractor;
}
@Override
public void setTrafficLightId(String pTrafficLightId) {
trafficLightId = pTrafficLightId;
}
@Override
public void resetTrafficLightId() {
trafficLightId = null;
}
@Override
public boolean isValid() {
return trafficLightId != null;
}
@Override
public Location getTrafficLightLocation() {
List<TrafficLight> trafficLights = controller.getAllTrafficLights();
List<Location> intersections = controller.getAllIntersections(true);
Location trafficLightLocation = null;
for (int i = 0 ; i < trafficLights.size() ; i++) {
if (trafficLights.get(i).getTrafficLightId().equalsIgnoreCase(trafficLightId)) {
trafficLightLocation = intersections.get(i);
break;
}
}
return trafficLightLocation;
}
@Override
public String getTrafficLightId() {
return trafficLightId;
}
@Override
public void initialize() {
}
@Override
public void shutdown() {
}
@Override
public Host getHost() {
return host;
}
}
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