Add agent components for tracking agents in the scene

This commit is contained in:
nsubiron 2018-02-22 15:49:49 +01:00
parent 6c8626d16a
commit ffe28db65d
16 changed files with 447 additions and 9 deletions

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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<FAgentMap> GetAgentMap(UWorld *World)
{
check(World != nullptr);
auto *GameMode = Cast<ACarlaGameModeBase>(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);
}

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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<FAgentMap> AgentMap = nullptr;
};

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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;
};

View File

@ -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 <https://opensource.org/licenses/MIT>.
#pragma once
class FAgentMap
{
public:
private:
friend class UAgentComponent;
TMap<uint32, UAgentComponent *> Agents;
};

View File

@ -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 <https://opensource.org/licenses/MIT>.
#pragma once
#include "Carla.h"
#include "TrafficSignAgentComponent.h"
UTrafficSignAgentComponent::UTrafficSignAgentComponent(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer) {}
void UTrafficSignAgentComponent::OnComponentCreated()
{
Super::OnComponentCreated();
}

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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:
};

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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<AWheeledVehicle>(GetOwner());
checkf(WheeledVehicle != nullptr, TEXT("UVehicleAgentComponent can only be attached to AWheeledVehicle"));
}

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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;
};

View File

@ -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 <https://opensource.org/licenses/MIT>.
#pragma once
#include "Carla.h"
#include "WalkerAgentComponent.h"
UWalkerAgentComponent::UWalkerAgentComponent(const FObjectInitializer &ObjectInitializer)
: Super(ObjectInitializer) {}
void UWalkerAgentComponent::OnComponentCreated()
{
Super::OnComponentCreated();
}

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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:
};

View File

@ -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<UVehicleAgentComponent>(TEXT("VehicleAgentComponent"));
GetVehicleMovementComponent()->bReverseAsBrake = false;
}

View File

@ -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;
};

View File

@ -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<APlayerStart *> &UnOccupiedStartPoints);
FDataRouter DataRouter;
CarlaGameControllerBase *GameController;
UPROPERTY()

View File

@ -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 <https://opensource.org/licenses/MIT>.
#include "Carla.h"
#include "DataRouter.h"
#include "Agent/AgentMap.h"
#include "Sensor/Sensor.h"
FDataRouter::FDataRouter() : Agents(MakeShared<FAgentMap>()) {}
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)
{
}

View File

@ -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 <https://opensource.org/licenses/MIT>.
#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<ISensorDataSink> InSensorDataSink)
{
SensorDataSink = InSensorDataSink;
}
void RegisterPlayer(ACarlaVehicleController *InPlayer);
void RegisterSensor(ASensor *InSensor);
const ACarlaPlayerState &GetPlayerState()
{
check(Player != nullptr);
return Player->GetPlayerState();
}
TSharedPtr<FAgentMap> GetAgents()
{
return Agents;
}
void ApplyVehicleControl(const FVehicleControl &VehicleControl);
private:
const TSharedPtr<FAgentMap> Agents;
ACarlaVehicleController *Player = nullptr;
TSharedPtr<ISensorDataSink> SensorDataSink = nullptr;
};

View File

@ -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: