CARLA
client/detail/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 
9 #include "carla/Memory.h"
10 #include "carla/NonCopyable.h"
11 #include "carla/Time.h"
12 #include "carla/geom/Transform.h"
13 #include "carla/geom/Location.h"
14 #include "carla/rpc/Actor.h"
17 #include "carla/rpc/Command.h"
20 #include "carla/rpc/EpisodeInfo.h"
23 #include "carla/rpc/LightState.h"
24 #include "carla/rpc/MapInfo.h"
25 #include "carla/rpc/MapLayer.h"
28 #include "carla/rpc/VehicleDoor.h"
34 #include "carla/rpc/Texture.h"
36 
37 #include <functional>
38 #include <memory>
39 #include <string>
40 #include <vector>
41 
42 // Forward declarations.
43 namespace carla {
44  class Buffer;
45 namespace rpc {
46  class AckermannControllerSettings;
47  class ActorDescription;
48  class DebugShape;
49  class VehicleAckermannControl;
50  class VehicleControl;
51  class WalkerControl;
52  class WalkerBoneControlIn;
53  class WalkerBoneControlOut;
54 }
55 namespace sensor {
56  class SensorData;
57 }
58 namespace streaming {
59  class Token;
60 }
61 }
62 
63 namespace carla {
64 namespace client {
65 namespace detail {
66 
67  /// Provides communication with the rpc and streaming servers of a CARLA
68  /// simulator.
69  class Client : private NonCopyable {
70  public:
71 
72  explicit Client(
73  const std::string &host,
74  uint16_t port,
75  size_t worker_threads = 0u);
76 
77  ~Client();
78 
79  /// Querry to know if a Traffic Manager is running on port
80  bool IsTrafficManagerRunning(uint16_t port) const;
81 
82  /// Gets a pair filled with the <IP, port> of the Trafic Manager running on port.
83  /// If there is no Traffic Manager running the pair will be ("", 0)
84  std::pair<std::string, uint16_t> GetTrafficManagerRunning(uint16_t port) const;
85 
86  /// Informs the server that a Traffic Manager is running on <IP, port>
87  bool AddTrafficManagerRunning(std::pair<std::string, uint16_t> trafficManagerInfo) const;
88 
89  void DestroyTrafficManager(uint16_t port) const;
90 
91  void SetTimeout(time_duration timeout);
92 
93  time_duration GetTimeout() const;
94 
95  const std::string GetEndpoint() const;
96 
97  std::string GetClientVersion();
98 
99  std::string GetServerVersion();
100 
101  void LoadEpisode(std::string map_name, bool reset_settings = true, rpc::MapLayer map_layer = rpc::MapLayer::All);
102 
103  void LoadLevelLayer(rpc::MapLayer map_layer) const;
104 
105  void UnloadLevelLayer(rpc::MapLayer map_layer) const;
106 
107  void CopyOpenDriveToServer(
108  std::string opendrive, const rpc::OpendriveGenerationParameters & params);
109 
110  void ApplyColorTextureToObjects(
111  const std::vector<std::string> &objects_name,
112  const rpc::MaterialParameter& parameter,
113  const rpc::TextureColor& Texture);
114 
115  void ApplyColorTextureToObjects(
116  const std::vector<std::string> &objects_name,
117  const rpc::MaterialParameter& parameter,
118  const rpc::TextureFloatColor& Texture);
119 
120  std::vector<std::string> GetNamesOfAllObjects() const;
121 
122  rpc::EpisodeInfo GetEpisodeInfo();
123 
124  rpc::MapInfo GetMapInfo();
125 
126  std::vector<uint8_t> GetNavigationMesh() const;
127 
128  bool SetFilesBaseFolder(const std::string &path);
129 
130  std::vector<std::string> GetRequiredFiles(const std::string &folder = "", const bool download = true) const;
131 
132  std::string GetMapData() const;
133 
134  void RequestFile(const std::string &name) const;
135 
136  std::vector<uint8_t> GetCacheFile(const std::string &name, const bool request_otherwise = true) const;
137 
138  std::vector<std::string> GetAvailableMaps();
139 
140  std::vector<rpc::ActorDefinition> GetActorDefinitions();
141 
142  rpc::Actor GetSpectator();
143 
144  rpc::EpisodeSettings GetEpisodeSettings();
145 
146  uint64_t SetEpisodeSettings(const rpc::EpisodeSettings &settings);
147 
148  rpc::WeatherParameters GetWeatherParameters();
149 
150  void SetWeatherParameters(const rpc::WeatherParameters &weather);
151 
152  std::vector<rpc::Actor> GetActorsById(const std::vector<ActorId> &ids);
153 
154  rpc::VehiclePhysicsControl GetVehiclePhysicsControl(rpc::ActorId vehicle) const;
155 
156  rpc::VehicleLightState GetVehicleLightState(rpc::ActorId vehicle) const;
157 
158  void ApplyPhysicsControlToVehicle(
159  rpc::ActorId vehicle,
160  const rpc::VehiclePhysicsControl &physics_control);
161 
162  void SetLightStateToVehicle(
163  rpc::ActorId vehicle,
164  const rpc::VehicleLightState &light_state);
165 
166  void OpenVehicleDoor(
167  rpc::ActorId vehicle,
168  const rpc::VehicleDoor door_idx);
169 
170  void CloseVehicleDoor(
171  rpc::ActorId vehicle,
172  const rpc::VehicleDoor door_idx);
173 
174  rpc::Actor SpawnActor(
175  const rpc::ActorDescription &description,
176  const geom::Transform &transform);
177 
178  rpc::Actor SpawnActorWithParent(
179  const rpc::ActorDescription &description,
180  const geom::Transform &transform,
181  rpc::ActorId parent,
182  rpc::AttachmentType attachment_type);
183 
184  bool DestroyActor(rpc::ActorId actor);
185 
186  void SetActorLocation(
187  rpc::ActorId actor,
188  const geom::Location &location);
189 
190  void SetActorTransform(
191  rpc::ActorId actor,
192  const geom::Transform &transform);
193 
194  void SetActorTargetVelocity(
195  rpc::ActorId actor,
196  const geom::Vector3D &vector);
197 
198  void SetActorTargetAngularVelocity(
199  rpc::ActorId actor,
200  const geom::Vector3D &vector);
201 
202  void EnableActorConstantVelocity(
203  rpc::ActorId actor,
204  const geom::Vector3D &vector);
205 
206  void DisableActorConstantVelocity(
207  rpc::ActorId actor);
208 
209  void AddActorImpulse(
210  rpc::ActorId actor,
211  const geom::Vector3D &impulse);
212 
213  void AddActorImpulse(
214  rpc::ActorId actor,
215  const geom::Vector3D &impulse,
216  const geom::Vector3D &location);
217 
218  void AddActorForce(
219  rpc::ActorId actor,
220  const geom::Vector3D &force);
221 
222  void AddActorForce(
223  rpc::ActorId actor,
224  const geom::Vector3D &force,
225  const geom::Vector3D &location);
226 
227  void AddActorAngularImpulse(
228  rpc::ActorId actor,
229  const geom::Vector3D &vector);
230 
231  void AddActorTorque(
232  rpc::ActorId actor,
233  const geom::Vector3D &vector);
234 
235  void SetActorSimulatePhysics(
236  rpc::ActorId actor,
237  bool enabled);
238 
239  void SetActorCollisions(
240  rpc::ActorId actor,
241  bool enabled);
242 
243  void SetActorDead(
244  rpc::ActorId actor);
245 
246  void SetActorEnableGravity(
247  rpc::ActorId actor,
248  bool enabled);
249 
250  void SetActorAutopilot(
251  rpc::ActorId vehicle,
252  bool enabled);
253 
254  void ShowVehicleDebugTelemetry(
255  rpc::ActorId vehicle,
256  bool enabled);
257 
258  void ApplyControlToVehicle(
259  rpc::ActorId vehicle,
260  const rpc::VehicleControl &control);
261 
262  void ApplyAckermannControlToVehicle(
263  rpc::ActorId vehicle,
264  const rpc::VehicleAckermannControl &control);
265 
266  rpc::AckermannControllerSettings GetAckermannControllerSettings(rpc::ActorId vehicle) const;
267 
268  void ApplyAckermannControllerSettings(
269  rpc::ActorId vehicle,
270  const rpc::AckermannControllerSettings &settings);
271 
272  void EnableCarSim(
273  rpc::ActorId vehicle,
274  std::string simfile_path);
275 
276  void UseCarSimRoad(
277  rpc::ActorId vehicle,
278  bool enabled);
279 
280  void SetWheelSteerDirection(
281  rpc::ActorId vehicle,
282  rpc::VehicleWheelLocation vehicle_wheel,
283  float angle_in_deg
284  );
285 
286  float GetWheelSteerAngle(
287  rpc::ActorId vehicle,
288  rpc::VehicleWheelLocation wheel_location
289  );
290 
291  void EnableChronoPhysics(
292  rpc::ActorId vehicle,
293  uint64_t MaxSubsteps,
294  float MaxSubstepDeltaTime,
295  std::string VehicleJSON,
296  std::string PowertrainJSON,
297  std::string TireJSON,
298  std::string BaseJSONPath);
299 
300  void ApplyControlToWalker(
301  rpc::ActorId walker,
302  const rpc::WalkerControl &control);
303 
304  rpc::WalkerBoneControlOut GetBonesTransform(
305  rpc::ActorId walker);
306 
307  void SetBonesTransform(
308  rpc::ActorId walker,
309  const rpc::WalkerBoneControlIn &bones);
310 
311  void BlendPose(
312  rpc::ActorId walker,
313  float blend);
314 
315  void GetPoseFromAnimation(
316  rpc::ActorId walker);
317 
318  void SetTrafficLightState(
319  rpc::ActorId traffic_light,
320  const rpc::TrafficLightState trafficLightState);
321 
322  void SetTrafficLightGreenTime(
323  rpc::ActorId traffic_light,
324  float green_time);
325 
326  void SetTrafficLightYellowTime(
327  rpc::ActorId traffic_light,
328  float yellow_time);
329 
330  void SetTrafficLightRedTime(
331  rpc::ActorId traffic_light,
332  float red_time);
333 
334  void FreezeTrafficLight(
335  rpc::ActorId traffic_light,
336  bool freeze);
337 
338  void ResetTrafficLightGroup(
339  rpc::ActorId traffic_light);
340 
341  void ResetAllTrafficLights();
342 
343  void FreezeAllTrafficLights(bool frozen);
344 
345  std::vector<geom::BoundingBox> GetLightBoxes(
346  rpc::ActorId traffic_light) const;
347 
348  /// Returns a list of pairs where the firts element is the vehicle ID
349  /// and the second one is the light state
350  rpc::VehicleLightStateList GetVehiclesLightStates();
351 
352  std::vector<ActorId> GetGroupTrafficLights(
353  rpc::ActorId traffic_light);
354 
355  std::string StartRecorder(std::string name, bool additional_data);
356 
357  void StopRecorder();
358 
359  std::string ShowRecorderFileInfo(std::string name, bool show_all);
360 
361  std::string ShowRecorderCollisions(std::string name, char type1, char type2);
362 
363  std::string ShowRecorderActorsBlocked(std::string name, double min_time, double min_distance);
364 
365  std::string ReplayFile(std::string name, double start, double duration,
366  uint32_t follow_id, bool replay_sensors);
367 
368  void SetReplayerTimeFactor(double time_factor);
369 
370  void SetReplayerIgnoreHero(bool ignore_hero);
371 
372  void SetReplayerIgnoreSpectator(bool ignore_spectator);
373 
374  void StopReplayer(bool keep_actors);
375 
376  void SubscribeToStream(
377  const streaming::Token &token,
378  std::function<void(Buffer)> callback);
379 
380  void SubscribeToGBuffer(
382  uint32_t GBufferId,
383  std::function<void(Buffer)> callback);
384 
385  void UnSubscribeFromStream(const streaming::Token &token);
386 
387  void EnableForROS(const streaming::Token &token);
388 
389  void DisableForROS(const streaming::Token &token);
390 
391  bool IsEnabledForROS(const streaming::Token &token);
392 
393  void UnSubscribeFromGBuffer(
394  rpc::ActorId ActorId,
395  uint32_t GBufferId);
396 
397  void DrawDebugShape(const rpc::DebugShape &shape);
398 
399  void ApplyBatch(
400  std::vector<rpc::Command> commands,
401  bool do_tick_cue);
402 
403  std::vector<rpc::CommandResponse> ApplyBatchSync(
404  std::vector<rpc::Command> commands,
405  bool do_tick_cue);
406 
407  uint64_t SendTickCue();
408 
409  std::vector<rpc::LightState> QueryLightsStateToServer() const;
410 
411  void UpdateServerLightsState(
412  std::vector<rpc::LightState>& lights,
413  bool discard_client = false) const;
414 
415  void UpdateDayNightCycle(const bool active) const;
416 
417  /// Returns all the BBs of all the elements of the level
418  std::vector<geom::BoundingBox> GetLevelBBs(uint8_t queried_tag) const;
419 
420  std::vector<rpc::EnvironmentObject> GetEnvironmentObjects(uint8_t queried_tag) const;
421 
422  void EnableEnvironmentObjects(
423  std::vector<uint64_t> env_objects_ids,
424  bool enable) const;
425 
426  std::pair<bool,rpc::LabelledPoint> ProjectPoint(
427  geom::Location location, geom::Vector3D direction, float search_distance) const;
428 
429  std::vector<rpc::LabelledPoint> CastRay(
430  geom::Location start_location, geom::Location end_location) const;
431 
432  private:
433 
434  class Pimpl;
435  const std::unique_ptr<Pimpl> _pimpl;
436  };
437 
438 } // namespace detail
439 } // namespace client
440 } // namespace carla
Seting for map generation from opendrive without additional geometry.
std::vector< std::pair< ActorId, VehicleLightState::flag_type > > VehicleLightStateList
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::deque< std::shared_ptr< SimpleWaypoint > > Buffer
const std::unique_ptr< Pimpl > _pimpl
Base class for all the objects containing data generated by a sensor.
Definition: SensorData.h:20
carla::ActorId ActorId
A token that uniquely identify a stream.
Definition: Token.h:17
uint32_t ActorId
Definition: ActorId.h:14
Provides communication with the rpc and streaming servers of a CARLA simulator.
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
Inherit (privately) to suppress copy/move construction and assignment.
Defines the physical appearance of a vehicle whitch is obtained by the sensors.