22#include "CoreGlobals.h"
34 carla::sensor::data::ActorDynamicState::TypeDependentState state{};
42 auto Controller = Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
43 if (Controller !=
nullptr)
46 state.vehicle_data.traffic_light_state =
static_cast<TLS
>(Controller->GetTrafficLightState());
47 state.vehicle_data.speed_limit = Controller->GetSpeedLimit();
51 state.vehicle_data.has_traffic_light =
true;
55 state.vehicle_data.traffic_light_id = TrafficLightView->
GetActorId();
59 state.vehicle_data.has_traffic_light =
false;
64 state.vehicle_data.has_traffic_light =
false;
69 state.vehicle_data.failure_state =
Vehicle->GetFailureState();
75 auto Walker = Cast<APawn>(View.
GetActor());
76 auto Controller = Walker !=
nullptr ? Cast<AWalkerController>(Walker->GetController()) :
nullptr;
77 if (Controller !=
nullptr)
87 auto* TrafficLightComponent =
92 if(TrafficLightComponent ==
nullptr)
95 state.traffic_light_data.sign_id[0] =
'\0';
96 state.traffic_light_data.state =
static_cast<TLS
>(
TrafficLight->GetTrafficLightState());
97 state.traffic_light_data.green_time =
TrafficLight->GetGreenTime();
98 state.traffic_light_data.yellow_time =
TrafficLight->GetYellowTime();
99 state.traffic_light_data.red_time =
TrafficLight->GetRedTime();
100 state.traffic_light_data.elapsed_time =
TrafficLight->GetElapsedTime();
101 state.traffic_light_data.time_is_frozen =
TrafficLight->GetTimeIsFrozen();
102 state.traffic_light_data.pole_index =
TrafficLight->GetPoleIndex();
111 UE_LOG(LogCarla, Error, TEXT(
"TrafficLightComponent doesn't have any Controller assigned"));
115 UE_LOG(LogCarla, Error, TEXT(
"TrafficLightComponent doesn't have any Group assigned"));
119 const FString fstring_sign_id = TrafficLightComponent->GetSignId();
120 const std::string sign_id = carla::rpc::FromFString(fstring_sign_id);
121 constexpr size_t max_size =
sizeof(state.traffic_light_data.sign_id);
122 size_t sign_id_length = sign_id.length();
123 if(max_size < sign_id_length)
125 UE_LOG(LogCarla, Warning, TEXT(
"The max size of a signal id is 32. %s (%d)"), *fstring_sign_id, sign_id.length());
126 sign_id_length = max_size;
128 std::memset(state.traffic_light_data.sign_id,
'\0', max_size);
129 std::memcpy(state.traffic_light_data.sign_id, sign_id.c_str(), sign_id_length);
130 state.traffic_light_data.state =
static_cast<TLS
>(TrafficLightComponent->GetLightState());
131 state.traffic_light_data.green_time = Controller->
GetGreenTime();
132 state.traffic_light_data.yellow_time = Controller->
GetYellowTime();
133 state.traffic_light_data.red_time = Controller->
GetRedTime();
134 state.traffic_light_data.elapsed_time = Controller->
GetElapsedTime();
135 state.traffic_light_data.time_is_frozen = Group->
IsFrozen();
136 state.traffic_light_data.pole_index =
TrafficLight->GetPoleIndex();
143 auto TrafficSign = Cast<ATrafficSignBase>(View.
GetActor());
144 if (TrafficSign !=
nullptr)
146 USignComponent* TrafficSignComponent =
147 Cast<USignComponent>(TrafficSign->FindComponentByClass<USignComponent>());
149 if(TrafficSignComponent)
151 const FString fstring_sign_id = TrafficSignComponent->GetSignId();
152 const std::string sign_id = carla::rpc::FromFString(fstring_sign_id);
153 constexpr size_t max_size =
sizeof(state.traffic_sign_data.sign_id);
154 size_t sign_id_length = sign_id.length();
155 if(max_size < sign_id_length)
157 UE_LOG(LogCarla, Warning, TEXT(
"The max size of a signal id is 32. %s (%d)"), *fstring_sign_id, sign_id.length());
158 sign_id_length = max_size;
160 std::memset(state.traffic_light_data.sign_id,
'\0', max_size);
161 std::memcpy(state.traffic_sign_data.sign_id, sign_id.c_str(), sign_id_length);
172 carla::sensor::data::ActorDynamicState::TypeDependentState state{};
179 state.vehicle_data.traffic_light_state = TLS::Green;
180 state.vehicle_data.speed_limit =
ActorData->SpeedLimit;
181 state.vehicle_data.has_traffic_light =
false;
192 state.walker_control =
ActorData->WalkerControl;
204 UE_LOG(LogCarla, Error, TEXT(
"TrafficLight doesn't have any Group assigned"));
208 const FString fstring_sign_id =
ActorData->SignId;
209 const std::string sign_id = carla::rpc::FromFString(fstring_sign_id);
210 constexpr size_t max_size =
sizeof(state.traffic_light_data.sign_id);
211 size_t sign_id_length = sign_id.length();
212 if(max_size < sign_id_length)
214 UE_LOG(LogCarla, Warning, TEXT(
"The max size of a signal id is 32. %s (%d)"), *fstring_sign_id, sign_id.length());
215 sign_id_length = max_size;
217 std::memset(state.traffic_light_data.sign_id,
'\0', max_size);
218 std::memcpy(state.traffic_light_data.sign_id, sign_id.c_str(), sign_id_length);
219 state.traffic_light_data.state =
static_cast<TLS
>(Controller->GetCurrentState().State);
220 state.traffic_light_data.green_time = Controller->GetGreenTime();
221 state.traffic_light_data.yellow_time = Controller->GetYellowTime();
222 state.traffic_light_data.red_time = Controller->GetRedTime();
223 state.traffic_light_data.elapsed_time = Controller->GetElapsedTime();
224 state.traffic_light_data.time_is_frozen = Group->
IsFrozen();
225 state.traffic_light_data.pole_index =
ActorData->PoleIndex;
232 const FString fstring_sign_id =
ActorData->SignId;
233 const std::string sign_id = carla::rpc::FromFString(fstring_sign_id);
234 constexpr size_t max_size =
sizeof(state.traffic_sign_data.sign_id);
235 size_t sign_id_length = sign_id.length();
236 if(max_size < sign_id_length)
238 UE_LOG(LogCarla, Warning, TEXT(
"The max size of a signal id is 32. %s (%d)"), *fstring_sign_id, sign_id.length());
239 sign_id_length = max_size;
241 std::memset(state.traffic_light_data.sign_id,
'\0', max_size);
242 std::memcpy(state.traffic_sign_data.sign_id, sign_id.c_str(), sign_id_length);
249 const auto RootComponent = Cast<UPrimitiveComponent>(
Actor.GetRootComponent());
250 const FVector AngularVelocity =
251 RootComponent !=
nullptr ?
252 RootComponent->GetPhysicsAngularVelocityInDegrees() :
253 FVector{0.0f, 0.0f, 0.0f};
254 return {AngularVelocity.X, AngularVelocity.Y, AngularVelocity.Z};
259 const FVector &Velocity,
260 const float DeltaSeconds)
263 const FVector Acceleration = (Velocity - PreviousVelocity) / DeltaSeconds;
264 PreviousVelocity = Velocity;
265 return {Acceleration.X, Acceleration.Y, Acceleration.Z};
270 const UCarlaEpisode &Episode,
273 bool PendingLightUpdates)
275 TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
278 using ActorDynamicState = carla::sensor::data::ActorDynamicState;
283 auto total_size =
sizeof(Serializer::Header) +
sizeof(ActorDynamicState) * Registry.
Num();
284 auto current_size = 0;
286 buffer.reset(total_size);
287 auto write_data = [¤t_size, &buffer](
const auto &data)
289 auto begin = buffer.begin() + current_size;
290 std::memcpy(
begin, &data,
sizeof(data));
291 current_size +=
sizeof(data);
294 constexpr float TO_METERS = 1e-2;
297 Serializer::Header header;
298 header.episode_id = Episode.GetId();
299 header.platform_timestamp = FPlatformTime::Seconds();
300 header.delta_seconds = DeltaSeconds;
301 FIntVector MapOrigin = Episode.GetCurrentMapOrigin();
302 FIntVector MapOriginInMeters = MapOrigin / 100;
305 uint8_t simulation_state = (SimulationState::MapChange * MapChange);
306 simulation_state |= (SimulationState::PendingLightUpdate * PendingLightUpdates);
308 header.simulation_state =
static_cast<SimulationState
>(simulation_state);
313 for (
auto& It : Registry)
318 FTransform ActorTransform;
319 FVector Velocity(0.0f);
322 carla::sensor::data::ActorDynamicState::TypeDependentState
State{};
330 Velocity = TO_METERS *
ActorData->Velocity;
340 Velocity = TO_METERS * View->
GetActor()->GetVelocity();
347 ActorDynamicState info = {
360 buffer.resize(current_size);
362 check(buffer.size() == current_size);
364 return std::move(buffer);
368 const UCarlaEpisode &Episode,
371 bool PendingLightUpdates)
373 TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
381 AsyncStream.PopBufferFromPool(),
385 PendingLightUpdates);
387 AsyncStream.SerializeAndSend(*
this, std::move(buffer));
UE_LOG(LogCarla, Log, TEXT("UActorDispatcher::Destroying actor: '%s' %x"), *Id, Actor)
auto begin() const noexcept
名称范围迭代支持
TSharedPtr< const FActorInfo > carla::rpc::ActorState UWorld Actor
static auto FWorldObserver_GetDormantActorState(const FCarlaActor &View, const FActorRegistry &Registry)
static auto FWorldObserver_GetActorState(const FCarlaActor &View, const FActorRegistry &Registry)
static carla::Buffer FWorldObserver_Serialize(carla::Buffer &&buffer, const UCarlaEpisode &Episode, float DeltaSeconds, bool MapChange, bool PendingLightUpdates)
static carla::geom::Vector3D FWorldObserver_GetAcceleration(const FCarlaActor &View, const FVector &Velocity, const float DeltaSeconds)
static carla::geom::Vector3D FWorldObserver_GetAngularVelocity(const AActor &Actor)
如果你的类继承自UObject,你的类名上方需要加入 UCLASS() 宏 实现交通信号灯状态改变的类
FCarlaActor * FindCarlaActor(IdType Id)
ActorType GetActorType() const
carla::rpc::ActorState GetActorState() const
FTransform GetActorGlobalTransform() const
FActorData * GetActorData()
const FActorInfo * GetActorInfo() const
IdType GetActorId() const
FAsyncDataStreamTmpl< T > MakeAsyncDataStream(const SensorT &Sensor, double Timestamp)
创建 FAsyncDataStream 对象。
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.
从 OpenDrive 映射一个控制器。控制相关交通信号灯并包含其循环
float GetYellowTime() const
float GetGreenTime() const
float GetElapsedTime() const
一块原始数据。 请注意,如果需要更多容量,则会分配一个新的内存块,并 删除旧的内存块。这意味着默认情况下,缓冲区只能增长。要释放内存,使用 clear 或 pop。
Serializes the current state of the whole episode.