Update to the new server API

This commit is contained in:
nsubiron 2017-03-24 14:10:05 +01:00
parent e860381644
commit 6c93056fd7
4 changed files with 102 additions and 68 deletions

View File

@ -5,6 +5,7 @@
#define LOCTEXT_NAMESPACE "FCarlaModule"
DEFINE_LOG_CATEGORY(LogCarla);
DEFINE_LOG_CATEGORY(LogCarlaServer);
void FCarlaModule::StartupModule()
{

View File

@ -9,6 +9,7 @@
#include "NonCopyable.h"
DECLARE_LOG_CATEGORY_EXTERN(LogCarla, Log, All);
DECLARE_LOG_CATEGORY_EXTERN(LogCarlaServer, Log, All);
class FCarlaModule : public IModuleInterface
{

View File

@ -11,8 +11,6 @@
#include <carla/CarlaServer.h>
#define CSTEXT(text) TEXT("CarlaServer: " text)
// =============================================================================
// -- Set functions ------------------------------------------------------------
// =============================================================================
@ -50,94 +48,111 @@ static void Set(std::vector<carla::Color> &cImage, const TArray<FColor> &BitMap)
// -- Other static methods -----------------------------------------------------
// =============================================================================
// Wait for the scene init to be sent, return false if we need to restart the
// server.
/// @todo At the moment we just ignored what it is sent.
static bool ReadSceneInit(carla::CarlaServer &Server)
{
carla::Mode mode;
uint32 scene;
UE_LOG(LogCarla, Log, CSTEXT("Waiting for tryReadSceneInit..."));
while (!Server.tryReadSceneInit(mode, scene)) {
if (Server.needsRestart())
carla::Mode Mode;
uint32 Scene;
bool Success = false;
UE_LOG(LogCarlaServer, Log, TEXT("(tryReadSceneInit) Waiting for client..."));
while (!Success) {
if (!Server.tryReadSceneInit(Mode, Scene, Success))
return false;
}
return true;
}
// Send the available start spots to the client and wait for them to answer.
// Return false if the server needs restart.
static bool SendAndReadSceneValues(
carla::CarlaServer &Server,
const TArray<APlayerStart *> &AvailableStartSpots,
uint32 &StartIndex)
{
check(AvailableStartSpots.Num() > 0);
// Retrieve the location of each player start.
carla::Scene_Values sceneValues;
sceneValues.possible_positions.reserve(AvailableStartSpots.Num());
for (APlayerStart *StartSpot : AvailableStartSpots) {
check(StartSpot != nullptr);
const FVector &Location = StartSpot->GetActorLocation();
UE_LOG(LogCarla, Log, CSTEXT("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, CSTEXT("Send scene values: %d positions"), sceneValues.possible_positions.size());
Server.sendSceneValues(sceneValues);
// Wait till we receive the answer.
// Send the positions.
/// @todo At the moment we don't send the cameras' projection matrices.
UE_LOG(LogCarlaServer, Log, TEXT("Sending %d available start positions"), sceneValues.possible_positions.size());
if (!Server.sendSceneValues(sceneValues))
return false;
// Wait till we receive an answer.
uint32 EndIndex;
UE_LOG(LogCarla, Log, CSTEXT("Waiting for episode start..."));
while (!Server.tryReadEpisodeStart(StartIndex, EndIndex)) {
if (Server.needsRestart())
bool Success = false;
UE_LOG(LogCarlaServer, Log, TEXT("(tryReadEpisodeStart) Waiting for client..."));
while (!Success) {
if (!Server.tryReadEpisodeStart(StartIndex, EndIndex, Success))
return false;
}
UE_LOG(LogCarla, Log, CSTEXT("Episode start received: %d -> %d"), StartIndex, EndIndex);
UE_LOG(LogCarlaServer, Log, TEXT("Episode start received: { StartIndex = %d, EndIndex = %d }"), StartIndex, EndIndex);
// Make sure the index is in range.
if (StartIndex >= AvailableStartSpots.Num()) {
UE_LOG(LogCarla, Warning, CSTEXT("Received invalid start index, using zero instead"));
UE_LOG(
LogCarlaServer,
Error,
TEXT("Requested start position #%d but we only have %d, using first position instead"),
StartIndex,
AvailableStartSpots.Num());
StartIndex = 0u;
}
return true;
}
static void SendReward(
static bool SendReward(
carla::CarlaServer &Server,
const ACarlaPlayerState &PlayerState)
{
carla::Reward_Values reward;
reward.timestamp = PlayerState.GetTimeStamp();
Set(reward.player_location, PlayerState.GetLocation());
Set(reward.player_orientation, PlayerState.GetOrientation());
Set(reward.player_acceleration, PlayerState.GetAcceleration());
Set(reward.forward_speed, PlayerState.GetForwardSpeed());
Set(reward.collision_car, PlayerState.GetCollisionIntensityCars());
Set(reward.collision_pedestrian, PlayerState.GetCollisionIntensityPedestrians());
Set(reward.collision_general, PlayerState.GetCollisionIntensityOther());
Set(reward.intersect_other_lane, PlayerState.GetOtherLaneIntersectionFactor());
Set(reward.intersect_offroad, PlayerState.GetOffRoadIntersectionFactor());
auto reward = std::make_unique<carla::Reward_Values>();
reward->timestamp = PlayerState.GetTimeStamp();
Set(reward->player_location, PlayerState.GetLocation());
Set(reward->player_orientation, PlayerState.GetOrientation());
Set(reward->player_acceleration, PlayerState.GetAcceleration());
Set(reward->forward_speed, PlayerState.GetForwardSpeed());
Set(reward->collision_car, PlayerState.GetCollisionIntensityCars());
Set(reward->collision_pedestrian, PlayerState.GetCollisionIntensityPedestrians());
Set(reward->collision_general, PlayerState.GetCollisionIntensityOther());
Set(reward->intersect_other_lane, PlayerState.GetOtherLaneIntersectionFactor());
Set(reward->intersect_offroad, PlayerState.GetOffRoadIntersectionFactor());
{ // Add images.
using CPS = ACarlaPlayerState;
auto &ImageRGB0 = PlayerState.GetImage(CPS::ImageRGB0);
if (ImageRGB0.BitMap.Num() > 0) {
// Do not add any camera if first is invalid, also assume all the images
// have the same size.
reward.image_width = ImageRGB0.SizeX;
reward.image_height = ImageRGB0.SizeY;
Set(reward.image_rgb_0, ImageRGB0.BitMap);
Set(reward.image_rgb_1, PlayerState.GetImage(CPS::ImageRGB1).BitMap);
Set(reward.image_depth_0, PlayerState.GetImage(CPS::ImageDepth0).BitMap);
Set(reward.image_depth_1, PlayerState.GetImage(CPS::ImageDepth1).BitMap);
reward->image_width = ImageRGB0.SizeX;
reward->image_height = ImageRGB0.SizeY;
Set(reward->image_rgb_0, ImageRGB0.BitMap);
Set(reward->image_rgb_1, PlayerState.GetImage(CPS::ImageRGB1).BitMap);
Set(reward->image_depth_0, PlayerState.GetImage(CPS::ImageDepth0).BitMap);
Set(reward->image_depth_1, PlayerState.GetImage(CPS::ImageDepth1).BitMap);
}
}
UE_LOG(LogCarla, Log, CSTEXT("Sending reward"));
Server.sendReward(reward);
UE_LOG(LogCarlaServer, Log, TEXT("Sending reward"));
return Server.sendReward(reward.release());
}
static void TryReadControl(carla::CarlaServer &Server, ACarlaVehicleController &Player)
static bool TryReadControl(carla::CarlaServer &Server, ACarlaVehicleController &Player)
{
float steer;
float throttle;
if (Server.tryReadControl(steer, throttle)) {
Player.SetSteeringInput(steer);
Player.SetThrottleInput(throttle);
UE_LOG(LogCarla, Log, CSTEXT("Read control: steer = %f, throttle = %f"), steer, throttle);
float Steer;
float Throttle;
bool Success = false;
bool bServerNeedsRestart = Server.tryReadControl(Steer, Throttle, Success);
if (Success) {
check(!bServerNeedsRestart);
UE_LOG(LogCarlaServer, Log, TEXT("Read control: { Steer = %f, Throttle = %f }"), Steer, Throttle);
Player.SetSteeringInput(Steer);
Player.SetThrottleInput(Throttle);
}
return bServerNeedsRestart;
}
// =============================================================================
@ -150,18 +165,17 @@ CarlaGameController::CarlaGameController() :
CarlaGameController::~CarlaGameController()
{
UE_LOG(LogCarla, Log, CSTEXT("Destroying CarlaGameController..."));
UE_LOG(LogCarlaServer, Log, TEXT("Destroying CarlaGameController..."));
}
void CarlaGameController::Initialize()
{
if (bServerNeedsRestart) {
UE_LOG(LogCarla, Log, CSTEXT("Initializing CarlaServer"));
Server->init(1u);
if (ReadSceneInit(*Server)) {
UE_LOG(LogCarlaServer, Log, TEXT("Initializing CarlaServer"));
if (Server->init(1u) && ReadSceneInit(*Server)) {
bServerNeedsRestart = false;
} else {
UE_LOG(LogCarla, Warning, CSTEXT("Read scene init failed, server needs restart"));
UE_LOG(LogCarlaServer, Warning, TEXT("Failed to initialize, server needs restart"));
}
}
}
@ -171,7 +185,7 @@ APlayerStart *CarlaGameController::ChoosePlayerStart(
{
uint32 StartIndex;
if (!SendAndReadSceneValues(*Server, AvailableStartSpots, StartIndex)) {
UE_LOG(LogCarla, Warning, CSTEXT("Read scene values failed, server needs restart"));
UE_LOG(LogCarlaServer, Warning, TEXT("Unable to choose start position, server needs restart"));
StartIndex = 0u;
}
return AvailableStartSpots[StartIndex];
@ -185,30 +199,45 @@ void CarlaGameController::RegisterPlayer(AController &NewPlayer)
void CarlaGameController::BeginPlay()
{
Server->sendEndReset();
if (!bServerNeedsRestart) {
UE_LOG(LogCarlaServer, Log, TEXT("Ready to play, notifying client"));
if (!Server->sendEndReset()) {
bServerNeedsRestart = true;
UE_LOG(LogCarlaServer, Warning, TEXT("Unable to send end reset, server needs restart"));
}
}
}
void CarlaGameController::Tick(float DeltaSeconds)
{
check(Player != nullptr);
if (bServerNeedsRestart || Server->needsRestart()) {
RestartLevel(true);
return;
if (bServerNeedsRestart || Server->needsRestart() || !TickServer()) {
UE_LOG(LogCarlaServer, Warning, TEXT("Client disconnected, server needs restart"));
bServerNeedsRestart = true;
RestartLevel();
}
if (Server->newEpisodeRequested()) {
UE_LOG(LogCarla, Log, CSTEXT("New episode requested"));
RestartLevel(false);
return;
}
SendReward(*Server, Player->GetPlayerState());
TryReadControl(*Server, *Player);
}
void CarlaGameController::RestartLevel(bool ServerNeedsRestart)
bool CarlaGameController::TickServer()
{
UE_LOG(LogCarla, Log, CSTEXT("Restarting..."));
bServerNeedsRestart = ServerNeedsRestart;
// Check if the client requested a new episode.
bool bNewEpisodeRequested = false;
if (!Server->newEpisodeRequested(bNewEpisodeRequested)) {
return false;
} else if (bNewEpisodeRequested) {
UE_LOG(LogCarlaServer, Log, TEXT("New episode requested"));
RestartLevel();
return true;
}
// Send reward and try to read control.
return
SendReward(*Server, Player->GetPlayerState()) &&
TryReadControl(*Server, *Player);
}
void CarlaGameController::RestartLevel()
{
UE_LOG(LogCarlaServer, Log, TEXT("Restarting the level..."));
Player->RestartLevel();
}

View File

@ -31,7 +31,10 @@ public:
private:
void RestartLevel(bool ServerNeedsRestart);
/// Return false if the server needs restart.
bool TickServer();
void RestartLevel();
TUniquePtr<carla::CarlaServer> Server;