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  if (GetTrafficLightState() != ETrafficLightState::Green)
82  {
83  Vehicles.Add(Controller);
84  Controller->SetTrafficLight(this);
85  }
86  }
87  }
88 }
89 
91 {
92  if (IsValid(Vehicle))
93  {
94  auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
95  if (Controller != nullptr)
96  {
97  Controller->SetTrafficLight(nullptr);
98  Controller->SetTrafficLightState(ETrafficLightState::Green);
99  }
100  }
101 }
102 
103 void ATrafficLightBase::SetGreenTime(float InGreenTime)
104 {
106  {
107  UTrafficLightController* TrafficLightController =
108  TrafficLightComponent->GetController();
109  check(TrafficLightController)
110  TrafficLightController->SetGreenTime(InGreenTime);
112  }
113 }
114 
116 {
118  {
119  auto* Controller = TrafficLightComponent->GetController();
120  check(Controller);
121  return Controller->GetGreenTime();
122  }
123  else
124  {
125  return 0;
126  }
127 }
128 
129 void ATrafficLightBase::SetYellowTime(float InYellowTime)
130 {
132  {
133  UTrafficLightController* TrafficLightController =
134  TrafficLightComponent->GetController();
135  check(TrafficLightController)
136  TrafficLightController->SetYellowTime(InYellowTime);
138  }
139 }
140 
142 {
144  {
145  auto* Controller = TrafficLightComponent->GetController();
146  check(Controller);
147  return Controller->GetYellowTime();
148  }
149  else
150  {
151  return 0;
152  }
153 }
154 
155 void ATrafficLightBase::SetRedTime(float InRedTime)
156 {
158  {
159  UTrafficLightController* TrafficLightController =
160  TrafficLightComponent->GetController();
161  check(TrafficLightController)
162  TrafficLightController->SetRedTime(InRedTime);
164  }
165 }
166 
168 {
170  {
171  auto* Controller = TrafficLightComponent->GetController();
172  check(Controller);
173  return Controller->GetRedTime();
174  }
175  else
176  {
177  return 0;
178  }
179 }
180 
182 {
184  {
185  auto* Controller = TrafficLightComponent->GetController();
186  check(Controller);
187  return Controller->GetElapsedTime();
188  }
189  else
190  {
191  return 0;
192  }
193 }
194 
195 void ATrafficLightBase::SetElapsedTime(float InElapsedTime)
196 {
198  {
199  auto* Controller = TrafficLightComponent->GetController();
200  check(Controller);
201  return Controller->SetElapsedTime(InElapsedTime);
202  }
203 }
204 
205 void ATrafficLightBase::SetTimeIsFrozen(bool InTimeIsFrozen)
206 {
208  {
209  TrafficLightComponent->SetFrozenGroup(InTimeIsFrozen);
210  }
211 }
212 
214 {
216  {
217  auto* Group = TrafficLightComponent->GetGroup();
218  check(Group);
219  return Group->IsFrozen();
220  }
221  return false;
222 }
223 
224 void ATrafficLightBase::SetPoleIndex(int InPoleIndex)
225 {
226  PoleIndex = InPoleIndex;
227 }
228 
230 {
231  return PoleIndex;
232 }
233 
234 TArray<ATrafficLightBase *> ATrafficLightBase::GetGroupTrafficLights() const
235 {
237  {
238  TArray<ATrafficLightBase *> result;
239 
240  ATrafficLightGroup* Group = TrafficLightComponent->GetGroup();
241  check(Group)
242 
243  for(auto& Controller : Group->GetControllers())
244  {
245  for(auto& TLComp : Controller->GetTrafficLights())
246  {
247  result.Add(Cast<ATrafficLightBase>(TLComp->GetOwner()));
248  }
249  }
250 
251  return result;
252  }
253  return GroupTrafficLights;
254 }
255 
256 void ATrafficLightBase::SetGroupTrafficLights(TArray<ATrafficLightBase *> InGroupTrafficLights)
257 {
258  GroupTrafficLights = InGroupTrafficLights;
259 }
260 
262 {
263  return TrafficLightComponent;
264 }
265 const UTrafficLightComponent* ATrafficLightBase::GetTrafficLightComponent() const
266 {
267  return TrafficLightComponent;
268 }
269 
271 {
272  auto * Recorder = UCarlaStatics::GetRecorder(GetWorld());
273  if (Recorder && Recorder->IsEnabled())
274  {
275  Recorder->AddTrafficLightTime(*this);
276  }
277 }
278 
280 {
281  OnTrafficLightStateChanged(NewLightState);
282 }
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