From 49fcf8acca40a22c01103fc2133970c8039a4eb8 Mon Sep 17 00:00:00 2001 From: mamiksik Date: Sat, 12 Aug 2023 11:10:40 +0200 Subject: [PATCH] Add crl+c handler Ensure that on kill signal the AI gracefully exits --- roboteam_ai/include/roboteam_ai/STPManager.h | 5 +- roboteam_ai/src/STPManager.cpp | 7 ++- roboteam_ai/src/roboteam_ai.cpp | 57 ++++++-------------- 3 files changed, 21 insertions(+), 48 deletions(-) diff --git a/roboteam_ai/include/roboteam_ai/STPManager.h b/roboteam_ai/include/roboteam_ai/STPManager.h index dc8911297..539ad7fee 100644 --- a/roboteam_ai/include/roboteam_ai/STPManager.h +++ b/roboteam_ai/include/roboteam_ai/STPManager.h @@ -17,7 +17,7 @@ class STPManager { * @param interfaceGateway The interface that belongs to this AI * @param mainWindow The interface that belongs to this AI */ - explicit STPManager(std::shared_ptr interfaceGateway, ai::interface::MainWindow* mainWindow); + explicit STPManager(std::shared_ptr interfaceGateway); private: /** @@ -29,7 +29,6 @@ class STPManager { int tickCounter = 0; /**< Counter that keeps track of the ticks */ bool fieldInitialized = false; /**< Indicates whether the field is initialized successfully */ bool robotsInitialized = false; /**< Indicates whether the robots are initialized successfully */ - ai::interface::MainWindow* mainWindow; /**< Interface window of the AI */ std::shared_ptr interfaceGateway; /**< pointer to the InterfaceGateway */ static inline ai::stp::Play* currentPlay{nullptr}; /**< Current best play as picked by the playDecider */ @@ -46,7 +45,7 @@ class STPManager { * @brief Starts the AI with a synchronized boolean to ensure that AI exits correctly * @param exitApplication Indicates whether the AI should exit */ - void start(std::atomic_bool& exitApplication); + void start(std::atomic_flag& exitApplication); static std::vector> plays; /**< The vector that contains all plays */ diff --git a/roboteam_ai/src/STPManager.cpp b/roboteam_ai/src/STPManager.cpp index 199f99675..e8b344b66 100644 --- a/roboteam_ai/src/STPManager.cpp +++ b/roboteam_ai/src/STPManager.cpp @@ -81,7 +81,7 @@ std::vector> STPManager::plays = ([] { })(); /// Start running behaviour trees. While doing so, publish settings and log the FPS of the system -void STPManager::start(std::atomic_bool &exitApplication) { +void STPManager::start(std::atomic_flag &exitApplication) { // make sure we start in halt state for safety ai::GameStateManager::forceNewGameState(RefCommand::HALT, std::nullopt); RTT_INFO("Start looping") @@ -132,7 +132,7 @@ void STPManager::start(std::atomic_bool &exitApplication) { stpTimer.limit([&]() { io::io.publishSettings(); }, ai::Constants::SETTINGS_BROADCAST_RATE()); } - if (exitApplication) { + if (exitApplication.test()) { stpTimer.stop(); } }, @@ -220,8 +220,7 @@ void STPManager::decidePlay(world::World *_world, bool ignoreWorldAge) { currentPlay->updateField(_world->getField().value()); } currentPlay->update(); - // mainWindow->updatePlay(currentPlay); } -STPManager::STPManager(std::shared_ptr interfaceGateway, ai::interface::MainWindow *mainWindow): interfaceGateway(std::move(interfaceGateway)) { this->mainWindow = mainWindow; } +STPManager::STPManager(std::shared_ptr interfaceGateway): interfaceGateway(std::move(interfaceGateway)) { } } // namespace rtt diff --git a/roboteam_ai/src/roboteam_ai.cpp b/roboteam_ai/src/roboteam_ai.cpp index 7132693a1..139d232de 100644 --- a/roboteam_ai/src/roboteam_ai.cpp +++ b/roboteam_ai/src/roboteam_ai.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include "RobotHubMode.h" #include "STPManager.h" @@ -11,31 +13,21 @@ namespace ui = rtt::ai::interface; -ui::MainWindow* window; +// Create a flag which signals to stpThread to stop +std::atomic_flag stopFlag = ATOMIC_FLAG_INIT; -void runStp(std::shared_ptr interfaceGateway, std::atomic_bool& exitApplication) { - rtt::STPManager app{interfaceGateway, window}; - app.start(exitApplication); +void initializeExitHandler() { + struct sigaction sa {}; + sa.sa_handler = [](int) { + RTT_INFO("SIGINT received, stopping...") + stopFlag.test_and_set(); + }; + sigaction(SIGINT, &sa, nullptr); } -void setDarkTheme() { - qApp->setStyle(QStyleFactory::create("Fusion")); - QPalette darkPalette; - darkPalette.setColor(QPalette::Window, QColor(53, 53, 53)); - darkPalette.setColor(QPalette::WindowText, Qt::white); - darkPalette.setColor(QPalette::Base, QColor(25, 25, 25)); - darkPalette.setColor(QPalette::AlternateBase, QColor(53, 53, 53)); - darkPalette.setColor(QPalette::ToolTipBase, Qt::white); - darkPalette.setColor(QPalette::ToolTipText, Qt::white); - darkPalette.setColor(QPalette::Text, Qt::white); - darkPalette.setColor(QPalette::Button, QColor(53, 53, 53)); - darkPalette.setColor(QPalette::ButtonText, Qt::white); - darkPalette.setColor(QPalette::BrightText, Qt::red); - darkPalette.setColor(QPalette::Link, QColor(42, 130, 218)); - darkPalette.setColor(QPalette::Highlight, QColor(42, 130, 218)); - darkPalette.setColor(QPalette::HighlightedText, Qt::black); - qApp->setPalette(darkPalette); - qApp->setStyleSheet("QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }"); +void runStp(std::shared_ptr interfaceGateway) { + rtt::STPManager app{std::move(interfaceGateway)}; + app.start(stopFlag); } int main(int argc, char** argv) { @@ -91,25 +83,8 @@ int main(int argc, char** argv) { RTT_DEBUG("Initialize Interface Server"); auto interfaceGateway = std::make_shared(rtt::GameSettings::isPrimaryAI() ? 12676 : 12677); /// Shared-prt because the variable is shared accross threads - // QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - // QApplication application(argc, argv); - // setDarkTheme(); - // window = new ui::MainWindow{}; - // window->setWindowState(Qt::WindowMaximized); - //Create a flag which signals to the STP thread to stop if the interface is stopped - std::atomic_bool exitApplication = false; - - std::thread stpThread(runStp, interfaceGateway, std::ref(exitApplication)); - - while(true){ - std::this_thread::sleep_for(std::chrono::milliseconds(200)); - } - - // window->show(); - // bool runQT = application.exec(); - - exitApplication = true; + initializeExitHandler(); + std::thread stpThread(runStp, interfaceGateway); stpThread.join(); - return false; }