21using namespace std::chrono_literals;
25 return static_cast<target_t &
>(data);
28 template <
typename RangeT>
31 if (!missing_ids.empty()) {
32 actors.
InsertRange(client.GetActorsById(missing_ids));
38 :
Episode(client, client.GetEpisodeInfo(), simulator) {}
43 _simulator(simulator),
49 }
catch (
const std::exception &e) {
50 log_error(
"exception trying to disconnect from episode:", e.what());
55 std::weak_ptr<Episode> weak = shared_from_this();
57 auto self = weak.lock();
58 if (self !=
nullptr) {
61 auto next = std::make_shared<const EpisodeState>(
CastData(*data));
62 auto prev = self->GetState();
65 bool HasMapChanged = next->HasMapChanged();
66 bool UpdateLights = next->IsLightUpdatePending();
69 if(self->_pending_exceptions) {
72 self->_pending_exceptions =
false;
75 auto exception(self->_pending_exceptions_msg);
77 self->_snapshot.SetException(std::runtime_error(exception));
81 bool episode_changed = (next->GetEpisodeId() != prev->GetEpisodeId());
84 if (prev->GetFrame() >= next->GetFrame() && !episode_changed) {
85 self->_on_tick_callbacks.Call(next);
88 }
while (!self->_state.compare_exchange(&prev, next));
90 if(UpdateLights || HasMapChanged) {
91 self->_on_light_update_callbacks.Call(next);
95 self->_should_update_map =
true;
100 self->OnEpisodeChanged();
104 self->_snapshot.SetValue(next);
107 self->_on_tick_callbacks.Call(next);
115 if (!actor.has_value()) {
117 if (!actor_list.empty()) {
118 actor = std::move(actor_list.front());
153 std::shared_ptr<WalkerNavigation> nav;
156 if (nav ==
nullptr) {
157 auto new_nav = std::make_shared<WalkerNavigation>(
_simulator);
160 }
while (nav ==
nullptr);
Keeps a list of actor descriptions to avoid requesting each time the descriptions to the server.
boost::optional< rpc::Actor > GetActorById(ActorId id) const
Retrieve the actor matching id, or empty optional if actor is not cached.
void InsertRange(RangeT range)
Inserts a range containing actors.
void Insert(rpc::Actor actor)
Inserts an actor into the list.
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)
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::vector< rpc::Actor > GetActorsById(const std::vector< ActorId > &actor_ids)
std::shared_ptr< const EpisodeState > GetState() const
const streaming::Token _token
AtomicSharedPtr< WalkerNavigation > _walker_navigation
bool HasMapChangedSinceLastCall()
std::vector< rpc::Actor > GetActors()
Episode(Client &client, std::weak_ptr< Simulator > simulator)
boost::optional< rpc::Actor > GetActorById(ActorId id)
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 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)