Add config file

This commit is contained in:
nsubiron 2017-03-31 16:54:06 +01:00
parent ebb5708bfc
commit 97c529da74
11 changed files with 301 additions and 29 deletions

View File

@ -3,6 +3,11 @@ CARLA UE4 Plugin
Plugin for Unreal Engine 4.
Settings
--------
See [Resources/Example.CarlaSettings.ini](Resources/Example.CarlaSettings.ini).
Compiling a UE4 project with CARLA plugin
-----------------------------------------

View File

@ -0,0 +1,22 @@
; Example of settings file for CARLA.
;
; Either provide through command-line `-carla-settings=<path-to-ini-file>`, or
; make a copy at `{ProjectFolder}/Config/CarlaSettings.ini`.
[CARLA/Server]
; If set to false, a mock controller will be used instead of waiting for a
; client to connect.
UseNetworking=true
; Ports to use for the server-client communication. This can be overriden by the
; command-line switch `-world-port=N`, write and read ports will be set to N+1
; and N+2 respectively.
WorldPort=2000
WritePort=2001
ReadPort=2002
[CARLA/SceneCapture]
; Valid values: Mono, Stereo, NoCapture.
Mode=Mono
; Size of the captured image in pixels.
ImageSizeX=720
ImageSizeY=512

View File

@ -0,0 +1,11 @@
CARLA UE4
=========
To run the game at fixed time-step, e.g. 30 FPS
$ CarlaUE4.sh -benchmark -fps=30
Other CARLA related command-line options
* `-carla-settings=<ini-file-path>` Load settings from the given INI file. See Example.CarlaSettings.ini.
* `-world-port=<port-number>` Listen for client connections at <port-number>, write and read ports are set to <port-number>+1 and <port-number>+2 respectively.

View File

@ -191,24 +191,9 @@ static bool TryReadControl(carla::CarlaServer &Server, ACarlaVehicleController &
// -- CarlaGameController ------------------------------------------------------
// =============================================================================
CarlaGameController::CarlaGameController() :
Server(nullptr),
Player(nullptr) {
uint32 WorldPort;
if (!FParse::Value(FCommandLine::Get(), TEXT("-world-port="), WorldPort)) {
WorldPort = 2000u;
}
const uint32 WritePort = WorldPort + 1u;
const uint32 ReadPort = WorldPort + 2u;
UE_LOG(
LogCarlaServer,
Log,
TEXT("Creating CarlaServer with ports: world %d, write %d, read %d"),
WorldPort,
WritePort,
ReadPort);
Server = MakeUnique<carla::CarlaServer>(WritePort, ReadPort, WorldPort);
}
CarlaGameController::CarlaGameController(uint32 WorldPort, uint32 WritePort, uint32 ReadPort) :
Server(MakeUnique<carla::CarlaServer>(WritePort, ReadPort, WorldPort)),
Player(nullptr) {}
CarlaGameController::~CarlaGameController()
{

View File

@ -15,7 +15,7 @@ class CARLA_API CarlaGameController : public CarlaGameControllerBase
{
public:
CarlaGameController();
CarlaGameController(uint32 WorldPort, uint32 WritePort, uint32 ReadPort);
~CarlaGameController();

View File

@ -4,18 +4,29 @@
#include "CarlaGameInstance.h"
#include "CarlaGameController.h"
#include "CarlaSettings.h"
#include "MockGameController.h"
UCarlaGameInstance::UCarlaGameInstance() {
CarlaSettings = CreateDefaultSubobject<UCarlaSettings>(TEXT("CarlaSettings"));
check(CarlaSettings != nullptr);
CarlaSettings->LoadSettings();
CarlaSettings->LogSettings();
}
UCarlaGameInstance::~UCarlaGameInstance() {}
void UCarlaGameInstance::InitializeGameControllerIfNotPresent(bool bUseMockController)
void UCarlaGameInstance::InitializeGameControllerIfNotPresent()
{
if (GameController == nullptr) {
if (bUseMockController) {
if (CarlaSettings->bUseNetworking) {
GameController = MakeUnique<CarlaGameController>(
CarlaSettings->WorldPort,
CarlaSettings->WritePort,
CarlaSettings->ReadPort);
} else {
GameController = MakeUnique<MockGameController>();
UE_LOG(LogCarla, Warning, TEXT("Using mock CARLA controller"));
} else {
GameController = MakeUnique<CarlaGameController>();
}
}
}

View File

@ -6,6 +6,8 @@
#include "CarlaGameControllerBase.h"
#include "CarlaGameInstance.generated.h"
class UCarlaSettings;
/**
*
*/
@ -16,9 +18,11 @@ class CARLA_API UCarlaGameInstance : public UGameInstance
public:
UCarlaGameInstance();
~UCarlaGameInstance();
void InitializeGameControllerIfNotPresent(bool bUseMockController);
void InitializeGameControllerIfNotPresent();
CarlaGameControllerBase &GetGameController()
{
@ -26,7 +30,16 @@ public:
return *GameController;
}
const UCarlaSettings &GetCarlaSettings() const
{
check(CarlaSettings != nullptr);
return *CarlaSettings;
}
private:
UPROPERTY(Category = "CARLA Settings", EditAnywhere)
UCarlaSettings *CarlaSettings;
TUniquePtr<CarlaGameControllerBase> GameController;
};

View File

@ -40,7 +40,7 @@ void ACarlaGameMode::InitGame(
checkf(
GameInstance != nullptr,
TEXT("GameInstance is not a UCarlaGameInstance, did you forget to set it in the project settings?"));
GameInstance->InitializeGameControllerIfNotPresent(bUseMockController);
GameInstance->InitializeGameControllerIfNotPresent();
GameController = &GameInstance->GetGameController();
GameController->Initialize();
}

View File

@ -17,10 +17,6 @@ class CARLA_API ACarlaGameMode : public AGameModeBase
{
GENERATED_BODY()
/** Use a mock controller instead of connecting to a client */
UPROPERTY(Category = "CARLA Game Controller", EditAnywhere)
bool bUseMockController = false;
public:
ACarlaGameMode();

View File

@ -0,0 +1,147 @@
// CARLA, Copyright (C) 2017 Computer Vision Center (CVC)
#include "Carla.h"
#include "CarlaSettings.h"
#include "CommandLine.h"
#include "ConfigCacheIni.h"
#include <limits>
template <typename TARGET, typename SOURCE>
static void SafeCastTo(SOURCE source, TARGET &target)
{
if ((source >= std::numeric_limits<TARGET>::min()) &&
(source <= std::numeric_limits<TARGET>::max())) {
target = static_cast<TARGET>(source);
}
}
class INIFile {
public:
explicit INIFile(const FString &FileName)
{
ConfigFile.Read(FileName);
}
void GetBool(const TCHAR* Section, const TCHAR* Key, bool &Target) const
{
bool Value;
if (ConfigFile.GetBool(Section, Key, Value)) {
Target = Value;
}
}
template <typename T>
void GetInt(const TCHAR* Section, const TCHAR* Key, T &Target) const
{
int64 Value;
if (ConfigFile.GetInt64(Section, Key, Value)) {
SafeCastTo<T>(Value, Target);
}
}
void GetString(const TCHAR* Section, const TCHAR* Key, FString &Target) const
{
FString Value;
if (ConfigFile.GetString(Section, Key, Value)) {
Target = Value;
}
}
void GetSceneCaptureMode(const TCHAR* Section, const TCHAR* Key, ESceneCaptureMode &Target) const
{
FString ModeString;
if (ConfigFile.GetString(Section, Key, ModeString)) {
if (ModeString == "Stereo") {
Target = ESceneCaptureMode::Stereo;
} else if (ModeString == "Mono") {
Target = ESceneCaptureMode::Mono;
} else if (ModeString == "NoCapture") {
Target = ESceneCaptureMode::NoCapture;
}
}
}
private:
FConfigFile ConfigFile;
};
#define CARLA_SERVER_SECTION TEXT("CARLA/Server")
#define CARLA_SCENECAPTURE_SECTION TEXT("CARLA/SceneCapture")
static void LoadSettingsFromFile(const FString &FileName, UCarlaSettings &Settings)
{
UE_LOG(LogCarla, Log, TEXT("Loading settings from \"%s\""), *FileName);
INIFile ConfigFile(FileName);
ConfigFile.GetBool(CARLA_SERVER_SECTION, TEXT("UseNetworking"), Settings.bUseNetworking);
ConfigFile.GetInt(CARLA_SERVER_SECTION, TEXT("WorldPort"), Settings.WorldPort);
ConfigFile.GetInt(CARLA_SERVER_SECTION, TEXT("WritePort"), Settings.WritePort);
ConfigFile.GetInt(CARLA_SERVER_SECTION, TEXT("ReadPort"), Settings.ReadPort);
ConfigFile.GetSceneCaptureMode(CARLA_SCENECAPTURE_SECTION, TEXT("Mode"), Settings.SceneCaptureMode);
ConfigFile.GetInt(CARLA_SCENECAPTURE_SECTION, TEXT("ImageSizeX"), Settings.ImageSizeX);
ConfigFile.GetInt(CARLA_SCENECAPTURE_SECTION, TEXT("ImageSizeY"), Settings.ImageSizeY);
}
static bool GetSettingsFileName(FString &Value)
{
// Try to get it from the command-line arguments.
if (FParse::Value(FCommandLine::Get(), TEXT("-carla-settings="), Value)) {
if (FPaths::FileExists(Value)) {
return true;
}
UE_LOG(LogCarla, Error, TEXT("Unable to find settings file \"%s\", falling back to default values"), *Value);
}
// If fails, check if there is one in the config folder.
Value = FPaths::Combine(FPaths::GameConfigDir(), TEXT("CarlaSettings.ini"));
if (FPaths::FileExists(Value)) {
return true;
}
return false;
}
void UCarlaSettings::LoadSettings()
{
FString FileName;
if (GetSettingsFileName(FileName)) {
LoadSettingsFromFile(FileName, *this);
CurrentFileName = FileName;
} else {
CurrentFileName = TEXT("");
}
// Override settings from command-line.
uint32 Value;
if (FParse::Value(FCommandLine::Get(), TEXT("-world-port="), Value)) {
WorldPort = Value;
WritePort = Value + 1u;
ReadPort = Value + 2u;
}
}
void UCarlaSettings::LogSettings()
{
auto ModeAsString = [](ESceneCaptureMode Mode) {
switch (Mode) {
case ESceneCaptureMode::Mono: return TEXT("Mono");
case ESceneCaptureMode::Stereo: return TEXT("Stereo");
case ESceneCaptureMode::NoCapture: return TEXT("NoCapture");
default: return TEXT("INVALID");
};
};
UE_LOG(LogCarla, Log, TEXT("== CARLA Settings =============================================================="));
UE_LOG(LogCarla, Log, TEXT("Settings file: %s"), *CurrentFileName);
UE_LOG(LogCarla, Log, TEXT("[%s]"), CARLA_SERVER_SECTION);
UE_LOG(LogCarla, Log, TEXT("UseNetworking=%s"), (bUseNetworking ? TEXT("true") : TEXT("false")));
UE_LOG(LogCarla, Log, TEXT("WorldPort=%d"), WorldPort);
UE_LOG(LogCarla, Log, TEXT("WritePort=%d"), WritePort);
UE_LOG(LogCarla, Log, TEXT("ReadPort=%d"), ReadPort);
UE_LOG(LogCarla, Log, TEXT("[%s]"), CARLA_SCENECAPTURE_SECTION);
UE_LOG(LogCarla, Log, TEXT("Mode=%s"), ModeAsString(SceneCaptureMode));
UE_LOG(LogCarla, Log, TEXT("ImageSizeX=%d"), ImageSizeX);
UE_LOG(LogCarla, Log, TEXT("ImageSizeY=%d"), ImageSizeY);
UE_LOG(LogCarla, Log, TEXT("================================================================================"));
}
#undef CARLA_SERVER_SECTION
#undef CARLA_SCENECAPTURE_SECTION

View File

@ -0,0 +1,82 @@
// CARLA, Copyright (C) 2017 Computer Vision Center (CVC)
#pragma once
#include "UObject/NoExportTypes.h"
#include "CarlaSettings.generated.h"
UENUM(BlueprintType)
enum class ESceneCaptureMode : uint8
{
Mono UMETA(DisplayName = "Mono: Single RGB capture"),
Stereo UMETA(DisplayName = "Stereo: Stereo RGB captures and Depth maps"),
NoCapture UMETA(DisplayName = "No capture")
};
/// Global settings for CARLA.
UCLASS()
class CARLA_API UCarlaSettings : public UObject
{
GENERATED_BODY()
private:
/** File name of the settings file used to load this settings. Empty if none used. */
UPROPERTY(Category = "CARLA Settings|Debug", VisibleAnywhere)
FString CurrentFileName;
// ===========================================================================
/// @name CARLA Server
// ===========================================================================
/// @{
public:
/** If active, wait for the client to connect and control the pawn. */
UPROPERTY(Category = "CARLA Server", EditDefaultsOnly)
bool bUseNetworking = true;
/** World port to listen for client connections. */
UPROPERTY(Category = "CARLA Server", EditDefaultsOnly, meta = (EditCondition = bUseNetworking))
uint32 WorldPort = 2000u;
/** If networking is active, rewards are sent to this port. */
UPROPERTY(Category = "CARLA Server", EditDefaultsOnly, meta = (EditCondition = bUseNetworking))
uint32 WritePort = 2001u;
/** If networking is active, controls are read from this port. */
UPROPERTY(Category = "CARLA Server", EditDefaultsOnly, meta = (EditCondition = bUseNetworking))
uint32 ReadPort = 2002u;
/// @}
// ===========================================================================
/// @name Scene Capture
// ===========================================================================
/// @{
public:
/** Controls the number and type of scene capture cameras that are added to the player. */
UPROPERTY(Category = "Scene Capture", EditDefaultsOnly)
ESceneCaptureMode SceneCaptureMode = ESceneCaptureMode::Mono;
/** X size in pixels of the captured image. */
UPROPERTY(Category = "Scene Capture", EditDefaultsOnly)
uint32 ImageSizeX = 720u;
/** Y size in pixels of the captured image. */
UPROPERTY(Category = "Scene Capture", EditDefaultsOnly)
uint32 ImageSizeY = 512u;
/// @}
// ===========================================================================
// -- Other ------------------------------------------------------------------
// ===========================================================================
public:
/** Load the settings based on the command-line arguments and the INI file if provided . */
void LoadSettings();
/** Log settings values . */
void LogSettings();
};