CARLA
 
载入中...
搜索中...
未找到
TrafficLightComponent.cpp
浏览该文件的文档.
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
9#include "Kismet/GameplayStatics.h"
11#include "TrafficLightGroup.h"
13#include "TrafficLightManager.h"
17
21
22UTrafficLightComponent::UTrafficLightComponent()
23 : Super()
24{
25}
26
27void UTrafficLightComponent::InitializeSign(const carla::road::Map &Map)
28{
29 const double epsilon = 0.00001;
30
31 auto References = GetAllReferencesToThisSignal(Map);
32
33 for (auto& Reference : References)
34 {
35 auto RoadId = Reference.first;
36 const auto* SignalReference = Reference.second;
37 for(auto &validity : SignalReference->GetValidities())
38 {
39 for(auto lane : carla::geom::Math::GenerateRange(validity._from_lane, validity._to_lane))
40 {
41 if(lane == 0)
42 continue;
43
44 carla::road::element::Waypoint signal_waypoint;
45 boost::optional<carla::road::element::Waypoint> opt_signal_waypoint = Map.GetWaypoint(
46 RoadId, lane, SignalReference->GetS());
47 if(opt_signal_waypoint){
48 signal_waypoint = opt_signal_waypoint.get();
49 }else{
50 UE_LOG(LogCarla, Error, TEXT("signal_waypoint is not valid") );
51 continue;
52 }
53 // Prevent adding the bounding box inside the intersection
54 if (Map.IsJunction(RoadId)) {
55 auto predecessors = Map.GetPredecessors(signal_waypoint);
56 if (predecessors.size() == 1) {
57 auto predecessor = predecessors.front();
58 if (!Map.IsJunction(predecessor.road_id)) {
59 signal_waypoint = predecessor;
60 }
61 }
62 }
63
64 if(Map.GetLane(signal_waypoint).GetType() != cr::Lane::LaneType::Driving)
65 continue;
66
67 // Get 50% of the half size of the width of the lane
68 float BoxWidth = static_cast<float>(
69 0.5f*Map.GetLaneWidth(signal_waypoint)*0.5);
70 float BoxLength = 1.5f;
71 float BoxHeight = 1.0f;
72
73 // Prevent a situation where the road width is 0,
74 // this could happen in a lane that is just appearing
75 BoxWidth = std::max(0.01f, BoxWidth);
76 // Get min and max
77 double LaneLength = Map.GetLane(signal_waypoint).GetLength();
78 double LaneDistance = Map.GetLane(signal_waypoint).GetDistance();
79 // Safe distance to avoid overlapping the bounding box with the intersection
80 float AdditionalDistance = 1.5f;
81 if(lane < 0)
82 {
83 signal_waypoint.s = FMath::Clamp(signal_waypoint.s - (BoxLength + AdditionalDistance),
84 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
85 }
86 else
87 {
88 signal_waypoint.s = FMath::Clamp(signal_waypoint.s + (BoxLength + AdditionalDistance),
89 LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
90 }
91 FTransform BoxTransform = Map.ComputeTransform(signal_waypoint);
92 ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld());
93 if (LargeMapManager)
94 {
95 BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform);
96 }
97 GenerateTrafficLightBox(BoxTransform, FVector(100*BoxLength, 100*BoxWidth, 100*BoxHeight));
98 }
99 }
100 }
101}
102
103void UTrafficLightComponent::GenerateTrafficLightBox(const FTransform BoxTransform,
104 const FVector BoxSize)
105{
106 UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
107 BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &UTrafficLightComponent::OnBeginOverlapTriggerBox);
108 BoxComponent->OnComponentEndOverlap.AddDynamic(this, &UTrafficLightComponent::OnEndOverlapTriggerBox);
109 AddEffectTriggerVolume(BoxComponent);
110}
111
112void UTrafficLightComponent::SetLightState(ETrafficLightState NewState)
113{
114 LightState = NewState;
115 LightChangeDispatcher.Broadcast();
116 if (GetOwner()->Implements<UTrafficLightInterface>())
117 {
118 ITrafficLightInterface::Execute_LightChanged(GetOwner(), LightState);
119 }
120 ATrafficLightBase* OldTrafficLight = Cast<ATrafficLightBase>(GetOwner());
121 if (OldTrafficLight)
122 {
123 OldTrafficLight->LightChangedCompatibility(NewState);
124 }
125
126 for (auto Controller : Vehicles)
127 {
128 if (Controller != nullptr)
129 {
130 Controller->SetTrafficLightState(LightState);
131 }
132 }
133}
134
135ETrafficLightState UTrafficLightComponent::GetLightState() const
136{
137 return LightState;
138}
139
140void UTrafficLightComponent::SetFrozenGroup(bool InFreeze)
141{
142 if (GetGroup())
143 {
144 GetGroup()->SetFrozenGroup(InFreeze);
145 }
146}
147
148ATrafficLightGroup* UTrafficLightComponent::GetGroup()
149{
150 return TrafficLightController->GetGroup();
151}
152
153const ATrafficLightGroup* UTrafficLightComponent::GetGroup() const
154{
155 return TrafficLightController->GetGroup();
156}
157
158UTrafficLightController* UTrafficLightComponent::GetController()
159{
160 return TrafficLightController;
161}
162
163const UTrafficLightController* UTrafficLightComponent::GetController() const
164{
165 return TrafficLightController;
166}
167
168void UTrafficLightComponent::OnBeginOverlapTriggerBox(UPrimitiveComponent *OverlappedComp,
169 AActor *OtherActor,
170 UPrimitiveComponent *OtherComp,
171 int32 OtherBodyIndex,
172 bool bFromSweep,
173 const FHitResult &SweepResult)
174{
175 ACarlaWheeledVehicle * Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
176 if (Vehicle)
177 {
178 AWheeledVehicleAIController* VehicleController =
179 Cast<AWheeledVehicleAIController>(Vehicle->GetController());
180 if (VehicleController)
181 {
182 VehicleController->SetTrafficLightState(LightState);
183 Vehicles.Add(VehicleController);
184 VehicleController->SetTrafficLight(Cast<ATrafficLightBase>(GetOwner()));
185 }
186 }
187}
188
189void UTrafficLightComponent::OnEndOverlapTriggerBox(UPrimitiveComponent *OverlappedComp,
190 AActor *OtherActor,
191 UPrimitiveComponent *OtherComp,
192 int32 OtherBodyIndex)
193{
194 ACarlaWheeledVehicle * Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
195 if (Vehicle)
196 {
197 AWheeledVehicleAIController* VehicleController =
198 Cast<AWheeledVehicleAIController>(Vehicle->GetController());
199 if (VehicleController)
200 {
201 VehicleController->SetTrafficLightState(ETrafficLightState::Green);
202 VehicleController->SetTrafficLight(nullptr);
203 Vehicles.Remove(VehicleController);
204 }
205 }
206}
207
208void UTrafficLightComponent::SetController(UTrafficLightController* Controller)
209{
210 TrafficLightController = Controller;
211}
FVehicleLightState LightState
Definition ActorData.h:131
UE_LOG(LogCarla, Log, TEXT("UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor)
Base class for CARLA wheeled vehicles.
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
void LightChangedCompatibility(ETrafficLightState NewLightState)
如果你的类继承自UObject,你的类名上方需要加入 UCLASS() 宏 实现交通信号灯状态改变的类
带可选 AI 的轮式车辆控制器。
void SetTrafficLightState(ETrafficLightState InTrafficLightState)
设置当前影响此车辆的红绿灯状态。
void SetTrafficLight(ATrafficLightBase *InTrafficLight)
设置当前影响此车辆的红绿灯状态。
地图类的前向声明,用于在LaneInvasionSensor类中可能的引用。
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
从 OpenDrive 映射一个控制器。控制相关交通信号灯并包含其循环
CARLA模拟器的主命名空间。
Definition Carla.cpp:139