19 #include "CoreGlobals.h" 39 auto Controller = Cast<AWheeledVehicleAIController>(
Vehicle->GetController());
40 if (Controller !=
nullptr)
43 state.vehicle_data.traffic_light_state =
static_cast<TLS>(Controller->GetTrafficLightState());
44 state.vehicle_data.speed_limit = Controller->GetSpeedLimit();
48 state.vehicle_data.has_traffic_light =
true;
52 state.vehicle_data.traffic_light_id = TrafficLightView->
GetActorId();
56 state.vehicle_data.has_traffic_light =
false;
61 state.vehicle_data.has_traffic_light =
false;
66 state.vehicle_data.failure_state =
Vehicle->GetFailureState();
72 auto Walker = Cast<APawn>(View.
GetActor());
73 auto Controller = Walker !=
nullptr ? Cast<AWalkerController>(Walker->GetController()) :
nullptr;
74 if (Controller !=
nullptr)
84 auto* TrafficLightComponent =
89 if(TrafficLightComponent ==
nullptr)
92 state.traffic_light_data.sign_id[0] =
'\0';
93 state.traffic_light_data.state =
static_cast<TLS>(
TrafficLight->GetTrafficLightState());
94 state.traffic_light_data.green_time =
TrafficLight->GetGreenTime();
95 state.traffic_light_data.yellow_time =
TrafficLight->GetYellowTime();
96 state.traffic_light_data.red_time =
TrafficLight->GetRedTime();
97 state.traffic_light_data.elapsed_time =
TrafficLight->GetElapsedTime();
98 state.traffic_light_data.time_is_frozen =
TrafficLight->GetTimeIsFrozen();
99 state.traffic_light_data.pole_index =
TrafficLight->GetPoleIndex();
108 UE_LOG(LogCarla, Error, TEXT(
"TrafficLightComponent doesn't have any Controller assigned"));
112 UE_LOG(LogCarla, Error, TEXT(
"TrafficLightComponent doesn't have any Group assigned"));
116 const FString fstring_sign_id = TrafficLightComponent->GetSignId();
117 const std::string sign_id = carla::rpc::FromFString(fstring_sign_id);
118 constexpr
size_t max_size =
sizeof(state.traffic_light_data.sign_id);
119 size_t sign_id_length = sign_id.length();
120 if(max_size < sign_id_length)
122 UE_LOG(LogCarla, Warning, TEXT(
"The max size of a signal id is 32. %s (%d)"), *fstring_sign_id, sign_id.length());
123 sign_id_length = max_size;
125 std::memset(state.traffic_light_data.sign_id,
'\0', max_size);
126 std::memcpy(state.traffic_light_data.sign_id, sign_id.c_str(), sign_id_length);
127 state.traffic_light_data.state =
static_cast<TLS>(TrafficLightComponent->GetLightState());
128 state.traffic_light_data.green_time = Controller->
GetGreenTime();
129 state.traffic_light_data.yellow_time = Controller->
GetYellowTime();
130 state.traffic_light_data.red_time = Controller->
GetRedTime();
131 state.traffic_light_data.elapsed_time = Controller->
GetElapsedTime();
132 state.traffic_light_data.time_is_frozen = Group->
IsFrozen();
133 state.traffic_light_data.pole_index =
TrafficLight->GetPoleIndex();
140 auto TrafficSign = Cast<ATrafficSignBase>(View.
GetActor());
141 if (TrafficSign !=
nullptr)
143 USignComponent* TrafficSignComponent =
144 Cast<USignComponent>(TrafficSign->FindComponentByClass<USignComponent>());
146 if(TrafficSignComponent)
148 const FString fstring_sign_id = TrafficSignComponent->GetSignId();
149 const std::string sign_id = carla::rpc::FromFString(fstring_sign_id);
150 constexpr
size_t max_size =
sizeof(state.traffic_sign_data.sign_id);
151 size_t sign_id_length = sign_id.length();
152 if(max_size < sign_id_length)
154 UE_LOG(LogCarla, Warning, TEXT(
"The max size of a signal id is 32. %s (%d)"), *fstring_sign_id, sign_id.length());
155 sign_id_length = max_size;
157 std::memset(state.traffic_light_data.sign_id,
'\0', max_size);
158 std::memcpy(state.traffic_sign_data.sign_id, sign_id.c_str(), sign_id_length);
176 state.vehicle_data.traffic_light_state = TLS::Green;
177 state.vehicle_data.speed_limit = ActorData->
SpeedLimit;
178 state.vehicle_data.has_traffic_light =
false;
201 UE_LOG(LogCarla, Error, TEXT(
"TrafficLight doesn't have any Group assigned"));
205 const FString fstring_sign_id = ActorData->
SignId;
206 const std::string sign_id = carla::rpc::FromFString(fstring_sign_id);
207 constexpr
size_t max_size =
sizeof(state.traffic_light_data.sign_id);
208 size_t sign_id_length = sign_id.length();
209 if(max_size < sign_id_length)
211 UE_LOG(LogCarla, Warning, TEXT(
"The max size of a signal id is 32. %s (%d)"), *fstring_sign_id, sign_id.length());
212 sign_id_length = max_size;
214 std::memset(state.traffic_light_data.sign_id,
'\0', max_size);
215 std::memcpy(state.traffic_light_data.sign_id, sign_id.c_str(), sign_id_length);
216 state.traffic_light_data.state =
static_cast<TLS>(Controller->GetCurrentState().State);
217 state.traffic_light_data.green_time = Controller->GetGreenTime();
218 state.traffic_light_data.yellow_time = Controller->GetYellowTime();
219 state.traffic_light_data.red_time = Controller->GetRedTime();
220 state.traffic_light_data.elapsed_time = Controller->GetElapsedTime();
221 state.traffic_light_data.time_is_frozen = Group->
IsFrozen();
222 state.traffic_light_data.pole_index = ActorData->
PoleIndex;
229 const FString fstring_sign_id = ActorData->
SignId;
230 const std::string sign_id = carla::rpc::FromFString(fstring_sign_id);
231 constexpr
size_t max_size =
sizeof(state.traffic_sign_data.sign_id);
232 size_t sign_id_length = sign_id.length();
233 if(max_size < sign_id_length)
235 UE_LOG(LogCarla, Warning, TEXT(
"The max size of a signal id is 32. %s (%d)"), *fstring_sign_id, sign_id.length());
236 sign_id_length = max_size;
238 std::memset(state.traffic_light_data.sign_id,
'\0', max_size);
239 std::memcpy(state.traffic_sign_data.sign_id, sign_id.c_str(), sign_id_length);
246 const auto RootComponent = Cast<UPrimitiveComponent>(Actor.GetRootComponent());
247 const FVector AngularVelocity =
248 RootComponent !=
nullptr ?
249 RootComponent->GetPhysicsAngularVelocityInDegrees() :
250 FVector{0.0f, 0.0f, 0.0f};
251 return {AngularVelocity.X, AngularVelocity.Y, AngularVelocity.Z};
256 const FVector &Velocity,
257 const float DeltaSeconds)
260 const FVector Acceleration = (Velocity - PreviousVelocity) / DeltaSeconds;
261 PreviousVelocity = Velocity;
262 return {Acceleration.X, Acceleration.Y, Acceleration.Z};
270 bool PendingLightUpdates)
272 TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
280 auto total_size =
sizeof(Serializer::Header) +
sizeof(ActorDynamicState) * Registry.
Num();
281 auto current_size = 0;
283 buffer.reset(total_size);
284 auto write_data = [¤t_size, &buffer](
const auto &data)
286 auto begin = buffer.begin() + current_size;
287 std::memcpy(begin, &data,
sizeof(data));
288 current_size +=
sizeof(data);
291 constexpr
float TO_METERS = 1e-2;
294 Serializer::Header header;
295 header.episode_id = Episode.
GetId();
296 header.platform_timestamp = FPlatformTime::Seconds();
297 header.delta_seconds = DeltaSeconds;
299 FIntVector MapOriginInMeters = MapOrigin / 100;
302 uint8_t simulation_state = (SimulationState::MapChange * MapChange);
303 simulation_state |= (SimulationState::PendingLightUpdate * PendingLightUpdates);
305 header.simulation_state =
static_cast<SimulationState
>(simulation_state);
310 for (
auto& It : Registry)
315 FTransform ActorTransform;
316 FVector Velocity(0.0f);
327 Velocity = TO_METERS * ActorData->
Velocity;
337 Velocity = TO_METERS * View->
GetActor()->GetVelocity();
344 ActorDynamicState info = {
357 buffer.resize(current_size);
359 check(buffer.size() == current_size);
361 return std::move(buffer);
368 bool PendingLightUpdates)
370 TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
374 AsyncStream.PopBufferFromPool(),
378 PendingLightUpdates);
380 AsyncStream.Send(*
this, std::move(buffer));
Dynamic state of an actor at a certain frame.
A registry of all the Carla actors.
carla::rpc::ActorState GetActorState() const
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.
Serializes the current state of the whole episode.
carla::rpc::WalkerControl WalkerControl
double GetElapsedGameTime() const
Game seconds since the start of this episode.
Class which implements the state changing of traffic lights.
static auto FWorldObserver_GetDormantActorState(const FCarlaActor &View, const FActorRegistry &Registry)
auto MakeAsyncDataStream(const SensorT &Sensor, double Timestamp)
Create a FAsyncDataStream object.
static carla::geom::Vector3D FWorldObserver_GetAcceleration(const FCarlaActor &View, const FVector &Velocity, const float DeltaSeconds)
static carla::Buffer FWorldObserver_Serialize(carla::Buffer &&buffer, const UCarlaEpisode &Episode, float DeltaSeconds, bool MapChange, bool PendingLightUpdates)
float GetYellowTime() const
const FActorRegistry & GetActorRegistry() const
FActorData * GetActorData()
carla::SharedPtr< cc::Actor > Actor
float GetElapsedTime() const
auto GetId() const
Return the unique id of this episode.
const FActorInfo * GetActorInfo() const
FIntVector GetCurrentMapOrigin() const
A view over an actor and its properties.
FTransform GetActorGlobalTransform() const
static carla::geom::Vector3D FWorldObserver_GetAngularVelocity(const AActor &Actor)
float GetGreenTime() const
IdType GetActorId() const
carla::rpc::TrafficLightState TLS
FCarlaActor * FindCarlaActor(IdType Id)
Maps a controller from OpenDrive.
UTrafficLightController * Controller
static auto FWorldObserver_GetActorState(const FCarlaActor &View, const FActorRegistry &Registry)
geom::Transform Transform
ActorType GetActorType() const
A view over an actor and its properties.