Skip to content

Commit

Permalink
Merge branch 'feature/interface-to-erforce-simulator' of github.com:R…
Browse files Browse the repository at this point in the history
…oboTeamTwente/roboteam into feature/interface-to-erforce-simulator
  • Loading branch information
emielsteerneman committed Aug 12, 2023
2 parents bc4674b + 49fcf8a commit b1f2a5e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 48 deletions.
5 changes: 2 additions & 3 deletions roboteam_ai/include/roboteam_ai/STPManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<rtt::ai::io::InterfaceGateway> interfaceGateway, ai::interface::MainWindow* mainWindow);
explicit STPManager(std::shared_ptr<rtt::ai::io::InterfaceGateway> interfaceGateway);

private:
/**
Expand All @@ -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<rtt::ai::io::InterfaceGateway> interfaceGateway; /**< pointer to the InterfaceGateway */

static inline ai::stp::Play* currentPlay{nullptr}; /**< Current best play as picked by the playDecider */
Expand All @@ -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<std::unique_ptr<rtt::ai::stp::Play>> plays; /**< The vector that contains all plays */

Expand Down
7 changes: 3 additions & 4 deletions roboteam_ai/src/STPManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ std::vector<std::unique_ptr<rtt::ai::stp::Play>> 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")
Expand Down Expand Up @@ -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();
}
},
Expand Down Expand Up @@ -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<rtt::ai::io::InterfaceGateway> interfaceGateway, ai::interface::MainWindow *mainWindow): interfaceGateway(std::move(interfaceGateway)) { this->mainWindow = mainWindow; }
STPManager::STPManager(std::shared_ptr<rtt::ai::io::InterfaceGateway> interfaceGateway): interfaceGateway(std::move(interfaceGateway)) { }
} // namespace rtt
57 changes: 16 additions & 41 deletions roboteam_ai/src/roboteam_ai.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <roboteam_utils/Print.h>
#include <memory>
#include <utility>
#include <csignal>

#include "RobotHubMode.h"
#include "STPManager.h"
Expand All @@ -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<rtt::ai::io::InterfaceGateway> 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<rtt::ai::io::InterfaceGateway> interfaceGateway) {
rtt::STPManager app{std::move(interfaceGateway)};
app.start(stopFlag);
}

int main(int argc, char** argv) {
Expand Down Expand Up @@ -91,25 +83,8 @@ int main(int argc, char** argv) {
RTT_DEBUG("Initialize Interface Server");
auto interfaceGateway = std::make_shared<rtt::ai::io::InterfaceGateway>(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;
}

0 comments on commit b1f2a5e

Please sign in to comment.