From d7d4dd4e1e0bfefb4569ced36c2798824180bc9f Mon Sep 17 00:00:00 2001 From: nsubiron Date: Tue, 16 Oct 2018 20:12:23 +0200 Subject: [PATCH] Add episode info message --- Docs/python_api.md | 2 ++ LibCarla/source/carla/client/World.cpp | 8 ++++++ LibCarla/source/carla/client/World.h | 4 +++ .../source/carla/client/detail/Client.cpp | 8 ++++++ LibCarla/source/carla/client/detail/Client.h | 5 ++++ .../source/carla/client/detail/EpisodeState.h | 2 -- .../source/carla/client/detail/Simulator.cpp | 25 ++++++++++++++++++- .../source/carla/client/detail/Simulator.h | 15 +++++++---- PythonAPI/source/libcarla/World.cpp | 2 ++ .../Carla/Source/Carla/Game/CarlaEpisode.cpp | 7 ++++++ .../Carla/Source/Carla/Game/CarlaEpisode.h | 9 +++++++ .../Source/Carla/Server/TheNewCarlaServer.cpp | 8 +++++- 12 files changed, 86 insertions(+), 9 deletions(-) diff --git a/Docs/python_api.md b/Docs/python_api.md index e790dc7f8..350031c78 100644 --- a/Docs/python_api.md +++ b/Docs/python_api.md @@ -15,6 +15,8 @@ ## `carla.World` +- `id` +- `map_name` - `get_blueprint_library()` - `get_spectator()` - `get_weather()` diff --git a/LibCarla/source/carla/client/World.cpp b/LibCarla/source/carla/client/World.cpp index 227f8b3d8..c3e705937 100644 --- a/LibCarla/source/carla/client/World.cpp +++ b/LibCarla/source/carla/client/World.cpp @@ -16,6 +16,14 @@ namespace carla { namespace client { + uint32_t World::GetId() const { + return _episode->GetCurrentEpisodeId(); + } + + const std::string &World::GetMapName() const { + return _episode->GetCurrentMapName(); + } + SharedPtr World::GetBlueprintLibrary() const { return _episode->GetBlueprintLibrary(); } diff --git a/LibCarla/source/carla/client/World.h b/LibCarla/source/carla/client/World.h index 44046fcb6..d3929854f 100644 --- a/LibCarla/source/carla/client/World.h +++ b/LibCarla/source/carla/client/World.h @@ -29,6 +29,10 @@ namespace client { World &operator=(const World &) = default; World &operator=(World &&) = default; + uint32_t GetId() const; + + const std::string &GetMapName() const; + SharedPtr GetBlueprintLibrary() const; SharedPtr GetSpectator() const; diff --git a/LibCarla/source/carla/client/detail/Client.cpp b/LibCarla/source/carla/client/detail/Client.cpp index 245f7f63f..f5753d32a 100644 --- a/LibCarla/source/carla/client/detail/Client.cpp +++ b/LibCarla/source/carla/client/detail/Client.cpp @@ -65,10 +65,18 @@ namespace detail { _pimpl->rpc_client.set_timeout(timeout.milliseconds()); } + std::string Client::GetClientVersion() { + return ::carla::version(); + } + std::string Client::GetServerVersion() { return _pimpl->CallAndWait("version"); } + rpc::EpisodeInfo Client::GetEpisodeInfo() { + return _pimpl->CallAndWait("get_episode_info"); + } + std::vector Client::GetActorDefinitions() { return _pimpl->CallAndWait>("get_actor_definitions"); } diff --git a/LibCarla/source/carla/client/detail/Client.h b/LibCarla/source/carla/client/detail/Client.h index 401dfb761..977296382 100644 --- a/LibCarla/source/carla/client/detail/Client.h +++ b/LibCarla/source/carla/client/detail/Client.h @@ -12,6 +12,7 @@ #include "carla/geom/Transform.h" #include "carla/rpc/Actor.h" #include "carla/rpc/ActorDefinition.h" +#include "carla/rpc/EpisodeInfo.h" #include "carla/rpc/WeatherParameters.h" #include @@ -49,8 +50,12 @@ namespace detail { void SetTimeout(time_duration timeout); + std::string GetClientVersion(); + std::string GetServerVersion(); + rpc::EpisodeInfo GetEpisodeInfo(); + std::vector GetActorDefinitions(); rpc::Actor GetSpectator(); diff --git a/LibCarla/source/carla/client/detail/EpisodeState.h b/LibCarla/source/carla/client/detail/EpisodeState.h index ca7a78d39..0b14d0c51 100644 --- a/LibCarla/source/carla/client/detail/EpisodeState.h +++ b/LibCarla/source/carla/client/detail/EpisodeState.h @@ -17,8 +17,6 @@ namespace detail { class EpisodeState : private MovableNonCopyable { public: - EpisodeState() = default; /// @todo - EpisodeState(rpc::EpisodeInfo description) : _description(std::move(description)) {} diff --git a/LibCarla/source/carla/client/detail/Simulator.cpp b/LibCarla/source/carla/client/detail/Simulator.cpp index bbd435fe9..1c4bd5d29 100644 --- a/LibCarla/source/carla/client/detail/Simulator.cpp +++ b/LibCarla/source/carla/client/detail/Simulator.cpp @@ -19,6 +19,18 @@ namespace carla { namespace client { namespace detail { + static void ValidateVersions(Client &client) { + const auto vc = client.GetClientVersion(); + const auto vs = client.GetServerVersion(); + if (vc != vs) { + log_warning( + "Version mismatch detected: You are trying to connect to a simulator", + "that might be incompatible with this API"); + log_warning("Client API version: =", vc); + log_warning("Simulator API version =", vs); + } + } + // =========================================================================== // -- Constructor ------------------------------------------------------------ // =========================================================================== @@ -30,10 +42,21 @@ namespace detail { const bool enable_garbage_collection) : LIBCARLA_INITIALIZE_LIFETIME_PROFILER("SimulatorClient("s + host + ":" + std::to_string(port) + ")"), _client(host, port, worker_threads), - _episode(), /// @todo _gc_policy(enable_garbage_collection ? GarbageCollectionPolicy::Enabled : GarbageCollectionPolicy::Disabled) {} + // =========================================================================== + // -- Access to current episode ---------------------------------------------- + // =========================================================================== + + EpisodeProxy Simulator::GetCurrentEpisode() { + if (_episode == nullptr) { + ValidateVersions(_client); + _episode = std::make_unique(_client.GetEpisodeInfo()); + } + return EpisodeProxy{shared_from_this()}; + } + // =========================================================================== // -- Access to global objects in the episode -------------------------------- // =========================================================================== diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index 5cffc50a9..06565dc0d 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -6,6 +6,7 @@ #pragma once +#include "carla/Debug.h" #include "carla/Memory.h" #include "carla/NonCopyable.h" #include "carla/Version.h" @@ -54,13 +55,17 @@ namespace detail { /// @{ auto GetCurrentEpisodeId() const { - return _episode.GetId(); + DEBUG_ASSERT(_episode != nullptr); + return _episode->GetId(); } - EpisodeProxy GetCurrentEpisode() { - return EpisodeProxy{shared_from_this()}; + const std::string &GetCurrentMapName() const { + DEBUG_ASSERT(_episode != nullptr); + return _episode->GetMapName(); } + EpisodeProxy GetCurrentEpisode(); + /// @} // ========================================================================= /// @name Garbage collection policy @@ -82,7 +87,7 @@ namespace detail { } std::string GetClientVersion() { - return ::carla::version(); + return _client.GetClientVersion(); } std::string GetServerVersion() { @@ -165,7 +170,7 @@ namespace detail { Client _client; - EpisodeState _episode; + std::unique_ptr _episode; GarbageCollectionPolicy _gc_policy; }; diff --git a/PythonAPI/source/libcarla/World.cpp b/PythonAPI/source/libcarla/World.cpp index 4514e13ba..cd480c3d0 100644 --- a/PythonAPI/source/libcarla/World.cpp +++ b/PythonAPI/source/libcarla/World.cpp @@ -24,6 +24,8 @@ void export_world() { (arg("blueprint"), arg("transform"), arg("attach_to")=carla::SharedPtr()) class_("World", no_init) + .add_property("id", &cc::World::GetId) + .add_property("map_name", CALL_RETURNING_COPY(cc::World, GetMapName)) .def("get_blueprint_library", CONST_CALL_WITHOUT_GIL(cc::World, GetBlueprintLibrary)) .def("get_spectator", CONST_CALL_WITHOUT_GIL(cc::World, GetSpectator)) .def("get_weather", CONST_CALL_WITHOUT_GIL(cc::World, GetWeather)) diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp index 93677e573..f43fcddb6 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.cpp @@ -10,6 +10,13 @@ #include "EngineUtils.h" #include "GameFramework/SpectatorPawn.h" +UCarlaEpisode::UCarlaEpisode(const FObjectInitializer &ObjectInitializer) + : Super(ObjectInitializer), + Id([]() { + static uint32 COUNTER = 0u; + return ++COUNTER; + }()) {} + void UCarlaEpisode::InitializeAtBeginPlay() { auto World = GetWorld(); diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h index dd59a2137..8e285ea96 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Game/CarlaEpisode.h @@ -21,6 +21,13 @@ class CARLA_API UCarlaEpisode : public UObject public: + UCarlaEpisode(const FObjectInitializer &ObjectInitializer); + + auto GetId() const + { + return Id; + } + UFUNCTION(BlueprintCallable) const FString &GetMapName() const { @@ -97,6 +104,8 @@ private: ActorDispatcher.Bind(ActorFactory); } + const uint32 Id = 0u; + UPROPERTY(VisibleAnywhere) FString MapName; diff --git a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp index e5c8de3ec..0db3a5c1a 100644 --- a/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp +++ b/Unreal/CarlaUE4/Plugins/Carla/Source/Carla/Server/TheNewCarlaServer.cpp @@ -16,6 +16,7 @@ #include #include #include +#include #include #include #include @@ -133,7 +134,12 @@ void FTheNewCarlaServer::FPimpl::BindActions() Server.BindAsync("ping", []() { return true; }); - Server.BindAsync("version", []() { return std::string(carla::version()); }); + Server.BindAsync("version", []() -> std::string { return carla::version(); }); + + Server.BindSync("get_episode_info", [this]() -> cr::EpisodeInfo { + RequireEpisode(); + return {Episode->GetId(), cr::FromFString(Episode->GetMapName())}; + }); Server.BindSync("get_actor_definitions", [this]() { RequireEpisode();