示例#1
0
    public void ChangeSumoTrafficLights(object sender, TrafficLight.TrafficLightChangeEventArgs trafficLightChangeEvent)
    {
        SumoTrafficLight sumoTrafficLight = sumoTrafficLights.Find(s => s.trafficLight.trafficLightId.Equals(trafficLightChangeEvent.trafficLight.trafficLightId));

        if (sumoTrafficLight == null)
        {
            Debug.Log("Unable to find sumo traffic light: " + trafficLightChangeEvent.trafficLight.trafficLightId);
            return;
        }
        string currentState = client.TrafficLight.GetState(sumoTrafficLight.junctionId).Content;
        string newState     = sumoTrafficLight.GetStateFromTrafficLightColour(currentState);

        client.TrafficLight.SetRedYellowGreenState(sumoTrafficLight.junctionId, newState);
    }
示例#2
0
    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++;
                        }
                    }
                }
            }
        }
    }