Update game controller to the new protocol

This commit is contained in:
nsubiron 2017-05-29 16:21:01 +01:00
parent 4beab3f54c
commit ded62becdb
2 changed files with 50 additions and 26 deletions

View File

@ -68,21 +68,36 @@ static void Set(carla::Image &cImage, const FCapturedImage &uImage)
// -- Other static methods ----------------------------------------------------- // -- Other static methods -----------------------------------------------------
// ============================================================================= // =============================================================================
// Wait for the scene init to be sent, return false if we need to restart the // Wait for a new episode to be received and update CarlaSettings. Return false
// server. // if we need to restart the server.
/// @todo At the moment we just ignored what it is sent. static bool WaitForNewEpisode(
static bool ReadSceneInit(carla::CarlaServer &Server) carla::CarlaServer &Server,
UCarlaSettings &CarlaSettings)
{ {
uint32 Scene; std::string IniStr;
bool Success = false; bool Success = false;
UE_LOG(LogCarlaServer, Log, TEXT("(tryReadSceneInit) Waiting for client...")); while (!Success) {
/*while (!Success) { if (!Server.newEpisodeRequested(IniStr, Success))
if (!Server.tryReadSceneInit(Scene, Success))
return false; return false;
}*/ }
CarlaSettings.LoadSettingsFromString(IniStr.c_str());
return true; return true;
} }
static bool TryReadNewEpisode(
carla::CarlaServer &Server,
UCarlaSettings &CarlaSettings,
bool &bNewEpisodeRequested)
{
std::string IniStr;
bool Success = false;
bool Result = Server.newEpisodeRequested(IniStr, Success);
if (Result && Success) {
CarlaSettings.LoadSettingsFromString(IniStr.c_str());
}
return Result;
}
// Send the available start spots to the client and wait for them to answer. // Send the available start spots to the client and wait for them to answer.
// Return false if the server needs restart. // Return false if the server needs restart.
static bool SendAndReadSceneValues( static bool SendAndReadSceneValues(
@ -100,7 +115,6 @@ static bool SendAndReadSceneValues(
sceneValues.possible_positions.push_back({Location.X, Location.Y}); sceneValues.possible_positions.push_back({Location.X, Location.Y});
} }
// Send the positions. // 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()); UE_LOG(LogCarlaServer, Log, TEXT("Sending %d available start positions"), sceneValues.possible_positions.size());
if (!Server.sendSceneValues(sceneValues)) if (!Server.sendSceneValues(sceneValues))
return false; return false;
@ -157,16 +171,26 @@ static bool SendReward(
static bool TryReadControl(carla::CarlaServer &Server, ACarlaVehicleController &Player) static bool TryReadControl(carla::CarlaServer &Server, ACarlaVehicleController &Player)
{ {
float Steer; carla::Control_Values Control;
float Throttle;
bool Success = false; bool Success = false;
bool Result = Server.tryReadControl(Steer, Throttle, Success); bool Result = Server.tryReadControl(Control, Success);
if (Result && Success) { if (Result && Success) {
#ifdef CARLA_SERVER_EXTRA_LOG #ifdef CARLA_SERVER_EXTRA_LOG
UE_LOG(LogCarlaServer, Log, TEXT("Read control: { Steer = %f, Throttle = %f }"), Steer, Throttle); UE_LOG(
LogCarlaServer,
Log,
TEXT("Read control: { Steer = %f, Throttle = %f, Brake = %f, Handbrake = %s, Reverse = %s }"),
Control.steer,
Control.gas,
Control.brake,
(Control.hand_brake ? TEXT("True") : TEXT("False")),
(Control.reverse ? TEXT("True") : TEXT("False")));
#endif // CARLA_SERVER_EXTRA_LOG #endif // CARLA_SERVER_EXTRA_LOG
Player.SetSteeringInput(Steer); Player.SetSteeringInput(Control.steer);
Player.SetThrottleInput(Throttle); Player.SetThrottleInput(Control.gas);
Player.SetBrakeInput(Control.brake);
Player.SetHandbrakeInput(Control.hand_brake);
Player.SetReverse(Control.reverse);
} }
return Result; return Result;
} }
@ -179,20 +203,18 @@ CarlaGameController::CarlaGameController(uint32 WorldPort, uint32 WritePort, uin
Server(MakeUnique<carla::CarlaServer>(WritePort, ReadPort, WorldPort)), Server(MakeUnique<carla::CarlaServer>(WritePort, ReadPort, WorldPort)),
Player(nullptr) {} Player(nullptr) {}
CarlaGameController::~CarlaGameController() CarlaGameController::~CarlaGameController() {}
{
UE_LOG(LogCarlaServer, Log, TEXT("Destroying CarlaGameController..."));
}
void CarlaGameController::Initialize(UCarlaSettings &CarlaSettings) void CarlaGameController::Initialize(UCarlaSettings &InCarlaSettings)
{ {
CarlaSettings = &InCarlaSettings;
if (bServerNeedsRestart) { if (bServerNeedsRestart) {
UE_LOG(LogCarlaServer, Log, TEXT("Initializing CarlaServer")); UE_LOG(LogCarlaServer, Log, TEXT("Initializing CarlaServer"));
/*if (Server->init(1u) && ReadSceneInit(*Server)) { if (WaitForNewEpisode(*Server, *CarlaSettings)) {
bServerNeedsRestart = false; bServerNeedsRestart = false;
} else { } else {
UE_LOG(LogCarlaServer, Warning, TEXT("Failed to initialize, server needs restart")); UE_LOG(LogCarlaServer, Warning, TEXT("Failed to initialize, server needs restart"));
}*/ }
} }
} }
@ -238,13 +260,13 @@ bool CarlaGameController::TickServer()
{ {
// Check if the client requested a new episode. // Check if the client requested a new episode.
bool bNewEpisodeRequested = false; bool bNewEpisodeRequested = false;
/*if (!Server->newEpisodeRequested(bNewEpisodeRequested)) { if (!TryReadNewEpisode(*Server, *CarlaSettings, bNewEpisodeRequested)) {
return false; return false;
} else if (bNewEpisodeRequested) { } else if (bNewEpisodeRequested) {
UE_LOG(LogCarlaServer, Log, TEXT("New episode requested")); UE_LOG(LogCarlaServer, Log, TEXT("New episode requested"));
RestartLevel(); RestartLevel();
return true; return true;
}*/ }
// Send reward and try to read control. // Send reward and try to read control.
return return

View File

@ -38,7 +38,9 @@ private:
TUniquePtr<carla::CarlaServer> Server; TUniquePtr<carla::CarlaServer> Server;
ACarlaVehicleController *Player; ACarlaVehicleController *Player = nullptr;
UCarlaSettings *CarlaSettings = nullptr;
bool bServerNeedsRestart = true; bool bServerNeedsRestart = true;
}; };