CARLA
TrafficLightBase.cpp
Go to the documentation of this file.
1 // Copyright (c) 2017 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 
7 #include "Carla.h"
8 #include "TrafficLightBase.h"
12 
13 // =============================================================================
14 // -- Static local methods -----------------------------------------------------
15 // =============================================================================
16 
18 {
19  return ((Vehicle != nullptr) && !Vehicle->IsPendingKill());
20 }
21 
23 {
24  switch (State)
25  {
26  case ETrafficLightState::Green:
28  case ETrafficLightState::Yellow:
30  default:
31  case ETrafficLightState::Red:
33  }
34 }
35 
36 // =============================================================================
37 // -- ATrafficLightBase --------------------------------------------------------
38 // =============================================================================
39 
40 ATrafficLightBase::ATrafficLightBase(const FObjectInitializer &ObjectInitializer)
41  : Super(ObjectInitializer)
42 {
43  PrimaryActorTick.bCanEverTick = false;
44  TrafficLightComponent = CreateDefaultSubobject<UTrafficLightComponent>(TEXT("TrafficLightComponent"));
45  if(TrafficLightComponent && RootComponent)
46  {
47  TrafficLightComponent->SetupAttachment(RootComponent);
48  }
50 }
51 
53 {
55  {
56  return TrafficLightComponent->GetLightState();
57  }
58  else
59  {
60  return ETrafficLightState::Red;
61  }
62 }
63 
65 {
67  {
68  TrafficLightComponent->SetLightState(InState);
69  }
70  // OnTrafficLightStateChanged(State);
71 }
72 
74 {
75  if (IsValid(Vehicle))
76  {
77  auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
78  if (Controller != nullptr)
79  {
80  Controller->SetTrafficLightState(GetTrafficLightState());
81  Vehicles.Add(Controller);
82  Controller->SetTrafficLight(this);
83  }
84  }
85 }
86 
88 {
89  if (IsValid(Vehicle))
90  {
91  auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
92  if (Controller != nullptr)
93  {
94  Controller->SetTrafficLight(nullptr);
95  Controller->SetTrafficLightState(ETrafficLightState::Green);
96  }
97  }
98 }
99 
100 void ATrafficLightBase::SetGreenTime(float InGreenTime)
101 {
103  {
104  UTrafficLightController* TrafficLightController =
105  TrafficLightComponent->GetController();
106  check(TrafficLightController)
107  TrafficLightController->SetGreenTime(InGreenTime);
109  }
110 }
111 
113 {
115  {
116  auto* Controller = TrafficLightComponent->GetController();
117  check(Controller);
118  return Controller->GetGreenTime();
119  }
120  else
121  {
122  return 0;
123  }
124 }
125 
126 void ATrafficLightBase::SetYellowTime(float InYellowTime)
127 {
129  {
130  UTrafficLightController* TrafficLightController =
131  TrafficLightComponent->GetController();
132  check(TrafficLightController)
133  TrafficLightController->SetYellowTime(InYellowTime);
135  }
136 }
137 
139 {
141  {
142  auto* Controller = TrafficLightComponent->GetController();
143  check(Controller);
144  return Controller->GetYellowTime();
145  }
146  else
147  {
148  return 0;
149  }
150 }
151 
152 void ATrafficLightBase::SetRedTime(float InRedTime)
153 {
155  {
156  UTrafficLightController* TrafficLightController =
157  TrafficLightComponent->GetController();
158  check(TrafficLightController)
159  TrafficLightController->SetRedTime(InRedTime);
161  }
162 }
163 
165 {
167  {
168  auto* Controller = TrafficLightComponent->GetController();
169  check(Controller);
170  return Controller->GetRedTime();
171  }
172  else
173  {
174  return 0;
175  }
176 }
177 
179 {
181  {
182  auto* Controller = TrafficLightComponent->GetController();
183  check(Controller);
184  return Controller->GetElapsedTime();
185  }
186  else
187  {
188  return 0;
189  }
190 }
191 
192 void ATrafficLightBase::SetElapsedTime(float InElapsedTime)
193 {
195  {
196  auto* Controller = TrafficLightComponent->GetController();
197  check(Controller);
198  return Controller->SetElapsedTime(InElapsedTime);
199  }
200 }
201 
202 void ATrafficLightBase::SetTimeIsFrozen(bool InTimeIsFrozen)
203 {
205  {
206  TrafficLightComponent->SetFrozenGroup(InTimeIsFrozen);
207  }
208 }
209 
211 {
213  {
214  auto* Group = TrafficLightComponent->GetGroup();
215  check(Group);
216  return Group->IsFrozen();
217  }
218  return false;
219 }
220 
221 void ATrafficLightBase::SetPoleIndex(int InPoleIndex)
222 {
223  PoleIndex = InPoleIndex;
224 }
225 
227 {
228  return PoleIndex;
229 }
230 
231 TArray<ATrafficLightBase *> ATrafficLightBase::GetGroupTrafficLights() const
232 {
234  {
235  TArray<ATrafficLightBase *> result;
236 
237  ATrafficLightGroup* Group = TrafficLightComponent->GetGroup();
238  check(Group)
239 
240  for(auto& Controller : Group->GetControllers())
241  {
242  for(auto& TLComp : Controller->GetTrafficLights())
243  {
244  result.Add(Cast<ATrafficLightBase>(TLComp->GetOwner()));
245  }
246  }
247 
248  return result;
249  }
250  return GroupTrafficLights;
251 }
252 
253 void ATrafficLightBase::SetGroupTrafficLights(TArray<ATrafficLightBase *> InGroupTrafficLights)
254 {
255  GroupTrafficLights = InGroupTrafficLights;
256 }
257 
259 {
260  return TrafficLightComponent;
261 }
262 const UTrafficLightComponent* ATrafficLightBase::GetTrafficLightComponent() const
263 {
264  return TrafficLightComponent;
265 }
266 
268 {
269  auto * Recorder = UCarlaStatics::GetRecorder(GetWorld());
270  if (Recorder && Recorder->IsEnabled())
271  {
272  Recorder->AddTrafficLightTime(*this);
273  }
274 }
275 
277 {
278  OnTrafficLightStateChanged(NewLightState);
279 }
ATrafficLightBase(const FObjectInitializer &ObjectInitializer)
TArray< AWheeledVehicleAIController * > Vehicles
void UnNotifyWheeledVehicle(ACarlaWheeledVehicle *Vehicle)
UTrafficLightComponent * GetTrafficLightComponent()
ETrafficSignState
Class which implements the state changing of traffic lights.
UTrafficLightComponent * TrafficLightComponent
float GetGreenTime() const
void SetYellowTime(float InYellowTime)
ETrafficLightState GetTrafficLightState() const
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
void SetGreenTime(float InGreenTime)
static ETrafficSignState ToTrafficSignState(ETrafficLightState State)
void OnTrafficLightStateChanged(ETrafficLightState TrafficLightState)
TArray< UTrafficLightController * > & GetControllers()
void SetElapsedTime(float InElapsedTime)
TArray< ATrafficLightBase * > GetGroupTrafficLights() const
void LightChangedCompatibility(ETrafficLightState NewLightState)
float GetElapsedTime() const
TArray< ATrafficLightBase * > GroupTrafficLights
float GetRedTime() const
float GetYellowTime() const
void SetGroupTrafficLights(TArray< ATrafficLightBase *> InGroupTrafficLights)
void SetTimeIsFrozen(bool InTimeIsFrozen)
void NotifyWheeledVehicle(ACarlaWheeledVehicle *Vehicle)
Base class for CARLA wheeled vehicles.
Maps a controller from OpenDrive.
void SetRedTime(float InRedTime)
void SetTrafficLightState(ETrafficLightState State)
void SetTrafficSignState(ETrafficSignState State)
void SetPoleIndex(int InPoleIndex)
bool GetTimeIsFrozen() const
static ACarlaRecorder * GetRecorder(const UObject *WorldContextObject)
Definition: CarlaStatics.h:80