// Start is called before the first frame update void Start() { client = new TraCIClient(); client.Connect("127.0.0.1", 4001); //connects to SUMO simulation tlightids = client.TrafficLight.GetIdList().Content; //all traffic light IDs in the simulation client.Gui.TrackVehicle("View #0", "0"); client.Gui.SetZoom("View #0", 1200); //tracking the player vehicle createTLS(); client.Control.SimStep(); client.Control.SimStep(); //making sure vehicle is loaded in client.Vehicle.SetSpeed("0", 0); //stops SUMO controlling player vehicle var shape = client.Vehicle.GetPosition("0").Content; var angle = client.Vehicle.GetAngle("0").Content; //puts the player vehicle in the starting position egoVehicle.transform.position = new Vector3((float)shape.X, 1.33f, (float)shape.Y); egoVehicle.transform.rotation = Quaternion.Euler(0, (float)angle, 0); carlist.Add(egoVehicle); }
static void Main(string[] args) { /* Create a TraCIClient for the commands */ var client = new TraCIClient(); var sumoCfgPath = DEFAULT_SUMOCFG; #region handling arguments if (args.Length > 0) { sumoCfgPath = args[0]; Console.WriteLine("Using sumocfg located at " + sumoCfgPath); } else { Console.WriteLine("No sumocfg file provided. Using default sumocfg: " + DEFAULT_SUMOCFG + "]"); } #endregion /* Create a new sumo process so the client can connect to it. * This step is optional if a sumo server is already running. */ var sumoProcess = ServeSumo(sumoCfgPath, 4321, useSumoGui: false, redirectOutputToConsole: false); if (sumoProcess == null) { Console.WriteLine("Something went wrong launching SUMO server. Maybe .sumocfg path is wrong" + "or sumo executables not defined in PATH.\n Sumo Configuration Path provided " + sumoCfgPath); } /* Connecting to Sumo Server is async but we wait for the task to complete for simplicity */ var task = client.ConnectAsync("127.0.0.1", 4321); while (!task.IsCompleted) /* Wait for task to be completed before using traci commands */ } {
private static void PrintDepartedVehicles(TraCIClient client) { vehicleIds = client.Simulation.GetDepartedIDList("ignored").Content; Console.Write("Departed Vehicles: ["); foreach (var id in vehicleIds) { Console.Write($"{id} ,"); } Console.WriteLine("]"); }
private static void PrintActiveVehicles(TraCIClient client) { vehicleIds = client.Vehicle.GetIdList().Content; Console.Write("Active Vehicles: ["); foreach (var id in vehicleIds) { Console.Write($"{id} ,"); } Console.WriteLine("]"); }
internal static T ExecuteGetCommand <T>(TraCIClient client, string id, byte commandType, byte messageType) { var command = GetCommand(id, commandType, messageType); var response = client.SendMessage(command); try { return((T)TraCIDataConverter.ExtractDataFromResponse(response, commandType, messageType)); } catch { throw; } }
internal static Tres ExecuteSetCommand <Tres, Tvalue>(TraCIClient client, string id, byte commandType, byte messageType, Tvalue value) { TraCICommand command = null; switch (value) { case int i: command = GetCommand(id, commandType, messageType, i); break; case double d: command = GetCommand(id, commandType, messageType, d); break; case string s: command = GetCommand(id, commandType, messageType, s); break; case List <string> los: command = GetCommand(id, commandType, messageType, los); break; default: { throw new InvalidCastException($"Type {value.GetType().Name} is not implemented in method TraCICommandHelper.ExecuteSetCommand()."); } } if (command != null) { var response = client.SendMessage(command); try { return((Tres)TraCIDataConverter.ExtractDataFromResponse(response, commandType, messageType)); } catch { throw; } } else { return(default(Tres)); } }
public void Get_Traffic_Lights() { TraCIClient the_client = Traci_GO.GetComponent <TraciController>().Client; if (the_client != null) { List <string> tl_ids = the_client.TrafficLight.GetIdList().Content; if (tl_ids == null) { return; } foreach (string id in tl_ids) { Traffic_Light tl = new Traffic_Light(); tl.Id = id; tl.ControlledLanes = the_client.TrafficLight.GetControlledLanes(id).Content; //List<List<string>> l = the_client.TrafficLight.GetControlledLinks(id).Content.Links; tl.Program = the_client.TrafficLight.GetCurrentProgram(id).Content; tl.PhaseDuration = (float)the_client.TrafficLight.GetPhaseDuration(id).Content; TL_List.Add(tl); } // A test traffic light complete program CodingConnected.TraCI.NET.Types.TrafficLightLogics tll = new CodingConnected.TraCI.NET.Types.TrafficLightLogics(); tll.SubId = "1"; tll.CurrentPhaseIndex = 0; tll.NumberOfPhases = 5; tll.TrafficLightPhases = new List <CodingConnected.TraCI.NET.Types.TrafficLightProgramPhase>(); for (int i = 0; i < 5; i++) { CodingConnected.TraCI.NET.Types.TrafficLightProgramPhase tp = new CodingConnected.TraCI.NET.Types.TrafficLightProgramPhase(); tp.Definition = program_def[i]; tll.TrafficLightPhases.Add(tp); } } }
public RouteCommands(TraCIClient client) : base(client) { }
// TODO: 'extended retrieval', see: http://sumo.dlr.de/wiki/TraCI/Vehicle_Value_Retrieval #endregion // Public Methods #region Constructor public VehicleCommands(TraCIClient client) : base(client) { }
public TrafficLightCommands(TraCIClient client) : base(client) { }
// TODO: 'extended retrieval', see: http://sumo.dlr.de/wiki/TraCI/Person_Value_Retrieval #endregion // Public Methods #region Constructor public PersonCommands(TraCIClient client) : base(client) { }
public ControlCommands(TraCIClient client) : base(client) { }
public MultiEntryExitDetectorCommands(TraCIClient client) : base(client) { }
public LaneAreaDetectorCommands(TraCIClient client) : base(client) { }
// TODO: 'extended retrieval', see: http://sumo.dlr.de/wiki/TraCI/Simulation_Value_Retrieval #endregion // Public Methods #region Constructor public SimulationCommands(TraCIClient client) : base(client) { }
public PolygonCommands(TraCIClient client) : base(client) { }
public JunctionCommands(TraCIClient client) : base(client) { }
protected TraCIContextSubscribableCommands(TraCIClient client) : base(client) { }
public InductionLoopCommands(TraCIClient client) : base(client) { }
internal static TraCIResponse <Tres> ExecuteSetCommand <Tres, Tvalue>(TraCIClient client, string id, byte commandType, byte messageType, Tvalue value) { TraCICommand command = null; switch (value) { case byte b: command = GetCommand(id, commandType, messageType, b); break; case int i: command = GetCommand(id, commandType, messageType, i); break; case double d: command = GetCommand(id, commandType, messageType, d); break; case string s: command = GetCommand(id, commandType, messageType, s); break; case List <string> los: command = GetCommand(id, commandType, messageType, los); break; case CompoundObject co: command = GetCommand(id, commandType, messageType, co); break; case Color c: command = GetCommand(id, commandType, messageType, c); break; case Position2D p2d: command = GetCommand(id, commandType, messageType, p2d); break; case Polygon p: command = GetCommand(id, commandType, messageType, p); break; case BoundaryBox bb: command = GetCommand(id, commandType, messageType, bb); break; default: { throw new InvalidCastException($"Type {value.GetType().Name} is not implemented in method TraCICommandHelper.ExecuteSetCommand()."); } } if (command != null) { var response = client.SendMessage(command); try { return(TraCIDataConverter.ExtractDataFromResponse <Tres>(response, commandType, messageType)); } catch { throw; } } else { return(default);
public GuiCommands(TraCIClient client) : base(client) { }
// TODO: 'extended retrieval', see: http://sumo.dlr.de/wiki/TraCI/Edge_Value_Retrieval #endregion // Public Methods #region Constructor public EdgeCommands(TraCIClient client) : base(client) { }
void Start() { vehicleFactory = FindObjectOfType <VehicleFactory>(); string filePath = System.IO.Path.Combine(Application.dataPath, "Sumo"); ImportAndGenerate.parseXMLfiles(filePath); ImportAndGenerate.CreateStreetNetwork(); client = new TraCIClient(); if (client.Connect(ip, port)) { Debug.Log("Connected to Sumo"); connected = true; } else { Debug.Log("Unable to connect to Sumo"); this.enabled = false; return; } FindObjectOfType <CameraManager>().frameRate = 60; StartCoroutine(Run()); vehicleFactory.StopAllCoroutines(); TrafficLightManager.GetInstance().RefreshTrafficLightsAndJunctions(); // Traffic Flow if (!IsControlledBySumo(SumoLinkControlPoint.TRAFFIC_FLOW)) { StartCoroutine(RunTraffic3DTrafficFlow()); } // Traffic Lights List <string> junctionIds = client.TrafficLight.GetIdList().Content; foreach (string id in junctionIds) { List <string> controlledLanes = client.TrafficLight.GetControlledLanes(id).Content; string currentState = client.TrafficLight.GetState(id).Content; for (int i = 0; i < controlledLanes.Count; i++) { TrafficLight trafficLight = TrafficLightManager.GetInstance().GetTrafficLight(controlledLanes[i]); if (trafficLight != null) { SumoTrafficLight sumoTrafficLight = sumoTrafficLights.Find(s => s.trafficLight.trafficLightId.Equals(trafficLight.trafficLightId)); if (sumoTrafficLight == null) { sumoTrafficLights.Add(new SumoTrafficLight(trafficLight, id, new HashSet <int>() { i })); } else { sumoTrafficLight.AddIndexState(i); } } } // Remove all current traffic light programs in Sumo if (!IsControlledBySumo(SumoLinkControlPoint.TRAFFIC_LIGHTS)) { client.TrafficLight.SetRedYellowGreenState(id, new string('r', currentState.Length)); client.TrafficLight.SetPhaseDuration(id, Double.MaxValue); } } if (IsControlledBySumo(SumoLinkControlPoint.TRAFFIC_LIGHTS)) { TrafficLightManager.GetInstance().StopAllCoroutines(); StartCoroutine(RunTrafficLights()); } else { TrafficLightManager.GetInstance().trafficLightChangeEvent += ChangeSumoTrafficLights; foreach (tlLogicType tlLogicType in ImportAndGenerate.trafficLightPrograms.Values) { int stateCounter = 0; Junction junction = FindObjectsOfType <Junction>().ToList().Find(j => j.junctionId.Equals(tlLogicType.id)); List <SumoTrafficLight> sumoTrafficLightsForJunction = sumoTrafficLights.FindAll(sumoTrafficLight => sumoTrafficLight.junctionId.Equals(tlLogicType.id)); foreach (object obj in tlLogicType.Items) { if (obj is phaseType) { stateCounter++; GameObject stateObject = new GameObject("State" + stateCounter); stateObject.transform.SetParent(junction.gameObject.transform); JunctionState junctionState = stateObject.AddComponent <JunctionState>(); junctionState.stateNumber = stateCounter; junctionState.trafficLightStates = new JunctionState.TrafficLightState[sumoTrafficLightsForJunction.Count()]; int trafficLightStateCounter = 0; phaseType phase = (phaseType)obj; foreach (SumoTrafficLight sumoTrafficLight in sumoTrafficLightsForJunction) { TrafficLight.LightColour lightColour = sumoTrafficLight.GetLightColourFromStateString(phase.state); junctionState.trafficLightStates[trafficLightStateCounter] = new JunctionState.TrafficLightState(sumoTrafficLight.trafficLight.trafficLightId, lightColour); trafficLightStateCounter++; } } } } } }
public POICommands(TraCIClient client) : base(client) { }
protected TraCICommandsBase(TraCIClient client) { Client = client; }
public LaneCommands(TraCIClient client) : base(client) { }