From d5abcee617381bc53cd3d59eede09fa9532b78a3 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Wed, 22 Mar 2017 13:53:27 +0100 Subject: [PATCH 1/3] Improve logging, debugging --- Source/Carla/Game/CarlaGameController.cpp | 20 +++++++++++++++----- Source/Carla/Game/MockGameController.cpp | 3 +-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Source/Carla/Game/CarlaGameController.cpp b/Source/Carla/Game/CarlaGameController.cpp index 5243c73ae..4e5408f29 100644 --- a/Source/Carla/Game/CarlaGameController.cpp +++ b/Source/Carla/Game/CarlaGameController.cpp @@ -63,7 +63,7 @@ static void SendSceneValues(carla::CarlaServer &Server, const TArray UE_LOG(LogCarla, Log, TEXT("Start position {%f, %f}"), Location.X, Location.Y); sceneValues.possible_positions.push_back({Location.X, Location.Y}); } - UE_LOG(LogCarla, Log, TEXT("Send scene values")); + UE_LOG(LogCarla, Log, TEXT("Send scene values: %d positions"), sceneValues.possible_positions.size()); Server.sendSceneValues(sceneValues); } @@ -83,7 +83,11 @@ static bool TryReadEpisodeStart(carla::CarlaServer &Server, uint32 &StartIndex) { uint32 EndIndex; UE_LOG(LogCarla, Log, TEXT("tryReadEpisodeStart")); - return Server.tryReadEpisodeStart(StartIndex, EndIndex); + if (Server.tryReadEpisodeStart(StartIndex, EndIndex)) { + UE_LOG(LogCarla, Warning, TEXT("Requested new episode")); + return true; + } + return false; } static void SendReward( @@ -92,7 +96,7 @@ static void SendReward( const std::array &Cameras) { carla::Reward_Values reward; - reward.timestamp = FMath::RoundHalfToZero(FPlatformTime::Seconds()); + reward.timestamp = FMath::RoundHalfToZero(1000.0 * FPlatformTime::Seconds()); Set(reward.player_location, PlayerState.GetLocation()); Set(reward.player_orientation, PlayerState.GetOrientation()); Set(reward.player_acceleration, PlayerState.GetAcceleration()); @@ -107,7 +111,7 @@ static void SendReward( reward.image_height = Cameras[0u]->GetImageSizeY(); } Set(reward.image_rgb_0, Cameras[0u]); - // Set(reward.image_rgb_1, Cameras[1u]); + Set(reward.image_rgb_1, Cameras[1u]); // Set(reward.image_depth_0, ); // Set(reward.image_depth_1, ); UE_LOG(LogCarla, Log, TEXT("Send reward")); @@ -137,7 +141,13 @@ static void SetActorTransform( Index = 0u; } check(AvailableStartTransforms.Num() > 0); - Player.SetActorTransform(AvailableStartTransforms[Index]); + UE_LOG(LogCarla, Log, TEXT("Before: Pawn position = %s"), *Player.GetPawn()->GetActorLocation().ToString()); + if (!Player.GetPawn()->SetActorTransform(AvailableStartTransforms[Index], false)) { + UE_LOG(LogCarla, Error, TEXT("Failed to set actor transform")); + } else { + Player.GetPawn()->MarkComponentsRenderStateDirty(); + } + UE_LOG(LogCarla, Log, TEXT("After: Pawn position = %s"), *Player.GetPawn()->GetActorLocation().ToString()); } // ============================================================================= diff --git a/Source/Carla/Game/MockGameController.cpp b/Source/Carla/Game/MockGameController.cpp index 4835d3267..5b830624e 100644 --- a/Source/Carla/Game/MockGameController.cpp +++ b/Source/Carla/Game/MockGameController.cpp @@ -13,8 +13,7 @@ void MockGameController::RegisterPlayer(AController &NewPlayer) { ACarlaVehicleController *VehicleController = Cast(&NewPlayer); if (VehicleController != nullptr) { - if (!VehicleController->IsInManualMode()) - VehicleController->SetManualMode(true); + VehicleController->SetManualMode(true); } else { UE_LOG(LogCarla, Warning, TEXT("Player is not a ACarlaVehicleController")); } From e0c7f1859ff710d512ecae252d6ae4646bf3aedc Mon Sep 17 00:00:00 2001 From: nsubiron Date: Wed, 22 Mar 2017 15:02:42 +0100 Subject: [PATCH 2/3] Temporary fix for linux --- Source/Carla/CityMapGenerator.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Carla/CityMapGenerator.cpp b/Source/Carla/CityMapGenerator.cpp index 8e29bc610..2c3fc528b 100644 --- a/Source/Carla/CityMapGenerator.cpp +++ b/Source/Carla/CityMapGenerator.cpp @@ -27,7 +27,7 @@ ACityMapGenerator::~ACityMapGenerator() {} void ACityMapGenerator::OnConstruction(const FTransform &Transform) { Super::OnConstruction(Transform); - UpdateMap(); + // UpdateMap(); /// @todo Do we need to update the map here? } #if WITH_EDITOR From 04c7e850121030c695ef2e22cec3d443c9e6fc45 Mon Sep 17 00:00:00 2001 From: nsubiron Date: Wed, 22 Mar 2017 15:12:32 +0100 Subject: [PATCH 3/3] Adapt Unreal part to new protocol --- Source/Carla/Game/CarlaGameController.cpp | 144 ++++++++++------------ Source/Carla/Game/CarlaGameController.h | 6 +- 2 files changed, 66 insertions(+), 84 deletions(-) diff --git a/Source/Carla/Game/CarlaGameController.cpp b/Source/Carla/Game/CarlaGameController.cpp index 4e5408f29..618b9132f 100644 --- a/Source/Carla/Game/CarlaGameController.cpp +++ b/Source/Carla/Game/CarlaGameController.cpp @@ -12,7 +12,7 @@ #include // ============================================================================= -// -- static local methods ----------------------------------------------------- +// -- Set functions ------------------------------------------------------------ // ============================================================================= static inline void Set(float &lhs, float rhs) @@ -44,50 +44,53 @@ static void Set(std::vector &cImage, const ASceneCaptureCamera *Ca } } -static void ReadSceneInit(carla::CarlaServer &Server) +// ============================================================================= +// -- Other static methods ----------------------------------------------------- +// ============================================================================= + +static bool ReadSceneInit(carla::CarlaServer &Server) { carla::Mode mode; uint32 scene; UE_LOG(LogCarla, Log, TEXT("Waiting for tryReadSceneInit...")); while (!Server.tryReadSceneInit(mode, scene)) { - // wait. + if (Server.needsRestart()) + return false; } + return true; } -static void SendSceneValues(carla::CarlaServer &Server, const TArray &Transforms) +static bool SendAndReadSceneValues( + carla::CarlaServer &Server, + const TArray &AvailableStartSpots, + uint32 &StartIndex) { + check(AvailableStartSpots.Num() > 0); + carla::Scene_Values sceneValues; - sceneValues.possible_positions.reserve(Transforms.Num()); - for (const FTransform &Transform : Transforms) { - const FVector &Location = Transform.GetLocation(); - UE_LOG(LogCarla, Log, TEXT("Start position {%f, %f}"), Location.X, Location.Y); + sceneValues.possible_positions.reserve(AvailableStartSpots.Num()); + for (APlayerStart *StartSpot : AvailableStartSpots) { + const FVector &Location = StartSpot->GetActorLocation(); + UE_LOG(LogCarla, Log, TEXT("Found start position {%f, %f}"), Location.X, Location.Y); sceneValues.possible_positions.push_back({Location.X, Location.Y}); } + // Send scene values. UE_LOG(LogCarla, Log, TEXT("Send scene values: %d positions"), sceneValues.possible_positions.size()); Server.sendSceneValues(sceneValues); -} - -static uint32 ReadEpisodeStart(carla::CarlaServer &Server) -{ - uint32 StartIndex; + // Wait till we receive the answer. uint32 EndIndex; - UE_LOG(LogCarla, Log, TEXT("Waiting for tryReadEpisodeStart...")); + UE_LOG(LogCarla, Log, TEXT("Waiting for episode start...")); while (!Server.tryReadEpisodeStart(StartIndex, EndIndex)) { - // wait. + if (Server.needsRestart()) + return false; } - UE_LOG(LogCarla, Log, TEXT("tryReadEpisodeStart received: %d - %d"), StartIndex, EndIndex); - return StartIndex; -} - -static bool TryReadEpisodeStart(carla::CarlaServer &Server, uint32 &StartIndex) -{ - uint32 EndIndex; - UE_LOG(LogCarla, Log, TEXT("tryReadEpisodeStart")); - if (Server.tryReadEpisodeStart(StartIndex, EndIndex)) { - UE_LOG(LogCarla, Warning, TEXT("Requested new episode")); - return true; + UE_LOG(LogCarla, Log, TEXT("Episode start received: %d -> %d"), StartIndex, EndIndex); + // Make sure the index is in range. + if (StartIndex >= AvailableStartSpots.Num()) { + UE_LOG(LogCarla, Warning, TEXT("Received invalid start index, using zero instead")); + StartIndex = 0u; } - return false; + return true; } static void SendReward( @@ -114,42 +117,21 @@ static void SendReward( Set(reward.image_rgb_1, Cameras[1u]); // Set(reward.image_depth_0, ); // Set(reward.image_depth_1, ); - UE_LOG(LogCarla, Log, TEXT("Send reward")); + UE_LOG(LogCarla, Log, TEXT("Sending reward")); Server.sendReward(reward); } -static void ReadControl(carla::CarlaServer &Server, ACarlaVehicleController &Player) +static void TryReadControl(carla::CarlaServer &Server, ACarlaVehicleController &Player) { float steer; float throttle; - UE_LOG(LogCarla, Log, TEXT("Read control")); if (Server.tryReadControl(steer, throttle)) { Player.SetSteeringInput(steer); Player.SetThrottleInput(throttle); -#ifdef WITH_EDITOR UE_LOG(LogCarla, Log, TEXT("Read control: steer = %f, throttle = %f"), steer, throttle); -#endif // WITH_EDITOR } } -static void SetActorTransform( - ACarlaVehicleController &Player, - const TArray &AvailableStartTransforms, - uint32 Index) -{ - if (Index >= AvailableStartTransforms.Num()) { - Index = 0u; - } - check(AvailableStartTransforms.Num() > 0); - UE_LOG(LogCarla, Log, TEXT("Before: Pawn position = %s"), *Player.GetPawn()->GetActorLocation().ToString()); - if (!Player.GetPawn()->SetActorTransform(AvailableStartTransforms[Index], false)) { - UE_LOG(LogCarla, Error, TEXT("Failed to set actor transform")); - } else { - Player.GetPawn()->MarkComponentsRenderStateDirty(); - } - UE_LOG(LogCarla, Log, TEXT("After: Pawn position = %s"), *Player.GetPawn()->GetActorLocation().ToString()); -} - // ============================================================================= // -- CarlaGameController ------------------------------------------------------ // ============================================================================= @@ -162,13 +144,21 @@ CarlaGameController::CarlaGameController() : APlayerStart *CarlaGameController::ChoosePlayerStart( const TArray &AvailableStartSpots) { - if (AvailableStartTransforms.Num() == 0) { - AvailableStartTransforms.Reserve(AvailableStartSpots.Num()); - for (APlayerStart *StartSpot : AvailableStartSpots) { - AvailableStartTransforms.Add(StartSpot->GetActorTransform()); + if (bServerNeedsRestart) { + UE_LOG(LogCarla, Log, TEXT("Initializing CarlaServer")); + Server->init(1u); + if (ReadSceneInit(*Server)) { + bServerNeedsRestart = false; + } else { + RestartLevel(true); } } - return AvailableStartSpots[0u]; + uint32 StartIndex; + if (SendAndReadSceneValues(*Server, AvailableStartSpots, StartIndex)) { + return AvailableStartSpots[StartIndex]; + } else { + RestartLevel(true); + } } void CarlaGameController::RegisterPlayer(AController &NewPlayer) @@ -196,32 +186,24 @@ void CarlaGameController::RegisterCaptureCamera(const ASceneCaptureCamera &Captu void CarlaGameController::Tick(float DeltaSeconds) { check(Player != nullptr); - if (!Server->clientConnected() || !Server->serverConnected() || !Server->worldConnected()) + if (Server->needsRestart()) { + RestartLevel(true); return; - - uint32 StartIndex; - if (bIsResetting || Server->needRestart()) { - UE_LOG(LogCarla, Log, TEXT("Initializing CarlaServer")); - Server->init(1u); - UE_LOG(LogCarla, Log, TEXT("Resetting the world")); - // Resetting the world. - ReadSceneInit(*Server); - SendSceneValues(*Server, AvailableStartTransforms); - StartIndex = ReadEpisodeStart(*Server); - SetActorTransform(*Player, AvailableStartTransforms, StartIndex); - Player->ResetPlayerState(); - Server->sendEndReset(); - bIsResetting = false; - } else if (TryReadEpisodeStart(*Server, StartIndex)) { - // Handle request for resetting the world. - UE_LOG(LogCarla, Log, TEXT("Handle request for resetting the world")); - SetActorTransform(*Player, AvailableStartTransforms, StartIndex); - Player->ResetPlayerState(); - Server->sendEndReset(); - } else { - // Regular tick. - UE_LOG(LogCarla, Log, TEXT("Tick!")); - SendReward(*Server, Player->GetPlayerState(), Cameras); - ReadControl(*Server, *Player); } + + if (Server->newEpisodeRequested()) { + UE_LOG(LogCarla, Log, TEXT("New episode requested")); + RestartLevel(false); + return; + } + + SendReward(*Server, Player->GetPlayerState(), Cameras); + TryReadControl(*Server, *Player); +} + +void CarlaGameController::RestartLevel(bool ServerNeedsRestart) +{ + UE_LOG(LogCarla, Log, TEXT("Restarting...")); + bServerNeedsRestart = ServerNeedsRestart; + Player->RestartLevel(); } diff --git a/Source/Carla/Game/CarlaGameController.h b/Source/Carla/Game/CarlaGameController.h index 7c409ec7d..75570bb48 100644 --- a/Source/Carla/Game/CarlaGameController.h +++ b/Source/Carla/Game/CarlaGameController.h @@ -27,13 +27,13 @@ public: private: + void RestartLevel(bool ServerNeedsRestart); + TUniquePtr Server; ACarlaVehicleController *Player; std::array Cameras; - TArray AvailableStartTransforms; - - bool bIsResetting = true; + bool bServerNeedsRestart = true; };