CARLA
 
载入中...
搜索中...
未找到
TrafficLightBase.cpp
浏览该文件的文档.
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
40ATrafficLightBase::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
100void 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
126void 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
152void 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
192void ATrafficLightBase::SetElapsedTime(float InElapsedTime)
193{
195 {
196 auto* Controller = TrafficLightComponent->GetController();
197 check(Controller);
198 return Controller->SetElapsedTime(InElapsedTime);
199 }
200}
201
202void 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
222{
223 PoleIndex = InPoleIndex;
224}
225
227{
228 return PoleIndex;
229}
230
231TArray<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
253void ATrafficLightBase::SetGroupTrafficLights(TArray<ATrafficLightBase *> InGroupTrafficLights)
254{
255 GroupTrafficLights = InGroupTrafficLights;
256}
257
259{
261}
262const UTrafficLightComponent* ATrafficLightBase::GetTrafficLightComponent() const
263{
265}
266
268{
269 auto * Recorder = UCarlaStatics::GetRecorder(GetWorld());
270 if (Recorder && Recorder->IsEnabled())
271 {
272 Recorder->AddTrafficLightTime(*this);
273 }
274}
275
static bool IsValid(const ACarlaWheeledVehicle *Vehicle)
static ETrafficSignState ToTrafficSignState(ETrafficLightState State)
ETrafficSignState
Base class for CARLA wheeled vehicles.
TArray< ATrafficLightBase * > GroupTrafficLights
void SetGreenTime(float InGreenTime)
UTrafficLightComponent * TrafficLightComponent
void SetRedTime(float InRedTime)
ATrafficLightBase(const FObjectInitializer &ObjectInitializer)
ETrafficLightState GetTrafficLightState() const
void SetTrafficLightState(ETrafficLightState State)
void SetPoleIndex(int InPoleIndex)
void OnTrafficLightStateChanged(ETrafficLightState TrafficLightState)
void LightChangedCompatibility(ETrafficLightState NewLightState)
float GetGreenTime() const
bool GetTimeIsFrozen() const
void SetYellowTime(float InYellowTime)
void NotifyWheeledVehicle(ACarlaWheeledVehicle *Vehicle)
float GetElapsedTime() const
float GetYellowTime() const
void SetElapsedTime(float InElapsedTime)
void UnNotifyWheeledVehicle(ACarlaWheeledVehicle *Vehicle)
void SetGroupTrafficLights(TArray< ATrafficLightBase * > InGroupTrafficLights)
TArray< AWheeledVehicleAIController * > Vehicles
TArray< ATrafficLightBase * > GetGroupTrafficLights() const
void SetTimeIsFrozen(bool InTimeIsFrozen)
UTrafficLightComponent * GetTrafficLightComponent()
Class which implements the state changing of traffic lights
TArray< UTrafficLightController * > & GetControllers()
void SetTrafficSignState(ETrafficSignState State)
static ACarlaRecorder * GetRecorder(const UObject *WorldContextObject)
Maps a controller from OpenDrive.