CARLA
TrafficLightController.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 //
4 // This work is licensed under the terms of the MIT license.
5 // For a copy, see <https://opensource.org/licenses/MIT>.
6 
8 
10 {
11 
12 }
13 
14 void UTrafficLightController::SetStates(TArray<FTrafficLightStage> States)
15 {
16  LightStates = States;
17  ResetState();
18 }
19 
21 {
22  return LightStates[CurrentState];
23 }
24 
26 {
27  CurrentState = (CurrentState + 1) % LightStates.Num();
29  return GetCurrentState().Time;
30 }
31 
33 {
34  ElapsedTime += DeltaTime;
35 
36  if(ElapsedTime > GetCurrentState().Time)
37  {
38  ElapsedTime = 0;
39  if (IsCycleFinished())
40  {
41  return true;
42  }
43  NextState();
44  }
45  return false;
46 }
47 
49 {
50  ElapsedTime = 0;
51  CurrentState = 0;
53 }
54 
55 const TArray<UTrafficLightComponent *> &UTrafficLightController::GetTrafficLights()
56 {
57  return TrafficLights;
58 }
59 
61 {
62  TrafficLights.Empty();
64 }
65 
67 {
68  TrafficLights.Add(TrafficLight);
69  TrafficLight->SetController(this);
70 }
71 
73 {
74  TrafficLights.Remove(TrafficLight);
75 }
76 
78 {
79  TrafficLightCarlaActors.Add(CarlaActor);
80 }
81 
83 {
84  TrafficLightCarlaActors.Remove(CarlaActor);
85 }
86 
88 {
89  return ControllerId;
90 }
91 
93 {
94  ControllerId = Id;
95 }
96 
98 {
99  return CurrentState == (LightStates.Num() - 1);
100 }
101 
103 {
104  SetCurrentLightState(NewState);
105  for(auto *Light : TrafficLights)
106  {
107  Light->SetLightState(NewState);
108  }
110  {
111  Light->SetTrafficLightState(NewState);
112  }
113 }
114 
116 {
117  return Sequence;
118 }
119 
121 {
122  Sequence = InSequence;
123 }
124 
126 {
127  CurrentState = (LightStates.Num() - 1);
129  ElapsedTime = 0;
130 }
131 
133 {
134  SetStateTime(ETrafficLightState::Yellow, NewTime);
135 }
136 
138 {
139  SetStateTime(ETrafficLightState::Red, NewTime);
140 }
141 
143 {
144  SetStateTime(ETrafficLightState::Green, NewTime);
145 }
146 
148 {
149  return GetStateTime(ETrafficLightState::Green);
150 }
151 
153 {
154  return GetStateTime(ETrafficLightState::Yellow);
155 }
156 
158 {
159  return GetStateTime(ETrafficLightState::Red);
160 }
161 
163 {
164  for(auto& LightState : LightStates)
165  {
166  if(LightState.State == State)
167  {
168  LightState.Time = NewTime;
169  }
170  }
171 }
172 
174 {
175  for(auto& LightState : LightStates)
176  {
177  if(LightState.State == State)
178  {
179  return LightState.Time;
180  }
181  }
182  return 0.0f;
183 }
184 
186 {
187  return ElapsedTime;
188 }
189 
191 {
192  ElapsedTime = InElapsedTime;
193 }
194 
196 {
197  TrafficLightGroup = Group;
198 }
199 
201 {
202  return TrafficLightGroup;
203 }
204 
206 {
207  return TrafficLightGroup;
208 }
bool AdvanceTimeAndCycleFinished(float DeltaTime)
float GetStateTime(const ETrafficLightState State) const
void SetElapsedTime(float InElapsedTime)
TArray< FTrafficLightStage > LightStates
void SetControllerId(const FString &Id)
const FString & GetControllerId() const
void SetGroup(ATrafficLightGroup *Group)
Class which implements the state changing of traffic lights.
void SetTrafficLightsState(ETrafficLightState NewState)
Defines a stage of a semaphor with a State and the time this state lasts.
TArray< UTrafficLightComponent * > TrafficLights
void RemoveTrafficLight(UTrafficLightComponent *TrafficLight)
const FTrafficLightStage & GetCurrentState() const
void RemoveCarlaActorTrafficLight(FCarlaActor *CarlaActor)
TArray< FCarlaActor * > TrafficLightCarlaActors
void SetCurrentLightState(ETrafficLightState NewState)
void SetStates(TArray< FTrafficLightStage > States)
void SetSequence(int InSequence)
void AddCarlaActorTrafficLight(FCarlaActor *CarlaActor)
const TArray< UTrafficLightComponent * > & GetTrafficLights()
void SetStateTime(const ETrafficLightState State, float NewTime)
ATrafficLightGroup * TrafficLightGroup
ATrafficLightGroup * GetGroup()
void AddTrafficLight(UTrafficLightComponent *TrafficLight)
A view over an actor and its properties.
Definition: CarlaActor.h:23