From 3ef383157dfe2d0d520738ed67862285a9531f64 Mon Sep 17 00:00:00 2001 From: Axel1092 Date: Thu, 2 Jul 2020 11:32:31 +0200 Subject: [PATCH] Fixed recorder for traffic lights in standalone mode. --- .../Source/Carla/Sensor/WorldObserver.cpp | 8 +- .../Source/Carla/Traffic/TrafficLightBase.cpp | 152 +++++++++++++----- .../Source/Carla/Traffic/TrafficLightBase.h | 14 +- .../Carla/Traffic/TrafficLightComponent.cpp | 10 ++ .../Carla/Traffic/TrafficLightComponent.h | 4 + .../Carla/Traffic/TrafficLightGroup.cpp | 7 +- .../Source/Carla/Traffic/TrafficLightGroup.h | 5 +- .../Carla/Traffic/TrafficLightManager.cpp | 15 +- 8 files changed, 159 insertions(+), 56 deletions(-) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp index 1c1d47675..58c47b1fb 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/WorldObserver.cpp @@ -70,8 +70,8 @@ static auto FWorldObserver_GetActorState(const FActorView &View, const FActorReg auto TrafficLight = Cast(View.GetActor()); if (TrafficLight != nullptr) { - UTrafficLightComponent* TrafficLightComponent = - Cast(TrafficLight->FindComponentByClass()); + auto* TrafficLightComponent = + TrafficLight->GetTrafficLightComponent(); using TLS = carla::rpc::TrafficLightState; @@ -89,8 +89,8 @@ static auto FWorldObserver_GetActorState(const FActorView &View, const FActorReg } else { - UTrafficLightController* Controller = TrafficLightComponent->GetController(); - ATrafficLightGroup* Group = TrafficLightComponent->GetGroup(); + const UTrafficLightController* Controller = TrafficLightComponent->GetController(); + const ATrafficLightGroup* Group = TrafficLightComponent->GetGroup(); if (!Controller) { diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp index 5dd7cada3..cb5a8d3c9 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.cpp @@ -53,7 +53,7 @@ void ATrafficLightBase::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); - if (TimeIsFrozen) + if (TimeIsFrozen || TrafficLightComponent) { return; } @@ -103,14 +103,26 @@ void ATrafficLightBase::PostEditChangeProperty(FPropertyChangedEvent &Event) } #endif // WITH_EDITOR +ETrafficLightState ATrafficLightBase::GetTrafficLightState() const +{ + if (TrafficLightComponent) + { + return TrafficLightComponent->GetLightState(); + } + else + { + return State; + } +} + void ATrafficLightBase::SetTrafficLightState(const ETrafficLightState InState) { - UTrafficLightComponent* TrafficLightComponent = - Cast(FindComponentByClass()); - - if(TrafficLightComponent) { + if(TrafficLightComponent) + { TrafficLightComponent->SetLightState(InState); - } else { + } + else + { ElapsedTime = 0.0f; State = InState; SetTrafficSignState(ToTrafficSignState(State)); @@ -185,83 +197,128 @@ void ATrafficLightBase::UnNotifyWheeledVehicle(ACarlaWheeledVehicle *Vehicle) void ATrafficLightBase::SetGreenTime(float InGreenTime) { - UTrafficLightComponent* TrafficLightComponent = - Cast(FindComponentByClass()); - - if(TrafficLightComponent) { + if(TrafficLightComponent) + { UTrafficLightController* TrafficLightController = - TrafficLightComponent->GetController(); + TrafficLightComponent->GetController(); check(TrafficLightController) TrafficLightController->SetGreenTime(InGreenTime); - } else { + } + else + { GreenTime = InGreenTime; } } float ATrafficLightBase::GetGreenTime() const { - return GreenTime; + if (TrafficLightComponent) + { + auto* Controller = TrafficLightComponent->GetController(); + check(Controller); + return Controller->GetGreenTime(); + } + else + { + return GreenTime; + } } void ATrafficLightBase::SetYellowTime(float InYellowTime) { - UTrafficLightComponent* TrafficLightComponent = - Cast(FindComponentByClass()); - - if(TrafficLightComponent) { + if(TrafficLightComponent) + { UTrafficLightController* TrafficLightController = TrafficLightComponent->GetController(); check(TrafficLightController) TrafficLightController->SetYellowTime(InYellowTime); - } else { + } + else + { YellowTime = InYellowTime; } } float ATrafficLightBase::GetYellowTime() const { - return YellowTime; + if (TrafficLightComponent) + { + auto* Controller = TrafficLightComponent->GetController(); + check(Controller); + return Controller->GetYellowTime(); + } + else + { + return YellowTime; + } } void ATrafficLightBase::SetRedTime(float InRedTime) { - UTrafficLightComponent* TrafficLightComponent = - Cast(FindComponentByClass()); - - if(TrafficLightComponent) { + if(TrafficLightComponent) + { UTrafficLightController* TrafficLightController = TrafficLightComponent->GetController(); check(TrafficLightController) TrafficLightController->SetRedTime(InRedTime); - } else { + } + else + { RedTime = InRedTime; } } float ATrafficLightBase::GetRedTime() const { - return RedTime; + if (TrafficLightComponent) + { + auto* Controller = TrafficLightComponent->GetController(); + check(Controller); + return Controller->GetRedTime(); + } + else + { + return RedTime; + } } float ATrafficLightBase::GetElapsedTime() const { - return ElapsedTime; + if (TrafficLightComponent) + { + auto* Group = TrafficLightComponent->GetGroup(); + check(Group); + return Group->GetElapsedTime(); + } + else + { + return ElapsedTime; + } } void ATrafficLightBase::SetElapsedTime(float InElapsedTime) { - ElapsedTime = InElapsedTime; + if (TrafficLightComponent) + { + auto* Group = TrafficLightComponent->GetGroup(); + check(Group); + return Group->SetElapsedTime(InElapsedTime); + } + else + { + ElapsedTime = InElapsedTime; + } } void ATrafficLightBase::SetTimeIsFrozen(bool InTimeIsFrozen) { - UTrafficLightComponent* TrafficLightComponent = - Cast(FindComponentByClass()); - - if(TrafficLightComponent) { + if(TrafficLightComponent) + { TrafficLightComponent->SetFrozenGroup(InTimeIsFrozen); - } else { + } + else + { TimeIsFrozen = InTimeIsFrozen; if (!TimeIsFrozen) { @@ -272,6 +329,12 @@ void ATrafficLightBase::SetTimeIsFrozen(bool InTimeIsFrozen) bool ATrafficLightBase::GetTimeIsFrozen() const { + if(TrafficLightComponent) + { + auto* Group = TrafficLightComponent->GetGroup(); + check(Group); + return Group->IsFrozen(); + } return TimeIsFrozen; } @@ -287,10 +350,8 @@ int ATrafficLightBase::GetPoleIndex() const TArray ATrafficLightBase::GetGroupTrafficLights() const { - UTrafficLightComponent* TrafficLightComponent = - Cast(FindComponentByClass()); - - if(TrafficLightComponent) { + if(TrafficLightComponent) + { TArray result; ATrafficLightGroup* Group = TrafficLightComponent->GetGroup(); @@ -313,3 +374,22 @@ void ATrafficLightBase::SetGroupTrafficLights(TArray InGrou { GroupTrafficLights = InGroupTrafficLights; } + +UTrafficLightComponent* ATrafficLightBase::CreateTrafficLightComponent() +{ + TrafficLightComponent = NewObject(RootComponent); + TrafficLightComponent->RegisterComponent(); + TrafficLightComponent->AttachToComponent( + GetRootComponent(), + FAttachmentTransformRules::KeepRelativeTransform); + return TrafficLightComponent; +} + +UTrafficLightComponent* ATrafficLightBase::GetTrafficLightComponent() +{ + return TrafficLightComponent; +} +const UTrafficLightComponent* ATrafficLightBase::GetTrafficLightComponent() const +{ + return TrafficLightComponent; +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.h index fb9dc0669..53bdcd979 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightBase.h @@ -9,6 +9,7 @@ #include "Traffic/TrafficSignBase.h" #include "Traffic/TrafficLightState.h" +#include "Traffic/TrafficLightComponent.h" #include "TrafficLightBase.generated.h" @@ -39,10 +40,7 @@ protected: public: UFUNCTION(Category = "Traffic Light", BlueprintCallable) - ETrafficLightState GetTrafficLightState() const - { - return State; - } + ETrafficLightState GetTrafficLightState() const; UFUNCTION(Category = "Traffic Light", BlueprintCallable) void SetTrafficLightState(ETrafficLightState State); @@ -99,6 +97,11 @@ public: // used from replayer void SetElapsedTime(float InElapsedTime); + UTrafficLightComponent* CreateTrafficLightComponent(); + + UTrafficLightComponent* GetTrafficLightComponent(); + const UTrafficLightComponent* GetTrafficLightComponent() const; + protected: UFUNCTION(Category = "Traffic Light", BlueprintImplementableEvent) @@ -132,4 +135,7 @@ private: UPROPERTY(Category = "Traffic Light", VisibleAnywhere) TArray GroupTrafficLights; + + UPROPERTY(Category = "Traffic Light", EditAnywhere) + UTrafficLightComponent * TrafficLightComponent = nullptr; }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp index e08957032..e4fbadf0c 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.cpp @@ -114,11 +114,21 @@ ATrafficLightGroup* UTrafficLightComponent::GetGroup() return TrafficLightGroup; } +const ATrafficLightGroup* UTrafficLightComponent::GetGroup() const +{ + return TrafficLightGroup; +} + UTrafficLightController* UTrafficLightComponent::GetController() { return TrafficLightController; } +const UTrafficLightController* UTrafficLightComponent::GetController() const +{ + return TrafficLightController; +} + void UTrafficLightComponent::OnOverlapTriggerBox(UPrimitiveComponent *OverlappedComp, AActor *OtherActor, UPrimitiveComponent *OtherComp, diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h index b38e41158..c6d7a1d60 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightComponent.h @@ -41,9 +41,13 @@ public: UFUNCTION(Category = "Traffic Light", BlueprintPure) ATrafficLightGroup* GetGroup(); + const ATrafficLightGroup* GetGroup() const; + UFUNCTION(Category = "Traffic Light", BlueprintPure) UTrafficLightController* GetController(); + const UTrafficLightController* GetController() const; + virtual void InitializeSign(const carla::road::Map &Map) override; protected: diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightGroup.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightGroup.cpp index 195eb632d..f28c860c2 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightGroup.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightGroup.cpp @@ -22,7 +22,7 @@ void ATrafficLightGroup::SetFrozenGroup(bool InFreeze) bIsFrozen = InFreeze; } -bool ATrafficLightGroup::IsFrozen() +bool ATrafficLightGroup::IsFrozen() const { return bIsFrozen; } @@ -47,6 +47,11 @@ float ATrafficLightGroup::GetElapsedTime() const return (CurrentStateTimer - Timer); } +void ATrafficLightGroup::SetElapsedTime(float ElapsedTime) +{ + Timer = CurrentStateTimer - ElapsedTime; +} + // Called every frame void ATrafficLightGroup::Tick(float DeltaTime) { diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightGroup.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightGroup.h index 2f9442b8f..eef904c7e 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightGroup.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightGroup.h @@ -35,7 +35,7 @@ public: void SetFrozenGroup(bool InFreeze); UFUNCTION(Category = "Traffic Group", BlueprintCallable) - bool IsFrozen(); + bool IsFrozen() const; UFUNCTION(Category = "Traffic Group", BlueprintPure) int GetJunctionId() const; @@ -46,6 +46,9 @@ public: UFUNCTION(Category = "Traffic Group", BlueprintCallable) float GetElapsedTime() const; + UFUNCTION(Category = "Traffic Group", BlueprintCallable) + void SetElapsedTime(float ElapsedTime); + protected: // Called every frame virtual void Tick(float DeltaTime) override; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightManager.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightManager.cpp index 101407eec..54105157e 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightManager.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightManager.cpp @@ -178,7 +178,7 @@ void ATrafficLightManager::BeginPlay() Super::BeginPlay(); // Should not run in empty maps - if (!GetMap()) + if (!GetMap()) { return; } @@ -213,11 +213,11 @@ void ATrafficLightManager::ResetTrafficLightObjects() } TrafficControllers.Empty(); - for (TActorIterator It(GetWorld()); It; ++It) + for (TActorIterator It(GetWorld()); It; ++It) { - ATrafficSignBase* trafficSignBase = (*It); + ATrafficLightBase* trafficSignBase = (*It); UTrafficLightComponent* TrafficLightComponent = - trafficSignBase->FindComponentByClass(); + trafficSignBase->GetTrafficLightComponent(); if(TrafficLightComponent) { @@ -290,13 +290,8 @@ void ATrafficLightManager::SpawnTrafficLights() TrafficSigns.Add(TrafficLight); - UTrafficLightComponent *TrafficLightComponent = - NewObject(TrafficLight); + UTrafficLightComponent *TrafficLightComponent = TrafficLight->CreateTrafficLightComponent(); TrafficLightComponent->SetSignId(SignalId.c_str()); - TrafficLightComponent->RegisterComponent(); - TrafficLightComponent->AttachToComponent( - TrafficLight->GetRootComponent(), - FAttachmentTransformRules::KeepRelativeTransform); auto ClosestWaypointToSignal = GetMap()->GetClosestWaypointOnRoad(CarlaTransform.location);