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 
28  /// Holds the current episode, and the current episode state.
29  ///
30  /// The episode state changes in the background each time a world tick is
31  /// received. The episode may change with any background update if the
32  /// simulator has loaded a new episode.
33  class Episode
34  : public std::enable_shared_from_this<Episode>,
35  private NonCopyable {
36  public:
37 
38  explicit Episode(Client &client);
39 
40  ~Episode();
41 
42  void Listen();
43 
44  auto GetId() const {
45  return GetState()->GetEpisodeId();
46  }
47 
48  std::shared_ptr<const EpisodeState> GetState() const {
49  return _state.load();
50  }
51 
52  std::shared_ptr<WalkerNavigation> CreateNavigationIfMissing();
53 
54  std::shared_ptr<WalkerNavigation> GetNavigation() const {
55  auto nav = _navigation.load();
56  DEBUG_ASSERT(nav != nullptr);
57  return nav;
58  }
59 
60  void RegisterActor(rpc::Actor actor) {
61  _actors.Insert(std::move(actor));
62  }
63 
64  boost::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  boost::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 
94  void RemoveLightUpdateChangeEvent(size_t id) {
95  _on_light_update_callbacks.Remove(id);
96  }
97 
98  void SetPedestriansCrossFactor(float percentage) {
99  auto nav = _navigation.load();
100  DEBUG_ASSERT(nav != nullptr);
101  nav->SetPedestriansCrossFactor(percentage);
102  }
103 
104  void AddPendingException(std::string e) {
105  _pending_exceptions = true;
107  }
108 
109  private:
110 
111  Episode(Client &client, const rpc::EpisodeInfo &info);
112 
113  void OnEpisodeStarted();
114 
115  void OnEpisodeChanged();
116 
118 
120 
122 
124 
126 
128 
130 
132 
134 
136 
137  bool _pending_exceptions = false;
138  };
139 
140 } // namespace detail
141 } // namespace client
142 } // namespace carla
AtomicSharedPtr< const EpisodeState > _state
Definition: Episode.h:119
Holds the current episode, and the current episode state.
Definition: Episode.h:33
CachedActorList _actors
Definition: Episode.h:125
CallbackList< WorldSnapshot > _on_map_change_callbacks
Definition: Episode.h:129
std::vector< rpc::Actor > GetActors()
Definition: Episode.cpp:142
std::string _pending_exceptions_msg
Definition: Episode.h:123
RecurrentSharedFuture< WorldSnapshot > _snapshot
Definition: Episode.h:133
AtomicSharedPtr< WalkerNavigation > _navigation
Definition: Episode.h:121
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
std::shared_ptr< WalkerNavigation > CreateNavigationIfMissing()
Definition: Episode.cpp:126
void AddPendingException(std::string e)
Definition: Episode.h:104
Keeps a list of actor descriptions to avoid requesting each time the descriptions to the server...
void RegisterActor(rpc::Actor actor)
Definition: Episode.h:60
void SetPedestriansCrossFactor(float percentage)
Definition: Episode.h:98
size_t RegisterLightUpdateChangeEvent(std::function< void(WorldSnapshot)> callback)
Definition: Episode.h:90
void Insert(rpc::Actor actor)
Inserts an actor into the list.
void RemoveOnTickEvent(size_t id)
Definition: Episode.h:78
#define DEBUG_ASSERT(predicate)
Definition: Debug.h:66
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:135
boost::optional< rpc::Actor > GetActorById(ActorId id)
Definition: Episode.cpp:114
CallbackList< WorldSnapshot > _on_light_update_callbacks
Definition: Episode.h:131
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:74
std::vector< rpc::Actor > GetActorsById(const std::vector< ActorId > &actor_ids)
Definition: Episode.cpp:138
void RemoveOnMapChangeEvent(size_t id)
Definition: Episode.h:86
std::shared_ptr< const EpisodeState > GetState() const
Definition: Episode.h:48
Provides communication with the rpc and streaming servers of a CARLA simulator.
size_t RegisterOnMapChangeEvent(std::function< void(WorldSnapshot)> callback)
Definition: Episode.h:82
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:70
CallbackList< WorldSnapshot > _on_tick_callbacks
Definition: Episode.h:127
std::shared_ptr< WalkerNavigation > GetNavigation() const
Definition: Episode.h:54
Inherit (privately) to suppress copy/move construction and assignment.
void RemoveLightUpdateChangeEvent(size_t id)
Definition: Episode.h:94