CARLA
client/Client.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/client/World.h"
11 #include "carla/client/Map.h"
12 #include "carla/PythonUtil.h"
14 
15 namespace carla {
16 namespace client {
17 
18  using namespace carla::traffic_manager;
19 
20  class Client {
21  public:
22 
23  /// Construct a carla client.
24  ///
25  /// @param host IP address of the host machine running the simulator.
26  /// @param port TCP port to connect with the simulator.
27  /// @param worker_threads number of asynchronous threads to use, or 0 to use
28  /// all available hardware concurrency.
29  explicit Client(
30  const std::string &host,
31  uint16_t port,
32  size_t worker_threads = 0u);
33 
34  /// Set a timeout for networking operations. If set, any networking
35  /// operation taking longer than @a timeout throws rpc::timeout.
36  void SetTimeout(time_duration timeout) {
37  _simulator->SetNetworkingTimeout(timeout);
38  }
39 
41  return _simulator->GetNetworkingTimeout();
42  }
43 
44  /// Return the version string of this client API.
45  std::string GetClientVersion() const {
46  return _simulator->GetClientVersion();
47  }
48 
49  /// Return the version string of the simulator we are connected to.
50  std::string GetServerVersion() const {
51  return _simulator->GetServerVersion();
52  }
53 
54  std::vector<std::string> GetAvailableMaps() const {
55  return _simulator->GetAvailableMaps();
56  }
57 
58  bool SetFilesBaseFolder(const std::string &path) {
59  return _simulator->SetFilesBaseFolder(path);
60  }
61 
62  std::vector<std::string> GetRequiredFiles(const std::string &folder = "", const bool download = true) const {
63  return _simulator->GetRequiredFiles(folder, download);
64  }
65 
66  void RequestFile(const std::string &name) const {
67  _simulator->RequestFile(name);
68  }
69 
70  World ReloadWorld(bool reset_settings = true) const {
71  return World{_simulator->ReloadEpisode(reset_settings)};
72  }
73 
75  std::string map_name,
76  bool reset_settings = true,
77  rpc::MapLayer map_layers = rpc::MapLayer::All) const {
78  return World{_simulator->LoadEpisode(std::move(map_name), reset_settings, map_layers)};
79  }
80 
81  /// Return (and load) a new world (map) only when the requested map is different from the current one
82 
84  std::string map_name,
85  bool reset_settings = true,
86  rpc::MapLayer map_layers = rpc::MapLayer::All) const {
87  carla::client::World world = GetWorld();
88  carla::SharedPtr<carla::client::Map> current_map = world.GetMap();
89  std::string current_map_name = current_map->GetName();
90  std::string map_name_prefix = "Carla/Maps/";
91  std::string map_name_without_prefix = map_name;
92  std::string map_name_with_prefix = map_name_prefix + map_name;
93  if(!(map_name_without_prefix == current_map_name) && !(map_name_with_prefix == current_map_name)){
94  World World{_simulator->LoadEpisode(std::move(map_name), reset_settings, map_layers)};
95  }else{}
96  }
97 
99  std::string opendrive,
100  const rpc::OpendriveGenerationParameters & params,
101  bool reset_settings = true) const {
102  return World{_simulator->LoadOpenDriveEpisode(
103  std::move(opendrive), params, reset_settings)};
104  }
105 
106  /// Return an instance of the world currently active in the simulator.
107  World GetWorld() const {
108  return World{_simulator->GetCurrentEpisode()};
109  }
110 
111  /// Return an instance of the TrafficManager currently active in the simulator.
113  return TrafficManager(_simulator->GetCurrentEpisode(), port);
114  }
115 
116  /// Return an instance of the Episode currently active in the simulator.
118  return _simulator->GetCurrentEpisode();
119  }
120 
121  std::string StartRecorder(std::string name, bool additional_data = false) {
122  return _simulator->StartRecorder(name, additional_data);
123  }
124 
125  void StopRecorder(void) {
126  _simulator->StopRecorder();
127  }
128 
129  std::string ShowRecorderFileInfo(std::string name, bool show_all) {
130  return _simulator->ShowRecorderFileInfo(name, show_all);
131  }
132 
133  std::string ShowRecorderCollisions(std::string name, char type1, char type2) {
134  return _simulator->ShowRecorderCollisions(name, type1, type2);
135  }
136 
137  std::string ShowRecorderActorsBlocked(std::string name, double min_time, double min_distance) {
138  return _simulator->ShowRecorderActorsBlocked(name, min_time, min_distance);
139  }
140 
141  std::string ReplayFile(std::string name, double start, double duration,
142  uint32_t follow_id, bool replay_sensors) {
143  return _simulator->ReplayFile(name, start, duration, follow_id, replay_sensors);
144  }
145 
146  void StopReplayer(bool keep_actors) {
147  _simulator->StopReplayer(keep_actors);
148  }
149 
150  void SetReplayerTimeFactor(double time_factor) {
151  _simulator->SetReplayerTimeFactor(time_factor);
152  }
153 
154  void SetReplayerIgnoreHero(bool ignore_hero) {
155  _simulator->SetReplayerIgnoreHero(ignore_hero);
156  }
157 
158  void SetReplayerIgnoreSpectator(bool ignore_spectator) {
159  _simulator->SetReplayerIgnoreSpectator(ignore_spectator);
160  }
161 
163  std::vector<rpc::Command> commands,
164  bool do_tick_cue = false) const {
165  _simulator->ApplyBatch(std::move(commands), do_tick_cue);
166  }
167 
168  std::vector<rpc::CommandResponse> ApplyBatchSync(
169  std::vector<rpc::Command> commands,
170  bool do_tick_cue = false) const {
171  auto responses = _simulator->ApplyBatchSync(std::move(commands), false);
172  if (do_tick_cue)
173  _simulator->Tick(_simulator->GetNetworkingTimeout());
174 
175  return responses;
176  }
177 
178  private:
179 
180  std::shared_ptr<detail::Simulator> _simulator;
181  };
182 
184  const std::string &host,
185  uint16_t port,
186  size_t worker_threads)
187  : _simulator(
188  new detail::Simulator(host, port, worker_threads),
189  PythonUtil::ReleaseGILDeleter()) {}
190 
191 } // namespace client
192 } // namespace carla
World ReloadWorld(bool reset_settings=true) const
Definition: client/Client.h:70
bool SetFilesBaseFolder(const std::string &path)
Definition: client/Client.h:58
Seting for map generation from opendrive without additional geometry.
std::vector< std::string > GetRequiredFiles(const std::string &folder="", const bool download=true) const
Definition: client/Client.h:62
std::vector< rpc::CommandResponse > ApplyBatchSync(std::vector< rpc::Command > commands, bool do_tick_cue=false) const
void SetReplayerIgnoreHero(bool ignore_hero)
void SetReplayerTimeFactor(double time_factor)
static const unsigned short TM_DEFAULT_PORT
Definition: Constants.h:22
boost::shared_ptr< T > SharedPtr
Use this SharedPtr (boost::shared_ptr) to keep compatibility with boost::python, but it would be nice...
Definition: Memory.h:20
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
SharedPtr< Map > GetMap() const
Return the map that describes this world.
Definition: World.cpp:24
std::vector< std::string > GetAvailableMaps() const
Definition: client/Client.h:54
World GetWorld() const
Return an instance of the world currently active in the simulator.
std::string ShowRecorderCollisions(std::string name, char type1, char type2)
std::string GetClientVersion() const
Return the version string of this client API.
Definition: client/Client.h:45
std::string ReplayFile(std::string name, double start, double duration, uint32_t follow_id, bool replay_sensors)
std::string StartRecorder(std::string name, bool additional_data=false)
std::shared_ptr< detail::Simulator > _simulator
This class integrates all the various stages of the traffic manager appropriately using messengers...
Client(const std::string &host, uint16_t port, size_t worker_threads=0u)
Construct a carla client.
World GenerateOpenDriveWorld(std::string opendrive, const rpc::OpendriveGenerationParameters &params, bool reset_settings=true) const
Definition: client/Client.h:98
std::string ShowRecorderActorsBlocked(std::string name, double min_time, double min_distance)
World LoadWorld(std::string map_name, bool reset_settings=true, rpc::MapLayer map_layers=rpc::MapLayer::All) const
Definition: client/Client.h:74
carla::client::detail::EpisodeProxy GetCurrentEpisode() const
Return an instance of the Episode currently active in the simulator.
TrafficManager GetInstanceTM(uint16_t port=TM_DEFAULT_PORT) const
Return an instance of the TrafficManager currently active in the simulator.
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
std::string GetServerVersion() const
Return the version string of the simulator we are connected to.
Definition: client/Client.h:50
void SetReplayerIgnoreSpectator(bool ignore_spectator)
void LoadWorldIfDifferent(std::string map_name, bool reset_settings=true, rpc::MapLayer map_layers=rpc::MapLayer::All) const
Return (and load) a new world (map) only when the requested map is different from the current one...
Definition: client/Client.h:83
std::string ShowRecorderFileInfo(std::string name, bool show_all)
void RequestFile(const std::string &name) const
Definition: client/Client.h:66
void SetTimeout(time_duration timeout)
Set a timeout for networking operations.
Definition: client/Client.h:36
void StopReplayer(bool keep_actors)
void ApplyBatch(std::vector< rpc::Command > commands, bool do_tick_cue=false) const
time_duration GetTimeout()
Definition: client/Client.h:40