// Use this for initialization void Start() { // Init Colors colors = new List <Color> { new Color(153f / 255f, 0f, 0f), new Color(51f / 255f, 102f / 255f, 0f), new Color(0f, 102f / 255f, 204f / 255f), new Color(1f, 1f, 1f), new Color(0f, 0f, 0f), new Color(77f / 255f, 77f / 255f, 77f / 255f), new Color(97f / 255f, 83f / 255f, 63f / 255f), new Color(1f, 1f, 102f / 255f), new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)), new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)), new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)), new Color(UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f), UnityEngine.Random.Range(0f, 1f)) }; // Read paths from text files string sumoBinPath; if (sumoGUIEnabled) { sumoBinPath = System.IO.File.ReadAllText(Application.dataPath + "\\Resources\\sumoBinPath.dat") + "\\sumo-gui.exe"; } else { sumoBinPath = System.IO.File.ReadAllText(Application.dataPath + "\\Resources\\sumoBinPath.dat") + "\\sumo.exe"; } string mapNet = (System.IO.File.ReadAllText(Application.dataPath + "\\Resources\\sumoFilesPath.dat") + "\\map.net.xml").Replace("/", "\\"); string routeNet = (System.IO.File.ReadAllText(Application.dataPath + "\\Resources\\sumoFilesPath.dat") + "\\map.rou.xml").Replace("/", "\\"); // Other vehicles vehiclesInScene = new List <GameObject>(); vehicles = new Dictionary <string, SUMOCombinedPositionOrientation>(); vehicles3D = new Dictionary <string, GameObject>(); conn = new SumoTraciConnection(sumoBinPath, mapNet, routeNet); conn.AddOption("step-length", stepLengthSeconds + ""); conn.AddOption("start", ""); // start sumo immediately conn.AddOption("quit-on-end", ""); // stop sumo immediately after unity3D stopped // start Traci Server conn.RunServer(remoteIpAddress, Convert.ToInt32(this.remotePort)); if (enableEgoVehicle) { // Insert ego vehicle // Get random route for being able to insert the ego vehicle (A new vehicle must have a route assigned) SumoStringList routes = (SumoStringList)conn.Do_job_get(Route.GetIDList()); conn.Do_job_set(Vehicle.Add("egoVehicle", "DEFAULT_VEHTYPE", routes.Get(0), 0, 0, 0, (byte)0)); conn.Do_job_set(Vehicle.SetColor("egoVehicle", new SumoColor(127, 0, 0, 127))); } // Find all traffic lights trafficLights3D = new Dictionary <string, GameObject>(); SumoStringList trafficLights = (SumoStringList)conn.Do_job_get(Trafficlights.GetIDList()); foreach (string s in trafficLights.getList()) { var objects = Resources.FindObjectsOfTypeAll <GameObject>().Where(obj => obj.name.Equals("TrafficLight_" + s)); foreach (GameObject g in objects) { trafficLights3D.Add(s + g.transform.Find("index").GetChild(0).name, g); } } // Timer for Realtime-Simulation unity3DstartTime = Stopwatch.StartNew(); // Timer for traffic lights update (No need to do this every sim step) trafficLightsUpdate = Stopwatch.StartNew(); // Timer for watchdog watchdog = new Stopwatch(); }
// Update is called once per frame void Update() { watchdog.Start(); // Simulation step with realtime check if ((unity3DstartTime.ElapsedMilliseconds > stepLengthSeconds * 1000 * simStep) && (simStep < maxSimSteps)) { // Update egoVehicle coordinates for forwarding them to SUMO double xEgo = 0;; double yEgo = 0;; double angleEgo = 0;; if (enableEgoVehicle) { egoVehicle = GameObject.Find("egoVehicle_Peugot_WASD(Clone)"); if (egoVehicle == null) { egoVehicle = GameObject.Find("egoVehicle_Peugot(Clone)"); } xEgo = egoVehicle.transform.position.x; yEgo = egoVehicle.transform.position.z; angleEgo = egoVehicle.transform.rotation.eulerAngles.y; } // Only do next time step if it's necessary (not to fast) conn.Do_timestep(); simStep++; if (enableEgoVehicle) { // Update egoVehicle conn.Do_job_set(Vehicle.MoveToVTD("egoVehicle", "0", 0, xEgo, yEgo, angleEgo, 2)); } // Before updating all vehicles store the vehicles from last step for steering angle approximation (and prob. other interpolations) lastStepVehicles = vehicles.ToDictionary(i => i.Key, i => i.Value); // Get IDs of all vehicles in Simulation SumoStringList vehicleIDs = (SumoStringList)conn.Do_job_get(Vehicle.GetIDList()); vehicles.Clear(); foreach (string id in vehicleIDs.getList()) { SumoPosition3D position = (SumoPosition3D)conn.Do_job_get(Vehicle.GetPosition3D(id)); double yawAngle = (double)conn.Do_job_get(Vehicle.GetAngle(id)); vehicles.Add(id, new SUMOCombinedPositionOrientation(id, position, yawAngle)); } // Update all 3D vehicle models update3DVehiclesInScene(); // Update all traffic lights every refreshTimeTrafficLightsMS miliseconds if (trafficLightsUpdate.ElapsedMilliseconds > refreshTimeTrafficLightsMS) { updateAllTrafficLights(); trafficLightsUpdate.Reset(); trafficLightsUpdate.Start(); } } // Watchdog long stopTime = watchdog.ElapsedMilliseconds; if (stopTime > stepLengthSeconds * 1000) { watchDogCount++; print("Warning: Stepwidth of " + stepLengthSeconds * 1000 + "ms is too short for calculation loop, needed " + stopTime + "ms, Exceeded " + watchDogCount + " times, Current total offset: " + (unity3DstartTime.ElapsedMilliseconds - 1000 * simStep * stepLengthSeconds + stopTime) + " ms"); } watchdog.Reset(); }