CARLA
SpeedLimitComponent.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 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 "SpeedLimitComponent.h"
11 #include "Game/CarlaStatics.h"
13 
15 #include "carla/geom/Transform.h"
19 
20 void USpeedLimitComponent::SetSpeedLimit(float Limit)
21 {
22  SpeedLimit = Limit;
23 }
24 
25 void USpeedLimitComponent::InitializeSign(const carla::road::Map &Map)
26 {
27  const double epsilon = 0.00001;
28 
29  auto References = GetAllReferencesToThisSignal(Map);
30  auto* Signal = GetSignal(Map);
31  if (Signal)
32  {
33  SetSpeedLimit(Signal->GetValue());
34  }
35 
36  for (auto& Reference : References)
37  {
38  auto RoadId = Reference.first;
39  const auto* SignalReference = Reference.second;
40  for(auto &val : SignalReference->GetValidities())
41  {
42  for(auto lane : carla::geom::Math::GenerateRange(val._from_lane, val._to_lane))
43  {
44  if(lane == 0)
45  continue;
46 
47  auto signal_waypoint = Map.GetWaypoint(
48  RoadId, lane, SignalReference->GetS()).get();
49 
50  if(Map.GetLane(signal_waypoint).GetType() != cr::Lane::LaneType::Driving)
51  continue;
52 
53  // Get 90% of the half size of the width of the lane
54  float BoxSize = static_cast<float>(
55  0.7f*Map.GetLaneWidth(signal_waypoint)*0.5);
56  // Prevent a situation where the road width is 0,
57  // this could happen in a lane that is just appearing
58  BoxSize = std::max(0.01f, BoxSize);
59  // Get min and max
60  double LaneLength = Map.GetLane(signal_waypoint).GetLength();
61  double LaneDistance = Map.GetLane(signal_waypoint).GetDistance();
62  if(lane < 0)
63  {
64  signal_waypoint.s = FMath::Clamp(signal_waypoint.s - BoxSize,
65  LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
66  }
67  else
68  {
69  signal_waypoint.s = FMath::Clamp(signal_waypoint.s + BoxSize,
70  LaneDistance + epsilon, LaneDistance + LaneLength - epsilon);
71  }
72  float UnrealBoxSize = 100*BoxSize;
73  FTransform BoxTransform = Map.ComputeTransform(signal_waypoint);
74  ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(GetWorld());
75  if (LargeMapManager)
76  {
77  BoxTransform = LargeMapManager->GlobalToLocalTransform(BoxTransform);
78  }
79  GenerateSpeedBox(BoxTransform, UnrealBoxSize);
80  }
81  }
82  }
83 
84 }
85 
86 void USpeedLimitComponent::GenerateSpeedBox(const FTransform BoxTransform, float BoxSize)
87 {
88  UBoxComponent* BoxComponent = GenerateTriggerBox(BoxTransform, BoxSize);
89  BoxComponent->OnComponentBeginOverlap.AddDynamic(this, &USpeedLimitComponent::OnOverlapBeginSpeedLimitBox);
90  AddEffectTriggerVolume(BoxComponent);
91 }
92 
93 void USpeedLimitComponent::OnOverlapBeginSpeedLimitBox(UPrimitiveComponent *OverlappedComp,
94  AActor *OtherActor,
95  UPrimitiveComponent *OtherComp,
96  int32 OtherBodyIndex,
97  bool bFromSweep,
98  const FHitResult &SweepResult)
99 {
100  ACarlaWheeledVehicle* Vehicle = Cast<ACarlaWheeledVehicle>(OtherActor);
101  if (Vehicle)
102  {
103  auto Controller = Cast<AWheeledVehicleAIController>(Vehicle->GetController());
104  if (Controller)
105  {
106  Controller->SetSpeedLimit(SpeedLimit);
107  }
108  }
109 }
LaneType GetType() const
Definition: Lane.cpp:38
boost::optional< element::Waypoint > GetWaypoint(const geom::Location &location, int32_t lane_type=static_cast< int32_t >(Lane::LaneType::Driving)) const
Definition: road/Map.cpp:212
const Lane & GetLane(Waypoint waypoint) const
======================================================================== – Road information --------...
Definition: road/Map.cpp:834
double GetDistance() const
Definition: Lane.cpp:46
double GetLength() const
Definition: Lane.cpp:51
geom::Transform ComputeTransform(Waypoint waypoint) const
Definition: road/Map.cpp:273
static std::vector< int > GenerateRange(int a, int b)
Definition: Math.cpp:151
FTransform GlobalToLocalTransform(const FTransform &InTransform) const
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
Definition: CarlaStatics.h:100
Base class for CARLA wheeled vehicles.
double GetLaneWidth(Waypoint waypoint) const
Definition: road/Map.cpp:285