Allow changing episode settings at runtime (sync mode and no-rendering mode)

This commit is contained in:
nsubiron 2019-02-17 17:49:28 +01:00
parent e90e82cc81
commit fe2ebc527c
15 changed files with 255 additions and 14 deletions

View File

@ -37,6 +37,14 @@ namespace client {
return _episode.Lock()->GetSpectator(); return _episode.Lock()->GetSpectator();
} }
rpc::EpisodeSettings World::GetSettings() const {
return _episode.Lock()->GetEpisodeSettings();
}
void World::ApplySettings(const rpc::EpisodeSettings &settings) {
_episode.Lock()->SetEpisodeSettings(settings);
}
rpc::WeatherParameters World::GetWeather() const { rpc::WeatherParameters World::GetWeather() const {
return _episode.Lock()->GetWeatherParameters(); return _episode.Lock()->GetWeatherParameters();
} }

View File

@ -13,6 +13,7 @@
#include "carla/client/detail/EpisodeProxy.h" #include "carla/client/detail/EpisodeProxy.h"
#include "carla/geom/Transform.h" #include "carla/geom/Transform.h"
#include "carla/rpc/Actor.h" #include "carla/rpc/Actor.h"
#include "carla/rpc/EpisodeSettings.h"
#include "carla/rpc/VehiclePhysicsControl.h" #include "carla/rpc/VehiclePhysicsControl.h"
#include "carla/rpc/WeatherParameters.h" #include "carla/rpc/WeatherParameters.h"
@ -53,6 +54,10 @@ namespace client {
/// simulator window. /// simulator window.
SharedPtr<Actor> GetSpectator() const; SharedPtr<Actor> GetSpectator() const;
rpc::EpisodeSettings GetSettings() const;
void ApplySettings(const rpc::EpisodeSettings &settings);
/// Retrieve the weather parameters currently active in the world. /// Retrieve the weather parameters currently active in the world.
rpc::WeatherParameters GetWeather() const; rpc::WeatherParameters GetWeather() const;

View File

@ -139,6 +139,14 @@ namespace detail {
return _pimpl->CallAndWait<carla::rpc::Actor>("get_spectator"); return _pimpl->CallAndWait<carla::rpc::Actor>("get_spectator");
} }
rpc::EpisodeSettings Client::GetEpisodeSettings() {
return _pimpl->CallAndWait<rpc::EpisodeSettings>("get_episode_settings");
}
void Client::SetEpisodeSettings(const rpc::EpisodeSettings &settings) {
_pimpl->AsyncCall("set_episode_settings", settings);
}
rpc::WeatherParameters Client::GetWeatherParameters() { rpc::WeatherParameters Client::GetWeatherParameters() {
return _pimpl->CallAndWait<rpc::WeatherParameters>("get_weather_parameters"); return _pimpl->CallAndWait<rpc::WeatherParameters>("get_weather_parameters");
} }

View File

@ -13,10 +13,11 @@
#include "carla/rpc/Actor.h" #include "carla/rpc/Actor.h"
#include "carla/rpc/ActorDefinition.h" #include "carla/rpc/ActorDefinition.h"
#include "carla/rpc/EpisodeInfo.h" #include "carla/rpc/EpisodeInfo.h"
#include "carla/rpc/EpisodeSettings.h"
#include "carla/rpc/MapInfo.h" #include "carla/rpc/MapInfo.h"
#include "carla/rpc/WeatherParameters.h"
#include "carla/rpc/TrafficLightState.h" #include "carla/rpc/TrafficLightState.h"
#include "carla/rpc/VehiclePhysicsControl.h" #include "carla/rpc/VehiclePhysicsControl.h"
#include "carla/rpc/WeatherParameters.h"
#include <functional> #include <functional>
#include <memory> #include <memory>
@ -76,6 +77,10 @@ namespace detail {
rpc::Actor GetSpectator(); rpc::Actor GetSpectator();
rpc::EpisodeSettings GetEpisodeSettings();
void SetEpisodeSettings(const rpc::EpisodeSettings &settings);
rpc::WeatherParameters GetWeatherParameters(); rpc::WeatherParameters GetWeatherParameters();
void SetWeatherParameters(const rpc::WeatherParameters &weather); void SetWeatherParameters(const rpc::WeatherParameters &weather);

View File

@ -128,6 +128,14 @@ namespace detail {
SharedPtr<Actor> GetSpectator(); SharedPtr<Actor> GetSpectator();
rpc::EpisodeSettings GetEpisodeSettings() {
return _client.GetEpisodeSettings();
}
void SetEpisodeSettings(const rpc::EpisodeSettings &settings) {
_client.SetEpisodeSettings(settings);
}
rpc::WeatherParameters GetWeatherParameters() { rpc::WeatherParameters GetWeatherParameters() {
return _client.GetWeatherParameters(); return _client.GetWeatherParameters();
} }

View File

@ -0,0 +1,78 @@
// 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/MsgPack.h"
#ifdef LIBCARLA_INCLUDED_FROM_UE4
# include "Carla/Settings/EpisodeSettings.h"
#endif // LIBCARLA_INCLUDED_FROM_UE4
namespace carla {
namespace rpc {
class EpisodeSettings {
public:
// =========================================================================
// -- Public data members --------------------------------------------------
// =========================================================================
bool synchronous_mode = false;
bool no_rendering_mode = false;
MSGPACK_DEFINE_ARRAY(synchronous_mode, no_rendering_mode);
// =========================================================================
// -- Constructors ---------------------------------------------------------
// =========================================================================
EpisodeSettings() = default;
EpisodeSettings(
bool synchronous_mode,
bool no_rendering_mode)
: synchronous_mode(synchronous_mode),
no_rendering_mode(no_rendering_mode) {}
// =========================================================================
// -- Comparison operators -------------------------------------------------
// =========================================================================
bool operator==(const EpisodeSettings &rhs) const {
return
(synchronous_mode == rhs.synchronous_mode) &&
(no_rendering_mode == rhs.no_rendering_mode);
}
bool operator!=(const EpisodeSettings &rhs) const {
return !(*this == rhs);
}
// =========================================================================
// -- Conversions to UE4 types ---------------------------------------------
// =========================================================================
#ifdef LIBCARLA_INCLUDED_FROM_UE4
EpisodeSettings(const FEpisodeSettings &Settings)
: synchronous_mode(Settings.bSynchronousMode),
no_rendering_mode(Settings.bNoRenderingMode) {}
operator FEpisodeSettings() const {
FEpisodeSettings Settings;
Settings.bSynchronousMode = synchronous_mode;
Settings.bNoRenderingMode = no_rendering_mode;
return Settings;
}
#endif // LIBCARLA_INCLUDED_FROM_UE4
};
} // namespace rpc
} // namespace carla

View File

@ -34,6 +34,19 @@ namespace client {
} // namespace client } // namespace client
} // namespace carla } // namespace carla
namespace carla {
namespace rpc {
std::ostream &operator<<(std::ostream &out, const EpisodeSettings &settings) {
auto BoolToStr = [](bool b) { return b ? "True" : "False"; };
out << "WorldSettings(synchronous_mode=" << BoolToStr(settings.synchronous_mode)
<< ",no_rendering_mode=" << BoolToStr(settings.no_rendering_mode) << ')';
return out;
}
} // namespace rpc
} // namespace carla
static auto WaitForTick(const carla::client::World &world, double seconds) { static auto WaitForTick(const carla::client::World &world, double seconds) {
carla::PythonUtil::ReleaseGIL unlock; carla::PythonUtil::ReleaseGIL unlock;
return world.WaitForTick(TimeDurationFromSeconds(seconds)); return world.WaitForTick(TimeDurationFromSeconds(seconds));
@ -47,6 +60,7 @@ void export_world() {
using namespace boost::python; using namespace boost::python;
namespace cc = carla::client; namespace cc = carla::client;
namespace cg = carla::geom; namespace cg = carla::geom;
namespace cr = carla::rpc;
class_<cc::Timestamp>("Timestamp") class_<cc::Timestamp>("Timestamp")
.def(init<size_t, double, double, double>( .def(init<size_t, double, double, double>(
@ -72,6 +86,17 @@ void export_world() {
.def(self_ns::str(self_ns::self)) .def(self_ns::str(self_ns::self))
; ;
class_<cr::EpisodeSettings>("WorldSettings")
.def(init<bool, bool>(
(arg("synchronous_mode")=false,
arg("no_rendering_mode")=false)))
.def_readwrite("synchronous_mode", &cr::EpisodeSettings::synchronous_mode)
.def_readwrite("no_rendering_mode", &cr::EpisodeSettings::no_rendering_mode)
.def("__eq__", &cc::Timestamp::operator==)
.def("__ne__", &cc::Timestamp::operator!=)
.def(self_ns::str(self_ns::self))
;
#define SPAWN_ACTOR_WITHOUT_GIL(fn) +[]( \ #define SPAWN_ACTOR_WITHOUT_GIL(fn) +[]( \
cc::World &self, \ cc::World &self, \
const cc::ActorBlueprint &blueprint, \ const cc::ActorBlueprint &blueprint, \
@ -89,6 +114,8 @@ void export_world() {
.def("get_blueprint_library", CONST_CALL_WITHOUT_GIL(cc::World, GetBlueprintLibrary)) .def("get_blueprint_library", CONST_CALL_WITHOUT_GIL(cc::World, GetBlueprintLibrary))
.def("get_map", CONST_CALL_WITHOUT_GIL(cc::World, GetMap)) .def("get_map", CONST_CALL_WITHOUT_GIL(cc::World, GetMap))
.def("get_spectator", CONST_CALL_WITHOUT_GIL(cc::World, GetSpectator)) .def("get_spectator", CONST_CALL_WITHOUT_GIL(cc::World, GetSpectator))
.def("get_settings", CONST_CALL_WITHOUT_GIL(cc::World, GetSettings))
.def("apply_settings", &cc::World::ApplySettings)
.def("get_weather", CONST_CALL_WITHOUT_GIL(cc::World, GetWeather)) .def("get_weather", CONST_CALL_WITHOUT_GIL(cc::World, GetWeather))
.def("set_weather", &cc::World::SetWeather) .def("set_weather", &cc::World::SetWeather)
.def("get_actors", CONST_CALL_WITHOUT_GIL(cc::World, GetActors)) .def("get_actors", CONST_CALL_WITHOUT_GIL(cc::World, GetActors))

View File

@ -8,7 +8,9 @@
#include "Carla/Game/CarlaEngine.h" #include "Carla/Game/CarlaEngine.h"
#include "Carla/Game/CarlaEpisode.h" #include "Carla/Game/CarlaEpisode.h"
#include "Carla/Game/CarlaStaticDelegates.h"
#include "Carla/Settings/CarlaSettings.h" #include "Carla/Settings/CarlaSettings.h"
#include "Carla/Settings/EpisodeSettings.h"
#include <thread> #include <thread>
@ -23,6 +25,7 @@ FCarlaEngine::~FCarlaEngine()
{ {
FWorldDelegates::OnWorldTickStart.Remove(OnPreTickHandle); FWorldDelegates::OnWorldTickStart.Remove(OnPreTickHandle);
FWorldDelegates::OnWorldPostActorTick.Remove(OnPostTickHandle); FWorldDelegates::OnWorldPostActorTick.Remove(OnPostTickHandle);
FCarlaStaticDelegates::OnEpisodeSettingsChange.Remove(OnEpisodeSettingsChangeHandle);
} }
} }
@ -40,7 +43,10 @@ void FCarlaEngine::NotifyInitGame(const UCarlaSettings &Settings)
&FCarlaEngine::OnPreTick); &FCarlaEngine::OnPreTick);
OnPostTickHandle = FWorldDelegates::OnWorldPostActorTick.AddRaw( OnPostTickHandle = FWorldDelegates::OnWorldPostActorTick.AddRaw(
this, this,
Settings.bSynchronousMode ? &FCarlaEngine::OnPostTickSync : &FCarlaEngine::OnPostTick); &FCarlaEngine::OnPostTick);
OnEpisodeSettingsChangeHandle = FCarlaStaticDelegates::OnEpisodeSettingsChange.AddRaw(
this,
&FCarlaEngine::OnEpisodeSettingsChanged);
bIsRunning = true; bIsRunning = true;
} }
@ -68,15 +74,20 @@ void FCarlaEngine::OnPreTick(ELevelTick TickType, float DeltaSeconds)
} }
void FCarlaEngine::OnPostTick(UWorld *, ELevelTick, float) void FCarlaEngine::OnPostTick(UWorld *, ELevelTick, float)
{
Server.RunSome(10u);
}
void FCarlaEngine::OnPostTickSync(UWorld *, ELevelTick, float)
{ {
do do
{ {
Server.RunSome(10u); Server.RunSome(10u);
} }
while (!Server.TickCueReceived()); while (bSynchronousMode && !Server.TickCueReceived());
}
void FCarlaEngine::OnEpisodeSettingsChanged(const FEpisodeSettings &Settings)
{
bSynchronousMode = Settings.bSynchronousMode;
if (GEngine && GEngine->GameViewport)
{
GEngine->GameViewport->bDisableWorldRendering = Settings.bNoRenderingMode;
}
} }

View File

@ -13,6 +13,7 @@
#include "Misc/CoreDelegates.h" #include "Misc/CoreDelegates.h"
class UCarlaSettings; class UCarlaSettings;
struct FEpisodeSettings;
class FCarlaEngine : private NonCopyable class FCarlaEngine : private NonCopyable
{ {
@ -42,10 +43,12 @@ private:
void OnPostTick(UWorld *World, ELevelTick TickType, float DeltaSeconds); void OnPostTick(UWorld *World, ELevelTick TickType, float DeltaSeconds);
void OnPostTickSync(UWorld *World, ELevelTick TickType, float DeltaSeconds); void OnEpisodeSettingsChanged(const FEpisodeSettings &Settings);
bool bIsRunning = false; bool bIsRunning = false;
bool bSynchronousMode = false;
FTheNewCarlaServer Server; FTheNewCarlaServer Server;
FWorldObserver WorldObserver; FWorldObserver WorldObserver;
@ -55,4 +58,6 @@ private:
FDelegateHandle OnPreTickHandle; FDelegateHandle OnPreTickHandle;
FDelegateHandle OnPostTickHandle; FDelegateHandle OnPostTickHandle;
FDelegateHandle OnEpisodeSettingsChangeHandle;
}; };

View File

@ -46,6 +46,12 @@ UCarlaEpisode::UCarlaEpisode(const FObjectInitializer &ObjectInitializer)
ActorDispatcher = CreateDefaultSubobject<UActorDispatcher>(TEXT("ActorDispatcher")); ActorDispatcher = CreateDefaultSubobject<UActorDispatcher>(TEXT("ActorDispatcher"));
} }
void UCarlaEpisode::ApplySettings(const FEpisodeSettings &Settings)
{
FCarlaStaticDelegates::OnEpisodeSettingsChange.Broadcast(Settings);
EpisodeSettings = Settings;
}
TArray<FTransform> UCarlaEpisode::GetRecommendedSpawnPoints() const TArray<FTransform> UCarlaEpisode::GetRecommendedSpawnPoints() const
{ {
TArray<FTransform> SpawnPoints; TArray<FTransform> SpawnPoints;

View File

@ -7,15 +7,16 @@
#pragma once #pragma once
#include "Carla/Actor/ActorDispatcher.h" #include "Carla/Actor/ActorDispatcher.h"
#include "Carla/Sensor/WorldObserver.h"
#include "Carla/Weather/Weather.h"
#include "Carla/Server/TheNewCarlaServer.h"
#include "Carla/Recorder/CarlaRecorder.h" #include "Carla/Recorder/CarlaRecorder.h"
#include "Carla/Sensor/WorldObserver.h"
#include "Carla/Server/TheNewCarlaServer.h"
#include "Carla/Settings/EpisodeSettings.h"
#include "Carla/Weather/Weather.h"
#include <compiler/disable-ue4-macros.h> #include <compiler/disable-ue4-macros.h>
#include <carla/geom/BoundingBox.h>
#include <carla/rpc/Actor.h> #include <carla/rpc/Actor.h>
#include <carla/rpc/ActorDescription.h> #include <carla/rpc/ActorDescription.h>
#include <carla/geom/BoundingBox.h>
#include <carla/streaming/Server.h> #include <carla/streaming/Server.h>
#include <compiler/enable-ue4-macros.h> #include <compiler/enable-ue4-macros.h>
@ -37,6 +38,21 @@ public:
UCarlaEpisode(const FObjectInitializer &ObjectInitializer); UCarlaEpisode(const FObjectInitializer &ObjectInitializer);
// ===========================================================================
// -- Episode settings -------------------------------------------------------
// ===========================================================================
public:
UFUNCTION(BlueprintCallable)
const FEpisodeSettings &GetSettings() const
{
return EpisodeSettings;
}
UFUNCTION(BlueprintCallable)
void ApplySettings(const FEpisodeSettings &Settings);
// =========================================================================== // ===========================================================================
// -- Retrieve info about this episode --------------------------------------- // -- Retrieve info about this episode ---------------------------------------
// =========================================================================== // ===========================================================================
@ -261,10 +277,13 @@ private:
const uint32 Id = 0u; const uint32 Id = 0u;
double ElapsedGameTime = 0.0;
UPROPERTY(VisibleAnywhere) UPROPERTY(VisibleAnywhere)
FString MapName; FString MapName;
double ElapsedGameTime = 0.0; UPROPERTY(VisibleAnywhere)
FEpisodeSettings EpisodeSettings;
UPROPERTY(VisibleAnywhere) UPROPERTY(VisibleAnywhere)
UActorDispatcher *ActorDispatcher = nullptr; UActorDispatcher *ActorDispatcher = nullptr;

View File

@ -0,0 +1,9 @@
// 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/Game/CarlaStaticDelegates.h"
FCarlaStaticDelegates::FOnEpisodeSettingsChange FCarlaStaticDelegates::OnEpisodeSettingsChange;

View File

@ -0,0 +1,17 @@
// 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/Settings/EpisodeSettings.h"
class CARLA_API FCarlaStaticDelegates
{
public:
DECLARE_MULTICAST_DELEGATE_OneParam(FOnEpisodeSettingsChange, const FEpisodeSettings &);
static FOnEpisodeSettingsChange OnEpisodeSettingsChange;
};

View File

@ -19,6 +19,7 @@
#include <carla/rpc/ActorDescription.h> #include <carla/rpc/ActorDescription.h>
#include <carla/rpc/DebugShape.h> #include <carla/rpc/DebugShape.h>
#include <carla/rpc/EpisodeInfo.h> #include <carla/rpc/EpisodeInfo.h>
#include <carla/rpc/EpisodeSettings.h>
#include <carla/rpc/MapInfo.h> #include <carla/rpc/MapInfo.h>
#include <carla/rpc/Response.h> #include <carla/rpc/Response.h>
#include <carla/rpc/Server.h> #include <carla/rpc/Server.h>
@ -142,6 +143,19 @@ void FTheNewCarlaServer::FPimpl::BindActions()
MakeVectorFromTArray<cg::Transform>(SpawnPoints)}; MakeVectorFromTArray<cg::Transform>(SpawnPoints)};
}); });
Server.BindSync("get_episode_settings", [this]() -> R<cr::EpisodeSettings>
{
REQUIRE_CARLA_EPISODE();
return cr::EpisodeSettings{Episode->GetSettings()};
});
Server.BindSync("set_episode_settings", [this](const cr::EpisodeSettings &settings) -> R<void>
{
REQUIRE_CARLA_EPISODE();
Episode->ApplySettings(settings);
return R<void>::Success();
});
Server.BindSync("get_actor_definitions", [this]() -> R<std::vector<cr::ActorDefinition>> Server.BindSync("get_actor_definitions", [this]() -> R<std::vector<cr::ActorDefinition>>
{ {
REQUIRE_CARLA_EPISODE(); REQUIRE_CARLA_EPISODE();

View File

@ -0,0 +1,21 @@
// 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 "EpisodeSettings.generated.h"
USTRUCT(BlueprintType)
struct CARLA_API FEpisodeSettings
{
GENERATED_BODY()
UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool bSynchronousMode = false;
UPROPERTY(EditAnywhere, BlueprintReadWrite)
bool bNoRenderingMode = false;
};