Fix compile errors in Linux
This commit is contained in:
parent
6eb9727cb8
commit
02baff814e
|
@ -1,6 +1,10 @@
|
||||||
cmake_minimum_required (VERSION 2.6)
|
cmake_minimum_required (VERSION 2.6)
|
||||||
project (CarlaServer)
|
project (CarlaServer)
|
||||||
|
|
||||||
|
SET(CMAKE_CXX_COMPILER clang++)
|
||||||
|
SET(CMAKE_CXX_STANDARD 14)
|
||||||
|
SET(CMAKE_CXX_STANDARD_REQUIRED 14)
|
||||||
|
|
||||||
# Boost configuration
|
# Boost configuration
|
||||||
SET(Boost_USE_STATIC_LIBS ON)
|
SET(Boost_USE_STATIC_LIBS ON)
|
||||||
find_package(Boost REQUIRED system date_time regex)
|
find_package(Boost REQUIRED system date_time regex)
|
||||||
|
|
|
@ -3,6 +3,9 @@ BUILD_FOLDER=build
|
||||||
vsproject:
|
vsproject:
|
||||||
cmake -H. -B$(BUILD_FOLDER) -G "Visual Studio 14 2015 Win64"
|
cmake -H. -B$(BUILD_FOLDER) -G "Visual Studio 14 2015 Win64"
|
||||||
|
|
||||||
|
stproject:
|
||||||
|
cmake -H. -B$(BUILD_FOLDER) -G "Sublime Text 2 - Ninja"
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -Rf build CMakeFiles
|
rm -Rf build CMakeFiles
|
||||||
|
|
||||||
|
|
|
@ -3,140 +3,144 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace carla {
|
namespace carla {
|
||||||
namespace server {
|
namespace server {
|
||||||
// -- Static methods ---------------------------------------------------------
|
// -- Static methods ---------------------------------------------------------
|
||||||
|
|
||||||
template <typename ERROR_CODE>
|
template <typename ERROR_CODE>
|
||||||
static void logTCPError(const std::string &text, const ERROR_CODE &errorCode) {
|
static void logTCPError(const std::string &text, const ERROR_CODE &errorCode) {
|
||||||
std::cerr << "CarlaConnection - TCP Server: " << text << ": " << errorCode.message() << std::endl;
|
std::cerr << "CarlaConnection - TCP Server: " << text << ": " << errorCode.message() << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// This is the thread that sends a string over the TCP socket.
|
// This is the thread that sends a string over the TCP socket.
|
||||||
static void serverWorkerThread(TCPServer &server, std::string &message) {
|
static void serverWorkerThread(TCPServer &server, const std::string &message) {
|
||||||
TCPServer::error_code error;
|
TCPServer::error_code error;
|
||||||
//message = message.size.c_ + message;
|
//message = message.size.c_ + message;
|
||||||
|
|
||||||
//if (!server.Connected()) server.AcceptSocket();
|
//if (!server.Connected()) server.AcceptSocket();
|
||||||
|
|
||||||
server.writeString(message, error);
|
|
||||||
|
|
||||||
if (error)
|
server.writeString(message, error);
|
||||||
logTCPError("Failed to send", error);
|
|
||||||
|
|
||||||
}
|
if (error)
|
||||||
|
logTCPError("Failed to send", error);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//TODO:
|
//TODO:
|
||||||
// Sortida amb google protocol
|
// Sortida amb google protocol
|
||||||
// This is the thread that listens for string over the TCP socket.
|
// This is the thread that listens for string over the TCP socket.
|
||||||
static std::string clientWorkerThread(TCPServer &server) {
|
static std::string clientWorkerThread(TCPServer &server) {
|
||||||
|
|
||||||
//if (!server.Connected()) server.AcceptSocket();
|
//if (!server.Connected()) server.AcceptSocket();
|
||||||
|
|
||||||
std::string message;
|
std::string message;
|
||||||
TCPServer::error_code error;
|
TCPServer::error_code error;
|
||||||
|
|
||||||
server.readString(message, error);
|
server.readString(message, error);
|
||||||
if (error && (error != boost::asio::error::eof)) { // eof is expected.
|
if (error && (error != boost::asio::error::eof)) { // eof is expected.
|
||||||
logTCPError("Failed to read", error);
|
logTCPError("Failed to read", error);
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
return message;
|
return message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// This is the thread that listens & sends a string over the TCP world socket.
|
// This is the thread that listens & sends a string over the TCP world socket.
|
||||||
static std::string worldReceiveThread(TCPServer &server) {
|
static std::string worldReceiveThread(TCPServer &server) {
|
||||||
std::string message;
|
std::string message;
|
||||||
TCPServer::error_code error;
|
TCPServer::error_code error;
|
||||||
|
|
||||||
//if (!server.Connected()) server.AcceptSocket();
|
//if (!server.Connected()) server.AcceptSocket();
|
||||||
|
|
||||||
server.readString(message, error);
|
server.readString(message, error);
|
||||||
if (error && (error != boost::asio::error::eof)) { // eof is expected.
|
if (error && (error != boost::asio::error::eof)) { // eof is expected.
|
||||||
logTCPError("Failed to read world", error);
|
logTCPError("Failed to read world", error);
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
return message;
|
return message;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void worldSendThread(TCPServer &server, std::string &message) {
|
static void worldSendThread(TCPServer &server, const std::string &message) {
|
||||||
TCPServer::error_code error;
|
TCPServer::error_code error;
|
||||||
//message = message.size + message;
|
//message = message.size + message;
|
||||||
|
|
||||||
//if (!server.Connected()) server.AcceptSocket();
|
//if (!server.Connected()) server.AcceptSocket();
|
||||||
|
|
||||||
server.writeString(message, error);
|
server.writeString(message, error);
|
||||||
if (error)
|
if (error)
|
||||||
logTCPError("Failed to send world", error);
|
logTCPError("Failed to send world", error);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void Connect(TCPServer &server) {
|
static void Connect(TCPServer &server) {
|
||||||
if (!server.Connected()) server.AcceptSocket();
|
if (!server.Connected()) server.AcceptSocket();
|
||||||
}
|
}
|
||||||
|
|
||||||
CarlaCommunication::CarlaCommunication(int worldPort, int writePort, int readPort) :
|
CarlaCommunication::CarlaCommunication(int worldPort, int writePort, int readPort) :
|
||||||
_world(worldPort),
|
_world(worldPort),
|
||||||
_server(writePort),
|
_server(writePort),
|
||||||
_client(readPort),
|
_client(readPort),
|
||||||
_worldThread([this]() {return worldReceiveThread(this->_world); },
|
_worldThread{
|
||||||
[this](std::string &msg) { worldSendThread(this->_world, msg); },
|
[this]() { return worldReceiveThread(this->_world); },
|
||||||
[this]() {Connect(this->_world); }),
|
[this](const std::string &msg) { worldSendThread(this->_world, msg); },
|
||||||
_serverThread([this](std::string &str) { serverWorkerThread(this->_server, str); },
|
[this]() { Connect(this->_world); }},
|
||||||
[this]() {Connect(this->_server); }),
|
_serverThread{
|
||||||
_clientThread([this]() { return clientWorkerThread(this->_client); },
|
[this](const std::string &str) { serverWorkerThread(this->_server, str); },
|
||||||
[this]() {Connect(this->_client); })
|
[this]() { Connect(this->_server); }},
|
||||||
{
|
_clientThread{
|
||||||
|
[this]() { return clientWorkerThread(this->_client); },
|
||||||
|
[this]() { Connect(this->_client); }}
|
||||||
|
{
|
||||||
|
|
||||||
/*std::cout << "WorldPort: " << worldPort << std::endl;
|
/*std::cout << "WorldPort: " << worldPort << std::endl;
|
||||||
std::cout << "writePort: " << writePort << std::endl;
|
std::cout << "writePort: " << writePort << std::endl;
|
||||||
std::cout << "readPort: " << readPort << std::endl;*/
|
std::cout << "readPort: " << readPort << std::endl;*/
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
void CarlaCommunication::sendReward(const Reward &reward) {
|
}
|
||||||
std::string message;
|
|
||||||
bool error = !reward.SerializeToString(&message);
|
|
||||||
if (!error) {
|
|
||||||
_serverThread.push(message);
|
|
||||||
std::cout << "Send Reward" << std::endl;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CarlaCommunication::tryReadControl(std::string &control) {
|
void CarlaCommunication::sendReward(const Reward &reward) {
|
||||||
return _clientThread.tryPop(control);
|
std::string message;
|
||||||
}
|
bool error = !reward.SerializeToString(&message);
|
||||||
|
if (!error) {
|
||||||
|
_serverThread.push(message);
|
||||||
|
std::cout << "Send Reward" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void CarlaCommunication::sendWorld(const World &world) {
|
bool CarlaCommunication::tryReadControl(std::string &control) {
|
||||||
std::string message;
|
return _clientThread.tryPop(control);
|
||||||
bool error = !world.SerializeToString(&message);
|
}
|
||||||
_worldThread.push(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CarlaCommunication::sendScene(const Scene &scene) {
|
void CarlaCommunication::sendWorld(const World &world) {
|
||||||
std::string message;
|
std::string message;
|
||||||
bool error = !scene.SerializeToString(&message);
|
bool error = !world.SerializeToString(&message);
|
||||||
_worldThread.push(message);
|
_worldThread.push(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CarlaCommunication::sendReset(const EpisodeReady &ready) {
|
void CarlaCommunication::sendScene(const Scene &scene) {
|
||||||
std::string message;
|
std::string message;
|
||||||
bool error = !ready.SerializeToString(&message);
|
bool error = !scene.SerializeToString(&message);
|
||||||
if (!error) {
|
_worldThread.push(message);
|
||||||
//std::cout << "Send End Reset" << std::endl;
|
}
|
||||||
_worldThread.push(message);
|
|
||||||
}
|
void CarlaCommunication::sendReset(const EpisodeReady &ready) {
|
||||||
else {
|
std::string message;
|
||||||
std::cout << " >> SEND RESET ERROR <<" << std::endl;
|
bool error = !ready.SerializeToString(&message);
|
||||||
}
|
if (!error) {
|
||||||
}
|
//std::cout << "Send End Reset" << std::endl;
|
||||||
|
_worldThread.push(message);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
std::cout << " >> SEND RESET ERROR <<" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CarlaCommunication::tryReadWorldInfo(std::string &info) {
|
||||||
|
return _worldThread.tryPop(info);
|
||||||
|
}
|
||||||
|
|
||||||
bool CarlaCommunication::tryReadWorldInfo(std::string &info) {
|
}
|
||||||
return _worldThread.tryPop(info);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,43 +3,44 @@
|
||||||
#include "carla/server/Protocol.h"
|
#include "carla/server/Protocol.h"
|
||||||
#include "carla/server/TCPServer.h"
|
#include "carla/server/TCPServer.h"
|
||||||
|
|
||||||
|
#include "carla/thread/AsyncReadWriteJobQueue.h"
|
||||||
#include "carla/thread/AsyncReaderJobQueue.h"
|
#include "carla/thread/AsyncReaderJobQueue.h"
|
||||||
#include "carla/thread/AsyncWriterJobQueue.h"
|
#include "carla/thread/AsyncWriterJobQueue.h"
|
||||||
#include "carla/thread/AsyncReadWriteJobQueue.h"
|
|
||||||
|
|
||||||
namespace carla {
|
namespace carla {
|
||||||
namespace server {
|
namespace server {
|
||||||
class CarlaCommunication : private NonCopyable {
|
|
||||||
public:
|
|
||||||
|
|
||||||
explicit CarlaCommunication(int worldPort, int writePort, int readPort);
|
class CarlaCommunication : private NonCopyable {
|
||||||
|
public:
|
||||||
|
|
||||||
void sendReward(const Reward &reward);
|
explicit CarlaCommunication(int worldPort, int writePort, int readPort);
|
||||||
|
|
||||||
bool tryReadControl(std::string &control);
|
void sendReward(const Reward &reward);
|
||||||
|
|
||||||
void sendScene(const Scene &scene);
|
bool tryReadControl(std::string &control);
|
||||||
|
|
||||||
void sendReset(const EpisodeReady &ready);
|
void sendScene(const Scene &scene);
|
||||||
|
|
||||||
void sendWorld(const World &world);
|
void sendReset(const EpisodeReady &ready);
|
||||||
|
|
||||||
bool tryReadWorldInfo(std::string &info);
|
void sendWorld(const World &world);
|
||||||
|
|
||||||
private:
|
bool tryReadWorldInfo(std::string &info);
|
||||||
|
|
||||||
TCPServer _server;
|
private:
|
||||||
|
|
||||||
TCPServer _client;
|
TCPServer _server;
|
||||||
|
|
||||||
TCPServer _world;
|
TCPServer _client;
|
||||||
|
|
||||||
thread::AsyncReaderJobQueue<std::string> _serverThread;
|
TCPServer _world;
|
||||||
|
|
||||||
thread::AsyncWriterJobQueue<std::string> _clientThread;
|
thread::AsyncReaderJobQueue<std::string> _serverThread;
|
||||||
|
|
||||||
thread::AsyncReadWriteJobQueue<std::string, std::string> _worldThread;
|
thread::AsyncWriterJobQueue<std::string> _clientThread;
|
||||||
|
|
||||||
};
|
thread::AsyncReadWriteJobQueue<std::string, std::string> _worldThread;
|
||||||
}
|
};
|
||||||
}
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,15 +7,14 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace carla {
|
namespace carla {
|
||||||
|
|
||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
// -- CarlaServer ------------------------------------------------------------
|
// -- CarlaServer ------------------------------------------------------------
|
||||||
|
|
||||||
CarlaServer::CarlaServer(int worldPort, int writePort, int readPort, int modesCount, int scenesCount) :
|
CarlaServer::CarlaServer(int worldPort, int writePort, int readPort, int modesCount, int scenesCount) :
|
||||||
_communication(std::make_unique<CarlaCommunication>(worldPort, writePort, readPort)), _proto(std::make_unique<Protocol>(this)),
|
_communication(std::make_unique<CarlaCommunication>(worldPort, writePort, readPort)), _proto(std::make_unique<Protocol>(this)),
|
||||||
_modes(modesCount),
|
_modes(modesCount),
|
||||||
_scenes(scenesCount){
|
_scenes(scenesCount){
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,120 +22,120 @@ namespace server {
|
||||||
}
|
}
|
||||||
|
|
||||||
void CarlaServer::sendReward(const Reward_Values &values) {
|
void CarlaServer::sendReward(const Reward_Values &values) {
|
||||||
Reward reward = _proto->LoadReward(values);
|
Reward reward = _proto->LoadReward(values);
|
||||||
_communication->sendReward(reward);
|
_communication->sendReward(reward);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CarlaServer::sendSceneValues(const Scene_Values &values) {
|
void CarlaServer::sendSceneValues(const Scene_Values &values) {
|
||||||
Scene scene = _proto->LoadScene(values);
|
Scene scene = _proto->LoadScene(values);
|
||||||
_communication->sendScene(scene);
|
_communication->sendScene(scene);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CarlaServer::sendEndReset() {
|
void CarlaServer::sendEndReset() {
|
||||||
EpisodeReady eReady;
|
EpisodeReady eReady;
|
||||||
eReady.set_ready(true);
|
eReady.set_ready(true);
|
||||||
_communication->sendReset(eReady);
|
_communication->sendReset(eReady);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CarlaServer::sendWorld() {
|
void CarlaServer::sendWorld() {
|
||||||
World world = _proto->LoadWorld();
|
World world = _proto->LoadWorld();
|
||||||
_communication->sendWorld(world);
|
_communication->sendWorld(world);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CarlaServer::tryReadControl(float &steer, float &gas) {
|
bool CarlaServer::tryReadControl(float &steer, float &gas) {
|
||||||
std::string controlMessage;
|
std::string controlMessage;
|
||||||
bool success = _communication->tryReadControl(controlMessage);
|
bool success = _communication->tryReadControl(controlMessage);
|
||||||
Control control;
|
Control control;
|
||||||
if (success) {
|
if (success) {
|
||||||
success &= control.ParseFromString(controlMessage);
|
success &= control.ParseFromString(controlMessage);
|
||||||
}
|
}
|
||||||
steer = control.steer();
|
steer = control.steer();
|
||||||
gas = control.gas();
|
gas = control.gas();
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
steer = 0.0f;
|
steer = 0.0f;
|
||||||
gas = 0.0f;
|
gas = 0.0f;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
steer = control.steer();
|
steer = control.steer();
|
||||||
gas = control.gas();
|
gas = control.gas();
|
||||||
//std::cout << "Steer: " << steer << " Gas: " << gas << std::endl;
|
//std::cout << "Steer: " << steer << " Gas: " << gas << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CarlaServer::tryReadSceneInit(int &mode, int &scene) {
|
bool CarlaServer::tryReadSceneInit(int &mode, int &scene) {
|
||||||
std::string initMessage;
|
std::string initMessage;
|
||||||
bool success = _communication->tryReadWorldInfo(initMessage);
|
bool success = _communication->tryReadWorldInfo(initMessage);
|
||||||
SceneInit sceneInit;
|
SceneInit sceneInit;
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
success &= sceneInit.ParseFromString(initMessage);
|
success &= sceneInit.ParseFromString(initMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!success) {
|
|
||||||
mode = -1;
|
|
||||||
scene = -1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
mode = sceneInit.mode();
|
|
||||||
scene = sceneInit.scene();
|
|
||||||
//std::cout << "Mode: " << mode << " Scene: " << scene << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
return success;
|
if (!success) {
|
||||||
|
mode = -1;
|
||||||
|
scene = -1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
mode = sceneInit.mode();
|
||||||
|
scene = sceneInit.scene();
|
||||||
|
//std::cout << "Mode: " << mode << " Scene: " << scene << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CarlaServer::tryReadEpisodeStart(float &start_index, float &end_index) {
|
bool CarlaServer::tryReadEpisodeStart(float &start_index, float &end_index) {
|
||||||
std::string startData;
|
std::string startData;
|
||||||
bool success = _communication->tryReadWorldInfo(startData);
|
bool success = _communication->tryReadWorldInfo(startData);
|
||||||
EpisodeStart episodeStart;
|
EpisodeStart episodeStart;
|
||||||
success &= episodeStart.ParseFromString(startData);
|
success &= episodeStart.ParseFromString(startData);
|
||||||
|
|
||||||
if (!success) {
|
if (!success) {
|
||||||
start_index = -1.0f;
|
start_index = -1.0f;
|
||||||
end_index = -1.0f;
|
end_index = -1.0f;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
start_index = episodeStart.start_index();
|
start_index = episodeStart.start_index();
|
||||||
end_index = episodeStart.end_index();
|
end_index = episodeStart.end_index();
|
||||||
//std::cout << "Start: " << start_index << " End: " << end_index << std::endl;
|
//std::cout << "Start: " << start_index << " End: " << end_index << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CarlaServer::setMode(Mode mode) {
|
void CarlaServer::setMode(Mode mode) {
|
||||||
_mode = mode;
|
_mode = mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
Mode CarlaServer::GetMode() const {
|
Mode CarlaServer::GetMode() const {
|
||||||
return _mode;
|
return _mode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CarlaServer::SetScene(int scene) {
|
void CarlaServer::SetScene(int scene) {
|
||||||
_scene = scene;
|
_scene = scene;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CarlaServer::GetScene() const {
|
int CarlaServer::GetScene() const {
|
||||||
return _scene;
|
return _scene;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CarlaServer::GetModesCount() const {
|
int CarlaServer::GetModesCount() const {
|
||||||
return _modes;
|
return _modes;
|
||||||
}
|
}
|
||||||
|
|
||||||
int CarlaServer::GetScenesCount() const {
|
int CarlaServer::GetScenesCount() const {
|
||||||
return _scenes;
|
return _scenes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CarlaServer::SetReset(bool reset) {
|
void CarlaServer::SetReset(bool reset) {
|
||||||
_reset = reset;
|
_reset = reset;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CarlaServer::Reset() const {
|
bool CarlaServer::Reset() const {
|
||||||
return _reset;
|
return _reset;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace server
|
} // namespace server
|
||||||
|
|
|
@ -13,41 +13,41 @@
|
||||||
namespace carla {
|
namespace carla {
|
||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
|
struct Color {
|
||||||
|
char red;
|
||||||
|
char green;
|
||||||
|
char blue;
|
||||||
|
//char alpha;
|
||||||
|
};
|
||||||
|
|
||||||
struct Color {
|
struct Position {
|
||||||
char red;
|
float x, y;
|
||||||
char green;
|
};
|
||||||
char blue;
|
|
||||||
//char alpha;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Position {
|
struct Reward_Values {
|
||||||
float x, y;
|
float player_x, player_y;
|
||||||
};
|
float speed;
|
||||||
|
float collision_gen, collision_ped, collision_car;
|
||||||
|
float intersect;
|
||||||
|
float inertia_x, inertia_y, inertia_z;
|
||||||
|
std::int32_t timestamp;
|
||||||
|
float ori_x = 0, ori_y, ori_z;
|
||||||
|
std::vector<Color> img;
|
||||||
|
std::vector<Color> img_2;
|
||||||
|
std::vector<Color> depth_1;
|
||||||
|
std::vector<Color> depth_2;
|
||||||
|
};
|
||||||
|
|
||||||
struct Reward_Values {
|
struct Scene_Values {
|
||||||
float player_x, player_y;
|
std::vector<Position> _possible_Positions;
|
||||||
float speed;
|
std::vector<const float *> _projection_Matrix;
|
||||||
float collision_gen, collision_ped, collision_car;
|
};
|
||||||
float intersect;
|
|
||||||
float inertia_x, inertia_y, inertia_z;
|
|
||||||
std::int32_t timestamp;
|
|
||||||
float ori_x = 0, ori_y, ori_z;
|
|
||||||
std::vector<Color> img;
|
|
||||||
std::vector<Color> img_2;
|
|
||||||
std::vector<Color> depth_1;
|
|
||||||
std::vector<Color> depth_2;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Scene_Values {
|
enum Mode {
|
||||||
std::vector<Position> _possible_Positions;
|
MONO = 0,
|
||||||
std::vector<const float*> _projection_Matrix;
|
STEREO = 1
|
||||||
};
|
};
|
||||||
|
|
||||||
enum Mode {
|
|
||||||
MONO = 0,
|
|
||||||
STEREO = 1
|
|
||||||
};
|
|
||||||
/// Asynchronous TCP server. Uses two ports, one for sending messages (write)
|
/// Asynchronous TCP server. Uses two ports, one for sending messages (write)
|
||||||
/// and one for receiving messages (read).
|
/// and one for receiving messages (read).
|
||||||
///
|
///
|
||||||
|
@ -67,55 +67,54 @@ namespace server {
|
||||||
///// Send values of the current player status
|
///// Send values of the current player status
|
||||||
void sendReward( const Reward_Values &values);
|
void sendReward( const Reward_Values &values);
|
||||||
|
|
||||||
//// Send the values of the generated scene
|
//// Send the values of the generated scene
|
||||||
void sendSceneValues( const Scene_Values &values);
|
void sendSceneValues( const Scene_Values &values);
|
||||||
|
|
||||||
//// Send a signal to the client to notify that the car is ready
|
//// Send a signal to the client to notify that the car is ready
|
||||||
void sendEndReset();
|
void sendEndReset();
|
||||||
|
|
||||||
void sendWorld();
|
void sendWorld();
|
||||||
|
|
||||||
///// Try to read the response of the client. Return false if the queue
|
///// Try to read the response of the client. Return false if the queue
|
||||||
///// is empty.
|
///// is empty.
|
||||||
bool tryReadControl(float &steer, float &gas);
|
bool tryReadControl(float &steer, float &gas);
|
||||||
|
|
||||||
////Try to read if the client has selected an scene and mode. Return false if the queue is empty
|
|
||||||
bool tryReadSceneInit(int &mode, int &scene);
|
|
||||||
|
|
||||||
////Try to read if the client has selected an end & start point. Return false if the queue is empty
|
////Try to read if the client has selected an scene and mode. Return false if the queue is empty
|
||||||
bool tryReadEpisodeStart(float &start_index, float &end_index);
|
bool tryReadSceneInit(int &mode, int &scene);
|
||||||
|
|
||||||
|
////Try to read if the client has selected an end & start point. Return false if the queue is empty
|
||||||
|
bool tryReadEpisodeStart(float &start_index, float &end_index);
|
||||||
|
|
||||||
|
int GetModesCount() const;
|
||||||
|
|
||||||
int GetModesCount() const;
|
int GetScenesCount() const;
|
||||||
int GetScenesCount() const;
|
|
||||||
|
|
||||||
void setMode(Mode mode);
|
void setMode(Mode mode);
|
||||||
Mode GetMode() const;
|
|
||||||
|
|
||||||
void SetScene(int scene);
|
Mode GetMode() const;
|
||||||
int GetScene() const;
|
|
||||||
|
|
||||||
void SetReset(bool reset);
|
void SetScene(int scene);
|
||||||
bool Reset() const;
|
|
||||||
|
|
||||||
|
int GetScene() const;
|
||||||
|
|
||||||
|
void SetReset(bool reset);
|
||||||
|
|
||||||
|
bool Reset() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
//std::mutex _mutex;
|
||||||
//std::mutex _mutex;
|
|
||||||
|
|
||||||
std::atomic<Mode> _mode = MONO;
|
std::atomic<Mode> _mode {MONO};
|
||||||
std::atomic_int _scene;
|
std::atomic_int _scene;
|
||||||
std::atomic_bool _reset;
|
std::atomic_bool _reset;
|
||||||
|
|
||||||
const int _modes;
|
const int _modes;
|
||||||
const int _scenes;
|
const int _scenes;
|
||||||
|
|
||||||
const std::unique_ptr<CarlaCommunication> _communication;
|
const std::unique_ptr<CarlaCommunication> _communication;
|
||||||
|
|
||||||
const std::unique_ptr<Protocol> _proto;
|
|
||||||
|
|
||||||
|
const std::unique_ptr<Protocol> _proto;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace server
|
} // namespace server
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
#include "Carla.h"
|
|
||||||
#include <boost/asio.hpp>
|
|
||||||
#include <boost\bind.hpp>
|
|
||||||
|
|
||||||
namespace carla {
|
|
||||||
namespace server {
|
|
||||||
|
|
||||||
class TCPConnection : public boost::enable_shared_from_this<TCPConnection>
|
|
||||||
{
|
|
||||||
|
|
||||||
public:
|
|
||||||
|
|
||||||
typedef boost::shared_ptr<TCPConnection> pointer;
|
|
||||||
|
|
||||||
static pointer create(boost::asio::io_service &io_service) {
|
|
||||||
return pointer(new TCPConnection(io_service));
|
|
||||||
}
|
|
||||||
|
|
||||||
boost::asio::ip::tcp::socket& socket() {
|
|
||||||
return socket_;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Send(const std::string &message) {
|
|
||||||
message_ = message;
|
|
||||||
boost::asio::async_write(socket_, boost::asio::buffer(message_),
|
|
||||||
boost::bind(&TCPConnection::handle_write, shared_from_this()));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
TCPConnection(boost::asio::io_service& io_service)
|
|
||||||
: socket_(io_service)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void handle_write(const boost::system::error_code& /*error*/,
|
|
||||||
size_t /*bytes_transferred*/)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
std::string message_;
|
|
||||||
boost::asio::ip::tcp::socket socket_;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -13,21 +13,24 @@ namespace thread {
|
||||||
|
|
||||||
/// Executes the given job asynchronously. Every item that the job returns is
|
/// Executes the given job asynchronously. Every item that the job returns is
|
||||||
/// added to the queue.
|
/// added to the queue.
|
||||||
template <typename W, typename R>
|
template<typename W, typename R>
|
||||||
class CARLA_API AsyncReadWriteJobQueue {
|
class CARLA_API AsyncReadWriteJobQueue {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
using WritingJob = std::function<W()>;
|
using WritingJob = std::function<W()>;
|
||||||
using ReadingJob = std::function<void(R)>;
|
using ReadingJob = std::function<void(R)>;
|
||||||
using ConnectJob = std::function<void()>;
|
using ConnectJob = std::function<void()>;
|
||||||
|
|
||||||
explicit AsyncReadWriteJobQueue(WritingJob &&writingJob, ReadingJob &&readingJob, ConnectJob &&connectJob) :
|
explicit AsyncReadWriteJobQueue(
|
||||||
_done(false),
|
WritingJob && writingJob,
|
||||||
_writeJob(std::move(writingJob)),
|
ReadingJob && readingJob,
|
||||||
_readJob(std::move(readingJob)),
|
ConnectJob && connectJob) :
|
||||||
_connectJob(std::move(connectJob)),
|
_done(false),
|
||||||
_writeQueue(),
|
_writeJob(std::move(writingJob)),
|
||||||
_thread(new std::thread(&AsyncReadWriteJobQueue::workerThread, this)) {}
|
_readJob(std::move(readingJob)),
|
||||||
|
_connectJob(std::move(connectJob)),
|
||||||
|
_writeQueue(),
|
||||||
|
_thread(new std::thread(&AsyncReadWriteJobQueue::workerThread, this)) {}
|
||||||
|
|
||||||
~AsyncReadWriteJobQueue() {
|
~AsyncReadWriteJobQueue() {
|
||||||
_done = true;
|
_done = true;
|
||||||
|
@ -37,30 +40,30 @@ namespace thread {
|
||||||
return _writeQueue.try_pop(value);
|
return _writeQueue.try_pop(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
void push(R item) {
|
void push(R item) {
|
||||||
_readQueue.push(item);
|
_readQueue.push(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void workerThread() {
|
void workerThread() {
|
||||||
_connectJob();
|
_connectJob();
|
||||||
while (!_done) {
|
while (!_done) {
|
||||||
R value;
|
R value;
|
||||||
_readQueue.wait_and_pop(value);
|
_readQueue.wait_and_pop(value);
|
||||||
_readJob(value);
|
_readJob(value);
|
||||||
_writeQueue.push(_writeJob());
|
_writeQueue.push(_writeJob());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::atomic_bool _done;
|
std::atomic_bool _done;
|
||||||
|
|
||||||
WritingJob _writeJob;
|
WritingJob _writeJob;
|
||||||
ReadingJob _readJob;
|
ReadingJob _readJob;
|
||||||
ConnectJob _connectJob;
|
ConnectJob _connectJob;
|
||||||
|
|
||||||
ThreadSafeQueue<W> _writeQueue;
|
ThreadSafeQueue<W> _writeQueue;
|
||||||
ThreadSafeQueue<R> _readQueue;
|
ThreadSafeQueue<R> _readQueue;
|
||||||
|
|
||||||
const ThreadUniquePointer _thread;
|
const ThreadUniquePointer _thread;
|
||||||
};
|
};
|
||||||
|
|
|
@ -30,148 +30,145 @@ static std::string daytimeString() {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
try {
|
try {
|
||||||
if (argc != 6) {
|
if (argc != 6) {
|
||||||
std::cerr << "Usage: server <send-port> <read-port>" << std::endl;
|
std::cerr << "Usage: server <send-port> <read-port>" << std::endl;
|
||||||
return InvalidArguments;
|
return InvalidArguments;
|
||||||
}
|
}
|
||||||
|
|
||||||
using namespace carla::server;
|
using namespace carla::server;
|
||||||
|
|
||||||
// This already starts the two threads.
|
// This already starts the two threads.
|
||||||
CarlaServer server(toInt(argv[1u]), toInt(argv[2u]), toInt(argv[3u]), toInt(argv[4u]), toInt(argv[5u]));
|
CarlaServer server(toInt(argv[1u]), toInt(argv[2u]), toInt(argv[3u]), toInt(argv[4u]), toInt(argv[5u]));
|
||||||
|
|
||||||
// Let's simulate the game loop.
|
// Let's simulate the game loop.
|
||||||
|
|
||||||
|
Color c1;
|
||||||
|
c1.red = 255;
|
||||||
|
c1.green = 0;
|
||||||
|
c1.blue = 0;
|
||||||
|
//c1.alpha = 0;
|
||||||
|
|
||||||
Color c1;
|
Color c2;
|
||||||
c1.red = 255;
|
c2.red = 4;
|
||||||
c1.green = 0;
|
c2.green = 5;
|
||||||
c1.blue = 0;
|
c2.blue = 6;
|
||||||
//c1.alpha = 0;
|
//c2.alpha = 0;
|
||||||
|
|
||||||
Color c2;
|
Color c3;
|
||||||
c2.red = 4;
|
c3.red = 7;
|
||||||
c2.green = 5;
|
c3.green = 8;
|
||||||
c2.blue = 6;
|
c3.blue = 9;
|
||||||
//c2.alpha = 0;
|
//c3.alpha = 0;
|
||||||
|
|
||||||
Color c3;
|
Color c4;
|
||||||
c3.red = 7;
|
c4.red = 10;
|
||||||
c3.green = 8;
|
c4.green = 11;
|
||||||
c3.blue = 9;
|
c4.blue = 12;
|
||||||
//c3.alpha = 0;
|
//c4.alpha = 0;
|
||||||
|
|
||||||
Color c4;
|
std::vector<Color> img;
|
||||||
c4.red = 10;
|
for (int i = 0; i < 1024; ++i) img.push_back(c1);
|
||||||
c4.green = 11;
|
|
||||||
c4.blue = 12;
|
|
||||||
//c4.alpha = 0;
|
|
||||||
|
|
||||||
std::vector<Color> img;
|
std::vector<Color> img_2;
|
||||||
for (int i=0; i < 1024; ++i) img.push_back(c1);
|
for (int i = 0; i < 1024; ++i) img_2.push_back(c2);
|
||||||
|
|
||||||
std::vector<Color> img_2;
|
|
||||||
for (int i = 0; i < 1024; ++i) img_2.push_back(c2);
|
|
||||||
|
|
||||||
std::vector<Color> depth_1;
|
std::vector<Color> depth_1;
|
||||||
for (int i = 0; i < 1024; ++i) depth_1.push_back(c3);
|
for (int i = 0; i < 1024; ++i) depth_1.push_back(c3);
|
||||||
|
|
||||||
std::vector<Color> depth_2;
|
std::vector<Color> depth_2;
|
||||||
for (int i = 0; i < 1024; ++i) depth_2.push_back(c4);
|
for (int i = 0; i < 1024; ++i) depth_2.push_back(c4);
|
||||||
|
|
||||||
Reward_Values testData;
|
Reward_Values testData;
|
||||||
testData.player_x = 1.0f;
|
testData.player_x = 1.0f;
|
||||||
testData.player_y = 1.0f;
|
testData.player_y = 1.0f;
|
||||||
testData.speed = 100.0f;
|
testData.speed = 100.0f;
|
||||||
testData.collision_gen = 10.0f;
|
testData.collision_gen = 10.0f;
|
||||||
testData.collision_ped = 10.0f;
|
testData.collision_ped = 10.0f;
|
||||||
testData.collision_car = 10.0f;
|
testData.collision_car = 10.0f;
|
||||||
testData.intersect = 50.0f;
|
testData.intersect = 50.0f;
|
||||||
testData.inertia_x = 0.5f;
|
testData.inertia_x = 0.5f;
|
||||||
testData.inertia_y = 0.1f;
|
testData.inertia_y = 0.1f;
|
||||||
testData.inertia_z = 0.02f;
|
testData.inertia_z = 0.02f;
|
||||||
testData.timestamp = 1;
|
testData.timestamp = 1;
|
||||||
testData.ori_x = 10.0f;
|
testData.ori_x = 10.0f;
|
||||||
testData.ori_y = 20.0f;
|
testData.ori_y = 20.0f;
|
||||||
testData.ori_z = 30.0f;
|
testData.ori_z = 30.0f;
|
||||||
testData.img = img;
|
testData.img = img;
|
||||||
testData.img_2 = img_2;
|
testData.img_2 = img_2;
|
||||||
testData.depth_1 = depth_1;
|
testData.depth_1 = depth_1;
|
||||||
testData.depth_2 = depth_2;
|
testData.depth_2 = depth_2;
|
||||||
|
|
||||||
|
std::cout << "Server send World" << std::endl;
|
||||||
std::cout << "Server send World" << std::endl;
|
server.sendWorld();
|
||||||
server.sendWorld();
|
|
||||||
|
|
||||||
int mode, scene;
|
int mode, scene;
|
||||||
bool end = false;
|
bool end = false;
|
||||||
|
|
||||||
std::cout << "Server wait scene init" << std::endl;
|
std::cout << "Server wait scene init" << std::endl;
|
||||||
do {
|
do {
|
||||||
end = server.tryReadSceneInit(mode, scene);
|
end = server.tryReadSceneInit(mode, scene);
|
||||||
}while (!end);
|
} while (!end);
|
||||||
|
|
||||||
std::vector<Position> positions;
|
std::vector<Position> positions;
|
||||||
std::vector<const float*> pMatrix;
|
std::vector<const float *> pMatrix;
|
||||||
|
|
||||||
positions.push_back(Position{ 0.0f, 0.0f });
|
positions.push_back(Position { 0.0f, 0.0f });
|
||||||
positions.push_back(Position{ 1.0f, 2.0f });
|
positions.push_back(Position { 1.0f, 2.0f });
|
||||||
positions.push_back(Position{ 3.0f, 4.0f });
|
positions.push_back(Position { 3.0f, 4.0f });
|
||||||
|
|
||||||
float list[16] = { 10.0, 10.0,10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 };
|
float list[16] = { 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0 };
|
||||||
|
|
||||||
pMatrix.push_back(list);
|
pMatrix.push_back(list);
|
||||||
|
|
||||||
Scene_Values sceneVal = {
|
Scene_Values sceneVal = {
|
||||||
positions,
|
positions,
|
||||||
pMatrix,
|
pMatrix,
|
||||||
};
|
};
|
||||||
|
|
||||||
std::cout << "Server send positions" << std::endl;
|
std::cout << "Server send positions" << std::endl;
|
||||||
|
|
||||||
server.sendSceneValues(sceneVal);
|
server.sendSceneValues(sceneVal);
|
||||||
|
|
||||||
end = false;
|
|
||||||
float startPoint, endPoint;
|
|
||||||
|
|
||||||
std::cout << "Server wait new episode" << std::endl;
|
end = false;
|
||||||
|
float startPoint, endPoint;
|
||||||
|
|
||||||
do {
|
std::cout << "Server wait new episode" << std::endl;
|
||||||
|
|
||||||
end = server.tryReadEpisodeStart(startPoint, endPoint);
|
do {
|
||||||
}
|
|
||||||
while (!end);
|
|
||||||
|
|
||||||
std::cout << "Server send end reset" << std::endl;
|
end = server.tryReadEpisodeStart(startPoint, endPoint);
|
||||||
|
} while (!end);
|
||||||
|
|
||||||
server.sendEndReset();
|
std::cout << "Server send end reset" << std::endl;
|
||||||
|
|
||||||
float steer, gas;
|
server.sendEndReset();
|
||||||
bool wait_control = false;
|
|
||||||
|
float steer, gas;
|
||||||
|
bool wait_control = false;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
|
||||||
if (server.tryReadEpisodeStart(startPoint, endPoint)) {
|
if (server.tryReadEpisodeStart(startPoint, endPoint)) {
|
||||||
std::cout << "------> RESET <------" << std::endl;
|
std::cout << "------> RESET <------" << std::endl;
|
||||||
std::cout << " --> Start: " << startPoint << " End: " << endPoint << std::endl;
|
std::cout << " --> Start: " << startPoint << " End: " << endPoint << std::endl;
|
||||||
server.sendEndReset();
|
server.sendEndReset();
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
|
|
||||||
if (wait_control && server.tryReadControl(steer, gas)) {
|
if (wait_control && server.tryReadControl(steer, gas)) {
|
||||||
std::cout << "Steer: " << steer << "Gas: " << gas << std::endl;
|
std::cout << "Steer: " << steer << "Gas: " << gas << std::endl;
|
||||||
wait_control = false;
|
wait_control = false;
|
||||||
}
|
} else if (!wait_control) {
|
||||||
else if (!wait_control) {
|
server.sendReward(testData);
|
||||||
server.sendReward(testData);
|
wait_control = true;
|
||||||
wait_control = true;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Sleep(100);
|
|
||||||
|
|
||||||
|
{
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
std::this_thread::sleep_for(100ms);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (const std::exception &e) {
|
} catch (const std::exception &e) {
|
||||||
|
|
Loading…
Reference in New Issue