CARLA
CarlaEngine.cpp
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 #include "Carla.h"
9 
16 
17 #include "Runtime/Core/Public/Misc/App.h"
18 #include "PhysicsEngine/PhysicsSettings.h"
20 
21 #include <thread>
22 
23 // =============================================================================
24 // -- Static local methods -----------------------------------------------------
25 // =============================================================================
26 
27 // init static frame counter
28 uint64_t FCarlaEngine::FrameCounter = 0;
29 
31 {
32  return std::max(std::thread::hardware_concurrency(), 4u) - 2u;
33 }
34 
35 static TOptional<double> FCarlaEngine_GetFixedDeltaSeconds()
36 {
37  return FApp::IsBenchmarking() ? FApp::GetFixedDeltaTime() : TOptional<double>{};
38 }
39 
40 static void FCarlaEngine_SetFixedDeltaSeconds(TOptional<double> FixedDeltaSeconds)
41 {
42  FApp::SetBenchmarking(FixedDeltaSeconds.IsSet());
43  FApp::SetFixedDeltaTime(FixedDeltaSeconds.Get(0.0));
44 }
45 
46 // =============================================================================
47 // -- FCarlaEngine -------------------------------------------------------------
48 // =============================================================================
49 
51 {
52  if (bIsRunning)
53  {
54  FWorldDelegates::OnWorldTickStart.Remove(OnPreTickHandle);
55  FWorldDelegates::OnWorldPostActorTick.Remove(OnPostTickHandle);
57  }
58 }
59 
61 {
62  TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
63  if (!bIsRunning)
64  {
65  const auto StreamingPort = Settings.StreamingPort.Get(Settings.RPCPort + 1u);
66  auto BroadcastStream = Server.Start(Settings.RPCPort, StreamingPort);
68 
69  WorldObserver.SetStream(BroadcastStream);
70 
71  OnPreTickHandle = FWorldDelegates::OnWorldTickStart.AddRaw(
72  this,
74  OnPostTickHandle = FWorldDelegates::OnWorldPostActorTick.AddRaw(
75  this,
78  this,
80 
81  bIsRunning = true;
82  }
83 
84  bMapChanged = true;
85 
86 }
87 
89 {
90  TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
92  CurrentEpisode = &Episode;
93 
95 
97 
98  // make connection between Episode and Recorder
99  if (Recorder)
100  {
101  Recorder->SetEpisode(&Episode);
102  Episode.SetRecorder(Recorder);
104  }
105 
106  Server.NotifyBeginEpisode(Episode);
107 }
108 
110 {
112  CurrentEpisode = nullptr;
113 }
114 
115 void FCarlaEngine::OnPreTick(UWorld *, ELevelTick TickType, float DeltaSeconds)
116 {
117  TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
118  if (TickType == ELevelTick::LEVELTICK_All)
119  {
120  // update frame counter
122 
123  // process RPC commands
124  do
125  {
126  Server.RunSome(10u);
127  }
129 
130  if (CurrentEpisode != nullptr)
131  {
132  CurrentEpisode->TickTimers(DeltaSeconds);
133  }
134  }
135 }
136 
137 void FCarlaEngine::OnPostTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
138 {
139  TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
140  // tick the recorder/replayer system
141  if (GetCurrentEpisode())
142  {
143  auto* EpisodeRecorder = GetCurrentEpisode()->GetRecorder();
144  if (EpisodeRecorder)
145  {
146  EpisodeRecorder->Ticking(DeltaSeconds);
147  }
148  }
149 
150  if ((TickType == ELevelTick::LEVELTICK_All) && (CurrentEpisode != nullptr))
151  {
152  // Look for lightsubsystem
153  bool LightUpdatePending = false;
154  if (World)
155  {
156  UCarlaLightSubsystem* CarlaLightSubsystem = World->GetSubsystem<UCarlaLightSubsystem>();
157  if (CarlaLightSubsystem)
158  {
159  LightUpdatePending = CarlaLightSubsystem->IsUpdatePending();
160  }
161  }
162 
163  // send the worldsnapshot
164  WorldObserver.BroadcastTick(*CurrentEpisode, DeltaSeconds, bMapChanged, LightUpdatePending);
166  }
167 }
168 
170 {
171  CurrentSettings = FEpisodeSettings(Settings);
172 
174 
175  if (GEngine && GEngine->GameViewport)
176  {
177  GEngine->GameViewport->bDisableWorldRendering = Settings.bNoRenderingMode;
178  }
179 
181 
182  // Setting parameters for physics substepping
183  UPhysicsSettings* PhysSett = UPhysicsSettings::Get();
184  PhysSett->bSubstepping = Settings.bSubstepping;
185  PhysSett->MaxSubstepDeltaTime = Settings.MaxSubstepDeltaTime;
186  PhysSett->MaxSubsteps = Settings.MaxSubsteps;
187 
188  UWorld* World = CurrentEpisode->GetWorld();
189  ALargeMapManager* LargeMapManager = UCarlaStatics::GetLargeMapManager(World);
190  if (LargeMapManager)
191  {
192  LargeMapManager->SetLayerStreamingDistance(Settings.TileStreamingDistance);
193  LargeMapManager->SetActorStreamingDistance(Settings.ActorActiveDistance);
194  }
195 }
196 
198 {
199  bMapChanged = false;
200 }
void CheckPlayAfterMapLoaded(void)
UCarlaEpisode * GetCurrentEpisode()
Definition: CarlaEngine.h:39
bool bMapChanged
Definition: CarlaEngine.h:79
void OnEpisodeSettingsChanged(const FEpisodeSettings &Settings)
void BroadcastTick(const UCarlaEpisode &Episode, float DeltaSeconds, bool MapChange, bool PendingLightUpdate)
Send a message to every connected client with the info about the given Episode.
FDelegateHandle OnEpisodeSettingsChangeHandle
Definition: CarlaEngine.h:95
void SetEpisode(UCarlaEpisode *ThisEpisode)
FCarlaServer Server
Definition: CarlaEngine.h:81
static void FCarlaEngine_SetFixedDeltaSeconds(TOptional< double > FixedDeltaSeconds)
Definition: CarlaEngine.cpp:40
static uint32 FCarlaEngine_GetNumberOfThreadsForRPCServer()
Definition: CarlaEngine.cpp:30
static FOnEpisodeSettingsChange OnEpisodeSettingsChange
static uint64_t UpdateFrameCounter()
Definition: CarlaEngine.h:54
FDelegateHandle OnPostTickHandle
Definition: CarlaEngine.h:93
void SetStream(FDataMultiStream InStream)
Replace the Stream associated with this sensor.
Definition: WorldObserver.h:22
FDataMultiStream Start(uint16_t RPCPort, uint16_t StreamingPort)
FWorldObserver WorldObserver
Definition: CarlaEngine.h:83
CarlaReplayer * GetReplayer(void)
static T Get(carla::rpc::Response< T > &response)
void NotifyEndEpisode()
static void ResetFrameCounter(uint64_t Value=0)
Definition: CarlaEngine.h:60
void NotifyInitGame(const UCarlaSettings &Settings)
Definition: CarlaEngine.cpp:60
void OnPostTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
static uint64_t FrameCounter
Definition: CarlaEngine.h:24
bool bIsRunning
Definition: CarlaEngine.h:75
void RunSome(uint32 Milliseconds)
TOptional< uint32 > StreamingPort
Optional setting for the secondary port.
Definition: CarlaSettings.h:86
A simulation episode.
Definition: CarlaEpisode.h:34
void NotifyBeginEpisode(UCarlaEpisode &Episode)
Definition: CarlaEngine.cpp:88
ACarlaRecorder * Recorder
Definition: CarlaEngine.h:89
void NotifyEndEpisode()
FEpisodeSettings CurrentSettings
Definition: CarlaEngine.h:87
void SetActorStreamingDistance(float Distance)
Global settings for CARLA.
Definition: CarlaSettings.h:21
UCarlaEpisode * CurrentEpisode
Definition: CarlaEngine.h:85
void ResetSimulationState()
bool TickCueReceived()
ACarlaRecorder * GetRecorder() const
Definition: CarlaEpisode.h:271
void SetLayerStreamingDistance(float Distance)
void Ticking(float DeltaSeconds)
void SetRecorder(ACarlaRecorder *Rec)
Definition: CarlaEpisode.h:276
void OnPreTick(UWorld *World, ELevelTick TickType, float DeltaSeconds)
static TOptional< double > FCarlaEngine_GetFixedDeltaSeconds()
Definition: CarlaEngine.cpp:35
bool bSynchronousMode
Definition: CarlaEngine.h:77
static ALargeMapManager * GetLargeMapManager(const UObject *WorldContextObject)
Definition: CarlaStatics.h:100
uint32 RPCPort
World port to listen for client connections.
Definition: CarlaSettings.h:83
FEpisodeSettings EpisodeSettings
Definition: CarlaEpisode.h:327
void AsyncRun(uint32 NumberOfWorkerThreads)
FDelegateHandle OnPreTickHandle
Definition: CarlaEngine.h:91
void TickTimers(float DeltaSeconds)
Definition: CarlaEpisode.h:314
void ApplySettings(const FEpisodeSettings &Settings)
void NotifyBeginEpisode(UCarlaEpisode &Episode)