22 #include "CoreGlobals.h" 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);
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;
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};
273 bool PendingLightUpdates)
275 TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
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;
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);
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);
371 bool PendingLightUpdates)
373 TRACE_CPUPROFILER_EVENT_SCOPE_STR(__FUNCTION__);
381 AsyncStream.PopBufferFromPool(),
385 PendingLightUpdates);
387 AsyncStream.SerializeAndSend(*
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.
FAsyncDataStreamTmpl< T > MakeAsyncDataStream(const SensorT &Sensor, double Timestamp)
Create a FAsyncDataStream object.
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)
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.