unique_ptr

This commit is contained in:
Xisco Bosch 2017-03-23 18:42:46 +01:00
parent d89f448e9d
commit d86ad9b2c3
12 changed files with 269 additions and 213 deletions

View File

@ -15,62 +15,68 @@ namespace carla {
CarlaServer::~CarlaServer() {}
void CarlaServer::init(uint32_t LevelCount) {
bool CarlaServer::init(uint32_t LevelCount) {
if (!worldConnected() && !clientConnected() && !serverConnected()) return false;
_pimpl->sendWorld(static_cast<uint32_t>(Mode::NUMBER_OF_MODES), LevelCount);
return true;
}
bool CarlaServer::tryReadSceneInit(Mode &mode, uint32_t &scene) {
return _pimpl->tryReadSceneInit(mode, scene);
bool CarlaServer::tryReadSceneInit(Mode &mode, uint32_t &scene, bool &readed) {
if (!worldConnected()) return false;
readed = _pimpl->tryReadSceneInit(mode, scene);
return true;
}
bool CarlaServer::tryReadEpisodeStart(uint32_t &startIndex, uint32_t &endIndex) {
return _pimpl->tryReadEpisodeStart(startIndex, endIndex);
bool CarlaServer::tryReadEpisodeStart(uint32_t &startIndex, uint32_t &endIndex, bool &readed) {
if (!worldConnected()) return false;
readed = _pimpl->tryReadEpisodeStart(startIndex, endIndex);
return true;
}
bool CarlaServer::tryReadControl(float &steer, float &throttle) {
return _pimpl->tryReadControl(steer, throttle);
bool CarlaServer::tryReadControl(float &steer, float &throttle, bool &readed) {
if (!clientConnected()) return false;
readed = _pimpl->tryReadControl(steer, throttle);
return true;
}
bool CarlaServer::newEpisodeRequested() {
return _pimpl->tryReadRequestNewEpisode();
bool CarlaServer::newEpisodeRequested(bool &newEpisode) {
if (!worldConnected()) return false;
newEpisode = _pimpl->tryReadRequestNewEpisode();
return true;
}
bool CarlaServer::sendReward(const Reward_Values &values) {
if (needsRestart()) return false;
_pimpl->sendReward(values);
bool CarlaServer::sendReward(std::unique_ptr<Reward_Values> values) {
if (!serverConnected()) return false;
_pimpl->sendReward(std::move(values));
return true;
}
bool CarlaServer::sendSceneValues(const Scene_Values &values) {
if (needsRestart()) return false;
if (!worldConnected()) return false;
_pimpl->sendSceneValues(values);
return true;
}
bool CarlaServer::sendEndReset() {
if (needsRestart()) return false;
if (!worldConnected()) return false;
_pimpl->sendEndReset();
return true;
}
/*bool CarlaServer::worldConnected(){
return _pimpl->worldConnected();
bool CarlaServer::worldConnected(){
return _pimpl->worldConnected() && !_pimpl->needsRestart();
}
bool CarlaServer::clientConnected(){
return _pimpl->clientConnected();
return _pimpl->clientConnected() && !_pimpl->needsRestart();
}
bool CarlaServer::serverConnected(){
return _pimpl->serverConnected();
return _pimpl->serverConnected() && !_pimpl->needsRestart();
}
bool CarlaServer::needRestart() {
return _pimpl->needRestart();
}*/
bool CarlaServer::needsRestart(){
return _pimpl->needsRestart() || (!_pimpl->worldConnected() || !_pimpl->clientConnected() || !_pimpl->serverConnected());
return _pimpl->needsRestart();
}
} // namespace carla

View File

@ -92,26 +92,26 @@ namespace carla {
/// Initialize the server.
///
/// @param LevelCount Number of levels available.
void 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.
///
/// If returned mode INVALID, ignore scene.
bool tryReadSceneInit(Mode &mode, uint32_t &scene);
bool tryReadSceneInit(Mode &mode, uint32_t &scene, bool &readed);
/// Try to read if the client has selected an end & start point. Return
/// false if the queue is empty.
bool tryReadEpisodeStart(uint32_t &startIndex, uint32_t &endIndex);
bool tryReadEpisodeStart(uint32_t &startIndex, uint32_t &endIndex, bool &readed);
/// Try to read the response of the client. Return false if the queue
/// is empty.
bool tryReadControl(float &steer, float &throttle);
bool tryReadControl(float &steer, float &throttle, bool &readed);
bool newEpisodeRequested();
bool newEpisodeRequested(bool &newEpisode);
/// Send values of the current player status.
bool sendReward(const Reward_Values &values);
bool sendReward(std::unique_ptr<Reward_Values> values);
/// Send the values of the generated scene.
bool sendSceneValues(const Scene_Values &values);

View File

@ -47,11 +47,11 @@ namespace server {
if (correctSerialize) {
server.writeString(message, error);
if (error) {
logTCPError("Failed to send", error);
}
//server.writeString("reward", error);
if (!server.Connected() && !thr.getRestart()) {
if (error) logTCPError("Failed to send", error);
if (!server.Connected()) {
thr.reconnect();
}
@ -66,11 +66,11 @@ namespace server {
//TODO:
// Sortida amb google protocol
// This is the thread that listens for string over the TCP socket.
static std::string clientWorkerThread(TCPServer &server, thread::AsyncWriterJobQueue<std::string> &thr) {
static std::unique_ptr<std::string> clientWorkerThread(TCPServer &server, thread::AsyncWriterJobQueue<std::string> &thr) {
//if (!server.Connected()) server.AcceptSocket();
std::string message;
auto message = std::make_unique<std::string>();
bool success = false;
do{
@ -78,14 +78,14 @@ namespace server {
if (!thr.getRestart()){
TCPServer::error_code error;
success = server.readString(message, error);
success = server.readString(*message, error);
if (error && (error != boost::asio::error::eof)) { // eof is expected.
logTCPError("Failed to read", error);
return std::string();
return nullptr;
}
if (!server.Connected() && !thr.getRestart()) {
if (!server.Connected()) {
thr.reconnect();
break;
}
@ -97,9 +97,10 @@ namespace server {
}
// This is the thread that listens & sends a string over the TCP world socket.
static 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) {
//std::lock_guard<std::mutex> lock(server.getMutex());
std::string message;
auto message = std::make_unique<std::string>();
bool success = false;
do{
@ -107,13 +108,13 @@ namespace server {
if (!thr.getRestart()){
TCPServer::error_code error;
success = server.readString(message, error);
success = server.readString(*message, error);
if (error && (error != boost::asio::error::eof)) { // eof is expected.
logTCPError("Failed to read world", error);
return std::string();
return nullptr;
}
if (!server.Connected() && !thr.getRestart()) {
if (!server.Connected()) {
thr.reconnect();
break;
}
@ -132,20 +133,22 @@ namespace server {
server.writeString(message, error);
//server.writeString("world", error);
Scene demo_scene;
if (demo_scene.ParseFromString(message)){
std::cout << "POSSIBLE POSITIONS 5"<< std::endl;
for (int i=0; i<demo_scene.position_size(); ++i){
std::cout << " x: " << demo_scene.position(i).pos_x() << " y: " << demo_scene.position(i).pos_y() << std::endl;
}
}
//Scene demo_scene;
//if (demo_scene.ParseFromString(message)){
//std::cout << "POSSIBLE POSITIONS"<< std::endl;
//for (int i=0; i<demo_scene.position_size(); ++i){
// std::cout << " x: " << demo_scene.position(i).pos_x() << " y: " << demo_scene.position(i).pos_y() << std::endl;
//}
//}
if (error) {
logTCPError("Failed to send world", error);
}
if (!server.Connected() && !thr.getRestart()) {
if (!server.Connected()) {
thr.reconnect();
}
}
@ -163,8 +166,6 @@ namespace server {
std::lock_guard<std::mutex> lock(_generalMutex);
if (!communication.NeedsRestart()){
std::cout << " ---- RECONNECT ALL ...." << std::endl;
@ -201,9 +202,9 @@ namespace server {
_client(readPort),
_needsRestart(false),
_proto(std::make_unique<Protocol>(this)),
_worldThread {
_worldThread {
[this]() { return worldReceiveThread(this->_world, this->_worldThread); },
[this](const std::string & msg) { worldSendThread(this->_world, this->_worldThread, msg); },
[this](const std::string &msg) { worldSendThread(this->_world, this->_worldThread, msg); },
[this]() { Connect(this->_world, *this); },
[this]() { ReconnectAll(*this);}
},
@ -217,6 +218,7 @@ namespace server {
[this]() { Connect(this->_client, *this); },
[this]() { ReconnectAll(*this);}
}
{
_mode = Mode::MONO;
/*std::cout << "WorldPort: " << worldPort << std::endl;
@ -225,26 +227,20 @@ namespace server {
}
CarlaCommunication::~CarlaCommunication(){
//delete &_worldThread;
//delete &_serverThread;
//delete &_clientThread;
}
void CarlaCommunication::sendReward(const Reward_Values &values) {
_serverThread.push(values);
void CarlaCommunication::sendReward(std::unique_ptr<Reward_Values> values) {
_serverThread.push(std::move(values));
}
bool CarlaCommunication::tryReadControl(float &steer, float &gas) {
steer = 0.0f;
gas = 0.0f;
std::string controlMessage;
if (!_clientThread.tryPop(controlMessage)) return false;
auto message = _clientThread.tryPop();
if (message == nullptr) return false;
Control control;
if (!control.ParseFromString(controlMessage)) return false;
if (!control.ParseFromString(*message)) return false;
steer = control.steer();
gas = control.gas();
@ -258,10 +254,9 @@ namespace server {
World world;
_proto->LoadWorld(world, modes, scenes);
std::string message;
bool error = !world.SerializeToString(&message);
_worldThread.push(message);
auto message = std::make_unique<std::string>();
if (world.SerializeToString(message.get()))
_worldThread.push(std::move(message));
}
void CarlaCommunication::sendScene(const Scene_Values &values) {
@ -270,8 +265,9 @@ namespace server {
Scene scene;
_proto -> LoadScene(scene, values);
std::string message;
if (scene.SerializeToString(&message))_worldThread.push(message);
auto message = std::make_unique<std::string>();
if (scene.SerializeToString(message.get()))
_worldThread.push(std::move(message));
}
@ -280,9 +276,9 @@ namespace server {
EpisodeReady eReady;
eReady.set_ready(true);
std::string message;
if (eReady.SerializeToString(&message)) {
_worldThread.push(message);
auto message = std::make_unique<std::string>();
if (eReady.SerializeToString(message.get())) {
_worldThread.push(std::move(message));
}
}
@ -291,12 +287,12 @@ namespace server {
mode = Mode::INVALID;
scene = 0u;
std::string info;
if (!_worldThread.tryPop(info)) return false;
std::unique_ptr<std::string> info = _worldThread.tryPop();
if (info == nullptr) return false;
SceneInit sceneInit;
if (!sceneInit.ParseFromString(info)) return false;
if (!sceneInit.ParseFromString(*info)) return false;
mode = getMode(sceneInit.mode());
scene = sceneInit.scene();
@ -310,11 +306,11 @@ namespace server {
start_index = 0;
end_index = 0;
std::string startData;
if (!_worldThread.tryPop(startData)) return false;
std::unique_ptr<std::string> startData = _worldThread.tryPop();
if (startData == nullptr) return false;
EpisodeStart episodeStart;
if(!episodeStart.ParseFromString(startData)) return false;
if(!episodeStart.ParseFromString(*startData)) return false;
start_index = episodeStart.start_index();
end_index = episodeStart.end_index();
@ -323,12 +319,16 @@ namespace server {
}
bool CarlaCommunication::tryReadRequestNewEpisode(){
std::string request;
if(!_worldThread.tryPop(request)) return false;
std::unique_ptr <std::string> request = _worldThread.tryPop();
if (request == nullptr) return false;
RequestNewEpisode reqEpisode;
return reqEpisode.ParseFromString(request);
if (!reqEpisode.ParseFromString(*request)){
_worldThread.undoPop(std::move(request));
return false;
}
else return true;
}
void CarlaCommunication::restartServer(){

View File

@ -26,9 +26,9 @@ namespace server {
explicit CarlaCommunication(int worldPort, int writePort, int readPort);
~CarlaCommunication();
//~CarlaCommunication();
void sendReward(const Reward_Values &values);
void sendReward(std::unique_ptr<Reward_Values> values);
bool tryReadControl(float &steer, float &gas);
@ -83,7 +83,7 @@ namespace server {
thread::AsyncWriterJobQueue<std::string> _clientThread;
thread::AsyncReadWriteJobQueue<std::string, std::string> _worldThread;
thread::AsyncReadWriteJobQueue< std::string, std::string> _worldThread;
std::atomic_bool _needsRestart;

View File

@ -21,11 +21,11 @@ namespace server {
Server::~Server() {
}
void Server::sendReward(const Reward_Values &values) {
void Server::sendReward( std::unique_ptr<Reward_Values> values) {
//Reward reward;
//_proto->LoadReward(reward, values);
//_communication->sendReward(reward);
_communication->sendReward(values);
_communication->sendReward(std::move(values));
}
void Server::sendSceneValues(const Scene_Values &values) {

View File

@ -33,7 +33,7 @@ namespace server {
~Server();
///// Send values of the current player status
void sendReward(const Reward_Values &values);
void sendReward(std::unique_ptr<Reward_Values> values);
//// Send the values of the generated scene
void sendSceneValues(const Scene_Values &values);

View File

@ -45,9 +45,9 @@ namespace carla {
try {
boost::asio::ip::tcp::acceptor acceptor(_service, tcp::endpoint(tcp::v4(), port));
acceptor.accept(_socket);
_connected = true;
_service.run();
std::cout << "Connected port " << port << std::endl;
_connected = true;
}
catch (boost::system::system_error) {std::cerr<<"Socket System error"<<std::endl;};
@ -65,6 +65,7 @@ namespace carla {
if (error)
{
std::cout << "DESCONECTED port " << port << std::endl;
_connected = false;
}
}
@ -84,6 +85,7 @@ namespace carla {
if (error)
{
std::cout << "DESCONECTED port " << port << std::endl;
_connected = false;
}
else if (!error){

View File

@ -18,8 +18,8 @@ namespace thread {
class AsyncReadWriteJobQueue {
public:
using WritingJob = std::function<W()>;
using ReadingJob = std::function<void(R)>;
using WritingJob = std::function<std::unique_ptr<W>()>;
using ReadingJob = std::function<void(const R &)>;
using ConnectJob = std::function<void()>;
using ReconnectJob = std::function<void()>;
@ -38,17 +38,20 @@ namespace thread {
_thread(new std::thread(&AsyncReadWriteJobQueue::workerThread, this)) {}
~AsyncReadWriteJobQueue() {
std::cout << "Destroyed thread world"<< std::endl;
_done = true;
}
bool tryPop(W &value) {
return _writeQueue.try_pop(value);
std::unique_ptr<W> tryPop() {
return _writeQueue.try_pop();
}
void push(R item) {
R temp;
while (_readQueue.try_pop(temp));
_readQueue.push(item);
void undoPop(std::unique_ptr<W> value){
_writeQueue.push(std::move(value));
}
void push(std::unique_ptr<R> item) {
_readQueue.push(std::move(item));
}
void reconnect(){
@ -73,20 +76,17 @@ namespace thread {
while(!_done){
_connectJob();
_restart = false;
W temp1; R temp2;
while(_writeQueue.try_pop(temp1));
while(_readQueue.try_pop(temp2));
_readQueue.canWait(true);
while (!_restart && !_done) {
R value;
if (_readQueue.wait_and_pop(value)) {
_readJob(value);
std::cout << "1" << std::endl;
auto value = _readQueue.wait_and_pop();
if (value != nullptr) {
_readJob(*value);
}
if (!_restart){
W write = _writeJob();
W temp;
while (_writeQueue.try_pop(temp));
_writeQueue.push(write);
_writeQueue.push(std::move(_writeJob()));
}
}

View File

@ -19,7 +19,7 @@ namespace thread {
class AsyncReaderJobQueue {
public:
using Job = std::function<void(T)>;
using Job = std::function<void(const T &)>;
using ConnectJob = std::function<void()>;
using ReconnectJob = std::function<void()>;
@ -33,14 +33,13 @@ namespace thread {
_thread(new std::thread(&AsyncReaderJobQueue::workerThread, this)) {}
~AsyncReaderJobQueue() {
std::cout << "Destroyed thread client"<< std::endl;
_done = true;
}
void push(T item) {
void push(std::unique_ptr<T> item) {
// Empty the queue before push a new item
T temp;
while (_queue.try_pop(temp));
_queue.push(item);
_queue.push(std::move(item));
}
void restart(){
@ -59,17 +58,21 @@ namespace thread {
private:
void workerThread() {
while (!_done){
std::cout << "2.1" << std::endl;
_connectionJob();
_restart = false;
T temp;
while(_queue.try_pop(temp));
_queue.canWait(true);
while (!_restart && !_done) {
T value;
if (_queue.wait_and_pop(value)) _job(value);
std::cout << "2.2" << std::endl;
auto value = _queue.wait_and_pop();
if (value != nullptr) _job(*value);
//Sleep(10);
}
}
std::cout << "2.3" << std::endl;
}
std::atomic_bool _done;

View File

@ -17,7 +17,7 @@ namespace thread {
class AsyncWriterJobQueue {
public:
using Job = std::function<T()>;
using Job = std::function<std::unique_ptr<T>()>;
using ConnectJob = std::function<void()>;
using ReconnectJob = std::function<void()>;
@ -32,11 +32,12 @@ namespace thread {
{}
~AsyncWriterJobQueue() {
std::cout << "Destroyed thread server"<< std::endl;
_done = true;
}
bool tryPop(T &value) {
return _queue.try_pop(value);
std::unique_ptr<T> tryPop() {
return std::move(_queue.try_pop());
}
void restart(){
@ -56,19 +57,22 @@ namespace thread {
void workerThread() {
while (!_done){
std::cout << "3.1" << std::endl;
_connectJob();
T temp;
while(_queue.try_pop(temp));
_restart = false;
_queue.canWait(true);
while (!_restart && !_done) {
T item = _job();
T temp;
while(_queue.try_pop(temp));
_queue.push(item);
std::cout << "3.2" << std::endl;
_queue.push(std::move(_job()));
//Sleep(10);
}
}
std::cout << "3.3" << std::endl;
}
std::atomic_bool _done;

View File

@ -8,6 +8,7 @@
#include <queue>
#include <iostream>
#include "../CarlaServer.h"
namespace carla {
namespace thread {
@ -19,21 +20,15 @@ namespace thread {
class ThreadSafeQueue {
public:
ThreadSafeQueue() = default;
ThreadSafeQueue(): _canWait(true), _empty(true){}
ThreadSafeQueue(const ThreadSafeQueue &other) {
std::lock_guard<std::mutex> lock(other._mutex);
_queue = other._queue;
_canWait = true;
_empty = true;
}
ThreadSafeQueue(const ThreadSafeQueue &other) = delete;
void push(T new_value) {
void push(std::unique_ptr<T> new_value) {
std::lock_guard<std::mutex> lock(_mutex);
//_queue.push(new_value);
//_condition.notify_one();
_value = new_value;
_empty = false;
_value = std::move(new_value);
}
void canWait(bool wait){
@ -42,46 +37,36 @@ namespace thread {
_condition.notify_one();
}
bool wait_and_pop(T &value) {
std::unique_ptr<T> wait_and_pop() {
std::unique_lock<std::mutex> lock(_mutex);
_condition.wait(lock, [this] {
//return !_queue.empty() || !_canWait;
return !_empty || !_canWait;
_condition.wait(lock, [this]() {
return _value != nullptr || !_canWait;
});
//while(_queue.empty() && _canWait);
//if (!_queue.empty() && _canWait) {
if (!_empty && _canWait){
// value = _queue.front();
//_queue.pop();
value = _value;
_empty = true;
return true;
}
else return false;
return std::move(_value);
}
bool try_pop(T &value) {
std::unique_ptr<T> try_pop() {
std::lock_guard<std::mutex> lock(_mutex);
//if (_queue.empty()) {
if(_empty){
return std::move(_value);
/*if(_empty){
return false;
}
else {
//value = _queue.front();
//_queue.pop();
value = _value;
empty = true;
_empty = true;
return true;
}
}*/
}
bool empty() const {
std::lock_guard<std::mutex> lock(_mutex);
//return _queue.empty();
return _empty;
return _value == nullptr;
}
@ -98,7 +83,7 @@ namespace thread {
//std::queue<T> _queue;
///////
bool _empty;
T _value;
std::unique_ptr<T> _value;
///////
std::condition_variable _condition;

View File

@ -50,6 +50,36 @@ static std::vector<carla::Color> makeImage(uint32_t width, uint32_t height) {
return image;
}
std::unique_ptr<carla::Reward_Values> makeReward(){
auto reward = std::make_unique<carla::Reward_Values>();
const uint32_t imageWidth = 512u;
const uint32_t imageHeight = 512u;
reward->player_location = {1.0f, 1.0f};
reward->player_orientation = {1.0f, 1.0f};
reward->player_acceleration = {1.0f, 1.0f};
reward->forward_speed = 100.0f;
reward->collision_general = 10.0f;
reward->collision_pedestrian = 10.0f;
reward->collision_car = 10.0f;
reward->intersect_other_lane = 0.5f;
reward->intersect_offroad = 0.5f;
reward->image_width = imageWidth;
reward->image_height = imageHeight;
reward->image_rgb_0 = makeImage(imageWidth, imageHeight);
reward->image_rgb_1 = makeImage(imageWidth, imageHeight);
reward->image_depth_0 = makeImage(imageWidth, imageHeight);
reward->image_depth_1 = makeImage(imageWidth, imageHeight);
static decltype(carla::Reward_Values::timestamp) timestamp = 0u;
reward->timestamp = timestamp++;
return reward;
}
int main(int argc, char *argv[]) {
try {
if (argc != 4) {
@ -67,44 +97,25 @@ int main(int argc, char *argv[]) {
// Let's simulate the game loop.
const uint32_t imageWidth = 512u;
const uint32_t imageHeight = 512u;
carla::Reward_Values reward;
reward.player_location = {1.0f, 1.0f};
reward.player_orientation = {1.0f, 1.0f};
reward.player_acceleration = {1.0f, 1.0f};
reward.forward_speed = 100.0f;
reward.collision_general = 10.0f;
reward.collision_pedestrian = 10.0f;
reward.collision_car = 10.0f;
reward.intersect_other_lane = 0.5f;
reward.intersect_offroad = 0.5f;
reward.image_width = imageWidth;
reward.image_height = imageHeight;
reward.image_rgb_0 = makeImage(imageWidth, imageHeight);
reward.image_rgb_1 = makeImage(imageWidth, imageHeight);
reward.image_depth_0 = makeImage(imageWidth, imageHeight);
reward.image_depth_1 = makeImage(imageWidth, imageHeight);
for (;;){
if (!server.needsRestart()){
server.init(1u);
if (server.init(1u)){
{
carla::Mode mode;
uint32_t scene;
bool error = false, readed = false;
do{
error = !server.tryReadSceneInit(mode, scene, readed);
}while(!readed && !error);
while(!server.needsRestart() && !server.tryReadSceneInit(mode, scene));
std::cout << "Received: mode = "
<< (mode == carla::Mode::MONO ? "MONO" : "STEREO")
<< ", scene = "
<< scene << std::endl;
if (error) std::cerr << "ERROR while sending SceneValues" << std::endl;
else std::cout << "Received: mode = " << (mode == carla::Mode::MONO ? "MONO" : "STEREO")
<< ", scene = " << scene << std::endl;
}
carla::Scene_Values sceneValues;
@ -114,40 +125,85 @@ int main(int argc, char *argv[]) {
const std::array<float, 16u> pMatrix = {{ 10.0 }};
sceneValues.projection_matrices.push_back(pMatrix);
std::cout << "POSSIBLE POSITIONS "<< std::endl;
for (int i=0; i<sceneValues.possible_positions.size(); ++i){
std::cout << "POSSIBLE POSITIONS 5"<< std::endl;
for (int i=0; i<sceneValues.possible_positions.size(); ++i){
std::cout << " x: " << sceneValues.possible_positions[i].x << " y: " << sceneValues.possible_positions[i].y << std::endl;
}
server.sendSceneValues(sceneValues);
if (!server.sendSceneValues(sceneValues)) std::cerr << "ERROR while sending SceneValues" << std::endl;
std::cout << "New episode" << std::endl;
uint32_t start, end;
while (!server.needsRestart() && !server.tryReadEpisodeStart(start, end));
std::cout << "Received: startIndex = " << start
<< ", endIndex = " << end << std::endl;
{
uint32_t start, end;
bool error = false, readed = false;
do{
error = !server.tryReadEpisodeStart(start, end, readed);
}while (!readed && !error);
if (error) std::cerr << "ERROR while reading EpisodeStart" << std::endl;
else std::cout << "Received: startIndex = " << start << ", endIndex = " << end << std::endl;
server.sendEndReset();
while (!server.needsRestart()) {
float steer, gas;
uint32_t startPoint, endPoint;
if (server.newEpisodeRequested()){
std::cout << "-------- RESET --------" << std::endl;
server.sendSceneValues(sceneValues);
while (!server.needsRestart() && !server.tryReadEpisodeStart(startPoint, endPoint));
std::cout << "--> Start: " << startPoint << " End: " << endPoint << " <--" << std::endl;
server.sendEndReset();
}else {
if (server.tryReadControl(steer, gas)) {
std::cout << "Steer: " << steer << " Gas: " << gas << std::endl;
}
static decltype(carla::Reward_Values::timestamp) timestamp = 0u;
reward.timestamp = timestamp++;
server.sendReward(reward);
}
if (!server.sendEndReset()) std::cerr << "ERROR while sending EndReset" << std::endl;
while (true) {
float steer, gas;
/*bool newEpisode = false;
if (!server.newEpisodeRequested(newEpisode)){
std::cerr << "ERROR while checking for newEpisode request" << std::endl;
break;
}
if (newEpisode){
std::cout << "-------- NEW EPISODE --------" << std::endl;
if (!server.sendSceneValues(sceneValues)){
std::cerr << "ERROR while sending SceneValues" << std::endl;
break;
}
std::cout << "Waiting Episode Start" << std::endl;
uint32_t startPoint, endPoint;
bool error = false, readed = false;
do{
error = !server.tryReadEpisodeStart(startPoint, endPoint, readed);
}while (!readed && !error);
if (error) {
std::cerr << "ERROR while reading EpisodeStart" << std::endl;
break;
}
else{
std::cout << "--> Start: " << startPoint << " End: " << endPoint << " <--" << std::endl;
server.sendEndReset();
}
}else {
*/
bool error = false, readed = false;
if (!server.tryReadControl(steer, gas, readed)){
std::cerr << "ERROR while reading Control" << std::endl;
break;
}
else if (readed)
std::cout << "CONTROL --> gas: " << gas << " steer: " << steer << std::endl;
if (!server.sendReward(makeReward())) {
std::cerr << "ERROR while sending Reward" << std::endl;
break;
}
//}
}
std::cout << " ----- RESTARTING -----" << std::endl;
}
}