diff --git a/LibCarla/source/carla/client/World.cpp b/LibCarla/source/carla/client/World.cpp index 6f90c25cc..117e9415f 100644 --- a/LibCarla/source/carla/client/World.cpp +++ b/LibCarla/source/carla/client/World.cpp @@ -37,6 +37,14 @@ namespace client { 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 { return _episode.Lock()->GetWeatherParameters(); } diff --git a/LibCarla/source/carla/client/World.h b/LibCarla/source/carla/client/World.h index 55679df5b..7f35e6b35 100644 --- a/LibCarla/source/carla/client/World.h +++ b/LibCarla/source/carla/client/World.h @@ -13,6 +13,7 @@ #include "carla/client/detail/EpisodeProxy.h" #include "carla/geom/Transform.h" #include "carla/rpc/Actor.h" +#include "carla/rpc/EpisodeSettings.h" #include "carla/rpc/VehiclePhysicsControl.h" #include "carla/rpc/WeatherParameters.h" @@ -53,6 +54,10 @@ namespace client { /// simulator window. SharedPtr GetSpectator() const; + rpc::EpisodeSettings GetSettings() const; + + void ApplySettings(const rpc::EpisodeSettings &settings); + /// Retrieve the weather parameters currently active in the world. rpc::WeatherParameters GetWeather() const; diff --git a/LibCarla/source/carla/client/detail/Client.cpp b/LibCarla/source/carla/client/detail/Client.cpp index 5f4718ec0..589f04ca7 100644 --- a/LibCarla/source/carla/client/detail/Client.cpp +++ b/LibCarla/source/carla/client/detail/Client.cpp @@ -139,6 +139,14 @@ namespace detail { return _pimpl->CallAndWait("get_spectator"); } + rpc::EpisodeSettings Client::GetEpisodeSettings() { + return _pimpl->CallAndWait("get_episode_settings"); + } + + void Client::SetEpisodeSettings(const rpc::EpisodeSettings &settings) { + _pimpl->AsyncCall("set_episode_settings", settings); + } + rpc::WeatherParameters Client::GetWeatherParameters() { return _pimpl->CallAndWait("get_weather_parameters"); } diff --git a/LibCarla/source/carla/client/detail/Client.h b/LibCarla/source/carla/client/detail/Client.h index 980fe04a5..49eaf004a 100644 --- a/LibCarla/source/carla/client/detail/Client.h +++ b/LibCarla/source/carla/client/detail/Client.h @@ -13,10 +13,11 @@ #include "carla/rpc/Actor.h" #include "carla/rpc/ActorDefinition.h" #include "carla/rpc/EpisodeInfo.h" +#include "carla/rpc/EpisodeSettings.h" #include "carla/rpc/MapInfo.h" -#include "carla/rpc/WeatherParameters.h" #include "carla/rpc/TrafficLightState.h" #include "carla/rpc/VehiclePhysicsControl.h" +#include "carla/rpc/WeatherParameters.h" #include #include @@ -76,6 +77,10 @@ namespace detail { rpc::Actor GetSpectator(); + rpc::EpisodeSettings GetEpisodeSettings(); + + void SetEpisodeSettings(const rpc::EpisodeSettings &settings); + rpc::WeatherParameters GetWeatherParameters(); void SetWeatherParameters(const rpc::WeatherParameters &weather); diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index dbbd4114e..ba389c117 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -128,6 +128,14 @@ namespace detail { SharedPtr GetSpectator(); + rpc::EpisodeSettings GetEpisodeSettings() { + return _client.GetEpisodeSettings(); + } + + void SetEpisodeSettings(const rpc::EpisodeSettings &settings) { + _client.SetEpisodeSettings(settings); + } + rpc::WeatherParameters GetWeatherParameters() { return _client.GetWeatherParameters(); } diff --git a/LibCarla/source/carla/rpc/EpisodeSettings.h b/LibCarla/source/carla/rpc/EpisodeSettings.h new file mode 100644 index 000000000..1f0010910 --- /dev/null +++ b/LibCarla/source/carla/rpc/EpisodeSettings.h @@ -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 . + +#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 diff --git a/PythonAPI/source/libcarla/World.cpp b/PythonAPI/source/libcarla/World.cpp index ede0bd165..3014191b2 100644 --- a/PythonAPI/source/libcarla/World.cpp +++ b/PythonAPI/source/libcarla/World.cpp @@ -34,6 +34,19 @@ namespace client { } // namespace client } // 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) { carla::PythonUtil::ReleaseGIL unlock; return world.WaitForTick(TimeDurationFromSeconds(seconds)); @@ -47,6 +60,7 @@ void export_world() { using namespace boost::python; namespace cc = carla::client; namespace cg = carla::geom; + namespace cr = carla::rpc; class_("Timestamp") .def(init( @@ -72,6 +86,17 @@ void export_world() { .def(self_ns::str(self_ns::self)) ; + class_("WorldSettings") + .def(init( + (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) +[]( \ cc::World &self, \ const cc::ActorBlueprint &blueprint, \ @@ -89,6 +114,8 @@ void export_world() { .def("get_blueprint_library", CONST_CALL_WITHOUT_GIL(cc::World, GetBlueprintLibrary)) .def("get_map", CONST_CALL_WITHOUT_GIL(cc::World, GetMap)) .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("set_weather", &cc::World::SetWeather) .def("get_actors", CONST_CALL_WITHOUT_GIL(cc::World, GetActors)) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.cpp index fdbd7eb3b..f446ceb83 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.cpp @@ -8,7 +8,9 @@ #include "Carla/Game/CarlaEngine.h" #include "Carla/Game/CarlaEpisode.h" +#include "Carla/Game/CarlaStaticDelegates.h" #include "Carla/Settings/CarlaSettings.h" +#include "Carla/Settings/EpisodeSettings.h" #include @@ -23,6 +25,7 @@ FCarlaEngine::~FCarlaEngine() { FWorldDelegates::OnWorldTickStart.Remove(OnPreTickHandle); FWorldDelegates::OnWorldPostActorTick.Remove(OnPostTickHandle); + FCarlaStaticDelegates::OnEpisodeSettingsChange.Remove(OnEpisodeSettingsChangeHandle); } } @@ -40,7 +43,10 @@ void FCarlaEngine::NotifyInitGame(const UCarlaSettings &Settings) &FCarlaEngine::OnPreTick); OnPostTickHandle = FWorldDelegates::OnWorldPostActorTick.AddRaw( this, - Settings.bSynchronousMode ? &FCarlaEngine::OnPostTickSync : &FCarlaEngine::OnPostTick); + &FCarlaEngine::OnPostTick); + OnEpisodeSettingsChangeHandle = FCarlaStaticDelegates::OnEpisodeSettingsChange.AddRaw( + this, + &FCarlaEngine::OnEpisodeSettingsChanged); bIsRunning = true; } @@ -68,15 +74,20 @@ void FCarlaEngine::OnPreTick(ELevelTick TickType, float DeltaSeconds) } void FCarlaEngine::OnPostTick(UWorld *, ELevelTick, float) -{ - Server.RunSome(10u); -} - -void FCarlaEngine::OnPostTickSync(UWorld *, ELevelTick, float) { do { 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; + } } diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.h index 224cbbc53..aa4f5a5a9 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEngine.h @@ -13,6 +13,7 @@ #include "Misc/CoreDelegates.h" class UCarlaSettings; +struct FEpisodeSettings; class FCarlaEngine : private NonCopyable { @@ -42,10 +43,12 @@ private: 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 bSynchronousMode = false; + FTheNewCarlaServer Server; FWorldObserver WorldObserver; @@ -55,4 +58,6 @@ private: FDelegateHandle OnPreTickHandle; FDelegateHandle OnPostTickHandle; + + FDelegateHandle OnEpisodeSettingsChangeHandle; }; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp index a1dec56a1..7c523a7d5 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp @@ -46,6 +46,12 @@ UCarlaEpisode::UCarlaEpisode(const FObjectInitializer &ObjectInitializer) ActorDispatcher = CreateDefaultSubobject(TEXT("ActorDispatcher")); } +void UCarlaEpisode::ApplySettings(const FEpisodeSettings &Settings) +{ + FCarlaStaticDelegates::OnEpisodeSettingsChange.Broadcast(Settings); + EpisodeSettings = Settings; +} + TArray UCarlaEpisode::GetRecommendedSpawnPoints() const { TArray SpawnPoints; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h index 0d5378d47..d21b1c4fd 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h @@ -7,15 +7,16 @@ #pragma once #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/Sensor/WorldObserver.h" +#include "Carla/Server/TheNewCarlaServer.h" +#include "Carla/Settings/EpisodeSettings.h" +#include "Carla/Weather/Weather.h" #include +#include #include #include -#include #include #include @@ -37,6 +38,21 @@ public: 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 --------------------------------------- // =========================================================================== @@ -261,10 +277,13 @@ private: const uint32 Id = 0u; + double ElapsedGameTime = 0.0; + UPROPERTY(VisibleAnywhere) FString MapName; - double ElapsedGameTime = 0.0; + UPROPERTY(VisibleAnywhere) + FEpisodeSettings EpisodeSettings; UPROPERTY(VisibleAnywhere) UActorDispatcher *ActorDispatcher = nullptr; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStaticDelegates.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStaticDelegates.cpp new file mode 100644 index 000000000..8ce9ab52b --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStaticDelegates.cpp @@ -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 . + +#include "Carla/Game/CarlaStaticDelegates.h" + +FCarlaStaticDelegates::FOnEpisodeSettingsChange FCarlaStaticDelegates::OnEpisodeSettingsChange; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStaticDelegates.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStaticDelegates.h new file mode 100644 index 000000000..17d48c18c --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaStaticDelegates.h @@ -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 . + +#pragma once + +#include "Carla/Settings/EpisodeSettings.h" + +class CARLA_API FCarlaStaticDelegates +{ +public: + + DECLARE_MULTICAST_DELEGATE_OneParam(FOnEpisodeSettingsChange, const FEpisodeSettings &); + static FOnEpisodeSettingsChange OnEpisodeSettingsChange; +}; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp index 5cedc2974..b37caae02 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -142,6 +143,19 @@ void FTheNewCarlaServer::FPimpl::BindActions() MakeVectorFromTArray(SpawnPoints)}; }); + Server.BindSync("get_episode_settings", [this]() -> R + { + REQUIRE_CARLA_EPISODE(); + return cr::EpisodeSettings{Episode->GetSettings()}; + }); + + Server.BindSync("set_episode_settings", [this](const cr::EpisodeSettings &settings) -> R + { + REQUIRE_CARLA_EPISODE(); + Episode->ApplySettings(settings); + return R::Success(); + }); + Server.BindSync("get_actor_definitions", [this]() -> R> { REQUIRE_CARLA_EPISODE(); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/EpisodeSettings.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/EpisodeSettings.h new file mode 100644 index 000000000..21c21d7df --- /dev/null +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Settings/EpisodeSettings.h @@ -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 . + +#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; +};