diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentComponent.cpp new file mode 100644 index 000000000..aa1069c53 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentComponent.cpp @@ -0,0 +1,62 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Carla.h" +#include "AgentComponent.h" + +#include "Agent/AgentMap.h" +#include "Game/CarlaGameModeBase.h" +#include "Game/DataRouter.h" + +static uint32 GetNextId() +{ + static uint32 COUNT = 0u; + return ++COUNT; +} + +static TSharedPtr GetAgentMap(UWorld *World) +{ + check(World != nullptr); + auto *GameMode = Cast(World->GetAuthGameMode()); + check(GameMode != nullptr); + return GameMode->GetDataRouter().GetAgents(); +} + +UAgentComponent::UAgentComponent(const FObjectInitializer &ObjectInitializer) + : Super(ObjectInitializer), + Id(GetNextId()) {} + +void UAgentComponent::AcceptVisitor(IAgentComponentVisitor &Visitor) const +{ + unimplemented(); +} + +void UAgentComponent::BeginPlay() +{ + Super::BeginPlay(); + + // Register this component in the World's list of agents. + auto AgentMapPtr = GetAgentMap(GetWorld()); + if (AgentMapPtr.IsValid()) { + AgentMapPtr->Agents.Add(Id, this); + AgentMap = AgentMapPtr; + } else { + UE_LOG(LogCarla, Error, TEXT("AgentComponent: Missing AgentMap!")); + } +} + +void UAgentComponent::EndPlay(const EEndPlayReason::Type EndPlayReason) +{ + // Deregister this component in the World's list of agents. + auto AgentMapPtr = AgentMap.Pin(); + if (AgentMapPtr.IsValid()) { + AgentMapPtr->Agents.Remove(Id); + } + + Super::EndPlay(EndPlayReason); +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentComponent.h new file mode 100644 index 000000000..e1788a058 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentComponent.h @@ -0,0 +1,48 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Agent/AgentComponentVisitor.h" + +#include "Components/ActorComponent.h" +#include "Templates/SharedPointer.h" + +#include "AgentComponent.generated.h" + +class FAgentMap; + +/// Actors with an UAgentComponent are registered as agents in the scene and +/// their status is sent to the client each frame (if requested by the client). +UCLASS(Abstract) +class CARLA_API UAgentComponent : public UActorComponent +{ + GENERATED_BODY() + +public: + + UAgentComponent(const FObjectInitializer &ObjectInitializer); + + uint32 GetId() const + { + return Id; + } + + virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const; + +protected: + + virtual void BeginPlay() override; + + virtual void EndPlay(EEndPlayReason::Type EndPlayReason) override; + +private: + + UPROPERTY(VisibleAnywhere) + uint32 Id; + + TWeakPtr AgentMap = nullptr; +}; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentComponentVisitor.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentComponentVisitor.h new file mode 100644 index 000000000..9229e7170 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentComponentVisitor.h @@ -0,0 +1,23 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +class UAgentComponent; +class UTrafficSignAgentComponent; +class UVehicleAgentComponent; +class UWalkerAgentComponent; + +class IAgentComponentVisitor +{ +public: + + virtual void Visit(const UTrafficSignAgentComponent &) = 0; + + virtual void Visit(const UVehicleAgentComponent &) = 0; + + virtual void Visit(const UWalkerAgentComponent &) = 0; +}; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentMap.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentMap.h new file mode 100644 index 000000000..207e9ed13 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/AgentMap.h @@ -0,0 +1,19 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +class FAgentMap +{ +public: + + +private: + + friend class UAgentComponent; + + TMap Agents; +}; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/TrafficSignAgentComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/TrafficSignAgentComponent.cpp new file mode 100644 index 000000000..f503a7e6a --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/TrafficSignAgentComponent.cpp @@ -0,0 +1,19 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Carla.h" +#include "TrafficSignAgentComponent.h" + +UTrafficSignAgentComponent::UTrafficSignAgentComponent(const FObjectInitializer &ObjectInitializer) + : Super(ObjectInitializer) {} + +void UTrafficSignAgentComponent::OnComponentCreated() +{ + Super::OnComponentCreated(); + +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/TrafficSignAgentComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/TrafficSignAgentComponent.h new file mode 100644 index 000000000..3ab481963 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/TrafficSignAgentComponent.h @@ -0,0 +1,36 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Agent/AgentComponent.h" + +#include "TrafficSignAgentComponent.generated.h" + +/// This component can be added to any AWheeledVehicle to be added as agent. +/// See UAgentComponent. +UCLASS() +class CARLA_API UTrafficSignAgentComponent : public UAgentComponent +{ + GENERATED_BODY() + +public: + + UTrafficSignAgentComponent(const FObjectInitializer &ObjectInitializer); + +protected: + + virtual void OnComponentCreated() override; + + virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final + { + Visitor.Visit(*this); + } + +private: + + +}; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/VehicleAgentComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/VehicleAgentComponent.cpp new file mode 100644 index 000000000..8c9ff1e5f --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/VehicleAgentComponent.cpp @@ -0,0 +1,23 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Carla.h" +#include "VehicleAgentComponent.h" + +#include "WheeledVehicle.h" + +UVehicleAgentComponent::UVehicleAgentComponent(const FObjectInitializer &ObjectInitializer) + : Super(ObjectInitializer) {} + +void UVehicleAgentComponent::OnComponentCreated() +{ + Super::OnComponentCreated(); + + WheeledVehicle = Cast(GetOwner()); + checkf(WheeledVehicle != nullptr, TEXT("UVehicleAgentComponent can only be attached to AWheeledVehicle")); +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/VehicleAgentComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/VehicleAgentComponent.h new file mode 100644 index 000000000..05b00ea35 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/VehicleAgentComponent.h @@ -0,0 +1,36 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Agent/AgentComponent.h" + +#include "VehicleAgentComponent.generated.h" + +class AWheeledVehicle; + +UCLASS() +class CARLA_API UVehicleAgentComponent : public UAgentComponent +{ + GENERATED_BODY() + +public: + + UVehicleAgentComponent(const FObjectInitializer &ObjectInitializer); + +protected: + + virtual void OnComponentCreated() override; + + virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final + { + Visitor.Visit(*this); + } + +private: + + AWheeledVehicle *WheeledVehicle = nullptr; +}; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/WalkerAgentComponent.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/WalkerAgentComponent.cpp new file mode 100644 index 000000000..30e3dc12b --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/WalkerAgentComponent.cpp @@ -0,0 +1,19 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Carla.h" +#include "WalkerAgentComponent.h" + +UWalkerAgentComponent::UWalkerAgentComponent(const FObjectInitializer &ObjectInitializer) + : Super(ObjectInitializer) {} + +void UWalkerAgentComponent::OnComponentCreated() +{ + Super::OnComponentCreated(); + +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/WalkerAgentComponent.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/WalkerAgentComponent.h new file mode 100644 index 000000000..db421a72a --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Agent/WalkerAgentComponent.h @@ -0,0 +1,36 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Agent/AgentComponent.h" + +#include "WalkerAgentComponent.generated.h" + +/// This component can be added to any ACharacter to be added as agent. +/// See UAgentComponent. +UCLASS() +class CARLA_API UWalkerAgentComponent : public UAgentComponent +{ + GENERATED_BODY() + +public: + + UWalkerAgentComponent(const FObjectInitializer &ObjectInitializer); + +protected: + + virtual void OnComponentCreated() override; + + virtual void AcceptVisitor(IAgentComponentVisitor &Visitor) const final + { + Visitor.Visit(*this); + } + +private: + + +}; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/CarlaWheeledVehicle.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/CarlaWheeledVehicle.cpp index fc5ca8bdc..20f93d696 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/CarlaWheeledVehicle.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/CarlaWheeledVehicle.cpp @@ -8,6 +8,7 @@ #include "CarlaWheeledVehicle.h" #include "AI/VehicleControl.h" +#include "Agent/VehicleAgentComponent.h" #include "Components/BoxComponent.h" #include "Engine/CollisionProfile.h" @@ -24,6 +25,8 @@ ACarlaWheeledVehicle::ACarlaWheeledVehicle(const FObjectInitializer& ObjectIniti VehicleBounds->SetHiddenInGame(true); VehicleBounds->SetCollisionProfileName(UCollisionProfile::NoCollision_ProfileName); + VehicleAgentComponent = CreateDefaultSubobject(TEXT("VehicleAgentComponent")); + GetVehicleMovementComponent()->bReverseAsBrake = false; } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/CarlaWheeledVehicle.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/CarlaWheeledVehicle.h index d143f85f5..8f53592d7 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/CarlaWheeledVehicle.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/CarlaWheeledVehicle.h @@ -12,6 +12,7 @@ #include "CarlaWheeledVehicle.generated.h" class UBoxComponent; +class UVehicleAgentComponent; struct FVehicleControl; /// Base class for CARLA wheeled vehicles. @@ -111,9 +112,12 @@ private: UPROPERTY(Category = "AI Controller", VisibleAnywhere) ECarlaWheeledVehicleState State = ECarlaWheeledVehicleState::UNKNOWN; - UPROPERTY(EditAnywhere) + UPROPERTY(Category = "CARLA Wheeled Vehicle", EditAnywhere) UBoxComponent *VehicleBounds; + UPROPERTY(Category = "CARLA Wheeled Vehicle", VisibleAnywhere) + UVehicleAgentComponent *VehicleAgentComponent; + UPROPERTY() bool bIsInReverse = false; }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h index 9dc4247a1..b782fe18f 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaGameModeBase.h @@ -7,21 +7,22 @@ #pragma once #include "GameFramework/GameModeBase.h" + +#include "DynamicWeather.h" #include "AI/VehicleSpawnerBase.h" #include "AI/WalkerSpawnerBase.h" -#include "CarlaGameControllerBase.h" -#include "DynamicWeather.h" -#include "MockGameControllerSettings.h" +#include "Game/CarlaGameControllerBase.h" +#include "Game/DataRouter.h" +#include "Game/MockGameControllerSettings.h" + #include "CarlaGameModeBase.generated.h" class ACarlaVehicleController; class APlayerStart; class ASceneCaptureCamera; +class UCarlaGameInstance; class UTaggerDelegate; -/** - * - */ UCLASS(HideCategories=(ActorTick)) class CARLA_API ACarlaGameModeBase : public AGameModeBase { @@ -39,6 +40,11 @@ public: virtual void Tick(float DeltaSeconds) override; + FDataRouter &GetDataRouter() + { + return DataRouter; + } + protected: /** Used only when networking is disabled. */ @@ -73,6 +79,8 @@ private: AController *Player, TArray &UnOccupiedStartPoints); + FDataRouter DataRouter; + CarlaGameControllerBase *GameController; UPROPERTY() diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/DataRouter.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/DataRouter.cpp new file mode 100644 index 000000000..b8f0a22fc --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/DataRouter.cpp @@ -0,0 +1,45 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#include "Carla.h" +#include "DataRouter.h" + +#include "Agent/AgentMap.h" +#include "Sensor/Sensor.h" + +FDataRouter::FDataRouter() : Agents(MakeShared()) {} + +FDataRouter::~FDataRouter() {} + +void FDataRouter::RegisterPlayer(ACarlaVehicleController *InPlayer) +{ + if (Player == nullptr) { + Player = InPlayer; + } else { + UE_LOG( + LogCarla, + Error, + TEXT("FDataRouter: Trying to register a second player but only one is supported")); + } +} + +void FDataRouter::RegisterSensor(ASensor *InSensor) +{ + if (SensorDataSink.Get() != nullptr) { + check(InSensor != nullptr); + InSensor->SetSensorDataSink(SensorDataSink); + } else { + UE_LOG( + LogCarla, + Error, + TEXT("FDataRouter: Trying to register a sensor but I don't have a SensorDataSink")); + } +} + +void FDataRouter::ApplyVehicleControl(const FVehicleControl &VehicleControl) +{ + +} diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/DataRouter.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/DataRouter.h new file mode 100644 index 000000000..f30968a83 --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/DataRouter.h @@ -0,0 +1,54 @@ +// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma +// de Barcelona (UAB). +// +// This work is licensed under the terms of the MIT license. +// For a copy, see . + +#pragma once + +#include "Agent/AgentMap.h" +#include "Game/CarlaVehicleController.h" +#include "Sensor/SensorDataSink.h" + +class ACarlaPlayerState; +class ASensor; +struct FVehicleControl; + +class FDataRouter +{ +public: + + FDataRouter(); + + ~FDataRouter(); + + void SetSensorDataSink(TSharedPtr InSensorDataSink) + { + SensorDataSink = InSensorDataSink; + } + + void RegisterPlayer(ACarlaVehicleController *InPlayer); + + void RegisterSensor(ASensor *InSensor); + + const ACarlaPlayerState &GetPlayerState() + { + check(Player != nullptr); + return Player->GetPlayerState(); + } + + TSharedPtr GetAgents() + { + return Agents; + } + + void ApplyVehicleControl(const FVehicleControl &VehicleControl); + +private: + + const TSharedPtr Agents; + + ACarlaVehicleController *Player = nullptr; + + TSharedPtr SensorDataSink = nullptr; +}; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/Sensor.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/Sensor.h index f6106dc5c..ce1802262 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/Sensor.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Sensor/Sensor.h @@ -36,8 +36,11 @@ protected: void WriteSensorData(const FSensorDataView &SensorData) { - check(SensorDataSink.Get() != nullptr); - SensorDataSink->Write(SensorData); + if (SensorDataSink.IsValid()) { + SensorDataSink->Write(SensorData); + } else { + UE_LOG(LogCarla, Warning, TEXT("Sensor %d has no data sink."), Id); + } } private: