From 84c599550913d0eea0d15f413ed7faecbd1735cb Mon Sep 17 00:00:00 2001 From: Axel Date: Thu, 3 Jun 2021 13:06:09 +0200 Subject: [PATCH] Updated CarlaRecorder and CarlaReplayer to use new FCarlaActor interface for vehicles, walkers and sensors. --- .../Carla/Source/Carla/Actor/CarlaActor.cpp | 4 +- .../Carla/Source/Carla/Actor/CarlaActor.h | 4 +- .../Carla/Source/Carla/Game/CarlaEpisode.h | 5 + .../Source/Carla/Recorder/CarlaRecorder.cpp | 19 ++- .../Carla/Recorder/CarlaReplayerHelper.cpp | 141 +++++++----------- .../Carla/Recorder/CarlaReplayerHelper.h | 8 +- .../Carla/Traffic/TrafficLightController.cpp | 1 + .../Carla/Traffic/TrafficLightController.h | 10 ++ 8 files changed, 89 insertions(+), 103 deletions(-) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/CarlaActor.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/CarlaActor.cpp index ff3d70c77..f268e65da 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/CarlaActor.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/CarlaActor.cpp @@ -803,7 +803,7 @@ ECarlaServerResponse FVehicleActor::GetVehicleControl(FVehicleControl& VehicleCo return ECarlaServerResponse::Success; } -ECarlaServerResponse FVehicleActor::SetActorAutopilot(bool bEnabled) +ECarlaServerResponse FVehicleActor::SetActorAutopilot(bool bEnabled, bool bKeepState) { if (IsDormant()) { @@ -820,7 +820,7 @@ ECarlaServerResponse FVehicleActor::SetActorAutopilot(bool bEnabled) { return ECarlaServerResponse::AutoPilotNotSupported; } - Controller->SetAutopilot(bEnabled); + Controller->SetAutopilot(bEnabled, bKeepState); } return ECarlaServerResponse::Success; } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/CarlaActor.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/CarlaActor.h index f8cd4b94d..ac75bb1cc 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/CarlaActor.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Actor/CarlaActor.h @@ -273,7 +273,7 @@ public: return ECarlaServerResponse::ActorTypeMismatch; } - virtual ECarlaServerResponse SetActorAutopilot(bool) + virtual ECarlaServerResponse SetActorAutopilot(bool, bool bKeepState = false) { return ECarlaServerResponse::ActorTypeMismatch; } @@ -431,7 +431,7 @@ public: virtual ECarlaServerResponse GetVehicleControl(FVehicleControl&) final; - virtual ECarlaServerResponse SetActorAutopilot(bool bEnabled) final; + virtual ECarlaServerResponse SetActorAutopilot(bool bEnabled, bool bKeepState = false) final; virtual ECarlaServerResponse EnableCarSim(const FString& SimfilePath) final; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h index 01d9454c0..f9177f082 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h @@ -136,6 +136,11 @@ public: return ActorDispatcher->GetActorRegistry(); } + FActorRegistry &GetActorRegistry() + { + return ActorDispatcher->GetActorRegistry(); + } + // =========================================================================== // -- Actor look up methods -------------------------------------------------- // =========================================================================== diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.cpp index 0fe3b4bb2..a012f4027 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaRecorder.cpp @@ -201,21 +201,20 @@ void ACarlaRecorder::AddWalkerAnimation(FCarlaActor *CarlaActor) void ACarlaRecorder::AddTrafficLightState(FCarlaActor *CarlaActor) { check(CarlaActor != nullptr); - - // get states - UTrafficLightController* Controller = - CarlaActor->GetTrafficLightController(); - if (Controller != nullptr) + // Todo: interface with FCarlaActor and UTrafficLightController + AActor *Actor = CarlaActor->GetActor(); + if(Actor) { - auto* Group = Controller->GetGroup(); - if (Group) + // get states + auto TrafficLight = Cast(Actor); + if (TrafficLight != nullptr) { AddState(CarlaRecorderStateTrafficLight { CarlaActor->GetActorId(), - Group->IsFrozen(), - Controller->GetElapsedTime(), - static_cast(Controller->GetCurrentState().State) + TrafficLight->GetTimeIsFrozen(), + TrafficLight->GetElapsedTime(), + static_cast(TrafficLight->GetTrafficLightState()) }); } } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.cpp index d2c09534c..26a039c16 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.cpp @@ -20,6 +20,8 @@ #include "Carla/Traffic/TrafficLightBase.h" #include "Carla/Vehicle/CarlaWheeledVehicle.h" #include "Engine/StaticMeshActor.h" +#include "Carla/Game/CarlaStatics.h" +#include "Carla/MapGen/LargeMapManager.h" #include #include @@ -38,16 +40,12 @@ std::pairCarlaReplayerHelper::TryToCreateReplayerActor( { check(Episode != nullptr); - FCarlaActor view_empty; - // check type of actor we need if (ActorDesc.Id.StartsWith("traffic.")) { - AActor *Actor = FindTrafficLightAt(Location); - if (Actor != nullptr) + FCarlaActor* CarlaActor = FindTrafficLightAt(Location); + if (CarlaActor != nullptr) { - // actor found - FCarlaActor* CarlaActor = Episode->FindCarlaActor(Actor); // reuse that actor return std::pair(2, CarlaActor); } @@ -85,6 +83,11 @@ std::pairCarlaReplayerHelper::TryToCreateReplayerActor( // relocate FTransform Trans2(Rot, Location, FVector(1, 1, 1)); Result.Value->GetActor()->SetActorTransform(Trans2, false, nullptr, ETeleportType::TeleportPhysics); + ALargeMapManager * LargeMapManager = UCarlaStatics::GetLargeMapManager(Episode->GetWorld()); + if (LargeMapManager) + { + LargeMapManager->OnActorSpawned(*Result.Value); + } return std::pair(1, Result.Value); } else @@ -100,7 +103,7 @@ std::pairCarlaReplayerHelper::TryToCreateReplayerActor( } } -AActor *CarlaReplayerHelper::FindTrafficLightAt(FVector Location) +FCarlaActor *CarlaReplayerHelper::FindTrafficLightAt(FVector Location) { check(Episode != nullptr); auto World = Episode->GetWorld(); @@ -111,19 +114,22 @@ AActor *CarlaReplayerHelper::FindTrafficLightAt(FVector Location) int y = static_cast(Location.Y); int z = static_cast(Location.Z); - // search an "traffic." actor at that position - for (TActorIterator It(World); It; ++It) + const FActorRegistry &Registry = Episode->GetActorRegistry(); + // through all actors in registry + for (auto It = Registry.begin(); It != Registry.end(); ++It) { - ATrafficSignBase *Actor = *It; - check(Actor != nullptr); - FVector vec = Actor->GetTransform().GetTranslation(); - int x2 = static_cast(vec.X); - int y2 = static_cast(vec.Y); - int z2 = static_cast(vec.Z); - if ((x2 == x) && (y2 == y) && (z2 == z)) + FCarlaActor* CarlaActor = It.Value().Get(); + if(CarlaActor->GetActorType() == FCarlaActor::ActorType::TrafficLight) { - // actor found - return static_cast(Actor); + FVector vec = CarlaActor->GetActorGlobalLocation(); + int x2 = static_cast(vec.X); + int y2 = static_cast(vec.Y); + int z2 = static_cast(vec.Z); + if ((x2 == x) && (y2 == y) && (z2 == z)) + { + // actor found + return CarlaActor; + } } } // actor not found @@ -131,41 +137,34 @@ AActor *CarlaReplayerHelper::FindTrafficLightAt(FVector Location) } // enable / disable physics for an actor -bool CarlaReplayerHelper::SetActorSimulatePhysics(const FCarlaActor* CarlaActor, bool bEnabled) +bool CarlaReplayerHelper::SetActorSimulatePhysics(FCarlaActor* CarlaActor, bool bEnabled) { if (!CarlaActor) { return false; } - auto RootComponent = Cast(CarlaActor->GetActor()->GetRootComponent()); - if (RootComponent == nullptr) + ECarlaServerResponse Response = + CarlaActor->SetActorSimulatePhysics(bEnabled); + if (Response != ECarlaServerResponse::Success) { return false; } - RootComponent->SetSimulatePhysics(bEnabled); - return true; } // enable / disable autopilot for an actor -bool CarlaReplayerHelper::SetActorAutopilot(const FCarlaActor* CarlaActor, bool bEnabled, bool bKeepState) +bool CarlaReplayerHelper::SetActorAutopilot(FCarlaActor* CarlaActor, bool bEnabled, bool bKeepState) { if (!CarlaActor) { return false; } - auto Vehicle = Cast(CarlaActor->GetActor()); - if (Vehicle == nullptr) + ECarlaServerResponse Response = + CarlaActor->SetActorAutopilot(bEnabled, bKeepState); + if (Response != ECarlaServerResponse::Success) { return false; } - auto Controller = Cast(Vehicle->GetController()); - if (Controller == nullptr) - { - return false; - } - Controller->SetAutopilot(bEnabled, bKeepState); - return true; } @@ -223,9 +222,9 @@ std::pair CarlaReplayerHelper::ProcessReplayerEventAdd( SetActorSimulatePhysics(result.second, true); } } + return std::make_pair(result.first, result.second->GetActorId()); } - - return std::make_pair(result.first, result.second->GetActorId()); + return std::make_pair(result.first, 0); } // replay event for removing actor @@ -265,10 +264,10 @@ bool CarlaReplayerHelper::ProcessReplayerEventParent(uint32_t ChildId, uint32_t bool CarlaReplayerHelper::ProcessReplayerPosition(CarlaRecorderPosition Pos1, CarlaRecorderPosition Pos2, double Per, double DeltaTime) { check(Episode != nullptr); - AActor *Actor = Episode->FindCarlaActor(Pos1.DatabaseId)->GetActor(); + FCarlaActor* CarlaActor = Episode->FindCarlaActor(Pos1.DatabaseId); FVector Location; FRotator Rotation; - if (Actor && !Actor->IsPendingKill()) + if(CarlaActor) { // check to assign first position or interpolate between both if (Per == 0.0) @@ -285,7 +284,7 @@ bool CarlaReplayerHelper::ProcessReplayerPosition(CarlaRecorderPosition Pos1, Ca } // set new transform FTransform Trans(Rotation, Location, FVector(1, 1, 1)); - Actor->SetActorTransform(Trans, false, nullptr, ETeleportType::None); + CarlaActor->SetActorGlobalTransform(Trans, ETeleportType::None); return true; } return false; @@ -299,7 +298,10 @@ bool CarlaReplayerHelper::SetCameraPosition(uint32_t Id, FVector Offset, FQuat R // get specator pawn APawn *Spectator = Episode->GetSpectatorPawn(); // get the actor to follow - AActor *Actor = Episode->FindCarlaActor(Id)->GetActor(); + FCarlaActor* CarlaActor = Episode->FindCarlaActor(Id); + if (!CarlaActor) + return false; + AActor *Actor = CarlaActor->GetActor(); // check if (!Spectator || !Actor) @@ -317,6 +319,7 @@ bool CarlaReplayerHelper::SetCameraPosition(uint32_t Id, FVector Offset, FQuat R bool CarlaReplayerHelper::ProcessReplayerStateTrafficLight(CarlaRecorderStateTrafficLight State) { check(Episode != nullptr); + // Todo: interface with FCarlaActor and UTrafficLightController AActor *Actor = Episode->FindCarlaActor(State.DatabaseId)->GetActor(); if (Actor && !Actor->IsPendingKill()) { @@ -336,15 +339,9 @@ bool CarlaReplayerHelper::ProcessReplayerStateTrafficLight(CarlaRecorderStateTra void CarlaReplayerHelper::ProcessReplayerAnimVehicle(CarlaRecorderAnimVehicle Vehicle) { check(Episode != nullptr); - AActor *Actor = Episode->FindCarlaActor(Vehicle.DatabaseId)->GetActor(); - if (Actor && !Actor->IsPendingKill()) + FCarlaActor *CarlaActor = Episode->FindCarlaActor(Vehicle.DatabaseId); + if (CarlaActor) { - auto Veh = Cast(Actor); - if (Veh == nullptr) - { - return; - } - FVehicleControl Control; Control.Throttle = Vehicle.Throttle; Control.Steer = Vehicle.Steering; @@ -353,7 +350,7 @@ void CarlaReplayerHelper::ProcessReplayerAnimVehicle(CarlaRecorderAnimVehicle Ve Control.bReverse = (Vehicle.Gear < 0); Control.Gear = Vehicle.Gear; Control.bManualGearShift = false; - Veh->ApplyVehicleControl(Control, EVehicleInputPriority::User); + CarlaActor->ApplyControlToVehicle(Control, EVehicleInputPriority::User); } } @@ -361,17 +358,11 @@ void CarlaReplayerHelper::ProcessReplayerAnimVehicle(CarlaRecorderAnimVehicle Ve void CarlaReplayerHelper::ProcessReplayerLightVehicle(CarlaRecorderLightVehicle LightVehicle) { check(Episode != nullptr); - AActor *Actor = Episode->FindCarlaActor(LightVehicle.DatabaseId)->GetActor(); - if (Actor && !Actor->IsPendingKill()) + FCarlaActor * CarlaActor = Episode->FindCarlaActor(LightVehicle.DatabaseId); + if (CarlaActor) { - auto Veh = Cast(Actor); - if (Veh == nullptr) - { - return; - } - carla::rpc::VehicleLightState LightState(LightVehicle.State); - Veh->SetVehicleLightState(FVehicleLightState(LightState)); + CarlaActor->SetVehicleLightState(FVehicleLightState(LightState)); } } @@ -410,7 +401,7 @@ bool CarlaReplayerHelper::ProcessReplayerFinish(bool bApplyAutopilot, bool bIgno const FActorRegistry& Registry = Episode->GetActorRegistry(); for (auto& It : Registry) { - const FCarlaActor* CarlaActor = It.Value.Get(); + FCarlaActor* CarlaActor = It.Value.Get(); // enable physics only on vehicles switch (CarlaActor->GetActorType()) @@ -452,21 +443,13 @@ bool CarlaReplayerHelper::ProcessReplayerFinish(bool bApplyAutopilot, bool bIgno return true; } -void CarlaReplayerHelper::SetActorVelocity(const FCarlaActor *CarlaActor, FVector Velocity) +void CarlaReplayerHelper::SetActorVelocity(FCarlaActor *CarlaActor, FVector Velocity) { if (!CarlaActor) { return; } - auto RootComponent = Cast(CarlaActor->GetActor()->GetRootComponent()); - if (RootComponent == nullptr) - { - return; - } - RootComponent->SetPhysicsLinearVelocity( - Velocity, - false, - "None"); + CarlaActor->SetActorTargetVelocity(Velocity); } // set the animation speed for walkers @@ -474,25 +457,13 @@ void CarlaReplayerHelper::SetWalkerSpeed(uint32_t ActorId, float Speed) { check(Episode != nullptr); FCarlaActor * CarlaActor = Episode->FindCarlaActor(ActorId); - AActor *Actor = nullptr; - if (CarlaActor) + if (!CarlaActor) { - Actor = CarlaActor->GetActor(); - } - if (Actor && !Actor->IsPendingKill()) - { - auto Wal = Cast(Actor); - if (Wal) - { - auto Controller = Cast(Wal->GetController()); - if (Controller != nullptr) - { - FWalkerControl Control; - Control.Speed = Speed; - Controller->ApplyWalkerControl(Control); - } - } + return; } + FWalkerControl Control; + Control.Speed = Speed; + CarlaActor->ApplyControlToWalker(Control); } void CarlaReplayerHelper::RemoveStaticProps() diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.h index f1893896a..20fe40bf0 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Recorder/CarlaReplayerHelper.h @@ -71,7 +71,7 @@ public: bool SetCameraPosition(uint32_t Id, FVector Offset, FQuat Rotation); // set the velocity of the actor - void SetActorVelocity(const FCarlaActor *CarlaActor, FVector Velocity); + void SetActorVelocity(FCarlaActor *CarlaActor, FVector Velocity); // set the animation speed for walkers void SetWalkerSpeed(uint32_t ActorId, float Speed); @@ -89,10 +89,10 @@ private: uint32_t DesiredId, bool SpawnSensors); - AActor *FindTrafficLightAt(FVector Location); + FCarlaActor* FindTrafficLightAt(FVector Location); // enable / disable physics for an actor - bool SetActorSimulatePhysics(const FCarlaActor *CarlaActor, bool bEnabled); + bool SetActorSimulatePhysics(FCarlaActor *CarlaActor, bool bEnabled); // enable / disable autopilot for an actor - bool SetActorAutopilot(const FCarlaActor *CarlaActor, bool bEnabled, bool bKeepState = false); + bool SetActorAutopilot(FCarlaActor *CarlaActor, bool bEnabled, bool bKeepState = false); }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.cpp index dc4c02117..e101fe199 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.cpp @@ -90,6 +90,7 @@ bool UTrafficLightController::IsCycleFinished() const void UTrafficLightController::SetTrafficLightsState(ETrafficLightState NewState) { + SetCurrentLightState(NewState); for(auto *Light : TrafficLights) { Light->SetLightState(NewState); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.h index ced9cbbc8..0b1d876df 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Traffic/TrafficLightController.h @@ -120,6 +120,15 @@ public: const ATrafficLightGroup* GetGroup() const; + ETrafficLightState GetCurrentLightState() const + { + return CurrentLightState; + } + void SetCurrentLightState(ETrafficLightState NewState) + { + CurrentLightState = NewState; + } + private: void SetStateTime(const ETrafficLightState State, float NewTime); @@ -153,4 +162,5 @@ private: UPROPERTY() float ElapsedTime = 0; + ETrafficLightState CurrentLightState = ETrafficLightState::Green; };