CARLA
 
载入中...
搜索中...
未找到
CarSimManagerComponent.cpp
浏览该文件的文档.
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
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
88void 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{
108 #ifdef WITH_CARSIM
109 // finish Carsim simulation
111 CarlaVehicle->GetMesh()->SetPhysicsLinearVelocity(FVector(0,0,0), false, "Vehicle_Base");
112 CarlaVehicle->GetVehicleMovementComponent()->SetComponentTickEnabled(true);
113 CarlaVehicle->GetVehicleMovementComponent()->Activate();
114 CarlaVehicle->GetMesh()->PhysicsTransformUpdateMode = EPhysicsTransformUpdateMode::SimulationUpatesComponentTransform;
115 auto * Bone = CarlaVehicle->GetMesh()->GetBodyInstance(NAME_None);
116 if (Bone)
117 {
118 Bone->SetInstanceSimulatePhysics(true);
119 }
120 else
121 {
122 carla::log_warning("No bone with name");
123 }
124 CarlaVehicle->GetMesh()->SetCollisionResponseToChannel(ECollisionChannel::ECC_WorldStatic, ECollisionResponse::ECR_Block);
125 CarlaVehicle->GetMesh()->SetCollisionProfileName("Vehicle");
127 #endif
128}
129
134
136 AActor *OtherActor,
137 FVector NormalImpulse,
138 const FHitResult &Hit)
139{
141}
142
143
145 AActor* OtherActor,
146 UPrimitiveComponent* OtherComp,
147 int32 OtherBodyIndex,
148 bool bFromSweep,
149 const FHitResult & SweepResult)
150{
151 if (OtherComp->GetCollisionResponseToChannel(
152 ECollisionChannel::ECC_WorldDynamic) ==
153 ECollisionResponse::ECR_Block)
154 {
156 }
157}
158
160 AActor* OtherActor,
161 UPrimitiveComponent* OtherComp,
162 int32 OtherBodyIndex)
163{
164}
165
167{
168 #ifdef WITH_CARSIM
169 CarSimMovementComponent->UseVehicleSimRoad = bEnabled;
170 CarSimMovementComponent->ResetVsVehicle(false);
171 CarSimMovementComponent->SyncVsVehicleLocOri();
172 #endif
173}
174
175
176void UCarSimManagerComponent::EndPlay(const EEndPlayReason::Type EndPlayReason)
177{
178 #ifdef WITH_CARSIM
179 if (OffsetActor)
180 {
181 OffsetActor->Destroy();
182 }
183
184 if(!CarlaVehicle)
185 {
186 UE_LOG(LogCarla, Warning, TEXT("Error: Owner is not properly set for UCarSimManagerComponent") );
187 return;
188 }
189
190 // reset callbacks to react to collisions
191 CarlaVehicle->OnActorHit.RemoveDynamic(this, &UCarSimManagerComponent::OnCarSimHit);
192 CarlaVehicle->GetMesh()->OnComponentBeginOverlap.RemoveDynamic(this, &UCarSimManagerComponent::OnCarSimOverlap);
193 CarlaVehicle->GetMesh()->OnComponentEndOverlap.RemoveDynamic(this, &UCarSimManagerComponent::OnCarSimEndOverlap);
194
195 #endif
196}
197
199{
200 #ifdef WITH_CARSIM
201 return GetOwner()->GetActorForwardVector() * CarSimMovementComponent->GetForwardSpeed();
202 #else
203 return FVector();
204 #endif
205}
206
208{
209 #ifdef WITH_CARSIM
210 return CarSimMovementComponent->GetCurrentGear();
211 #else
212 return 0;
213 #endif
214}
215
217{
218 #ifdef WITH_CARSIM
219 return CarSimMovementComponent->GetForwardSpeed();
220 #else
221 return 0.0;
222 #endif
223}
Base class for CARLA wheeled vehicles.
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction) override
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override
int32 GetVehicleCurrentGear() const override
void OnCarSimEndOverlap(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex)
void OnCarSimHit(AActor *Actor, AActor *OtherActor, FVector NormalImpulse, const FHitResult &Hit)
float GetVehicleForwardSpeed() const override
void ProcessControl(FVehicleControl &Control) override
void OnCarSimOverlap(UPrimitiveComponent *OverlappedComponent, AActor *OtherActor, UPrimitiveComponent *OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult &SweepResult)
static void CreateCarsimComponent(ACarlaWheeledVehicle *Vehicle, FString Simfile)
virtual void DisableSpecialPhysics() override
virtual void BeginPlay() override
FVector GetVelocity() const override
static void CreateDefaultMovementComponent(ACarlaWheeledVehicle *Vehicle)
static void log_warning(Args &&... args)
Definition Logging.h:96