23using namespace std::chrono_literals;
27 return static_cast<target_t &
>(data);
30 template <
typename RangeT>
33 if (!missing_ids.empty()) {
40 :
Episode(client, client.GetEpisodeInfo(), simulator) {}
46 _simulator(simulator),
47 _handled_episode_id(info.id) {}
55 }
catch (
const std::exception &e) {
56 log_error(
"exception trying to disconnect from episode:", e.what());
61 std::weak_ptr<Episode> weak = shared_from_this();
63 auto self = weak.lock();
64 if (self !=
nullptr) {
67 auto next = std::make_shared<const EpisodeState>(
CastData(*data));
68 auto prev = self->GetState();
71 bool HasMapChanged = next->HasMapChanged();
72 bool UpdateLights = next->IsLightUpdatePending();
75 if(self->_pending_exceptions) {
78 self->_pending_exceptions =
false;
81 auto exception(self->_pending_exceptions_msg);
83 self->_snapshot.SetException(std::runtime_error(exception));
87 bool episode_changed = (next->GetEpisodeId() != prev->GetEpisodeId());
89 if (episode_changed) {
98 self->_walker_navigation.reset();
102 self->_should_update_map =
true;
106 if (prev->GetFrame() >= next->GetFrame() && !episode_changed) {
107 self->_on_tick_callbacks.Call(next);
110 }
while (!self->_state.compare_exchange(&prev, next));
112 if(UpdateLights || HasMapChanged) {
115 self->_on_light_update_callbacks.Call(next);
116 }
catch (
const std::exception &e) {
117 log_error(
"failed to update the lights for the new snapshot:", e.what());
122 self->_should_update_map =
true;
126 if(episode_changed) {
127 self->OnEpisodeChanged(next->GetEpisodeId());
131 self->_snapshot.SetValue(next);
134 self->_on_tick_callbacks.Call(next);
142 if (!actor.has_value()) {
144 if (!actor_list.empty()) {
145 actor = std::move(actor_list.front());
175 }
catch (
const std::exception &e) {
176 log_error(
"failed to reset the Traffic Manager for the new episode:", e.what());
191 std::shared_ptr<WalkerNavigation> nav;
194 if (nav ==
nullptr) {
198 auto new_nav = std::make_shared<WalkerNavigation>(
202 }
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.
Holds the current episode, and the current episode state.
std::weak_ptr< Simulator > _simulator
CallbackList< WorldSnapshot > _on_tick_callbacks
std::shared_ptr< WalkerNavigation > CreateNavigationIfMissing()
std::atomic< uint64_t > _handled_episode_id
std::vector< rpc::Actor > GetActorsById(const std::vector< ActorId > &actor_ids)
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
std::thread _tm_reset_thread
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 Reset(uint64_t episode_id)
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.
static void log_error(Args &&... args)