CARLA
TrafficLightController.h
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 
7 #pragma once
8 
9 #include "CoreMinimal.h"
10 #include "Object.h"
11 #include "TrafficLightState.h"
12 #include "TrafficLightComponent.h"
13 #include "Containers/Map.h"
14 #include "Carla/Actor/CarlaActor.h"
15 #include "TrafficLightController.generated.h"
16 
17 class ATrafficLightGroup;
18 
19 /// Defines a stage of a semaphor with a State and
20 /// the time this state lasts
21 USTRUCT(BlueprintType)
23 {
24  GENERATED_BODY()
25 
26  UPROPERTY(EditAnywhere, BlueprintReadWrite)
27  float Time;
28 
29  UPROPERTY(EditAnywhere, BlueprintReadWrite)
31 };
32 
33 /// Maps a controller from OpenDrive.
34 /// Controls the asociated traffic lights and contains its cycles
35 UCLASS(BlueprintType)
36 class CARLA_API UTrafficLightController : public UObject
37 {
38  GENERATED_BODY()
39 
40 public:
41 
43 
44  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
45  void SetStates(TArray<FTrafficLightStage> States);
46 
47  UFUNCTION(Category = "Traffic Controller", BlueprintPure)
48  const FTrafficLightStage &GetCurrentState() const;
49 
50  // Updates traffic light components to the next state
51  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
52  float NextState();
53 
54  // Advances the counter of the controller and returns true if The cicle is finished
55  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
56  bool AdvanceTimeAndCycleFinished(float DeltaTime);
57 
58  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
59  void StartCycle();
60 
61  UFUNCTION(Category = "Traffic Controller", BlueprintPure)
62  const TArray<UTrafficLightComponent *> &GetTrafficLights();
63 
64  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
65  void EmptyTrafficLights();
66 
67  UFUNCTION(Category = "Traffic Controller", BlueprintPure)
68  const FString &GetControllerId() const;
69 
70  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
71  void SetControllerId(const FString &Id);
72 
73  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
74  void AddTrafficLight(UTrafficLightComponent * TrafficLight);
75 
76  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
77  void RemoveTrafficLight(UTrafficLightComponent * TrafficLight);
78 
79  void AddCarlaActorTrafficLight(FCarlaActor* CarlaActor);
80 
81  void RemoveCarlaActorTrafficLight(FCarlaActor* CarlaActor);
82 
83  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
84  bool IsCycleFinished() const;
85 
86  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
87  void SetTrafficLightsState(ETrafficLightState NewState);
88 
89  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
90  int GetSequence() const;
91 
92  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
93  void SetSequence(int InSequence);
94 
95  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
96  void ResetState();
97 
98  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
99  void SetYellowTime(float NewTime);
100 
101  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
102  void SetRedTime(float NewTime);
103 
104  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
105  void SetGreenTime(float NewTime);
106 
107  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
108  float GetGreenTime() const;
109 
110  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
111  float GetYellowTime() const;
112 
113  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
114  float GetRedTime() const;
115 
116  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
117  float GetElapsedTime() const;
118 
119  UFUNCTION(Category = "Traffic Controller", BlueprintCallable)
120  void SetElapsedTime(float InElapsedTime);
121 
122  void SetGroup(ATrafficLightGroup* Group);
123 
124  ATrafficLightGroup* GetGroup();
125 
126  const ATrafficLightGroup* GetGroup() const;
127 
129  {
130  return CurrentLightState;
131  }
133  {
134  CurrentLightState = NewState;
135  }
136 
137 private:
138 
139  void SetStateTime(const ETrafficLightState State, float NewTime);
140 
141  float GetStateTime(const ETrafficLightState State) const;
142 
143  UPROPERTY(Category = "Traffic Controller", EditAnywhere)
144  FString ControllerId = "";
145 
146  UPROPERTY(Category = "Traffic Controller", EditAnywhere)
147  int CurrentState = 0;
148 
149  // Pairs with the state of the semaphors (time - state) e.g. 10s in green
150  UPROPERTY(Category = "Traffic Controller", EditAnywhere)
151  TArray<FTrafficLightStage> LightStates = {
152  {10, ETrafficLightState::Green},
153  { 3, ETrafficLightState::Yellow},
154  { 2, ETrafficLightState::Red}
155  };
156 
157  UPROPERTY(Category = "Traffic Controller", EditAnywhere)
158  TArray<UTrafficLightComponent *> TrafficLights;
159 
160  TArray<FCarlaActor *> TrafficLightCarlaActors;
161 
162  UPROPERTY(Category = "Traffic Controller", VisibleAnywhere)
163  ATrafficLightGroup* TrafficLightGroup;
164 
165  // Sequence within junction (unused for now)
166  UPROPERTY(Category = "Traffic Controller", EditAnywhere)
167  int Sequence = 0;
168 
169  UPROPERTY()
170  float ElapsedTime = 0;
171 
172  ETrafficLightState CurrentLightState = ETrafficLightState::Green;
173 };
Class which implements the state changing of traffic lights.
Defines a stage of a semaphor with a State and the time this state lasts.
void SetCurrentLightState(ETrafficLightState NewState)
ETrafficLightState GetCurrentLightState() const
Maps a controller from OpenDrive.
A view over an actor and its properties.
Definition: CarlaActor.h:23