From e3e72206a28242b2eb3dd7c53f36093edd0133f2 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Mon, 17 Jun 2019 18:33:40 +0200 Subject: [PATCH] Make on_tick and wait_for_tick pass WorldSnapshot instead of Timestamps --- Docs/python_api.md | 2 +- LibCarla/source/carla/RecurrentSharedFuture.h | 7 +++++-- LibCarla/source/carla/client/GnssSensor.cpp | 4 ++-- LibCarla/source/carla/client/LaneInvasionSensor.cpp | 4 ++-- LibCarla/source/carla/client/World.cpp | 4 ++-- LibCarla/source/carla/client/World.h | 4 ++-- LibCarla/source/carla/client/WorldSnapshot.h | 8 +++----- LibCarla/source/carla/client/detail/Episode.cpp | 6 +++--- LibCarla/source/carla/client/detail/Episode.h | 11 ++++++----- LibCarla/source/carla/client/detail/Simulator.cpp | 2 +- LibCarla/source/carla/client/detail/Simulator.h | 4 ++-- PythonAPI/carla/source/libcarla/Snapshot.cpp | 6 ++++++ PythonAPI/examples/automatic_control.py | 3 +-- PythonAPI/examples/dynamic_weather.py | 2 +- PythonAPI/examples/synchronous_mode.py | 2 +- PythonAPI/examples/vehicle_gallery.py | 2 +- PythonAPI/test/smoke/test_sync.py | 2 +- 17 files changed, 40 insertions(+), 33 deletions(-) diff --git a/Docs/python_api.md b/Docs/python_api.md index f242d6c21..63e606f93 100644 --- a/Docs/python_api.md +++ b/Docs/python_api.md @@ -39,7 +39,7 @@ - `get_actors(actor_ids=None) -> carla.ActorList` - `spawn_actor(blueprint, transform, attach_to=None)` - `try_spawn_actor(blueprint, transform, attach_to=None, attachment_type=carla.AttachmentType.Rigid)` -- `wait_for_tick(seconds=1.0)` +- `wait_for_tick(seconds=1.0) -> carla.WorldSnapshot` - `on_tick(callback)` - `tick()` diff --git a/LibCarla/source/carla/RecurrentSharedFuture.h b/LibCarla/source/carla/RecurrentSharedFuture.h index 8e1f0b12e..824a075f1 100644 --- a/LibCarla/source/carla/RecurrentSharedFuture.h +++ b/LibCarla/source/carla/RecurrentSharedFuture.h @@ -63,7 +63,7 @@ namespace detail { struct mapped_type { bool should_wait; - boost::variant value; + boost::variant value; }; std::map _map; @@ -80,6 +80,9 @@ namespace detail { class SharedException : public std::exception { public: + SharedException() + : _exception(std::make_shared("uninitialized SharedException")) {} + SharedException(std::shared_ptr e) : _exception(std::move(e)) {} @@ -106,7 +109,7 @@ namespace detail { if (!_cv.wait_for(lock, timeout.to_chrono(), [&]() { return !r.should_wait; })) { return {}; } - if (r.value.which() == 1) { + if (r.value.which() == 0) { throw_exception(boost::get(r.value)); } return boost::get(std::move(r.value)); diff --git a/LibCarla/source/carla/client/GnssSensor.cpp b/LibCarla/source/carla/client/GnssSensor.cpp index 49220927b..cb8f11e02 100644 --- a/LibCarla/source/carla/client/GnssSensor.cpp +++ b/LibCarla/source/carla/client/GnssSensor.cpp @@ -37,10 +37,10 @@ namespace client { log_debug(GetDisplayId(), ": subscribing to tick event"); GetEpisode().Lock()->RegisterOnTickEvent([ cb=std::move(callback), - weak_self=WeakPtr(self)](const auto ×tamp) { + weak_self=WeakPtr(self)](const auto &snapshot) { auto self = weak_self.lock(); if (self != nullptr) { - auto data = self->TickGnssSensor(timestamp); + auto data = self->TickGnssSensor(snapshot.GetTimestamp()); if (data != nullptr) { cb(std::move(data)); } diff --git a/LibCarla/source/carla/client/LaneInvasionSensor.cpp b/LibCarla/source/carla/client/LaneInvasionSensor.cpp index 70043e33c..3b91b7117 100644 --- a/LibCarla/source/carla/client/LaneInvasionSensor.cpp +++ b/LibCarla/source/carla/client/LaneInvasionSensor.cpp @@ -62,10 +62,10 @@ namespace client { log_debug(GetDisplayId(), ": subscribing to tick event"); GetEpisode().Lock()->RegisterOnTickEvent([ cb=std::move(callback), - weak_self=WeakPtr(self)](const auto ×tamp) { + weak_self=WeakPtr(self)](const auto &snapshot) { auto self = weak_self.lock(); if (self != nullptr) { - auto data = self->TickLaneInvasionSensor(timestamp); + auto data = self->TickLaneInvasionSensor(snapshot.GetTimestamp()); if (data != nullptr) { cb(std::move(data)); } diff --git a/LibCarla/source/carla/client/World.cpp b/LibCarla/source/carla/client/World.cpp index 883638986..98f633317 100644 --- a/LibCarla/source/carla/client/World.cpp +++ b/LibCarla/source/carla/client/World.cpp @@ -89,11 +89,11 @@ namespace client { } } - Timestamp World::WaitForTick(time_duration timeout) const { + WorldSnapshot World::WaitForTick(time_duration timeout) const { return _episode.Lock()->WaitForTick(timeout); } - void World::OnTick(std::function callback) { + void World::OnTick(std::function callback) { return _episode.Lock()->RegisterOnTickEvent(std::move(callback)); } diff --git a/LibCarla/source/carla/client/World.h b/LibCarla/source/carla/client/World.h index 4003794b9..04cf21a68 100644 --- a/LibCarla/source/carla/client/World.h +++ b/LibCarla/source/carla/client/World.h @@ -95,10 +95,10 @@ namespace client { rpc::AttachmentType attachment_type = rpc::AttachmentType::Rigid) noexcept; /// Block calling thread until a world tick is received. - Timestamp WaitForTick(time_duration timeout) const; + WorldSnapshot WaitForTick(time_duration timeout) const; /// Register a @a callback to be called every time a world tick is received. - void OnTick(std::function callback); + void OnTick(std::function callback); /// Signal the simulator to continue to next tick (only has effect on /// synchronous mode). diff --git a/LibCarla/source/carla/client/WorldSnapshot.h b/LibCarla/source/carla/client/WorldSnapshot.h index 24b45d834..201c6cd17 100644 --- a/LibCarla/source/carla/client/WorldSnapshot.h +++ b/LibCarla/source/carla/client/WorldSnapshot.h @@ -18,6 +18,9 @@ namespace client { class WorldSnapshot { public: + WorldSnapshot(std::shared_ptr state) + : _state(std::move(state)) {} + /// Get the id of the episode associated with this world. uint64_t GetId() const { return _state->GetEpisodeId(); @@ -63,11 +66,6 @@ namespace client { private: - friend class detail::Simulator; - - explicit WorldSnapshot(std::shared_ptr &&state) - : _state(std::move(state)) {} - std::shared_ptr _state; }; diff --git a/LibCarla/source/carla/client/detail/Episode.cpp b/LibCarla/source/carla/client/detail/Episode.cpp index 21043bcd1..515be0e74 100644 --- a/LibCarla/source/carla/client/detail/Episode.cpp +++ b/LibCarla/source/carla/client/detail/Episode.cpp @@ -57,7 +57,7 @@ namespace detail { auto prev = self->GetState(); do { if (prev->GetFrameCount() >= next->GetFrameCount()) { - self->_on_tick_callbacks.Call(next->GetTimestamp()); + self->_on_tick_callbacks.Call(next); return; } } while (!self->_state.compare_exchange(&prev, next)); @@ -67,8 +67,8 @@ namespace detail { } // Notify waiting threads and do the callbacks. - self->_timestamp.SetValue(next->GetTimestamp()); - self->_on_tick_callbacks.Call(next->GetTimestamp()); + self->_snapshot.SetValue(next); + self->_on_tick_callbacks.Call(next); } }); } diff --git a/LibCarla/source/carla/client/detail/Episode.h b/LibCarla/source/carla/client/detail/Episode.h index 196d1bf28..b41a5c863 100644 --- a/LibCarla/source/carla/client/detail/Episode.h +++ b/LibCarla/source/carla/client/detail/Episode.h @@ -10,6 +10,7 @@ #include "carla/NonCopyable.h" #include "carla/RecurrentSharedFuture.h" #include "carla/client/Timestamp.h" +#include "carla/client/WorldSnapshot.h" #include "carla/client/detail/CachedActorList.h" #include "carla/client/detail/CallbackList.h" #include "carla/client/detail/EpisodeState.h" @@ -57,11 +58,11 @@ namespace detail { std::vector GetActors(); - boost::optional WaitForState(time_duration timeout) { - return _timestamp.WaitFor(timeout); + boost::optional WaitForState(time_duration timeout) { + return _snapshot.WaitFor(timeout); } - void RegisterOnTickEvent(std::function callback) { + void RegisterOnTickEvent(std::function callback) { _on_tick_callbacks.RegisterCallback(std::move(callback)); } @@ -77,9 +78,9 @@ namespace detail { CachedActorList _actors; - CallbackList _on_tick_callbacks; + CallbackList _on_tick_callbacks; - RecurrentSharedFuture _timestamp; + RecurrentSharedFuture _snapshot; const streaming::Token _token; }; diff --git a/LibCarla/source/carla/client/detail/Simulator.cpp b/LibCarla/source/carla/client/detail/Simulator.cpp index c69ae4e63..de7c4a7bd 100644 --- a/LibCarla/source/carla/client/detail/Simulator.cpp +++ b/LibCarla/source/carla/client/detail/Simulator.cpp @@ -95,7 +95,7 @@ namespace detail { // -- Tick ------------------------------------------------------------------- // =========================================================================== - Timestamp Simulator::WaitForTick(time_duration timeout) { + WorldSnapshot Simulator::WaitForTick(time_duration timeout) { DEBUG_ASSERT(_episode != nullptr); auto result = _episode->WaitForState(timeout); if (!result.has_value()) { diff --git a/LibCarla/source/carla/client/detail/Simulator.h b/LibCarla/source/carla/client/detail/Simulator.h index 98754cdcb..794fbc2e0 100644 --- a/LibCarla/source/carla/client/detail/Simulator.h +++ b/LibCarla/source/carla/client/detail/Simulator.h @@ -137,9 +137,9 @@ namespace detail { // ========================================================================= /// @{ - Timestamp WaitForTick(time_duration timeout); + WorldSnapshot WaitForTick(time_duration timeout); - void RegisterOnTickEvent(std::function callback) { + void RegisterOnTickEvent(std::function callback) { DEBUG_ASSERT(_episode != nullptr); _episode->RegisterOnTickEvent(std::move(callback)); } diff --git a/PythonAPI/carla/source/libcarla/Snapshot.cpp b/PythonAPI/carla/source/libcarla/Snapshot.cpp index 06be807bf..d5733099a 100644 --- a/PythonAPI/carla/source/libcarla/Snapshot.cpp +++ b/PythonAPI/carla/source/libcarla/Snapshot.cpp @@ -43,6 +43,12 @@ void export_snapshot() { class_("WorldSnapshot", no_init) .add_property("id", &cc::WorldSnapshot::GetId) .add_property("timestamp", CALL_RETURNING_COPY(cc::WorldSnapshot, GetTimestamp)) + /// Deprecated, use timestamp @{ + .add_property("frame_count", +[](const cc::WorldSnapshot &self) { return self.GetTimestamp().frame_count; }) + .add_property("elapsed_seconds", +[](const cc::WorldSnapshot &self) { return self.GetTimestamp().elapsed_seconds; }) + .add_property("delta_seconds", +[](const cc::WorldSnapshot &self) { return self.GetTimestamp().delta_seconds; }) + .add_property("platform_timestamp", +[](const cc::WorldSnapshot &self) { return self.GetTimestamp().platform_timestamp; }) + /// @} .def("has_actor", &cc::WorldSnapshot::Contains, (arg("actor_id"))) .def("find", CALL_RETURNING_OPTIONAL_1(cc::WorldSnapshot, Find, carla::ActorId), (arg("actor_id"))) .def("__len__", &cc::WorldSnapshot::size) diff --git a/PythonAPI/examples/automatic_control.py b/PythonAPI/examples/automatic_control.py index f80ee409d..bc96e125e 100755 --- a/PythonAPI/examples/automatic_control.py +++ b/PythonAPI/examples/automatic_control.py @@ -753,8 +753,7 @@ def game_loop(args): return # as soon as the server is ready continue! - if not world.world.wait_for_tick(10.0): - continue + world.world.wait_for_tick(10.0) world.tick(clock) world.render(display) diff --git a/PythonAPI/examples/dynamic_weather.py b/PythonAPI/examples/dynamic_weather.py index b3d61647d..4fd3e04da 100755 --- a/PythonAPI/examples/dynamic_weather.py +++ b/PythonAPI/examples/dynamic_weather.py @@ -132,7 +132,7 @@ def main(): elapsed_time = 0.0 while True: - timestamp = world.wait_for_tick(seconds=30.0) + timestamp = world.wait_for_tick(seconds=30.0).timestamp elapsed_time += timestamp.delta_seconds if elapsed_time > update_freq: weather.tick(speed_factor * elapsed_time) diff --git a/PythonAPI/examples/synchronous_mode.py b/PythonAPI/examples/synchronous_mode.py index 7e7c5c416..a1efd9adb 100755 --- a/PythonAPI/examples/synchronous_mode.py +++ b/PythonAPI/examples/synchronous_mode.py @@ -118,7 +118,7 @@ def main(): clock.tick() world.tick() - ts = world.wait_for_tick() + ts = world.wait_for_tick().timestamp if frame is not None: if ts.frame_count != frame + 1: diff --git a/PythonAPI/examples/vehicle_gallery.py b/PythonAPI/examples/vehicle_gallery.py index b6477577f..06777648e 100755 --- a/PythonAPI/examples/vehicle_gallery.py +++ b/PythonAPI/examples/vehicle_gallery.py @@ -49,7 +49,7 @@ def main(): angle = 0 while angle < 356: - timestamp = world.wait_for_tick() + timestamp = world.wait_for_tick().timestamp angle += timestamp.delta_seconds * 60.0 spectator.set_transform(get_transform(vehicle.get_location(), angle - 90)) diff --git a/PythonAPI/test/smoke/test_sync.py b/PythonAPI/test/smoke/test_sync.py index 73b61d333..9f5eee257 100644 --- a/PythonAPI/test/smoke/test_sync.py +++ b/PythonAPI/test/smoke/test_sync.py @@ -51,7 +51,7 @@ class TestSynchronousMode(SmokeTest): for _ in range(0, 100): self.world.tick() - ts = self.world.wait_for_tick() + ts = self.world.wait_for_tick().timestamp if frame is not None: self.assertEqual(ts.frame_count, frame + 1)