CARLA
Loading...
Searching...
No Matches
Episode.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
10#include "carla/NonCopyable.h"
19
20#include <atomic>
21#include <thread>
22#include <vector>
23
24namespace carla {
25namespace client {
26namespace detail {
27
28 class Client;
29 class WalkerNavigation;
30
31 /// Holds the current episode, and the current episode state.
32 ///
33 /// The episode state changes in the background each time a world tick is
34 /// received. The episode may change with any background update if the
35 /// simulator has loaded a new episode.
36 class Episode
37 : public std::enable_shared_from_this<Episode>,
38 private NonCopyable {
39 public:
40
41 explicit Episode(Client &client, std::weak_ptr<Simulator> simulator);
42
43 ~Episode();
44
45 void Listen();
46
47 auto GetId() const {
48 return GetState()->GetEpisodeId();
49 }
50
51 /// Whether OnEpisodeChanged has finished for the episode GetId() returns.
53 return _handled_episode_id.load() == GetId();
54 }
55
56 std::shared_ptr<const EpisodeState> GetState() const {
57 return _state.load();
58 }
59
61 _actors.Insert(std::move(actor));
62 }
63
64 std::optional<rpc::Actor> GetActorById(ActorId id);
65
66 std::vector<rpc::Actor> GetActorsById(const std::vector<ActorId> &actor_ids);
67
68 std::vector<rpc::Actor> GetActors();
69
70 std::optional<WorldSnapshot> WaitForState(time_duration timeout) {
71 return _snapshot.WaitFor(timeout);
72 }
73
74 size_t RegisterOnTickEvent(std::function<void(WorldSnapshot)> callback) {
75 return _on_tick_callbacks.Push(std::move(callback));
76 }
77
78 void RemoveOnTickEvent(size_t id) {
79 _on_tick_callbacks.Remove(id);
80 }
81
82 size_t RegisterOnMapChangeEvent(std::function<void(WorldSnapshot)> callback) {
83 return _on_map_change_callbacks.Push(std::move(callback));
84 }
85
86 void RemoveOnMapChangeEvent(size_t id) {
87 _on_map_change_callbacks.Remove(id);
88 }
89
90 size_t RegisterLightUpdateChangeEvent(std::function<void(WorldSnapshot)> callback) {
91 return _on_light_update_callbacks.Push(std::move(callback));
92 }
93
96 }
97
98 void SetPedestriansCrossFactor(float percentage);
99
100 void SetPedestriansSeed(unsigned int seed);
101
102 void AddPendingException(std::string e) {
103 _pending_exceptions = true;
105 }
106
108
109 std::shared_ptr<WalkerNavigation> CreateNavigationIfMissing();
110
111 private:
112
113 Episode(Client &client, const rpc::EpisodeInfo &info, std::weak_ptr<Simulator> simulator);
114
115 void OnEpisodeStarted();
116
117 void OnEpisodeChanged(uint64_t episode_id);
118
120
122
124
126
128
130
132
134
136
138
140
142
143 std::weak_ptr<Simulator> _simulator;
144
145 std::atomic<uint64_t> _handled_episode_id;
146
147 std::thread _tm_reset_thread;
148 };
149
150} // namespace detail
151} // namespace client
152} // namespace carla
A very simple atomic shared ptr with release-acquire memory order.
Inherit (privately) to suppress copy/move construction and assignment.
This class is meant to be used similar to a shared future, but the value can be set any number of tim...
Keeps a list of actor descriptions to avoid requesting each time the descriptions to the server.
void Insert(rpc::Actor actor)
Inserts an actor into the list.
Provides communication with the rpc and streaming servers of a CARLA simulator.
Holds the current episode, and the current episode state.
Definition Episode.h:38
void RemoveOnTickEvent(size_t id)
Definition Episode.h:78
RecurrentSharedFuture< WorldSnapshot > _snapshot
Definition Episode.h:133
size_t RegisterLightUpdateChangeEvent(std::function< void(WorldSnapshot)> callback)
Definition Episode.h:90
AtomicSharedPtr< const EpisodeState > _state
Definition Episode.h:121
std::weak_ptr< Simulator > _simulator
Definition Episode.h:143
CallbackList< WorldSnapshot > _on_tick_callbacks
Definition Episode.h:127
void AddPendingException(std::string e)
Definition Episode.h:102
void RegisterActor(rpc::Actor actor)
Definition Episode.h:60
size_t RegisterOnTickEvent(std::function< void(WorldSnapshot)> callback)
Definition Episode.h:74
std::shared_ptr< WalkerNavigation > CreateNavigationIfMissing()
Definition Episode.cpp:190
std::atomic< uint64_t > _handled_episode_id
Definition Episode.h:145
bool IsEpisodeChangeHandled() const
Whether OnEpisodeChanged has finished for the episode GetId() returns.
Definition Episode.h:52
std::optional< WorldSnapshot > WaitForState(time_duration timeout)
Definition Episode.h:70
std::vector< rpc::Actor > GetActorsById(const std::vector< ActorId > &actor_ids)
Definition Episode.cpp:152
std::string _pending_exceptions_msg
Definition Episode.h:123
size_t RegisterOnMapChangeEvent(std::function< void(WorldSnapshot)> callback)
Definition Episode.h:82
std::shared_ptr< const EpisodeState > GetState() const
Definition Episode.h:56
std::optional< rpc::Actor > GetActorById(ActorId id)
Definition Episode.cpp:140
void OnEpisodeChanged(uint64_t episode_id)
Definition Episode.cpp:167
const streaming::Token _token
Definition Episode.h:137
AtomicSharedPtr< WalkerNavigation > _walker_navigation
Definition Episode.h:135
void RemoveLightUpdateChangeEvent(size_t id)
Definition Episode.h:94
void SetPedestriansSeed(unsigned int seed)
void RemoveOnMapChangeEvent(size_t id)
Definition Episode.h:86
std::vector< rpc::Actor > GetActors()
Definition Episode.cpp:156
void SetPedestriansCrossFactor(float percentage)
CallbackList< WorldSnapshot > _on_light_update_callbacks
Definition Episode.h:131
CallbackList< WorldSnapshot > _on_map_change_callbacks
Definition Episode.h:129
A token that uniquely identify a stream.
Definition Token.h:17
Positive time duration up to milliseconds resolution.
Definition Time.h:20
This file contains definitions of common data structures used in traffic manager.