new protocol with CarlaSettings.ini

This commit is contained in:
Xisco Bosch 2017-05-23 15:03:15 +02:00
parent 2e7d13b1a8
commit f570d041aa
8 changed files with 61 additions and 48 deletions

View File

@ -30,6 +30,7 @@ namespace carla {
CarlaServer::~CarlaServer() {} CarlaServer::~CarlaServer() {}
/*
bool CarlaServer::init(uint32_t levelCount) { bool CarlaServer::init(uint32_t levelCount) {
if (!worldConnected() && !clientConnected() && !serverConnected()) if (!worldConnected() && !clientConnected() && !serverConnected())
return false; return false;
@ -43,6 +44,7 @@ namespace carla {
readed = _pimpl->communication.tryReadSceneInit(scene); readed = _pimpl->communication.tryReadSceneInit(scene);
return true; return true;
} }
*/
bool CarlaServer::tryReadEpisodeStart(uint32_t &startIndex, uint32_t &endIndex, bool &readed) { bool CarlaServer::tryReadEpisodeStart(uint32_t &startIndex, uint32_t &endIndex, bool &readed) {
if (!worldConnected()) if (!worldConnected())
@ -58,10 +60,12 @@ namespace carla {
return true; return true;
} }
bool CarlaServer::newEpisodeRequested(bool &newEpisode) { bool CarlaServer::newEpisodeRequested(std::string &init_file, bool &readed) {
if (!worldConnected()) if (!worldConnected())
return false; return false;
newEpisode = _pimpl->communication.tryReadRequestNewEpisode();
readed = _pimpl->communication.tryReadRequestNewEpisode(init_file);
return true; return true;
} }

View File

@ -78,7 +78,7 @@ namespace carla {
/// Possible world positions to spawn the player. /// Possible world positions to spawn the player.
std::vector<Vector2D> possible_positions; std::vector<Vector2D> possible_positions;
/// Projection matrices of the cameras. /// Projection matrices of the cameras.
std::vector<std::array<float, 16u>> projection_matrices; //std::vector<std::array<float, 16u>> projection_matrices;
}; };
/// Asynchronous TCP server. Uses three ports, one for sending messages /// Asynchronous TCP server. Uses three ports, one for sending messages
@ -102,11 +102,11 @@ namespace carla {
/// Initialize the server. /// Initialize the server.
/// ///
/// @param LevelCount Number of levels available. /// @param LevelCount Number of levels available.
bool init(uint32_t levelCount); //bool init(uint32_t levelCount);
/// Try to read if the client has selected an scene and mode. Return false /// Try to read if the client has selected an scene and mode. Return false
/// if the queue is empty. /// if the queue is empty.
bool tryReadSceneInit(uint32_t &scene, bool &readed); //bool tryReadSceneInit(uint32_t &scene, bool &readed);
/// Try to read if the client has selected an end & start point. Return /// Try to read if the client has selected an end & start point. Return
/// false if the queue is empty. /// false if the queue is empty.
@ -116,7 +116,7 @@ namespace carla {
/// is empty. /// is empty.
bool tryReadControl(float &steer, float &throttle, bool &readed); bool tryReadControl(float &steer, float &throttle, bool &readed);
bool newEpisodeRequested(bool &newEpisode); bool newEpisodeRequested(std::string &init_file, bool &readed);
/// Send values of the current player status. /// Send values of the current player status.
/// ///

View File

@ -81,7 +81,6 @@ namespace server {
static std::unique_ptr<std::string> worldReceiveThread(TCPServer &server, thread::AsyncReadWriteJobQueue<std::string, std::string> &thr) { static std::unique_ptr<std::string> worldReceiveThread(TCPServer &server, thread::AsyncReadWriteJobQueue<std::string, std::string> &thr) {
auto message = std::make_unique<std::string>(); auto message = std::make_unique<std::string>();
bool success = false; bool success = false;
do { do {
if (!thr.getRestart()) { if (!thr.getRestart()) {
@ -201,7 +200,7 @@ namespace server {
return true; return true;
} }
/*
void CarlaCommunication::sendWorld(const uint32_t scenes) { void CarlaCommunication::sendWorld(const uint32_t scenes) {
//ClearThreads //ClearThreads
_worldThread.clear(); _worldThread.clear();
@ -217,7 +216,7 @@ namespace server {
_worldThread.push(std::move(message)); _worldThread.push(std::move(message));
} }
} }
*/
void CarlaCommunication::sendScene(const Scene_Values &values) { void CarlaCommunication::sendScene(const Scene_Values &values) {
Scene scene; Scene scene;
@ -240,6 +239,7 @@ namespace server {
} }
} }
/*
bool CarlaCommunication::tryReadSceneInit(uint32_t &scene) { bool CarlaCommunication::tryReadSceneInit(uint32_t &scene) {
scene = 0u; scene = 0u;
@ -253,6 +253,7 @@ namespace server {
scene = sceneInit.scene(); scene = sceneInit.scene();
return true; return true;
} }
*/
bool CarlaCommunication::tryReadEpisodeStart(uint32_t &start_index, uint32_t &end_index) { bool CarlaCommunication::tryReadEpisodeStart(uint32_t &start_index, uint32_t &end_index) {
start_index = 0; start_index = 0;
@ -270,17 +271,29 @@ namespace server {
return true; return true;
} }
bool CarlaCommunication::tryReadRequestNewEpisode() { bool CarlaCommunication::tryReadRequestNewEpisode(std::string &init_file) {
std::unique_ptr<std::string> request = _worldThread.tryPop(); std::unique_ptr<std::string> request = _worldThread.tryPop();
if (request == nullptr) { return false; } if (request == nullptr) {
std::cout << "NO NEW EPISODE " << std::endl;
return false;
}
RequestNewEpisode reqEpisode; RequestNewEpisode reqEpisode;
if (!reqEpisode.ParseFromString(*request)) { if (!reqEpisode.ParseFromString(*request)) {
_worldThread.undoPop(std::move(request)); _worldThread.undoPop(std::move(request));
return false; return false;
} else { return true; } } else {
init_file = reqEpisode.init_file();
std::cout << init_file << std::endl;
return true;
}
} }
void CarlaCommunication::restartServer() { void CarlaCommunication::restartServer() {

View File

@ -36,13 +36,13 @@ namespace server {
void sendReset(); void sendReset();
void sendWorld(const uint32_t scenes); //void sendWorld(const uint32_t scenes);
bool tryReadSceneInit(uint32_t &scene); //bool tryReadSceneInit(uint32_t &scene);
bool tryReadEpisodeStart(uint32_t &start_index, uint32_t &end_index); bool tryReadEpisodeStart(uint32_t &start_index, uint32_t &end_index);
bool tryReadRequestNewEpisode(); bool tryReadRequestNewEpisode(std::string &init_file);
void restartServer(); void restartServer();

View File

@ -294,7 +294,7 @@ static bool getPNGImages(const std::vector<Image> &images, Reward &rwd) {
void Protocol::LoadScene(Scene &scene, const Scene_Values &values) { void Protocol::LoadScene(Scene &scene, const Scene_Values &values) {
scene.set_number_of_cameras(values.projection_matrices.size()); //scene.set_number_of_cameras(values.projection_matrices.size());
std::string positions_bytes = ""; std::string positions_bytes = "";
@ -318,7 +318,7 @@ static bool getPNGImages(const std::vector<Image> &images, Reward &rwd) {
scene.set_positions(positions_bytes); scene.set_positions(positions_bytes);
std::string matrices; /* std::string matrices;
for (auto i = 0u; i < values.projection_matrices.size(); ++i) { for (auto i = 0u; i < values.projection_matrices.size(); ++i) {
for (auto e = 0u; e < 16u; ++e){ for (auto e = 0u; e < 16u; ++e){
@ -332,14 +332,15 @@ static bool getPNGImages(const std::vector<Image> &images, Reward &rwd) {
} }
} }
scene.set_projection_matrices(matrices); scene.set_projection_matrices(matrices);*/
} }
/*
void Protocol::LoadWorld(World &world, const int modes, const int scenes) { void Protocol::LoadWorld(World &world, const int modes, const int scenes) {
world.set_modes(modes); world.set_modes(modes);
world.set_scenes(scenes); world.set_scenes(scenes);
} }
*/
} }
} }

View File

@ -6,8 +6,7 @@ syntax = "proto2";
message RequestNewEpisode { message RequestNewEpisode {
optional bool request =1; optional bytes init_file = 1;
} }
@ -20,12 +19,6 @@ message EpisodeStart {
} }
message SceneInit {
optional int32 scene =2;
}
message Control { message Control {
@ -38,19 +31,13 @@ message Control {
// Server messages // Server messages
message World {
optional int32 modes = 1; // the number of modes
optional int32 scenes = 2;
}
message Scene { message Scene {
optional int32 number_of_cameras = 1; //optional int32 number_of_cameras = 1;
optional bytes positions = 2; optional bytes positions = 1;
optional bytes projection_matrices = 3; //optional bytes projection_matrices = 3;
} }
@ -63,6 +50,7 @@ message EpisodeReady{
message Reward { message Reward {
optional float player_x = 1; optional float player_x = 1;

View File

@ -84,14 +84,15 @@ namespace thread {
_restart = false; _restart = false;
_readQueue.canWait(true); _readQueue.canWait(true);
while (!_restart && !_done) { while (!_restart && !_done) {
auto value = _readQueue.wait_and_pop();
if (value != nullptr) {
_readJob(*value);
}
if (!_restart){ if (!_restart){
_writeQueue.push(std::move(_writeJob())); _writeQueue.push(std::move(_writeJob()));
} }
auto value = _readQueue.wait_and_pop();
if (value != nullptr) {
_readJob(*value);
}
} }
} }
} }

View File

@ -97,9 +97,14 @@ int main(int argc, char *argv[]) {
carla::CarlaServer server(writePort, readPort, worldPort); carla::CarlaServer server(writePort, readPort, worldPort);
// Let's simulate the game loop. // Let's simulate the game loop.
for (;;) { std::string file;
if (server.init(1u)) { bool read;
for (;;) {
if (server.newEpisodeRequested(file, read) && read) {
std::cout << "Entra" << std::endl;
/*
{ {
uint32_t scene; uint32_t scene;
bool error = false, readed = false; bool error = false, readed = false;
@ -112,7 +117,7 @@ int main(int argc, char *argv[]) {
} }
else std::cout << "Received: scene = " << scene << std::endl; else std::cout << "Received: scene = " << scene << std::endl;
} }
*/
carla::Scene_Values sceneValues; carla::Scene_Values sceneValues;
for (int i = 0; i < 1; ++i){ for (int i = 0; i < 1; ++i){
@ -121,8 +126,8 @@ int main(int argc, char *argv[]) {
sceneValues.possible_positions.push_back({3.0f, 4.0f}); sceneValues.possible_positions.push_back({3.0f, 4.0f});
} }
const std::array<float, 16u> pMatrix = {{ 10.0 }}; //const std::array<float, 16u> pMatrix = {{ 10.0 }};
for (int i = 0; i < 100; ++i) sceneValues.projection_matrices.push_back(pMatrix); //for (int i = 0; i < 100; ++i) sceneValues.projection_matrices.push_back(pMatrix);
if (!server.sendSceneValues(sceneValues)) { if (!server.sendSceneValues(sceneValues)) {
std::cerr << "ERROR while sending SceneValues" << std::endl; std::cerr << "ERROR while sending SceneValues" << std::endl;
@ -149,13 +154,14 @@ int main(int argc, char *argv[]) {
while (true) { while (true) {
float steer, gas; float steer, gas;
bool newEpisode = false; bool readed = false;
if (!server.newEpisodeRequested(newEpisode)){ std::string newConfigFile;
if (!server.newEpisodeRequested(newConfigFile, readed)){
std::cerr << "ERROR while checking for newEpisode request" << std::endl; std::cerr << "ERROR while checking for newEpisode request" << std::endl;
break; break;
} }
if (newEpisode){ if (readed){
std::cout << "-------- NEW EPISODE --------" << std::endl; std::cout << "-------- NEW EPISODE --------" << std::endl;
if (!server.sendSceneValues(sceneValues)){ if (!server.sendSceneValues(sceneValues)){
std::cerr << "ERROR while sending SceneValues" << std::endl; std::cerr << "ERROR while sending SceneValues" << std::endl;