CARLA
CarSimManagerComponent.cpp
Go to the documentation of this file.
1 // Copyright (c) 2021 Computer Vision Center (CVC) at the Universitat Autonoma
2 // de Barcelona (UAB).
3 // Copyright (c) 2019 Intel Corporation
4 //
5 // This work is licensed under the terms of the MIT license.
6 // For a copy, see <https://opensource.org/licenses/MIT>.
7 
11 #include "Carla/Util/EmptyActor.h"
12 
13 #ifdef WITH_CARSIM
14 #include "CarSimMovementComponent.h"
15 #include "VsVar.h"
16 #endif
17 
19  ACarlaWheeledVehicle* Vehicle, FString Simfile)
20 {
21  #ifdef WITH_CARSIM
22  UCarSimManagerComponent* CarSimManagerComponent = NewObject<UCarSimManagerComponent>(Vehicle);
23  CarSimManagerComponent->SimfilePath = Simfile;
24  Vehicle->SetCarlaMovementComponent(CarSimManagerComponent);
25  CarSimManagerComponent->RegisterComponent();
26  #else
27  UE_LOG(LogCarla, Warning, TEXT("Error: CarSim plugin is not enabled") );
28  #endif
29 }
30 
32 {
33  Super::BeginPlay();
34  #ifdef WITH_CARSIM
35 
36  if(!CarlaVehicle)
37  {
38  UE_LOG(LogCarla, Warning, TEXT("Error: Owner is not properly set for UCarSimManagerComponent") );
39  return;
40  }
41 
42  // workarround to compensate carsim coordinate origin offset
43  FActorSpawnParameters SpawnParams;
44  SpawnParams.SpawnCollisionHandlingOverride =
45  ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
46  OffsetActor = GetWorld()->SpawnActor<AEmptyActor>(
47  CarlaVehicle->GetActorLocation() + CarlaVehicle->GetActorForwardVector() * CarlaVehicle->CarSimOriginOffset,
48  CarlaVehicle->GetActorRotation(),
49  SpawnParams);
50  CarSimMovementComponent = NewObject<UCarSimMovementComponent>(OffsetActor);
51 
52  carla::log_warning("Loading simfile:", carla::rpc::FromFString(SimfilePath));
53  CarlaVehicle->GetVehicleMovementComponent()->SetComponentTickEnabled(false);
54  CarlaVehicle->GetVehicleMovementComponent()->Deactivate();
55  CarSimMovementComponent->DisableVehicle = false;
56  CarSimMovementComponent->VsConfigFile = SimfilePath;
57  CarSimMovementComponent->Activate();
58  CarSimMovementComponent->RegisterComponent();
59 
60  CarSimMovementComponent->ResetVsVehicle(false);
61  // set carsim position to actor's
62  CarSimMovementComponent->SyncVsVehicleLocOri();
63  CarSimMovementComponent->SetComponentTickEnabled(true);
64  // set kinematic mode for root component and bones
65  CarlaVehicle->GetMesh()->PhysicsTransformUpdateMode = EPhysicsTransformUpdateMode::ComponentTransformIsKinematic;
66  auto * Bone = CarlaVehicle->GetMesh()->GetBodyInstance(NAME_None);
67  if (Bone)
68  {
69  Bone->SetInstanceSimulatePhysics(false);
70  }
71  // set callbacks to react to collisions
72  CarlaVehicle->OnActorHit.AddDynamic(this, &UCarSimManagerComponent::OnCarSimHit);
73  CarlaVehicle->GetMesh()->OnComponentBeginOverlap.AddDynamic(this, &UCarSimManagerComponent::OnCarSimOverlap);
74  CarlaVehicle->GetMesh()->OnComponentEndOverlap.AddDynamic(this, &UCarSimManagerComponent::OnCarSimEndOverlap);
75  CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Overlap);
76 
77  // workaround to prevent carsim from interacting with its own car
78  CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_Visibility, ECollisionResponse::ECR_Overlap);
79 
80  // attach to actor with an offset
81  CarlaVehicle->AttachToActor(OffsetActor, FAttachmentTransformRules::KeepWorldTransform);
82  #else
83  UE_LOG(LogCarla, Warning, TEXT("Error: CarSim plugin is not enabled") );
84  return;
85  #endif
86 }
87 
88 void UCarSimManagerComponent::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
89 {
90 }
91 
93 {
94  #ifdef WITH_CARSIM
95  CarSimMovementComponent->SetThrottleInput(Control.Throttle);
96  CarSimMovementComponent->SetSteeringInput(Control.Steer);
97  CarSimMovementComponent->SetBrakeInput(Control.Brake);
98  if (Control.bHandBrake)
99  {
100  CarSimMovementComponent->SetBrakeInput(Control.Brake + 1.0);
101  }
102  Control.Gear = CarSimMovementComponent->GetCurrentGear();
103  #endif
104 }
105 
107  AActor *OtherActor,
108  FVector NormalImpulse,
109  const FHitResult &Hit)
110 {
111  #ifdef WITH_CARSIM
112  // finish Carsim simulation
114  CarlaVehicle->GetMesh()->SetPhysicsLinearVelocity(FVector(0,0,0), false, "Vehicle_Base");
115  CarlaVehicle->GetVehicleMovementComponent()->SetComponentTickEnabled(true);
116  CarlaVehicle->GetVehicleMovementComponent()->Activate();
117  CarlaVehicle->GetMesh()->PhysicsTransformUpdateMode = EPhysicsTransformUpdateMode::SimulationUpatesComponentTransform;
118  auto * Bone = CarlaVehicle->GetMesh()->GetBodyInstance(NAME_None);
119  if (Bone)
120  {
121  Bone->SetInstanceSimulatePhysics(true);
122  }
123  else
124  {
125  carla::log_warning("No bone with name");
126  }
127  CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
128  CarlaVehicle->GetMesh()->SetCollisionProfileName("Vehicle");
130  #endif
131 }
132 
133 
135  AActor* OtherActor,
136  UPrimitiveComponent* OtherComp,
137  int32 OtherBodyIndex,
138  bool bFromSweep,
139  const FHitResult & SweepResult)
140 {
141  if (OtherComp->GetCollisionResponseToChannel(
142  ECollisionChannel::ECC_WorldDynamic) ==
143  ECollisionResponse::ECR_Block)
144  {
145  #ifdef WITH_CARSIM
146  // finish Carsim simulation
148  CarlaVehicle->GetMesh()->SetPhysicsLinearVelocity(FVector(0,0,0), false, "Vehicle_Base");
149  CarlaVehicle->GetVehicleMovementComponent()->SetComponentTickEnabled(true);
150  CarlaVehicle->GetVehicleMovementComponent()->Activate();
151  CarlaVehicle->GetMesh()->PhysicsTransformUpdateMode = EPhysicsTransformUpdateMode::SimulationUpatesComponentTransform;
152  auto * Bone = CarlaVehicle->GetMesh()->GetBodyInstance(NAME_None);
153  if (Bone)
154  {
155  Bone->SetInstanceSimulatePhysics(true);
156  }
157  else
158  {
159  carla::log_warning("No bone with name");
160  }
161  CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
162  CarlaVehicle->GetMesh()->SetCollisionProfileName("Vehicle");
164  #endif
165  }
166 }
167 
169  AActor* OtherActor,
170  UPrimitiveComponent* OtherComp,
171  int32 OtherBodyIndex)
172 {
173 }
174 
176 {
177  #ifdef WITH_CARSIM
178  CarSimMovementComponent->UseVehicleSimRoad = bEnabled;
179  CarSimMovementComponent->ResetVsVehicle(false);
180  CarSimMovementComponent->SyncVsVehicleLocOri();
181  #endif
182 }
183 
184 
185 void UCarSimManagerComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
186 {
187  #ifdef WITH_CARSIM
188  if (OffsetActor)
189  {
190  OffsetActor->Destroy();
191  }
192 
193  if(!CarlaVehicle)
194  {
195  UE_LOG(LogCarla, Warning, TEXT("Error: Owner is not properly set for UCarSimManagerComponent") );
196  return;
197  }
198 
199  // reset callbacks to react to collisions
200  CarlaVehicle->OnActorHit.RemoveDynamic(this, &UCarSimManagerComponent::OnCarSimHit);
201  CarlaVehicle->GetMesh()->OnComponentBeginOverlap.RemoveDynamic(this, &UCarSimManagerComponent::OnCarSimOverlap);
202  CarlaVehicle->GetMesh()->OnComponentEndOverlap.RemoveDynamic(this, &UCarSimManagerComponent::OnCarSimEndOverlap);
203 
204  #endif
205 }
206 
208 {
209  #ifdef WITH_CARSIM
210  return GetOwner()->GetActorForwardVector() * CarSimMovementComponent->GetForwardSpeed();
211  #else
212  return FVector();
213  #endif
214 }
215 
217 {
218  #ifdef WITH_CARSIM
219  return CarSimMovementComponent->GetCurrentGear();
220  #else
221  return 0;
222  #endif
223 }
224 
226 {
227  #ifdef WITH_CARSIM
228  return CarSimMovementComponent->GetForwardSpeed();
229  #else
230  return 0.0;
231  #endif
232 }
virtual void BeginPlay() override
int32 GetVehicleCurrentGear() const override
void ProcessControl(FVehicleControl &Control) override
static void CreateCarsimComponent(ACarlaWheeledVehicle *Vehicle, FString Simfile)
float GetVehicleForwardSpeed() const override
void OnCarSimHit(AActor *Actor, AActor *OtherActor, FVector NormalImpulse, const FHitResult &Hit)
carla::SharedPtr< cc::Actor > Actor
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override
FVector GetVelocity() const override
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
static void log_warning(Args &&... args)
Definition: Logging.h:96
void OnCarSimEndOverlap(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
Base class for CARLA wheeled vehicles.
static void CreateDefaultMovementComponent(ACarlaWheeledVehicle *Vehicle)
void OnCarSimOverlap(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
void SetCarlaMovementComponent(UBaseCarlaMovementComponent *MoementComponent)