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

View File

@ -78,7 +78,7 @@ namespace carla {
/// Possible world positions to spawn the player.
std::vector<Vector2D> possible_positions;
/// 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
@ -102,11 +102,11 @@ namespace carla {
/// Initialize the server.
///
/// @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
/// 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
/// false if the queue is empty.
@ -116,7 +116,7 @@ namespace carla {
/// is empty.
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.
///

View File

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

View File

@ -36,13 +36,13 @@ namespace server {
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 tryReadRequestNewEpisode();
bool tryReadRequestNewEpisode(std::string &init_file);
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) {
scene.set_number_of_cameras(values.projection_matrices.size());
//scene.set_number_of_cameras(values.projection_matrices.size());
std::string positions_bytes = "";
@ -318,7 +318,7 @@ static bool getPNGImages(const std::vector<Image> &images, Reward &rwd) {
scene.set_positions(positions_bytes);
std::string matrices;
/* std::string matrices;
for (auto i = 0u; i < values.projection_matrices.size(); ++i) {
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) {
world.set_modes(modes);
world.set_scenes(scenes);
}
*/
}
}

View File

@ -6,8 +6,7 @@ syntax = "proto2";
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 {
@ -38,19 +31,13 @@ message Control {
// Server messages
message World {
optional int32 modes = 1; // the number of modes
optional int32 scenes = 2;
}
message Scene {
optional int32 number_of_cameras = 1;
optional bytes positions = 2;
optional bytes projection_matrices = 3;
//optional int32 number_of_cameras = 1;
optional bytes positions = 1;
//optional bytes projection_matrices = 3;
}
@ -63,6 +50,7 @@ message EpisodeReady{
message Reward {
optional float player_x = 1;

View File

@ -84,14 +84,15 @@ namespace thread {
_restart = false;
_readQueue.canWait(true);
while (!_restart && !_done) {
auto value = _readQueue.wait_and_pop();
if (value != nullptr) {
_readJob(*value);
}
if (!_restart){
_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);
// Let's simulate the game loop.
for (;;) {
if (server.init(1u)) {
std::string file;
bool read;
for (;;) {
if (server.newEpisodeRequested(file, read) && read) {
std::cout << "Entra" << std::endl;
/*
{
uint32_t scene;
bool error = false, readed = false;
@ -112,7 +117,7 @@ int main(int argc, char *argv[]) {
}
else std::cout << "Received: scene = " << scene << std::endl;
}
*/
carla::Scene_Values sceneValues;
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});
}
const std::array<float, 16u> pMatrix = {{ 10.0 }};
for (int i = 0; i < 100; ++i) sceneValues.projection_matrices.push_back(pMatrix);
//const std::array<float, 16u> pMatrix = {{ 10.0 }};
//for (int i = 0; i < 100; ++i) sceneValues.projection_matrices.push_back(pMatrix);
if (!server.sendSceneValues(sceneValues)) {
std::cerr << "ERROR while sending SceneValues" << std::endl;
@ -149,13 +154,14 @@ int main(int argc, char *argv[]) {
while (true) {
float steer, gas;
bool newEpisode = false;
if (!server.newEpisodeRequested(newEpisode)){
bool readed = false;
std::string newConfigFile;
if (!server.newEpisodeRequested(newConfigFile, readed)){
std::cerr << "ERROR while checking for newEpisode request" << std::endl;
break;
}
if (newEpisode){
if (readed){
std::cout << "-------- NEW EPISODE --------" << std::endl;
if (!server.sendSceneValues(sceneValues)){
std::cerr << "ERROR while sending SceneValues" << std::endl;