Skip to content

Commit

Permalink
Merge pull request #16 from Immortals-Robotics/config-visualization
Browse files Browse the repository at this point in the history
Config visualization
  • Loading branch information
lordhippo authored May 2, 2024
2 parents 407c69d + c17c64f commit 1458bcc
Show file tree
Hide file tree
Showing 9 changed files with 293 additions and 105 deletions.
9 changes: 8 additions & 1 deletion source/common/config/config.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include "config.h"

namespace Tyr::Common
{
ConfigReader::ConfigReader(const std::string_view t_file_path)
Expand All @@ -17,4 +16,12 @@ void ConfigReader::load()

m_table = std::move(config);
}

void ConfigReader::save(toml::table t_table)
{
std::ofstream ofs(m_file_path.string());
ofs << t_table;
ofs.close();
}

} // namespace Tyr::Common
6 changes: 4 additions & 2 deletions source/common/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ class ConfigReader

[[nodiscard]] auto getRoot() const
{
return static_cast<toml::node_view<const toml::node>>(m_table);
return m_table;
}

void save(toml::table t_table);

private:
std::filesystem::path m_file_path;

Expand All @@ -26,6 +28,6 @@ struct IConfig
~IConfig() = default;

public:
virtual void load(toml::node_view<const toml::node> t_node) = 0;
virtual void load(toml::table t_table) = 0;
};
} // namespace Tyr::Common
2 changes: 2 additions & 0 deletions source/common/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <span>
#include <string_view>
#include <vector>
#include <fstream>

#include <fmt/format.h>
#include <spdlog/sinks/basic_file_sink.h>
Expand Down Expand Up @@ -43,3 +44,4 @@
#include "math/helpers.h"
#include "math/vector.h"
#include "services.h"
#
24 changes: 20 additions & 4 deletions source/common/services.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@ struct Services
static void initialize()
{
s_logger = new Logger();
s_configReader = new ConfigReader("config.toml");

const ConfigReader config("config.toml");
s_setting = new Setting();
s_setting->load(config.getRoot());
s_setting->load(s_configReader->getRoot());

s_debug = new Debug(s_setting->debug_address, s_setting->enable_debug);

s_global_timer = new Timer();
s_global_timer->start();
}

static void saveConfig()
{
s_configReader->save(s_setting->getConfigTable());
}

static void shutdown()
{
delete s_setting;
Expand All @@ -31,7 +36,12 @@ struct Services
delete s_logger;
}

static const Setting &setting()
static ConfigReader &configReader()
{
return *s_configReader;
}

static Setting &setting()
{
return *s_setting;
}
Expand All @@ -56,9 +66,15 @@ struct Services
static inline Debug *s_debug;
static inline Logger *s_logger;
static inline Timer *s_global_timer;
static inline ConfigReader *s_configReader;
};

static const Setting &setting()
static ConfigReader &configReader()
{
return Services::configReader();
}

static Setting &setting()
{
return Services::setting();
}
Expand Down
51 changes: 46 additions & 5 deletions source/common/setting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace Tyr::Common
{
void Setting::load(const toml::node_view<const toml::node> t_node)
void Setting::load(const toml::table t_table)
{
const auto common = t_node["common"];
m_config_table = t_table;
const auto common = t_table["common"];

immortals_is_the_best_team = common["immortals_is_the_best_team"].value_or(immortals_is_the_best_team);

Expand All @@ -14,7 +15,7 @@ void Setting::load(const toml::node_view<const toml::node> t_node)

enable_debug = common["enable_debug"].value_or(enable_debug);

const auto network = t_node["network"];
const auto network = t_table["network"];

tracker_address.load(network["tracker"]);
debug_address.load(network["debug"]);
Expand All @@ -39,7 +40,7 @@ void Setting::load(const toml::node_view<const toml::node> t_node)
yellow_robot_simulation_address.load(network["yellow_robot_simulation"]);

// vision
const auto vision = t_node["vision"];
const auto vision = t_table["vision"];

if (auto *use_camera_array = vision["use_camera"].as_array())
{
Expand All @@ -64,7 +65,7 @@ void Setting::load(const toml::node_view<const toml::node> t_node)
use_kalman_ang = vision["use_kalman_ang"].value_or(use_kalman_ang);

// soccer
const auto soccer = t_node["soccer"];
const auto soccer = t_table["soccer"];

fillEnum(soccer["our_side"], our_side);

Expand Down Expand Up @@ -124,4 +125,44 @@ void Setting::load(const toml::node_view<const toml::node> t_node)
}
}
}

template <typename T>
void Setting::updateSetting(const std::string &_settings_key, const T &_new_value)
{
toml::node *node = m_config_table.at_path(_settings_key).node();
if (!node)
{
logError("Key not found {}", _settings_key);
return;
}
if constexpr (std::is_same<T, int>::value || std::is_same<T, int64_t>::value)
{
node->ref<toml::value<int64_t>>() = _new_value;
}
else if constexpr (std::is_same<T, double>::value)
{
node->ref<toml::value<double>>() = _new_value;
}
else if constexpr (std::is_same<T, bool>::value)
{
node->ref<toml::value<bool>>() = static_cast<int64_t>(_new_value);
}
else if constexpr (std::is_same<T, std::string>::value)
{
node->ref<toml::value<std::string>>() = _new_value;
}

load(m_config_table);
}

template void Setting::updateSetting<int>(const std::string &key, const int &value);
template void Setting::updateSetting<double>(const std::string &key, const double &value);
template void Setting::updateSetting<std::string>(const std::string &key, const std::string &value);
template void Setting::updateSetting<bool>(const std::string &key, const bool &value);

toml::table Setting::getConfigTable(void)
{
return m_config_table;
}

} // namespace Tyr::Common
12 changes: 8 additions & 4 deletions source/common/setting.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ enum class TeamSide
Right = 1,
};

struct NetworkAddress final : public IConfig
struct NetworkAddress
{
NetworkAddress() = default;

NetworkAddress(const std::string_view ip, const unsigned short port) : ip(ip), port(port)
{}

void load(toml::node_view<const toml::node> t_node) override
void load(toml::node_view<const toml::node> t_node)
{
ip = t_node["ip"].value_or(ip);
port = t_node["port"].value_or(port);
Expand Down Expand Up @@ -58,14 +58,18 @@ struct Setting : IConfig
Setting() = default;
~Setting() = default;

void load(toml::node_view<const toml::node> t_node) override;

void load(toml::table t_table) override;
toml::table m_config_table;
friend struct Services;

public:
Setting(const Setting &) = delete;
Setting &operator=(const Setting &) = delete;

template<typename T>
void updateSetting(const std::string& _settings_key,const T& _new_value);

toml::table getConfigTable(void);
static constexpr size_t kMaxUdpPacketSize = 1024 * 16; // TODO what should the size be really?

// The variety of standard patterns that we can have is 16
Expand Down
12 changes: 6 additions & 6 deletions source/gui/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ void Application::update()
renderer->drawRobots(ssl_packet.detection().robots_blue(), Common::TeamColor::Blue);
renderer->drawRobots(ssl_packet.detection().robots_yellow(), Common::TeamColor::Yellow);
renderer->drawBalls(ssl_packet.detection().balls());
if (config_menu->IsNetworkDataUpdated() == NetworkInput::VisionPort ||
config_menu->IsNetworkDataUpdated() == NetworkInput::VisionIp)
if (config_menu->isNetworkDataUpdated() == InputCallbackType::VISION_PORT ||
config_menu->isNetworkDataUpdated() == InputCallbackType::VISION_IP)
{
updated_address.ip = config_menu->GetNetworkParam(NetworkInput::VisionIp);
updated_address.ip = config_menu->getNetworkParam(InputCallbackType::VISION_IP);
updated_address.port =
static_cast<unsigned short>(std::stoi(config_menu->GetNetworkParam(NetworkInput::VisionPort)));
config_menu->UpdateNetworkData();
static_cast<unsigned short>(std::stoi(config_menu->getNetworkParam(InputCallbackType::VISION_PORT)));
config_menu->updateNetworkData();
udp_client->Update(updated_address);
}
vision_mutex.unlock();
Expand All @@ -117,7 +117,7 @@ void Application::update()
ImGui::End();
}
// end ImGui Content
config_menu->Draw();
config_menu->draw();

rlImGuiEnd();

Expand Down
Loading

0 comments on commit 1458bcc

Please sign in to comment.