CARLA
Episode.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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"
12 #include "carla/client/Timestamp.h"
18 #include "carla/rpc/EpisodeInfo.h"
19 
20 #include <vector>
21 
22 namespace carla {
23 namespace client {
24 namespace detail {
25 
26  class Client;
27  class WalkerNavigation;
28 
29  /// Holds the current episode, and the current episode state.
30  ///
31  /// The episode state changes in the background each time a world tick is
32  /// received. The episode may change with any background update if the
33  /// simulator has loaded a new episode.
34  class Episode
35  : public std::enable_shared_from_this<Episode>,
36  private NonCopyable {
37  public:
38 
39  explicit Episode(Client &client, std::weak_ptr<Simulator> simulator);
40 
41  ~Episode();
42 
43  void Listen();
44 
45  auto GetId() const {
46  return GetState()->GetEpisodeId();
47  }
48 
49  std::shared_ptr<const EpisodeState> GetState() const {
50  return _state.load();
51  }
52 
53  void RegisterActor(rpc::Actor actor) {
54  _actors.Insert(std::move(actor));
55  }
56 
57  boost::optional<rpc::Actor> GetActorById(ActorId id);
58 
59  std::vector<rpc::Actor> GetActorsById(const std::vector<ActorId> &actor_ids);
60 
61  std::vector<rpc::Actor> GetActors();
62 
63  boost::optional<WorldSnapshot> WaitForState(time_duration timeout) {
64  return _snapshot.WaitFor(timeout);
65  }
66 
67  size_t RegisterOnTickEvent(std::function<void(WorldSnapshot)> callback) {
68  return _on_tick_callbacks.Push(std::move(callback));
69  }
70 
71  void RemoveOnTickEvent(size_t id) {
72  _on_tick_callbacks.Remove(id);
73  }
74 
75  size_t RegisterOnMapChangeEvent(std::function<void(WorldSnapshot)> callback) {
76  return _on_map_change_callbacks.Push(std::move(callback));
77  }
78 
79  void RemoveOnMapChangeEvent(size_t id) {
80  _on_map_change_callbacks.Remove(id);
81  }
82 
83  size_t RegisterLightUpdateChangeEvent(std::function<void(WorldSnapshot)> callback) {
84  return _on_light_update_callbacks.Push(std::move(callback));
85  }
86 
87  void RemoveLightUpdateChangeEvent(size_t id) {
88  _on_light_update_callbacks.Remove(id);
89  }
90 
91  void SetPedestriansCrossFactor(float percentage);
92 
93  void SetPedestriansSeed(unsigned int seed);
94 
95  void AddPendingException(std::string e) {
96  _pending_exceptions = true;
98  }
99 
101 
102  std::shared_ptr<WalkerNavigation> CreateNavigationIfMissing();
103 
104  private:
105 
106  Episode(Client &client, const rpc::EpisodeInfo &info, std::weak_ptr<Simulator> simulator);
107 
108  void OnEpisodeStarted();
109 
110  void OnEpisodeChanged();
111 
113 
115 
117 
119 
121 
123 
125 
127 
129 
131 
132  bool _pending_exceptions = false;
133 
134  bool _should_update_map = true;
135 
136  std::weak_ptr<Simulator> _simulator;
137  };
138 
139 } // namespace detail
140 } // namespace client
141 } // namespace carla
AtomicSharedPtr< const EpisodeState > _state
Definition: Episode.h:114
Holds the current episode, and the current episode state.
Definition: Episode.h:34
CachedActorList _actors
Definition: Episode.h:118
CallbackList< WorldSnapshot > _on_map_change_callbacks
Definition: Episode.h:122
std::vector< rpc::Actor > GetActors()
Definition: Episode.cpp:129
std::string _pending_exceptions_msg
Definition: Episode.h:116
RecurrentSharedFuture< WorldSnapshot > _snapshot
Definition: Episode.h:126
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::shared_ptr< WalkerNavigation > CreateNavigationIfMissing()
Definition: Episode.cpp:152
void AddPendingException(std::string e)
Definition: Episode.h:95
Keeps a list of actor descriptions to avoid requesting each time the descriptions to the server...
void RegisterActor(rpc::Actor actor)
Definition: Episode.h:53
void SetPedestriansCrossFactor(float percentage)
size_t RegisterLightUpdateChangeEvent(std::function< void(WorldSnapshot)> callback)
Definition: Episode.h:83
void Insert(rpc::Actor actor)
Inserts an actor into the list.
void RemoveOnTickEvent(size_t id)
Definition: Episode.h:71
This class is meant to be used similar to a shared future, but the value can be set any number of tim...
const streaming::Token _token
Definition: Episode.h:130
boost::optional< rpc::Actor > GetActorById(ActorId id)
Definition: Episode.cpp:113
CallbackList< WorldSnapshot > _on_light_update_callbacks
Definition: Episode.h:124
carla::ActorId ActorId
A token that uniquely identify a stream.
Definition: Token.h:17
size_t RegisterOnTickEvent(std::function< void(WorldSnapshot)> callback)
Definition: Episode.h:67
std::vector< rpc::Actor > GetActorsById(const std::vector< ActorId > &actor_ids)
Definition: Episode.cpp:125
void RemoveOnMapChangeEvent(size_t id)
Definition: Episode.h:79
std::shared_ptr< const EpisodeState > GetState() const
Definition: Episode.h:49
Provides communication with the rpc and streaming servers of a CARLA simulator.
size_t RegisterOnMapChangeEvent(std::function< void(WorldSnapshot)> callback)
Definition: Episode.h:75
Episode(Client &client, std::weak_ptr< Simulator > simulator)
Definition: Episode.cpp:37
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
A very simple atomic shared ptr with release-acquire memory order.
boost::optional< WorldSnapshot > WaitForState(time_duration timeout)
Definition: Episode.h:63
CallbackList< WorldSnapshot > _on_tick_callbacks
Definition: Episode.h:120
void SetPedestriansSeed(unsigned int seed)
std::weak_ptr< Simulator > _simulator
Definition: Episode.h:136
Inherit (privately) to suppress copy/move construction and assignment.
void RemoveLightUpdateChangeEvent(size_t id)
Definition: Episode.h:87
AtomicSharedPtr< WalkerNavigation > _walker_navigation
Definition: Episode.h:128