Skip to content

Commit

Permalink
refactor: use jitsuyo to get config
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobilizes committed Jun 19, 2024
1 parent 2e61b7c commit 5c3807e
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 162 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ endif()
find_package(ament_cmake REQUIRED)
find_package(ament_index_cpp REQUIRED)
find_package(aruku_interfaces REQUIRED)
find_package(jitsuyo REQUIRED)
find_package(kansei REQUIRED)
find_package(kansei_interfaces REQUIRED)
find_package(keisan REQUIRED)
Expand All @@ -32,7 +33,6 @@ message(STATUS "Using gRPC ${gRPC_VERSION}")

add_library(${PROJECT_NAME} SHARED
"src/${PROJECT_NAME}/config/node/config_node.cpp"
"src/${PROJECT_NAME}/config/utils/config.cpp"
"src/${PROJECT_NAME}/node/aruku_node.cpp"
"src/${PROJECT_NAME}/walking/node/walking_manager.cpp"
"src/${PROJECT_NAME}/walking/node/walking_node.cpp"
Expand Down Expand Up @@ -66,6 +66,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC
ament_target_dependencies(${PROJECT_NAME}
ament_index_cpp
aruku_interfaces
jitsuyo
kansei
kansei_interfaces
keisan
Expand All @@ -78,6 +79,7 @@ ament_target_dependencies(${PROJECT_NAME}
ament_target_dependencies(${PROJECT_NAME}_exported
ament_index_cpp
aruku_interfaces
jitsuyo
kansei
kansei_interfaces
keisan
Expand Down Expand Up @@ -142,6 +144,7 @@ endif()
ament_export_dependencies(
ament_index_cpp
aruku_interfaces
jitsuyo
kansei
kansei_interfaces
keisan
Expand Down
1 change: 0 additions & 1 deletion include/aruku/config/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#ifndef ARUKU__CONFIG__CONFIG_HPP_
#define ARUKU__CONFIG__CONFIG_HPP_

#include "aruku/config/utils/config.hpp"
#include "aruku/config/node/config_node.hpp"

#endif // ARUKU__CONFIG__CONFIG_HPP_
1 change: 0 additions & 1 deletion include/aruku/config/node/config_node.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <memory>
#include <string>

#include "aruku/config/utils/config.hpp"
#include "aruku/config/grpc/config.hpp"
#include "aruku/walking/node/walking_manager.hpp"
#include "aruku/walking/node/walking_node.hpp"
Expand Down
51 changes: 0 additions & 51 deletions include/aruku/config/utils/config.hpp

This file was deleted.

7 changes: 3 additions & 4 deletions src/aruku/config/grpc/call_data_get_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

#include "aruku/config/grpc/call_data_get_config.hpp"

#include "aruku/config/utils/config.hpp"
#include "aruku_interfaces/aruku.grpc.pb.h"
#include "aruku_interfaces/aruku.pb.h"
#include "jitsuyo/config.hpp"
#include "rclcpp/rclcpp.hpp"

namespace aruku
Expand All @@ -44,9 +44,8 @@ void CallDataGetConfig::WaitForRequest()

void CallDataGetConfig::HandleRequest()
{
Config config(path_);
reply_.set_json_kinematic(config.get_config("kinematic"));
reply_.set_json_walking(config.get_config("walking"));
reply_.set_json_kinematic(jitsuyo::load_ordered_config(path_, "kinematic.json").dump());
reply_.set_json_walking(jitsuyo::load_ordered_config(path_, "walking.json").dump());
RCLCPP_INFO(rclcpp::get_logger("Get config"), "config has been sent!");
}
} // namespace aruku
2 changes: 1 addition & 1 deletion src/aruku/config/grpc/call_data_load_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

#include "aruku/config/grpc/call_data_load_config.hpp"

#include "aruku/config/utils/config.hpp"
#include "aruku_interfaces/aruku.grpc.pb.h"
#include "aruku_interfaces/aruku.pb.h"
#include "nlohmann/json.hpp"
#include "rclcpp/rclcpp.hpp"

namespace aruku
Expand Down
2 changes: 1 addition & 1 deletion src/aruku/config/grpc/call_data_publish_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

#include "aruku/config/grpc/call_data_publish_config.hpp"

#include "aruku/config/utils/config.hpp"
#include "aruku_interfaces/aruku.grpc.pb.h"
#include "aruku_interfaces/aruku.pb.h"
#include "nlohmann/json.hpp"
#include "rclcpp/rclcpp.hpp"

namespace aruku {
Expand Down
25 changes: 15 additions & 10 deletions src/aruku/config/grpc/call_data_save_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@

#include "aruku/config/grpc/call_data_save_config.hpp"

#include "aruku/config/utils/config.hpp"
#include "aruku_interfaces/aruku.grpc.pb.h"
#include "aruku_interfaces/aruku.pb.h"
#include "jitsuyo/config.hpp"
#include "rclcpp/rclcpp.hpp"

namespace aruku
Expand All @@ -47,15 +47,20 @@ void CallDataSaveConfig::WaitForRequest()

void CallDataSaveConfig::HandleRequest()
{
Config config(path_);
try {
nlohmann::ordered_json kinematic_data = nlohmann::ordered_json::parse(request_.json_kinematic());
nlohmann::ordered_json walking_data = nlohmann::ordered_json::parse(request_.json_walking());
config.save_config(kinematic_data, walking_data);
walking_manager_->load_config(path_);
RCLCPP_INFO(rclcpp::get_logger("Save config"), "config has been saved!");
} catch (const nlohmann::json::exception & e) {
RCLCPP_ERROR(rclcpp::get_logger("Save config"), e.what());
nlohmann::ordered_json kinematic_data = nlohmann::ordered_json::parse(request_.json_kinematic());
nlohmann::ordered_json walking_data = nlohmann::ordered_json::parse(request_.json_walking());

if (!jitsuyo::save_config(path_, "kinematic.json", kinematic_data)) {
RCLCPP_ERROR(rclcpp::get_logger("Save config"), "Failed to save kinematic config!");
return;
}

if (!jitsuyo::save_config(path_, "walking.json", walking_data)) {
RCLCPP_ERROR(rclcpp::get_logger("Save config"), "Failed to save walking config!");
return;
}

walking_manager_->load_config(path_);
RCLCPP_INFO(rclcpp::get_logger("Save config"), "config has been saved!");
}
} // namespace aruku
3 changes: 1 addition & 2 deletions src/aruku/config/grpc/call_data_set_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

#include "aruku_interfaces/aruku.grpc.pb.h"
#include "aruku_interfaces/aruku.pb.h"
#include "aruku/config/utils/config.hpp"
#include "nlohmann/json.hpp"

namespace aruku {
CallDataSetConfig::CallDataSetConfig(
Expand All @@ -47,7 +47,6 @@ void CallDataSetConfig::WaitForRequest()

void CallDataSetConfig::HandleRequest()
{
Config config(path_);
try {
bool run = request_.run();
double x_move = request_.x_move();
Expand Down
7 changes: 3 additions & 4 deletions src/aruku/config/grpc/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include "aruku/config/utils/config.hpp"

#include <chrono>
#include <csignal>
#include <future>
Expand All @@ -36,6 +34,7 @@
#include "aruku/config/grpc/call_data_set_app_status.hpp"
#include "aruku/config/grpc/call_data_set_config.hpp"
#include "aruku/config/grpc/config.hpp"
#include "jitsuyo/config.hpp"
#include "rclcpp/rclcpp.hpp"

using grpc::ServerBuilder;
Expand All @@ -60,9 +59,9 @@ void ConfigGrpc::Run(const std::string &path,
const rclcpp::Node::SharedPtr & node,
const std::shared_ptr<WalkingNode> &walking_node,
const std::shared_ptr<WalkingManager> &walking_manager) {
Config config(path);
nlohmann::json grpc_config = jitsuyo::load_ordered_config(path, "grpc.json");
std::string server_address = absl::StrFormat(
"0.0.0.0:%d", config.get_grpc_config()["port"].get<uint16_t>());
"0.0.0.0:%d", grpc_config["port"].get<uint16_t>());

ServerBuilder builder;
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
Expand Down
34 changes: 19 additions & 15 deletions src/aruku/config/node/config_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@

#include "aruku/config/node/config_node.hpp"
#include "aruku/node/aruku_node.hpp"
#include "aruku/config/utils/config.hpp"
#include "aruku_interfaces/srv/save_config.hpp"
#include "aruku_interfaces/srv/get_config.hpp"
#include "jitsuyo/config.hpp"
#include "nlohmann/json.hpp"
#include "rclcpp/rclcpp.hpp"
#include "aruku/config/grpc/config.hpp"
Expand All @@ -36,29 +36,33 @@ namespace aruku

ConfigNode::ConfigNode(rclcpp::Node::SharedPtr node, const std::string & path,
const std::shared_ptr<WalkingNode> & walking_node, const std::shared_ptr<WalkingManager> & walking_manager)
: node(node), config(path), set_config_subscriber(nullptr), walking_node(walking_node), walking_manager(walking_manager)
: node(node), set_config_subscriber(nullptr), walking_node(walking_node), walking_manager(walking_manager)
{
get_config_server = node->create_service<GetConfig>(
get_node_prefix() + "/get_config",
[this](GetConfig::Request::SharedPtr request, GetConfig::Response::SharedPtr response) {
response->json_walking = this->config.get_config("walking");
response->json_kinematic = this->config.get_config("kinematic");
[this, path](GetConfig::Request::SharedPtr request, GetConfig::Response::SharedPtr response) {
response->json_walking = jitsuyo::load_ordered_config(path, "walking.json");
response->json_kinematic = jitsuyo::load_ordered_config(path, "kinematic.json");
});

save_config_server = node->create_service<SaveConfig>(
get_node_prefix() + "/save_config",
[this](SaveConfig::Request::SharedPtr request, SaveConfig::Response::SharedPtr response) {
try {
nlohmann::json kinematic_data = nlohmann::json::parse(request->json_kinematic);
nlohmann::json walking_data = nlohmann::json::parse(request->json_walking);
[this, path](SaveConfig::Request::SharedPtr request, SaveConfig::Response::SharedPtr response) {
nlohmann::ordered_json kinematic_data = nlohmann::ordered_json::parse(request->json_kinematic);
nlohmann::ordered_json walking_data = nlohmann::ordered_json::parse(request->json_walking);
response->status = false;

this->config.save_config(kinematic_data, walking_data);
response->status = true;
} catch (std::ofstream::failure) {
response->status = false;
} catch (nlohmann::json::exception) {
response->status = false;
if (!jitsuyo::save_config(path, "kinematic.json", kinematic_data)) {
RCLCPP_ERROR(rclcpp::get_logger("Save config server"), "Failed to save kinematic config");
return;
}

if (!jitsuyo::save_config(path, "walking.json", walking_data)) {
RCLCPP_ERROR(rclcpp::get_logger("Save config server"), "Failed to save walking config");
return;
}

response->status = true;
});
config_grpc.Run(path, node, walking_node, walking_manager);
RCLCPP_INFO(rclcpp::get_logger("GrpcServers"), "grpc running");
Expand Down
71 changes: 0 additions & 71 deletions src/aruku/config/utils/config.cpp

This file was deleted.

0 comments on commit 5c3807e

Please sign in to comment.