23using namespace std::chrono_literals;
27 return static_cast<target_t &
>(data);
30 template <
typename RangeT>
33 if (!missing_ids.empty()) {
52 }
catch (
const std::exception &e) {
53 log_error(
"exception trying to disconnect from episode:", e.what());
58 std::weak_ptr<Episode> weak = shared_from_this();
60 auto self = weak.lock();
61 if (self !=
nullptr) {
64 auto next = std::make_shared<const EpisodeState>(
CastData(*data));
65 auto prev = self->GetState();
68 bool HasMapChanged = next->HasMapChanged();
69 bool UpdateLights = next->IsLightUpdatePending();
72 if(self->_pending_exceptions) {
75 self->_pending_exceptions =
false;
78 auto exception(self->_pending_exceptions_msg);
80 self->_snapshot.SetException(std::runtime_error(exception));
84 bool episode_changed = (next->GetEpisodeId() != prev->GetEpisodeId());
86 if (episode_changed) {
95 self->_walker_navigation.reset();
99 self->_should_update_map =
true;
103 if (prev->GetFrame() >= next->GetFrame() && !episode_changed) {
104 self->_on_tick_callbacks.Call(next);
107 }
while (!self->_state.compare_exchange(&prev, next));
109 if(UpdateLights || HasMapChanged) {
112 self->_on_light_update_callbacks.Call(next);
113 }
catch (
const std::exception &e) {
114 log_error(
"failed to update the lights for the new snapshot:", e.what());
119 self->_should_update_map =
true;
123 if(episode_changed) {
124 self->OnEpisodeChanged(next->GetEpisodeId());
128 self->_snapshot.SetValue(next);
131 self->_on_tick_callbacks.Call(next);
139 if (!actor.has_value()) {
141 if (!actor_list.empty()) {
142 actor = std::move(actor_list.front());
179 std::shared_ptr<WalkerNavigation>
nav;
182 if (
nav ==
nullptr) {
186 auto new_nav = std::make_shared<WalkerNavigation>(
190 }
while (
nav ==
nullptr);
Keeps a list of actor descriptions to avoid requesting each time the descriptions to the server.
void InsertRange(RangeT range)
Inserts a range containing actors.
void Insert(rpc::Actor actor)
Inserts an actor into the list.
std::optional< rpc::Actor > GetActorById(ActorId id) const
Retrieve the actor matching id, or empty optional if actor is not cached.
std::vector< rpc::Actor > GetActorsById(const RangeT &range) const
Retrieve the actors matching the ids in range.
std::vector< ActorId > GetMissingIds(const RangeT &range) const
Return the actor ids present in range that haven't been added to this list.
Provides communication with the rpc and streaming servers of a CARLA simulator.
std::vector< rpc::Actor > GetActorsById(const std::vector< ActorId > &ids)
bool IsNavigationServerSide() const
True when the server hosts the pedestrian navigation (a World-Partitioned navmesh exists in the curre...
void UnSubscribeFromStream(const streaming::Token &token)
void SubscribeToStream(const streaming::Token &token, std::function< void(Buffer)> callback)
Represents the state of all the actors of an episode at a given frame.
AtomicSharedPtr< const EpisodeState > _state
std::weak_ptr< Simulator > _simulator
CallbackList< WorldSnapshot > _on_tick_callbacks
std::shared_ptr< WalkerNavigation > CreateNavigationIfMissing()
std::vector< rpc::Actor > GetActorsById(const std::vector< ActorId > &actor_ids)
std::shared_ptr< std::atomic< uint64_t > > _handled_episode_id
Shared with the TM reset thread, which may outlive this Episode.
std::shared_ptr< const EpisodeState > GetState() const
std::optional< rpc::Actor > GetActorById(ActorId id)
void OnEpisodeChanged(uint64_t episode_id)
const streaming::Token _token
AtomicSharedPtr< WalkerNavigation > _walker_navigation
bool HasMapChangedSinceLastCall()
std::vector< rpc::Actor > GetActors()
Episode(Client &client, std::weak_ptr< Simulator > simulator)
static SharedPtr< SensorData > Deserialize(Buffer &&buffer)
Base class for all the objects containing data generated by a sensor.
State of the episode at a given frame.
static void RequestReset(uint64_t episode_id, std::function< void()> on_done)
Non-blocking: reset all TMs once per episode on the reset thread, then call on_done.
static auto GetActorsById_Impl(Client &client, CachedActorList &actors, const RangeT &actor_ids)
static auto & CastData(const sensor::SensorData &data)
This file contains definitions of common data structures used in traffic manager.