Sychronous mode bug resolution

This commit is contained in:
Praveen Kumar 2020-03-09 18:34:35 +05:30 committed by Marc Garcia Puig
parent 4f21f94ac0
commit e55d5750e9
6 changed files with 37 additions and 19 deletions

View File

@ -65,28 +65,46 @@ void BatchControlStage::DataSender() {
messenger->Pop();
bool synch_mode = parameters.GetSynchronousMode();
if (commands != nullptr) {
// Run asynchronous mode commands.
episode_proxy_bcs.Lock()->ApplyBatch(*commands.get(), false);
}
// Limiting updates to 100 frames per second.
// Asynchronous mode.
if (!synch_mode) {
// Apply batch command through an asynchronous RPC call.
if (commands != nullptr) {
episode_proxy_bcs.Lock()->ApplyBatch(*commands.get(), false);
}
// Limiting updates to 100 frames per second.
std::this_thread::sleep_for(10ms);
} else {
}
// Synchronous mode.
else {
std::unique_lock<std::mutex> lock(step_execution_mutex);
// Get timeout value in milliseconds.
double timeout = parameters.GetSynchronousModeTimeOutInMiliSecond();
// Wait for service to finish.
step_execution_notifier.wait_for(lock, timeout * 1ms, [this]() { return run_step.load(); });
// TODO: Re-introduce timeout while waiting for RunStep() call.
while (!run_step.load()) {
step_execution_notifier.wait_for(lock, 1ms, [this]() {return run_step.load();});
}
// Apply batch command in synchronous RPC call.
if (commands != nullptr) {
episode_proxy_bcs.Lock()->ApplyBatchSync(*commands.get(), false);
}
// Set flag to false, unblock RunStep() call and release mutex lock.
run_step.store(false);
step_complete_notifier.notify_one();
lock.unlock();
}
}
bool BatchControlStage::RunStep() {
// Set run set flag.
run_step.store(true);
bool synch_mode = parameters.GetSynchronousMode();
if (synch_mode) {
std::unique_lock<std::mutex> lock(step_execution_mutex);
// Set flag to true, notify DataSender and wait for a return notification
run_step.store(true);
step_execution_notifier.notify_one();
while (run_step.load()) {
step_complete_notifier.wait_for(lock, 1ms, [this]() {return !run_step.load();});
}
}
return true;
}

View File

@ -47,11 +47,12 @@ private:
/// Parameter object for changing synchronous behaviour.
Parameters &parameters;
/// Step runner flag.
std::atomic<bool> run_step;
std::atomic<bool> run_step {false};
/// Mutex for progressing synchronous execution.
std::mutex step_execution_mutex;
/// Condition variables for progressing synchronous execution.
std::condition_variable step_execution_notifier;
std::condition_variable step_complete_notifier;
public:

View File

@ -13,9 +13,6 @@ namespace traffic_manager {
Parameters::Parameters() {
/// Set default synchronous mode to false.
synchronous_mode.store(false);
/// Set default synchronous mode time out.
synchronous_time_out = std::chrono::duration<int, std::milli>(10);
}

View File

@ -57,7 +57,7 @@ namespace traffic_manager {
/// Map containing % of keep right rule.
AtomicMap<ActorId, float> perc_keep_right;
/// Synchronous mode switch.
std::atomic<bool> synchronous_mode;
std::atomic<bool> synchronous_mode {false};
/// Distance Margin
std::atomic<float> distance_margin {2.0};

View File

@ -29,5 +29,6 @@ void export_trafficmanager() {
.def("ignore_lights_percentage", &carla::traffic_manager::TrafficManager::SetPercentageRunningLight)
.def("ignore_signs_percentage", &carla::traffic_manager::TrafficManager::SetPercentageRunningSign)
.def("set_global_distance_to_leading_vehicle", &carla::traffic_manager::TrafficManager::SetGlobalDistanceToLeadingVehicle)
.def("set_percentage_keep_right_rule", &carla::traffic_manager::TrafficManager::SetKeepRightPercentage);
.def("set_percentage_keep_right_rule", &carla::traffic_manager::TrafficManager::SetKeepRightPercentage)
.def("set_synchronous_mode", &carla::traffic_manager::TrafficManager::SetSynchronousMode);
}

View File

@ -98,6 +98,7 @@ def main():
if args.sync:
settings = world.get_settings()
traffic_manager.set_synchronous_mode(True)
if not settings.synchronous_mode:
synchronous_master = True
settings.synchronous_mode = True