CARLA
CarlaRecorder.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"
11 
15 
16 #include "CarlaRecorder.h"
17 #include "CarlaReplayerHelper.h"
18 
19 #include <ctime>
20 #include <sstream>
21 
23 {
24  PrimaryActorTick.TickGroup = TG_PrePhysics;
25  Disable();
26 }
27 
28 ACarlaRecorder::ACarlaRecorder(const FObjectInitializer &ObjectInitializer)
29  : Super(ObjectInitializer)
30 {
31  PrimaryActorTick.TickGroup = TG_PrePhysics;
32  Disable();
33 }
34 
35 std::string ACarlaRecorder::ShowFileInfo(std::string Name, bool bShowAll)
36 {
37  return Query.QueryInfo(Name, bShowAll);
38 }
39 
40 std::string ACarlaRecorder::ShowFileCollisions(std::string Name, char Type1, char Type2)
41 {
42  return Query.QueryCollisions(Name, Type1, Type2);
43 }
44 
45 std::string ACarlaRecorder::ShowFileActorsBlocked(std::string Name, double MinTime, double MinDistance)
46 {
47  return Query.QueryBlocked(Name, MinTime, MinDistance);
48 }
49 
50 std::string ACarlaRecorder::ReplayFile(std::string Name, double TimeStart, double Duration,
51  uint32_t FollowId, bool ReplaySensors)
52 {
53  Stop();
54  return Replayer.ReplayFile(Name, TimeStart, Duration, FollowId, ReplaySensors);
55 }
56 
58 {
59  Replayer.SetTimeFactor(TimeFactor);
60 }
61 
63 {
64  Replayer.SetIgnoreHero(IgnoreHero);
65 }
66 
67 void ACarlaRecorder::StopReplayer(bool KeepActors)
68 {
69  Replayer.Stop(KeepActors);
70 }
71 
72 void ACarlaRecorder::Ticking(float DeltaSeconds)
73 {
74  TRACE_CPUPROFILER_EVENT_SCOPE(ACarlaRecorder::Ticking);
75  Super::Tick(DeltaSeconds);
76 
77  if (!Episode)
78  return;
79 
80  // check if recording
81  if (Enabled)
82  {
84  const FActorRegistry &Registry = Episode->GetActorRegistry();
85 
86  // through all actors in registry
87  for (auto It = Registry.begin(); It != Registry.end(); ++It)
88  {
89  FCarlaActor* View = It.Value().Get();
90 
91  switch (View->GetActorType())
92  {
93  // save the transform for props
95  AddActorPosition(View);
96  break;
97 
98  // save the transform of all vehicles
100  AddActorPosition(View);
101  AddVehicleAnimation(View);
102  AddVehicleLight(View);
103  if (bAdditionalData)
104  {
105  AddActorKinematics(View);
106  }
107  break;
108 
109  // save the transform of all walkers
111  AddActorPosition(View);
112  AddWalkerAnimation(View);
113  if (bAdditionalData)
114  {
115  AddActorKinematics(View);
116  }
117  break;
118 
119  // save the state of each traffic light
121  AddTrafficLightState(View);
122  break;
123  }
124  }
125 
126  // write all data for this frame
127  Write(DeltaSeconds);
128  }
129  else if (Episode->GetReplayer()->IsEnabled())
130  {
131  // replayer
132  Episode->GetReplayer()->Tick(DeltaSeconds);
133  }
134 }
135 
137 {
138  PrimaryActorTick.bCanEverTick = true;
139  Enabled = true;
140 }
141 
143 {
144  PrimaryActorTick.bCanEverTick = false;
145  Enabled = false;
146 }
147 
149 {
150  check(CarlaActor != nullptr);
151 
152  FTransform Transform = CarlaActor->GetActorGlobalTransform();
153  // get position of the vehicle
155  {
156  CarlaActor->GetActorId(),
157  Transform.GetLocation(),
158  Transform.GetRotation().Euler()
159  });
160 }
161 
163 {
164  check(CarlaActor != nullptr);
165 
166  if (CarlaActor->IsPendingKill())
167  {
168  return;
169  }
170 
171  FVehicleControl Control;
172  CarlaActor->GetVehicleControl(Control);
173 
174  // save
176  Record.DatabaseId = CarlaActor->GetActorId();
177  Record.Steering = Control.Steer;
178  Record.Throttle = Control.Throttle;
179  Record.Brake = Control.Brake;
180  Record.bHandbrake = Control.bHandBrake;
181  Record.Gear = Control.Gear;
182  AddAnimVehicle(Record);
183 }
184 
186 {
187  check(CarlaActor != nullptr);
188 
189  if (!CarlaActor->IsPendingKill())
190  {
191  FWalkerControl Control;
192  CarlaActor->GetWalkerControl(Control);
194  {
195  CarlaActor->GetActorId(),
196  Control.Speed
197  });
198  }
199 }
200 
202 {
203  check(CarlaActor != nullptr);
204 
205  ETrafficLightState LightState = CarlaActor->GetTrafficLightState();
206  UTrafficLightController* Controller = CarlaActor->GetTrafficLightController();
207  if (Controller)
208  {
209  ATrafficLightGroup* Group = Controller->GetGroup();
210  if (Group)
211  {
213  {
214  CarlaActor->GetActorId(),
215  Group->IsFrozen(),
216  Controller->GetElapsedTime(),
217  static_cast<char>(LightState)
218  });
219  }
220  }
221 }
222 
224 {
225  check(CarlaActor != nullptr);
226 
227  FVehicleLightState LightState;
228  CarlaActor->GetVehicleLightState(LightState);
229  CarlaRecorderLightVehicle LightVehicle;
230  LightVehicle.DatabaseId = CarlaActor->GetActorId();
231  LightVehicle.State = carla::rpc::VehicleLightState(LightState).light_state;
232  AddLightVehicle(LightVehicle);
233 }
234 
236 {
237  check(CarlaActor != nullptr);
238 
239  FVector Velocity, AngularVelocity;
240  constexpr float TO_METERS = 1e-2;
241  Velocity = TO_METERS* CarlaActor->GetActorVelocity();
242  AngularVelocity = CarlaActor->GetActorAngularVelocity();
243  CarlaRecorderKinematics Kinematic =
244  {
245  CarlaActor->GetActorId(),
246  Velocity,
247  AngularVelocity
248  };
249  AddKinematics(Kinematic);
250 }
252 {
253  check(CarlaActor != nullptr);
254 
255  const auto &Box = CarlaActor->GetActorInfo()->BoundingBox;
257  {
258  CarlaActor->GetActorId(),
259  {Box.Origin, Box.Extent}
260  };
261 
262  AddBoundingBox(BoundingBox);
263 }
264 
266 {
267  if (bAdditionalData)
268  {
269  TArray<UBoxComponent*> Triggers = TrafficSign.GetTriggerVolumes();
270  if(!Triggers.Num())
271  {
272  return;
273  }
274  UBoxComponent* Trigger = Triggers.Top();
275  auto VolumeOrigin = Trigger->GetComponentLocation();
276  auto VolumeExtent = Trigger->GetScaledBoxExtent();
278  {
280  {VolumeOrigin, VolumeExtent}
281  };
282  TriggerVolumes.Add(TriggerVolume);
283  }
284 }
285 
287 {
288  if (bAdditionalData)
289  {
291  Control.DatabaseId = Episode->GetActorRegistry().FindCarlaActor(&Vehicle)->GetActorId();
293  PhysicsControls.Add(Control);
294  }
295 }
296 
298 {
299  if (bAdditionalData)
300  {
301  auto DatabaseId = Episode->GetActorRegistry().FindCarlaActor(&TrafficLight)->GetActorId();
303  DatabaseId,
304  TrafficLight.GetGreenTime(),
305  TrafficLight.GetYellowTime(),
306  TrafficLight.GetRedTime()
307  };
309  }
310 }
311 
312 std::string ACarlaRecorder::Start(std::string Name, FString MapName, bool AdditionalData)
313 {
314  // stop replayer if any in course
315  if (Replayer.IsEnabled())
316  Replayer.Stop();
317 
318  // stop recording
319  Stop();
320 
321  // reset collisions Id
322  NextCollisionId = 0;
323 
324  // get the final path + filename
325  std::string Filename = GetRecorderFilename(Name);
326 
327  // binary file
328  File.open(Filename, std::ios::binary);
329  if (!File.is_open())
330  {
331  return "";
332  }
333 
334  // save info
335  Info.Version = 1;
336  Info.Magic = TEXT("CARLA_RECORDER");
337  Info.Date = std::time(0);
338  Info.Mapfile = MapName;
339 
340  // write general info
341  Info.Write(File);
342 
343  Frames.Reset();
345 
346  Enable();
347 
348  bAdditionalData = AdditionalData;
349 
350  // add all existing actors
352 
353  return std::string(Filename);
354 }
355 
357 {
358  Disable();
359 
360  if (File)
361  {
362  File.close();
363  }
364 
365  Clear();
366 }
367 
369 {
370  EventsAdd.Clear();
371  EventsDel.Clear();
373  Collisions.Clear();
374  Positions.Clear();
375  States.Clear();
376  Vehicles.Clear();
377  Walkers.Clear();
379  LightScenes.Clear();
380  Kinematics.Clear();
385 }
386 
387 void ACarlaRecorder::Write(double DeltaSeconds)
388 {
389  // update this frame data
390  Frames.SetFrame(DeltaSeconds);
391 
392  // start
394 
395  // events
400 
401  // positions and states
403  States.Write(File);
404 
405  // animations
407  Walkers.Write(File);
410 
411  // additional info
412  if (bAdditionalData)
413  {
420  }
421 
422  // end
424 
425  Clear();
426 }
427 
429 {
430  if (Enabled)
431  {
432  Positions.Add(Position);
433  }
434 }
435 
437 {
438  if (Enabled)
439  {
440  EventsAdd.Add(std::move(Event));
441  }
442 }
443 
445 {
446  if (Enabled)
447  {
448  EventsDel.Add(std::move(Event));
449  }
450 }
451 
453 {
454  if (Enabled)
455  {
456  EventsParent.Add(std::move(Event));
457  }
458 }
459 
461 {
462  if (Enabled)
463  {
465 
466  // some inits
467  Collision.Id = NextCollisionId++;
468  Collision.IsActor1Hero = false;
469  Collision.IsActor2Hero = false;
470 
471  // check actor 1
472  FCarlaActor *FoundActor1 = Episode->GetActorRegistry().FindCarlaActor(Actor1);
473  if (FoundActor1 != nullptr) {
474  if (FoundActor1->GetActorInfo() != nullptr)
475  {
476  auto Role = FoundActor1->GetActorInfo()->Description.Variations.Find("role_name");
477  if (Role != nullptr)
478  Collision.IsActor1Hero = (Role->Value == "hero");
479  }
480  Collision.DatabaseId1 = FoundActor1->GetActorId();
481  }
482  else {
483  Collision.DatabaseId1 = uint32_t(-1); // actor1 is not a registered Carla actor
484  }
485 
486  // check actor 2
487  FCarlaActor *FoundActor2 = Episode->GetActorRegistry().FindCarlaActor(Actor2);
488  if (FoundActor2 != nullptr) {
489  if (FoundActor2->GetActorInfo() != nullptr)
490  {
491  auto Role = FoundActor2->GetActorInfo()->Description.Variations.Find("role_name");
492  if (Role != nullptr)
493  Collision.IsActor2Hero = (Role->Value == "hero");
494  }
495  Collision.DatabaseId2 = FoundActor2->GetActorId();
496  }
497  else {
498  Collision.DatabaseId2 = uint32_t(-1); // actor2 is not a registered Carla actor
499  }
500 
501  Collisions.Add(std::move(Collision));
502  }
503 }
504 
506 {
507  if (Enabled)
508  {
509  States.Add(State);
510  }
511 }
512 
514 {
515  if (Enabled)
516  {
517  Vehicles.Add(Vehicle);
518  }
519 }
520 
522 {
523  if (Enabled)
524  {
525  Walkers.Add(Walker);
526  }
527 }
528 
530 {
531  if (Enabled)
532  {
533  LightVehicles.Add(LightVehicle);
534  }
535 }
536 
537 void ACarlaRecorder::AddEventLightSceneChanged(const UCarlaLight* Light)
538 {
539  if (Enabled)
540  {
541  CarlaRecorderLightScene LightScene =
542  {
543  Light->GetId(),
544  Light->GetLightIntensity(),
545  Light->GetLightColor(),
546  Light->GetLightOn(),
547  static_cast<uint8>(Light->GetLightType())
548  };
549 
550  LightScenes.Add(LightScene);
551  }
552 }
553 
555 {
556  if (Enabled)
557  {
558  Kinematics.Add(ActorKinematics);
559  }
560 }
561 
563 {
564  if (Enabled)
565  {
566  BoundingBoxes.Add(ActorBoundingBox);
567  }
568 }
569 
571 {
572  // registring all existing actors in first frame
574  for (auto& It : Registry)
575  {
576  const FCarlaActor* CarlaActor = It.Value.Get();
577  if (CarlaActor != nullptr)
578  {
579  // create event
581  CarlaActor->GetActorId(),
582  static_cast<uint8_t>(CarlaActor->GetActorType()),
583  CarlaActor->GetActorGlobalTransform(),
584  CarlaActor->GetActorInfo()->Description);
585  }
586  }
587 
588  UWorld *World = GetWorld();
589  if(World)
590  {
591  UCarlaLightSubsystem* CarlaLightSubsystem = World->GetSubsystem<UCarlaLightSubsystem>();
592  const auto& Lights = CarlaLightSubsystem->GetLights();
593  for (const auto& LightPair : Lights)
594  {
595  UCarlaLight* Light = LightPair.Value;
597  }
598  }
599 
600 }
601 
603  uint32_t DatabaseId,
604  uint8_t Type,
605  const FTransform &Transform,
606  FActorDescription ActorDescription)
607 {
608  CarlaRecorderActorDescription Description;
609  Description.UId = ActorDescription.UId;
610  Description.Id = ActorDescription.Id;
611 
612  // attributes
613  Description.Attributes.reserve(ActorDescription.Variations.Num());
614  for (const auto &item : ActorDescription.Variations)
615  {
617  Attr.Type = static_cast<uint8_t>(item.Value.Type);
618  Attr.Id = item.Value.Id;
619  Attr.Value = item.Value.Value;
620  // check for empty attributes
621  if (!Attr.Id.IsEmpty())
622  {
623  Description.Attributes.emplace_back(std::move(Attr));
624  }
625  }
626 
627  // recorder event
628  CarlaRecorderEventAdd RecEvent
629  {
630  DatabaseId,
631  Type,
632  Transform.GetTranslation(),
633  Transform.GetRotation().Euler(),
634  std::move(Description)
635  };
636  AddEvent(std::move(RecEvent));
637 
638  FCarlaActor* CarlaActor = Episode->FindCarlaActor(DatabaseId);
639  // Other events related to spawning actors
640  // check if it is a vehicle to get initial physics control
641  ACarlaWheeledVehicle* Vehicle = Cast<ACarlaWheeledVehicle>(CarlaActor->GetActor());
642  if (Vehicle)
643  {
644  AddPhysicsControl(*Vehicle);
645  }
646 
647  ATrafficLightBase* TrafficLight = Cast<ATrafficLightBase>(CarlaActor->GetActor());
648  if (TrafficLight)
649  {
650  AddTrafficLightTime(*TrafficLight);
651  }
652 
653  ATrafficSignBase* TrafficSign = Cast<ATrafficSignBase>(CarlaActor->GetActor());
654  if (TrafficSign)
655  {
656  // Trigger volume in global coordinates
657  AddTriggerVolume(*TrafficSign);
658  }
659  else
660  {
661  // Bounding box in local coordinates
662  AddActorBoundingBox(CarlaActor);
663  }
664 }
void AddEventLightSceneChanged(const UCarlaLight *Light)
FCarlaActor * FindCarlaActor(FCarlaActor::IdType ActorId)
Find a Carla actor by id.
Definition: CarlaEpisode.h:152
CarlaReplayer * GetReplayer() const
Definition: CarlaEpisode.h:281
CarlaRecorderTrafficLightTimes TrafficLightTimes
CarlaRecorderFrames Frames
CarlaRecorderQuery Query
bool IsEnabled(void)
Definition: CarlaReplayer.h:65
void Add(const CarlaRecorderAnimVehicle &InObj)
CarlaRecorderAnimVehicles Vehicles
auto end() const noexcept
std::vector< carla::rpc::LightState > GetLights(FString Client)
CarlaRecorderInfo Info
void SetTimeFactor(double NewTimeFactor)
Definition: CarlaReplayer.h:78
void StopReplayer(bool KeepActors=false)
A registry of all the Carla actors.
Definition: ActorRegistry.h:20
CarlaRecorderPlatformTime PlatformTime
void Add(const CarlaRecorderPosition &InObj)
std::string Start(std::string Name, FString MapName, bool AdditionalData=false)
FVehiclePhysicsControl GetVehiclePhysicsControl() const
TArray< UBoxComponent * > GetTriggerVolumes() const
FVector GetActorAngularVelocity() const
Definition: CarlaActor.cpp:369
virtual ETrafficLightState GetTrafficLightState() const
Definition: CarlaActor.h:325
std::string ShowFileCollisions(std::string Name, char Type1, char Type2)
virtual ECarlaServerResponse GetWalkerControl(FWalkerControl &)
Definition: CarlaActor.h:365
void Write(std::ofstream &OutFile)
CarlaReplayer Replayer
AActor * GetActor()
Definition: CarlaActor.h:90
CarlaRecorderLightScenes LightScenes
void Add(const CarlaRecorderCollision &Collision)
void AddAnimVehicle(const CarlaRecorderAnimVehicle &Vehicle)
CarlaRecorderAnimWalkers Walkers
void WriteStart(std::ofstream &OutFile)
void Add(const CarlaRecorderLightScene &InObj)
void Add(const CarlaRecorderTrafficLightTime &InObj)
void AddBoundingBox(const CarlaRecorderActorBoundingBox &ActorBoundingBox)
auto begin() const noexcept
void SetIgnoreHero(bool InIgnoreHero)
Definition: CarlaReplayer.h:84
void SetReplayerTimeFactor(double TimeFactor)
void AddAnimWalker(const CarlaRecorderAnimWalker &Walker)
void AddActorBoundingBox(FCarlaActor *CarlaActor)
Class which implements the state changing of traffic lights.
std::string ShowFileActorsBlocked(std::string Name, double MinTime=30, double MinDistance=10)
float GetGreenTime() const
void Add(const CarlaRecorderEventAdd &Event)
void AddVehicleLight(FCarlaActor *CarlaActor)
void AddEvent(const CarlaRecorderEventAdd &Event)
void Add(const CarlaRecorderStateTrafficLight &State)
void AddWalkerAnimation(FCarlaActor *CarlaActor)
void WriteEnd(std::ofstream &OutFile)
std::string QueryBlocked(std::string Filename, double MinTime=30, double MinDistance=10)
void Write(std::ofstream &OutFile)
CarlaRecorderActorBoundingBoxes BoundingBoxes
bg::model::box< Point3D > Box
Definition: InMemoryMap.h:45
void AddKinematics(const CarlaRecorderKinematics &ActorKinematics)
void Write(std::ofstream &OutFile)
void Clear(void)
void AddActorKinematics(FCarlaActor *CarlaActor)
void AddTrafficLightTime(const ATrafficLightBase &TrafficLight)
CarlaRecorderPhysicsControls PhysicsControls
CarlaRecorderLightVehicles LightVehicles
void Add(const CarlaRecorderPhysicsControl &InObj)
std::string QueryCollisions(std::string Filename, char Category1='a', char Category2='a')
void Add(const CarlaRecorderLightVehicle &InObj)
void AddCollision(AActor *Actor1, AActor *Actor2)
const FActorRegistry & GetActorRegistry() const
Definition: CarlaEpisode.h:134
void CreateRecorderEventAdd(uint32_t DatabaseId, uint8_t Type, const FTransform &Transform, FActorDescription ActorDescription)
TMap< FString, FActorAttribute > Variations
User selected variations of the actor.
CarlaRecorderEventsParent EventsParent
void Add(const CarlaRecorderActorBoundingBox &InObj)
void Add(const CarlaRecorderKinematics &InObj)
void Write(std::ofstream &File)
std::string ReplayFile(std::string Name, double TimeStart, double Duration, uint32_t FollowId, bool ReplaySensors)
UCarlaEpisode * Episode
void Write(std::ofstream &OutFile)
std::ofstream File
std::string QueryInfo(std::string Filename, bool bShowAll=false)
void SetFrame(double DeltaSeconds)
std::vector< CarlaRecorderActorAttribute > Attributes
CarlaRecorderEventsAdd EventsAdd
CarlaRecorderCollisions Collisions
void AddPosition(const CarlaRecorderPosition &Position)
void Write(std::ofstream &OutFile)
void Write(std::ofstream &OutFile)
A description of a Carla Actor with all its variation.
FVector GetActorVelocity() const
Definition: CarlaActor.cpp:357
FActorDescription Description
Definition: ActorInfo.h:26
void SetReplayerIgnoreHero(bool IgnoreHero)
const FActorInfo * GetActorInfo() const
Definition: CarlaActor.h:100
void AddTrafficLightState(FCarlaActor *CarlaActor)
FBoundingBox BoundingBox
Definition: ActorInfo.h:30
bool IsPendingKill() const
Definition: CarlaActor.h:75
float GetRedTime() const
void AddExistingActors(void)
void Ticking(float DeltaSeconds)
float GetYellowTime() const
void Write(std::ofstream &OutFile)
std::string ReplayFile(std::string Filename, double TimeStart=0.0f, double Duration=0.0f, uint32_t FollowId=0, bool ReplaySensors=false)
uint32_t NextCollisionId
CarlaRecorderActorsKinematics Kinematics
void AddVehicleAnimation(FCarlaActor *CarlaActor)
void Write(std::ofstream &OutFile)
virtual UTrafficLightController * GetTrafficLightController()
Definition: CarlaActor.h:330
std::string ShowFileInfo(std::string Name, bool bShowAll=false)
CarlaRecorderPositions Positions
void Write(std::ofstream &OutFile)
FTransform GetActorGlobalTransform() const
Definition: CarlaActor.cpp:196
void Tick(float Time)
uint32 UId
UId of the definition in which this description was based.
CarlaRecorderActorTriggerVolumes TriggerVolumes
void Write(std::ofstream &OutFile)
std::string GetRecorderFilename(std::string Filename)
CarlaRecorderEventsDel EventsDel
void Enable(void)
virtual ECarlaServerResponse GetVehicleLightState(FVehicleLightState &)
Definition: CarlaActor.h:246
virtual ECarlaServerResponse GetVehicleControl(FVehicleControl &)
Definition: CarlaActor.h:287
void Stop(bool KeepActors=false)
void Write(std::ofstream &OutFile)
IdType GetActorId() const
Definition: CarlaActor.h:80
void Add(const CarlaRecorderEventParent &Event)
void Add(const CarlaRecorderAnimWalker &InObj)
FCarlaActor * FindCarlaActor(IdType Id)
Definition: ActorRegistry.h:69
void Disable(void)
void Write(std::ofstream &OutFile)
void AddActorPosition(FCarlaActor *CarlaActor)
void AddLightVehicle(const CarlaRecorderLightVehicle &LightVehicle)
ATrafficLightGroup * GetGroup()
void Add(const CarlaRecorderActorBoundingBox &InObj)
void Add(const CarlaRecorderEventDel &Event)
flag_type light_state
Lights state flag, all turned off by default.
Base class for CARLA wheeled vehicles.
void Write(std::ofstream &OutFile)
Maps a controller from OpenDrive.
Defines the physical appearance of a vehicle whitch is obtained by the sensors.
CarlaRecorderStates States
void AddPhysicsControl(const ACarlaWheeledVehicle &Vehicle)
geom::Transform Transform
Definition: rpc/Transform.h:16
FVehiclePhysicsControl VehiclePhysicsControl
void Write(std::ofstream &OutFile)
void Write(double DeltaSeconds)
void AddTriggerVolume(const ATrafficSignBase &TrafficSign)
void AddState(const CarlaRecorderStateTrafficLight &State)
ActorType GetActorType() const
Definition: CarlaActor.h:85
A view over an actor and its properties.
Definition: CarlaActor.h:23