예제 #1
0
    public void PreviousState()
    {
        switch (state)
        {
        case TrafficLightState.RED:
            state       = TrafficLightState.YELLOW;
            elapsedTime = yellowTime;
            break;

        case TrafficLightState.YELLOW:
            state       = TrafficLightState.GREEN;
            elapsedTime = greenTime;

            break;

        case TrafficLightState.GREEN:
            state = TrafficLightState.RED;
            break;
        }

        foreach (trafficLightController cont in TFcontrollers)
        {
            if ((int)state == (int)TrafficLightState.LENGHT - 1)
            {
                cont.changeState(0, (int)state);
            }
            else
            {
                cont.changeState((int)state + 1, (int)state);
            }
        }
    }
예제 #2
0
 public PedestrianTrafficLight()
 {
     states = new TrafficLightState[] {
         new TrafficLightState(ColorIndicator.RED, 60.0f, Icon.STOP_MAN),
         new TrafficLightState(ColorIndicator.GREEN, 30.0f, Icon.WALK_MAN)
     };
 }
예제 #3
0
    public void NextState()
    {
        elapsedTime = 0;
        switch (state)
        {
        case TrafficLightState.RED:
            state = TrafficLightState.GREEN;
            break;

        case TrafficLightState.YELLOW:
            state = TrafficLightState.RED;
            break;

        case TrafficLightState.GREEN:
            state = TrafficLightState.YELLOW;
            break;
        }

        foreach (trafficLightController cont in TFcontrollers)
        {
            if ((int)state == 0)
            {
                cont.changeState((int)TrafficLightState.LENGHT - 1, (int)state);
            }
            else
            {
                cont.changeState((int)state - 1, (int)state);
            }
        }
    }
예제 #4
0
 public void SetMasterStateTo(TrafficLightState state)
 {
     foreach (var light in TrafficLights)
     {
         light.State = state;
     }
 }
예제 #5
0
    private void Update()
    {
        if (!hasStarted)
        {
            return;
        }

        switch (State)
        {
        case TrafficLightState.Red:
            if (_redTimeLeft <= 0)
            {
                State        = TrafficLightState.Green;
                _redTimeLeft = DefaultRedTime;
                break;
            }
            _redTimeLeft -= Time.deltaTime;
            break;

        case TrafficLightState.Green:
            if (_greenTimeLeft <= 0)
            {
                State          = TrafficLightState.Red;
                _greenTimeLeft = DefaultGreenTime;
            }
            _greenTimeLeft -= Time.deltaTime;
            break;
        }
    }
예제 #6
0
 public TramTrafficLight()
 {
     states = new TrafficLightState[] {
         new TrafficLightState(ColorIndicator.RED, 30.0f),
         new TrafficLightState(ColorIndicator.GREEN, 30.0f)
     };
 }
예제 #7
0
    /*
     * Toggles traffic light left side
     */
    IEnumerator ToggleTrafficLightsL(TrafficLightState state, float delayTime)
    {
        yield return(new WaitForSeconds(delayTime));

        trafficLeft = state;

        switch (state)
        {
        case TrafficLightState.Red:
            vehicleRedLightL.SetActive(true);
            vehicleYellowLightL.SetActive(false);
            vehicleGreenLightL.SetActive(false);
            break;

        case TrafficLightState.Yellow:
            vehicleRedLightL.SetActive(false);
            vehicleYellowLightL.SetActive(true);
            vehicleGreenLightL.SetActive(false);
            break;

        case TrafficLightState.Green:
            vehicleRedLightL.SetActive(false);
            vehicleYellowLightL.SetActive(false);
            vehicleGreenLightL.SetActive(true);
            break;
        }
    }
예제 #8
0
        public static void TrafficCodeSample()
        {
            TrafficLightState originalLightState       = TrafficLightState.Yellow;
            TrafficLightState currentTrafficLightState = GetNextLight(originalLightState);

            Console.WriteLine(currentTrafficLightState.ToString());
        }
예제 #9
0
 public ClassicTrafficLight()
 {
     states = new TrafficLightState[] {
         new TrafficLightState(ColorIndicator.RED, 30.0f),
         new TrafficLightState(ColorIndicator.YELLOW, 1.5f),
         new TrafficLightState(ColorIndicator.GREEN, 45.0f)
     };
 }
 public TrafficLightSystem(Crossing crossing)
 {
     this.crossing = crossing;
     roadindex = 0;
     roadlist = new List<Road>();
     time = 0;
     trafficlightdictionary = new Dictionary<Road, List<TrafficLight>>();
     trafficlightstate = TrafficLightState.Yellow;
 }
예제 #11
0
        private void LightBulb(TrafficLightState lightValue)
        {
            foreach (var pair in this._gpioLightPins)
            {
                pair.Value.Write(pair.Key == lightValue ? GpioPinValue.High : GpioPinValue.Low);
            }

            this._currentState = lightValue;
        }
        public void GetTrafficLightState(
#if DEBUG
            ushort vehicleId,
            ref Vehicle vehicleData,
#endif
            ushort nodeId,
            ushort fromSegmentId,
            byte fromLaneIndex,
            ushort toSegmentId,
            ref NetSegment segmentData,
            uint frame,
            out TrafficLightState vehicleLightState,
            out TrafficLightState pedestrianLightState)
        {
            bool callStockMethod = true;

#if BENCHMARK
            using (var bm = new Benchmark(null, "callStockMethod")) {
#endif
            callStockMethod = !Options.timedLightsEnabled ||
                              !Instance.TrafficLightSimulations[nodeId].IsSimulationRunning();
#if BENCHMARK
        }
#endif

            if (callStockMethod)
            {
                RoadBaseAI.GetTrafficLightState(
                    nodeId,
                    ref segmentData,
                    frame,
                    out vehicleLightState,
                    out pedestrianLightState);
            }
            else
            {
#if BENCHMARK
                using (var bm = new Benchmark(null, "GetCustomTrafficLightState")) {
#endif
                GetCustomTrafficLightState(
#if DEBUG
                    vehicleId,
                    ref vehicleData,
#endif
                    nodeId,
                    fromSegmentId,
                    fromLaneIndex,
                    toSegmentId,
                    out vehicleLightState,
                    out pedestrianLightState,
                    ref Instance.TrafficLightSimulations[nodeId]);
#if BENCHMARK
            }
#endif
            }
        }
예제 #13
0
 public override void Update(float deltaTime)
 {
     base.Update(deltaTime);
     Counter += deltaTime;
     if (Counter >= PhaseLength[(int)State])
     {
         State   = (TrafficLightState)(((int)State + 1) % 3);
         Counter = 0;
     }
 }
예제 #14
0
        public void SetMasterStateTo(TrafficLightState state)
        {
            MasterTrafficLight.State = state;
            var polarState = TrafficLight.GetPolarState(state);

            foreach (var light in TrafficLights)
            {
                light.State = polarState;
            }
        }
        public void GetTrafficLightState(
#if DEBUG
            ushort vehicleId,
            ref Vehicle vehicleData,
#endif
            ushort nodeId,
            ushort fromSegmentId,
            byte fromLaneIndex,
            ushort toSegmentId,
            ref NetSegment segmentData,
            uint frame,
            out TrafficLightState vehicleLightState,
            out TrafficLightState pedestrianLightState,
            out bool vehicles,
            out bool pedestrians)
        {
            bool callStockMethod;

            using (var bm = Benchmark.MaybeCreateBenchmark(null, "callStockMethod")) {
                callStockMethod = !Options.timedLightsEnabled ||
                                  !Instance.TrafficLightSimulations[nodeId].IsSimulationRunning();
            }

            if (callStockMethod)
            {
                RoadBaseAI.GetTrafficLightState(
                    nodeId,
                    ref segmentData,
                    frame,
                    out vehicleLightState,
                    out pedestrianLightState,
                    out vehicles,
                    out pedestrians);
            }
            else
            {
                using (var bm = Benchmark.MaybeCreateBenchmark(null, "GetCustomTrafficLightState")) {
                    GetCustomTrafficLightState(
#if DEBUG
                        vehicleId,
                        ref vehicleData,
#endif
                        nodeId,
                        fromSegmentId,
                        fromLaneIndex,
                        toSegmentId,
                        out vehicleLightState,
                        out pedestrianLightState,
                        ref Instance.TrafficLightSimulations[nodeId]);
                }

                vehicles    = false;
                pedestrians = false;
            }
        }
예제 #16
0
 public void SetState(TrafficLightState newState)
 {
     if (newState == TrafficLightState.Green)
     {
         LightConeSprite.color = Color.green;
     }
     else
     {
         LightConeSprite.color = Color.red;
     }
 }
예제 #17
0
 void SetState(TrafficLightState newState)
 {
     State = newState;
     if (newState == TrafficLightState.Green)
     {
         lightConeSprite.color = Color.green;
     }
     else if (newState == TrafficLightState.Red)
     {
         lightConeSprite.color = Color.red;
     }
 }
예제 #18
0
 public void SetTrafficLightState(TrafficLightState state)
 {
     if (State != state)
     {
         LastStateNofSteps = 0;
         State             = state;
     }
     else
     {
         LastStateNofSteps++;
     }
 }
예제 #19
0
        /// <summary>
        /// draws the traffic light in the console
        /// </summary>
        /// <param name="tls"></param>
        static void RenderTraficLight(TrafficLightState tls)
        {
            Console.SetCursorPosition(StartingTraficLightLeft + 3, StartingTraficLightTop + 2);
            int x = Console.CursorLeft;
            int y = Console.CursorTop;

            for (int bulb = 1; bulb <= 3; bulb++)
            {
                string lightString = "";
                if (bulb == 1 && tls == TrafficLightState.RED)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.BackgroundColor = ConsoleColor.Red;
                    lightString             = ActiveLight;
                }
                if (bulb == 1 && tls != TrafficLightState.RED)
                {
                    lightString = NonActiveLight;
                }
                if (bulb == 2 && tls == TrafficLightState.YELLOW)
                {
                    Console.BackgroundColor = ConsoleColor.Yellow;
                    lightString             = ActiveLight;
                }
                else if (bulb == 2 && tls != TrafficLightState.YELLOW)
                {
                    lightString = NonActiveLight;
                }
                else if (bulb == 3 && tls == TrafficLightState.LEFTTURNGREEN)
                {
                    Console.ForegroundColor = ConsoleColor.Green;
                    lightString             = LeftGreenLight;
                }
                else if (bulb == 3 && tls == TrafficLightState.GREEN)
                {
                    Console.BackgroundColor = ConsoleColor.Green;
                    Console.ForegroundColor = ConsoleColor.Green;
                    lightString             = ActiveLight;
                }
                else if (bulb == 3 && tls != TrafficLightState.LEFTTURNGREEN && tls != TrafficLightState.GREEN)
                {
                    lightString = NonActiveLight;
                }
                foreach (char c in lightString)
                {
                    if (c is '\n')
                    {
                        Console.SetCursorPosition(x, ++y);
                    }
                    else if (Console.CursorLeft < Width - 1 && (!(c is ' ') || true))
                    {
                        Console.Write(c);
                    }
        private void LightBulb(TrafficLightState lightValue)
        {
            if (lightValue == TrafficLightState.Broken)
            {
                LightBulbs(new[] { TrafficLightState.Red, TrafficLightState.Green });
                return;
            }

            foreach (var pair in this._gpioLightPins)
            {
                pair.Value.Write(pair.Key == lightValue ? GpioPinValue.High : GpioPinValue.Low);
            }
        }
 public static bool Prefix(ushort nodeID,
                           ref NetSegment segmentData,
                           uint frame,
                           TrafficLightState vehicleLightState,
                           TrafficLightState pedestrianLightState,
                           bool vehicles,
                           bool pedestrians)
 {
     return(!Options.timedLightsEnabled ||
            !Constants.ManagerFactory
            .TrafficLightSimulationManager
            .TrafficLightSimulations[nodeID]
            .IsSimulationRunning());
 }
예제 #22
0
 void Start()
 {
     state = TrafficLightState.RED;
     if (yellowTime < 0)
     {
         yellowTime = 0;
     }
     elapsedTime = 0;
     collider    = GetComponent <BoxCollider>();
     foreach (trafficLightController cont in TFcontrollers)
     {
         cont.InitState();
     }
 }
    void OnTriggerStay(Collider col)
    {
        switch (col.gameObject.tag)
        {
        case Constants.Tag.TagVehicleStopZoneAvenueRight:
            TrafficLightsController trafficControllerR = col.transform.GetComponentInParent <TrafficLightsController>();
            trafficLightState = trafficControllerR.trafficRight;
            break;

        case Constants.Tag.TagVehicleStopZoneAvenueLeft:
            TrafficLightsController trafficControllerL = col.transform.GetComponentInParent <TrafficLightsController>();
            trafficLightState = trafficControllerL.trafficLeft;
            break;
        }
    }
예제 #24
0
    public void SetRandomState()
    {
        var rand = new Random();

        if (rand.Next(2) > 0)
        {
            _greenTimeLeft = (float)rand.NextDouble() * DefaultGreenTime;
            State          = TrafficLightState.Green;
        }
        else
        {
            _redTimeLeft = (float)rand.NextDouble() * DefaultRedTime;
            State        = TrafficLightState.Red;
        }
    }
    // Update is called once per frame
    private void Update()
    {
        // if selected then tell the info panel manager
        UpdateInforPanel();

        // Check if we need to change the status of the traffic light
        if (m_currentTimer > m_currentDelay)
        {
            // Reset the timer
            m_currentTimer = 0;

            // check if the config has changed due to the time of day
            CheckIfConfigChanged();

            // Set the current state of traffic light
            switch (m_CurrentState)
            {
            case TrafficLightState.Green:
                m_CurrentState = TrafficLightState.Amber;
                m_currentDelay = m_currentConfig.AmberDelay;
                break;

            case TrafficLightState.Amber:
                m_CurrentState = TrafficLightState.Red;
                m_currentDelay = m_currentConfig.RedDelay;
                break;

            case TrafficLightState.Red:
                m_CurrentState = TrafficLightState.RedAmber;
                m_currentDelay = m_currentConfig.RedAmberDelay;
                break;

            case TrafficLightState.RedAmber:
                m_CurrentState = TrafficLightState.Green;
                m_currentDelay = m_currentConfig.GreenDelay;
                break;

            default:
                break;
            }

            // Set the colour depending on the status
            SetColourOfTrafficLight();
        }

        m_currentTimer += Time.deltaTime;
    }
예제 #26
0
    /*
     * Toggles pedestrian light right side
     */
    IEnumerator TogglePedestrianLightsL(TrafficLightState state, float delayTime)
    {
        yield return(new WaitForSeconds(delayTime));

        switch (state)
        {
        case TrafficLightState.Red:
            pedestrianGreenLightL.SetActive(false);
            pedestrianRedLightL.SetActive(true);
            break;

        case TrafficLightState.Green:
            pedestrianGreenLightL.SetActive(true);
            pedestrianRedLightL.SetActive(false);
            break;
        }
    }
예제 #27
0
    /*
     * Resets all traffic lights to default
     */
    void ResetLights()
    {
        vehicleRedLightR.SetActive(false);
        vehicleYellowLightR.SetActive(false);
        vehicleGreenLightR.SetActive(true);
        pedestrianGreenLightR.SetActive(false);
        pedestrianRedLightR.SetActive(true);

        vehicleRedLightL.SetActive(false);
        vehicleYellowLightL.SetActive(false);
        vehicleGreenLightL.SetActive(true);
        pedestrianGreenLightL.SetActive(false);
        pedestrianRedLightL.SetActive(true);

        trafficRight = TrafficLightState.Green;
        trafficLeft  = TrafficLightState.Green;
    }
예제 #28
0
        public TrafficLight(TrafficLightState initialState, string name)
        {
            // Instantiate a new state machine
            _machine = new StateMachine <TrafficLightState, TrafficLightTrigger>(initialState);

            _machine.Configure(TrafficLightState.Red)
            .Permit(TrafficLightTrigger.PermitTraffic, TrafficLightState.Green)
            .Ignore(TrafficLightTrigger.WarnTraffic)
            .Ignore(TrafficLightTrigger.StopTraffic);

            _machine.Configure(TrafficLightState.Orange)
            .Permit(TrafficLightTrigger.StopTraffic, TrafficLightState.Red)
            .Ignore(TrafficLightTrigger.WarnTraffic);

            _machine.Configure(TrafficLightState.Green)
            .Permit(TrafficLightTrigger.WarnTraffic, TrafficLightState.Orange)
            .Ignore(TrafficLightTrigger.PermitTraffic);
        }
예제 #29
0

        
예제 #30
0
        private static void TrafficLightState_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MiniTrafficLight  sender = d as MiniTrafficLight;
            TrafficLightState state  = (TrafficLightState)e.NewState;

            if (state == TrafficLightState.Red)
            {
                //...
            }
            else if (state == TrafficLightState.Yellow)
            {
                //...
            }
            else
            {
                //...
            }
        }
예제 #31
0
        public void GetTrafficLightState(
#if DEBUG
            ushort vehicleId,
            ref Vehicle vehicleData,
#endif
            ushort nodeId,
            ushort fromSegmentId,
            byte fromLaneIndex,
            ushort toSegmentId,
            ref NetSegment segmentData,
            uint frame,
            out TrafficLightState vehicleLightState,
            out TrafficLightState pedestrianLightState)
        {
            bool callStockMethod = !Options.timedLightsEnabled ||
                                   !Instance.TrafficLightSimulations[nodeId].IsSimulationRunning();

            if (callStockMethod)
            {
                RoadBaseAI.GetTrafficLightState(
                    nodeId,
                    ref segmentData,
                    frame,
                    out vehicleLightState,
                    out pedestrianLightState);
            }
            else
            {
                GetCustomTrafficLightState(
#if DEBUG
                    vehicleId,
                    ref vehicleData,
#endif
                    nodeId,
                    fromSegmentId,
                    fromLaneIndex,
                    toSegmentId,
                    out vehicleLightState,
                    out pedestrianLightState,
                    ref Instance.TrafficLightSimulations[nodeId]);
            }
        }
예제 #32
0
        public Lane(string id, Server server, Vehicle type, settings Settings)
        {
            _id = id;
            _laneNr = Convert.ToInt32(id.Substring(1,1));
            _direction = (WindDirection) Enum.Parse(typeof(WindDirection),
                Enum.GetNames(typeof(WindDirection)).First((s) => s.StartsWith(id.Substring(0, 1))), true);

            _server = server;
            _vehicle = type;
            _state = TrafficLightState.Red;
            _orangeTime = Settings.orangeTime;
            BusDirections = new Queue<Direction>();

            int windPriority = _direction == WindDirection.North || _direction == WindDirection.South ? 10 : 0;
            switch (type)
            {
                case Vehicle.PEDESTRIAN:
                    _priority = 73;
                    break;
                case Vehicle.BICYCLE:
                    _priority = 100;
                    break;
                case Vehicle.CAR:
                    _priority = 200 + windPriority;
                    break;
                case Vehicle.BUS:
                    _priority = 300 + windPriority;
                    break;
            }

            _compatibilityList = new bool[][,] { _compatibilitySelf, _compatibilityLeft, _compatibilityStraight, _compatibilityRight };
        }
예제 #33
0
 public void SetTafficLight(TrafficLightState state)
 {
     this._state = state;
     string arg = string.Format("{0},{1}",_id, (int)state);
     _server.RPCSendQueue.Enqueue(new RPCData(){type = 1, arg = arg});
 }
 public void SetAnyTrafficLights(Predicate<Lane> predicate, int timeOut, TrafficLightState state)
 {
     var disableList = _lanes.Values.ToList().FindAll(predicate);
     disableList.ForEach((l) => l.SetTafficLight(state, timeOut));
 }
예제 #35
0
 public void SetTafficLight(TrafficLightState state, int timeout)
 {
     timer = new Stopwatch();
     timer.Start();
     SetTafficLight(state);
     this._timeout = timeout * 1000;
 }
예제 #36
0
 // used to set the other lights based on StateN
 private void SetLightsFromN()
 {
     StateS = StateN;
     switch (StateN)
     {
         case TrafficLightState.RED:
             StateE = StateW = TrafficLightState.GREEN;
             break;
         case TrafficLightState.YELLOW:
             StateE = StateW = TrafficLightState.YELLOW;
             break;
         case TrafficLightState.GREEN:
             StateE = StateW = TrafficLightState.RED;
             break;
     }
 }
예제 #37
0
 // Sets the StateN and calls the next method
 private void SetStateN(TrafficLightState state)
 {
     StateN = state;
     SetLightsFromN();
 }
        public void Next(double seconds)
        {
            time += seconds;

            while ((trafficlightstate == TrafficLightState.Green && time > ParameterPanel.GreenTime) ||
                   (trafficlightstate == TrafficLightState.Yellow && time > ParameterPanel.YellowTime))
            {
                int current = roadlist[roadindex].GetVehiclesAtLastOfRoadToward(crossing.Location, checkdistance);

                int maximum;
                if (trafficlightstate == TrafficLightState.Green && (time > ParameterPanel.GreenTime || current == 0))
                {
                    maximum = roadlist.Max(road => road.GetVehiclesAtLastOfRoadToward(crossing.Location, checkdistance));

                    if (current == maximum)
                    {
                        if (current > 0)
                            time -= 1;
                        return;
                    }

                    foreach (TrafficLight trafficlight in trafficlightdictionary[roadlist[roadindex]])
                        trafficlight.TrafficLightState = TrafficLightState.Yellow;

                    trafficlightstate = TrafficLightState.Yellow;
                    time -= Math.Max(ParameterPanel.GreenTime, time);
                }

                if (trafficlightstate != TrafficLightState.Yellow || !(time > ParameterPanel.YellowTime)) continue;
                var subroadlist = roadlist.Where(road => road != roadlist[roadindex]).ToList();
                maximum = subroadlist.Max(
                    road => road.GetVehiclesAtLastOfRoadToward(crossing.Location, checkdistance));
                roadindex =
                    roadlist.IndexOf(
                        subroadlist.First(
                            road =>
                            road.GetVehiclesAtLastOfRoadToward(crossing.Location, checkdistance) == maximum));

                for (int i = 0; i < roadlist.Count; i++)
                    foreach (var trafficlight in trafficlightdictionary[roadlist[i]])
                        trafficlight.TrafficLightState = i != roadindex
                                                             ? TrafficLightState.Red
                                                             : TrafficLightState.Green;

                trafficlightstate = TrafficLightState.Green;
                time -= ParameterPanel.YellowTime;
            }
        }
예제 #39
0
        public void SwitchState()
        {
            if (this.state == TrafficLightState.Green)
            {
                this.state = TrafficLightState.Yellow;
            }
            else if (this.state == TrafficLightState.Yellow)
            {
                this.state = TrafficLightState.Red;
            }
            else
            {
                this.state = TrafficLightState.Green;
            }

            Console.ForegroundColor = this.GetColor();
            Console.WriteLine("Switched to state " + this.state);
        }