From 3d38f81fcaaf4f9d76861577f7857b63d1d9cfa5 Mon Sep 17 00:00:00 2001 From: marfanr Date: Tue, 22 Aug 2023 19:40:30 +0700 Subject: [PATCH 01/17] feat/add-grpc --- CMakeLists.txt | 60 +++++++- include/akushon/config/grpc/config.hpp | 130 ++++++++++++++++ include/akushon/config/node/config_node.hpp | 2 + src/akushon/config/grpc/config.cpp | 156 ++++++++++++++++++++ src/akushon/config/node/config_node.cpp | 33 +++-- 5 files changed, 363 insertions(+), 18 deletions(-) create mode 100644 include/akushon/config/grpc/config.hpp create mode 100644 src/akushon/config/grpc/config.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 16267be..90834bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,49 @@ find_package(std_msgs REQUIRED) find_package(tachimawari REQUIRED) find_package(tachimawari_interfaces REQUIRED) +set(protobuf_MODULE_COMPATIBLE TRUE) +find_package(Protobuf CONFIG REQUIRED) +message(STATUS "Using protobuf ${Protobuf_VERSION}") + +set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf) +set(_REFLECTION gRPC::grpc++_reflection) +if(CMAKE_CROSSCOMPILING) + find_program(_PROTOBUF_PROTOC protoc) +else() + set(_PROTOBUF_PROTOC $) +endif() + +# Find gRPC installation +# Looks for gRPCConfig.cmake file installed by gRPC's cmake installation. +find_package(gRPC CONFIG REQUIRED) +message(STATUS "Using gRPC ${gRPC_VERSION}") + +set(_GRPC_GRPCPP gRPC::grpc++) +if(CMAKE_CROSSCOMPILING) + find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin) +else() + set(_GRPC_CPP_PLUGIN_EXECUTABLE $) +endif() + +# Proto file +get_filename_component(akushon_proto "../akushon_interfaces/proto/akushon.proto" ABSOLUTE) +get_filename_component(akushon_proto_path "${akushon_proto}" PATH) + +# Generated sources +set(akushon_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/akushon.pb.cc") +set(akushon_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/akushon.pb.h") +set(akushon_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/akushon.grpc.pb.cc") +set(akushon_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/akushon.grpc.pb.h") +add_custom_command( + OUTPUT "${akushon_proto_srcs}" "${akushon_proto_hdrs}" "${akushon_grpc_srcs}" "${akushon_grpc_hdrs}" + COMMAND ${_PROTOBUF_PROTOC} + ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" + --cpp_out "${CMAKE_CURRENT_BINARY_DIR}" + -I "${akushon_proto_path}" + --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}" + "${akushon_proto}" + DEPENDS "${akushon_proto}") + add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/action/model/action_name.cpp" "src/${PROJECT_NAME}/action/model/action.cpp" @@ -32,9 +75,15 @@ add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/action/process/joint_process.cpp" "src/${PROJECT_NAME}/config/node/config_node.cpp" "src/${PROJECT_NAME}/config/utils/config.cpp" - "src/${PROJECT_NAME}/node/akushon_node.cpp") + "src/${PROJECT_NAME}/config/grpc/config.cpp" + "src/${PROJECT_NAME}/node/akushon_node.cpp" + "${akushon_proto_srcs}" + "${akushon_proto_hdrs}" + "${akushon_grpc_srcs}" + "${akushon_grpc_hdrs}") target_include_directories(${PROJECT_NAME} PUBLIC + $ $ $) @@ -45,7 +94,14 @@ ament_target_dependencies(${PROJECT_NAME} rclcpp_action std_msgs tachimawari - tachimawari_interfaces) + tachimawari_interfaces + gRPC) + + target_link_libraries(${PROJECT_NAME} + ${_REFLECTION} + ${_GRPC_GRPCPP} + ${_PROTOBUF_LIBPROTOBUF} +) install(DIRECTORY "include" DESTINATION ".") diff --git a/include/akushon/config/grpc/config.hpp b/include/akushon/config/grpc/config.hpp new file mode 100644 index 0000000..7333a1f --- /dev/null +++ b/include/akushon/config/grpc/config.hpp @@ -0,0 +1,130 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef AKUSHON__CONFIG__GRPC__CONFIG_HPP_ +#define AKUSHON__CONFIG__GRPC__CONFIG_HPP_ + +#include +#include +#include +#include +#include +#include +#include + +#include "absl/flags/flag.h" +#include "absl/flags/parse.h" +#include "absl/strings/str_format.h" +#include "akushon.grpc.pb.h" +#include "akushon.pb.h" +#include "grpc/support/log.h" +#include "grpcpp/grpcpp.h" +#include "nlohmann/json.hpp" +#include "rclcpp/rclcpp.hpp" + +using akushon_interfaces::proto::Config; + +namespace akushon { + +class ConfigGrpc { +public: + explicit ConfigGrpc(); + explicit ConfigGrpc(const std::string &path); + + ~ConfigGrpc(); + + void Run(uint16_t port, const std::string path); + +private: + std::string path; + static void SignIntHandler(int signum); + + class CallDataBase { + public: + CallDataBase(); + + virtual void Proceed() = 0; + + protected: + virtual void WaitForRequest() = 0; + virtual void HandleRequest() = 0; + }; + + template + class CallData : CallDataBase { + public: + CallData(akushon_interfaces::proto::Config::AsyncService *service, + grpc::ServerCompletionQueue *cq, const std::string path); + + virtual void Proceed() override; + + protected: + virtual void AddNextToCompletionQueue() = 0; + + enum CallStatus { CREATE, PROCESS, FINISH }; + + CallStatus status_; // The current serving state. + + akushon_interfaces::proto::Config::AsyncService *service_; + + const std::string path_; + + grpc::ServerCompletionQueue *cq_; + grpc::ServerContext ctx_; + ConfigRequest request_; + ConfigReply reply_; + grpc::ServerAsyncResponseWriter responder_; + }; + + class CallDataGetConfig : CallData { + public: + CallDataGetConfig(akushon_interfaces::proto::Config::AsyncService *service, + grpc::ServerCompletionQueue *cq, const std::string path); + + protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + }; + + class CallDataSaveConfig : CallData { + public: + CallDataSaveConfig(akushon_interfaces::proto::Config::AsyncService *service, + grpc::ServerCompletionQueue *cq, const std::string path); + + protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + }; + + static inline std::unique_ptr cq_; + static inline std::unique_ptr server_; + std::shared_ptr thread_; + akushon_interfaces::proto::Config::AsyncService service_; + + rclcpp::TimerBase::SharedPtr timer_; +}; + +} // namespace akushon + +#endif // AKUSHON__CONFIG__GRPC__CONFIG_HPP_ diff --git a/include/akushon/config/node/config_node.hpp b/include/akushon/config/node/config_node.hpp index f97c804..ad62aa2 100644 --- a/include/akushon/config/node/config_node.hpp +++ b/include/akushon/config/node/config_node.hpp @@ -25,6 +25,7 @@ #include #include "akushon/config/utils/config.hpp" +#include "akushon/config/grpc/config.hpp" #include "akushon_interfaces/srv/save_actions.hpp" #include "akushon_interfaces/srv/get_actions.hpp" #include "rclcpp/rclcpp.hpp" @@ -44,6 +45,7 @@ class ConfigNode std::string get_node_prefix() const; Config config_util; + ConfigGrpc config_grpc; rclcpp::Service::SharedPtr save_actions_service; rclcpp::Service::SharedPtr get_actions_service; diff --git a/src/akushon/config/grpc/config.cpp b/src/akushon/config/grpc/config.cpp new file mode 100644 index 0000000..c982fb6 --- /dev/null +++ b/src/akushon/config/grpc/config.cpp @@ -0,0 +1,156 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/config.hpp" + +#include +#include +#include + +#include "akushon/config/utils/config.hpp" +#include "rclcpp/rclcpp.hpp" + +using grpc::ServerBuilder; +using namespace std::chrono_literals; + +namespace akushon { + +ConfigGrpc::ConfigGrpc() {} +ConfigGrpc::ConfigGrpc(const std::string &path) : path(path) {} + +ConfigGrpc::~ConfigGrpc() { + server_->Shutdown(); + cq_->Shutdown(); +} + +void ConfigGrpc::SignIntHandler(int signum) { + server_->Shutdown(); + cq_->Shutdown(); + exit(signum); +} + +void ConfigGrpc::Run(uint16_t port, const std::string path) { + std::string server_address = absl::StrFormat("0.0.0.0:%d", port); + + ServerBuilder builder; + builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); + builder.RegisterService(&service_); + + cq_ = builder.AddCompletionQueue(); + server_ = builder.BuildAndStart(); + std::cout << "Server listening on " << server_address << std::endl; + + signal(SIGINT, SignIntHandler); + thread_ = std::make_shared([&path, this]() { + new ConfigGrpc::CallDataGetConfig(&service_, cq_.get(), path); + new ConfigGrpc::CallDataSaveConfig(&service_, cq_.get(), path); + void *tag; // uniquely identifies a request. + bool ok = true; + while (true) { + this->cq_->Next(&tag, &ok); + if (ok) { + static_cast(tag)->Proceed(); + } + + } + }); + thread_->join(); +} + +ConfigGrpc::CallDataBase::CallDataBase() {} + +template +ConfigGrpc::CallData::CallData( + akushon_interfaces::proto::Config::AsyncService *service, + grpc::ServerCompletionQueue *cq, const std::string path) + : status_(CREATE), service_(service), cq_(cq), responder_(&ctx_), + path_(path) {} + +template +void ConfigGrpc::CallData::Proceed() { + if (status_ == CREATE) { + status_ = PROCESS; + WaitForRequest(); + } else if (status_ == PROCESS) { + AddNextToCompletionQueue(); + HandleRequest(); + status_ = FINISH; + responder_.Finish(reply_, grpc::Status::OK, this); + } else { + GPR_ASSERT(status_ == FINISH); + delete this; + } +} + +ConfigGrpc::CallDataGetConfig::CallDataGetConfig( + akushon_interfaces::proto::Config::AsyncService *service, + grpc::ServerCompletionQueue *cq, const std::string path) + : CallData(service, cq, path) { + Proceed(); +} + +void ConfigGrpc::CallDataGetConfig::AddNextToCompletionQueue() { + new ConfigGrpc::CallDataGetConfig(service_, cq_, path_); +} + +void ConfigGrpc::CallDataGetConfig::WaitForRequest() { + service_->RequestGetConfig(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void ConfigGrpc::CallDataGetConfig::HandleRequest() { + Config config(path_); + + reply_.set_json(config.get_config()); + RCLCPP_INFO(rclcpp::get_logger("GetConfig"), "config has been sent!"); +} + +ConfigGrpc::CallDataSaveConfig::CallDataSaveConfig( + akushon_interfaces::proto::Config::AsyncService *service, + grpc::ServerCompletionQueue *cq, const std::string path) + : CallData(service, cq, path) { + Proceed(); +} + +void ConfigGrpc::CallDataSaveConfig::AddNextToCompletionQueue() { + new ConfigGrpc::CallDataSaveConfig(service_, cq_, path_); +} + +void ConfigGrpc::CallDataSaveConfig::WaitForRequest() { + service_->RequestSaveConfig(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void ConfigGrpc::CallDataSaveConfig::HandleRequest() { + Config config(path_); + try { + nlohmann::json akushon_data = + nlohmann::json::parse(request_.json()); + + config.save_config(akushon_data); + RCLCPP_INFO(rclcpp::get_logger("SaveConfig"), "config has been saved! "); + + } catch (std::ofstream::failure f) { + RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), f.what()); + + } catch (nlohmann::json::exception e) { + RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), e.what()); + } +} + +} // namespace aruku \ No newline at end of file diff --git a/src/akushon/config/node/config_node.cpp b/src/akushon/config/node/config_node.cpp index 7baa907..271d9ee 100644 --- a/src/akushon/config/node/config_node.cpp +++ b/src/akushon/config/node/config_node.cpp @@ -34,22 +34,23 @@ namespace akushon ConfigNode::ConfigNode(rclcpp::Node::SharedPtr node, const std::string & path) : config_util(path) { - get_actions_service = node->create_service( - get_node_prefix() + "/get_actions", - [this](std::shared_ptr request, - std::shared_ptr response) { - response->json = this->config_util.get_config(); - } - ); - - save_actions_service = node->create_service( - get_node_prefix() + "/save_actions", - [this](std::shared_ptr request, - std::shared_ptr response) { - this->config_util.save_config(request->json); - response->status = "SAVED"; - } - ); + // get_actions_service = node->create_service( + // get_node_prefix() + "/get_actions", + // [this](std::shared_ptr request, + // std::shared_ptr response) { + // response->json = this->config_util.get_config(); + // } + // ); + + // save_actions_service = node->create_service( + // get_node_prefix() + "/save_actions", + // [this](std::shared_ptr request, + // std::shared_ptr response) { + // this->config_util.save_config(request->json); + // response->status = "SAVED"; + // } + // ); + config_grpc.Run(5060, path); } std::string ConfigNode::get_node_prefix() const From f356700c766c20734c151d5051723df1c3454001 Mon Sep 17 00:00:00 2001 From: marfanr Date: Tue, 3 Oct 2023 19:15:51 +0700 Subject: [PATCH 02/17] feat:add implementation for new proto update --- include/akushon/config/grpc/config.hpp | 121 +++++++++-- src/akushon/config/grpc/config.cpp | 257 ++++++++++++++++++++---- src/akushon/config/node/config_node.cpp | 33 +-- 3 files changed, 336 insertions(+), 75 deletions(-) diff --git a/include/akushon/config/grpc/config.hpp b/include/akushon/config/grpc/config.hpp index 7333a1f..12470b7 100644 --- a/include/akushon/config/grpc/config.hpp +++ b/include/akushon/config/grpc/config.hpp @@ -38,15 +38,20 @@ #include "grpcpp/grpcpp.h" #include "nlohmann/json.hpp" #include "rclcpp/rclcpp.hpp" +#include "tachimawari_interfaces/msg/set_joints.hpp" +#include "tachimawari_interfaces/msg/set_torques.hpp" +#include "tachimawari_interfaces/msg/current_joints.hpp" +#include "akushon_interfaces/msg/run_action.hpp" using akushon_interfaces::proto::Config; -namespace akushon { - -class ConfigGrpc { +namespace akushon +{ +class ConfigGrpc +{ public: explicit ConfigGrpc(); - explicit ConfigGrpc(const std::string &path); + explicit ConfigGrpc(const std::string & path); ~ConfigGrpc(); @@ -56,7 +61,8 @@ class ConfigGrpc { std::string path; static void SignIntHandler(int signum); - class CallDataBase { + class CallDataBase + { public: CallDataBase(); @@ -68,10 +74,12 @@ class ConfigGrpc { }; template - class CallData : CallDataBase { + class CallData : CallDataBase + { public: - CallData(akushon_interfaces::proto::Config::AsyncService *service, - grpc::ServerCompletionQueue *cq, const std::string path); + CallData( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path); virtual void Proceed() override; @@ -80,41 +88,110 @@ class ConfigGrpc { enum CallStatus { CREATE, PROCESS, FINISH }; - CallStatus status_; // The current serving state. + CallStatus status_; // The current serving state. - akushon_interfaces::proto::Config::AsyncService *service_; + akushon_interfaces::proto::Config::AsyncService * service_; const std::string path_; - grpc::ServerCompletionQueue *cq_; + grpc::ServerCompletionQueue * cq_; grpc::ServerContext ctx_; ConfigRequest request_; ConfigReply reply_; grpc::ServerAsyncResponseWriter responder_; }; - class CallDataGetConfig : CallData { + class CallDataGetConfig + : CallData + { + public: + CallDataGetConfig( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path); + + protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + }; + + class CallDataSaveConfig + : CallData + { + public: + CallDataSaveConfig( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path); + + protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + }; + + class CallDataPublishSetJoints + : CallData + { + public: + CallDataPublishSetJoints( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node); + + protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + rclcpp::Node::SharedPtr node_; + rclcpp::Publisher::SharedPtr set_joints_publisher_; + }; + + class CallDataRunAction + : CallData + { + public: + CallDataRunAction( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node); + + protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + rclcpp::Node::SharedPtr node_; + rclcpp::Publisher::SharedPtr run_action_publisher_; + }; + + class CallDataPublishSetTorques + : CallData + { public: - CallDataGetConfig(akushon_interfaces::proto::Config::AsyncService *service, - grpc::ServerCompletionQueue *cq, const std::string path); + CallDataPublishSetTorques( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node); protected: virtual void AddNextToCompletionQueue() override; virtual void WaitForRequest() override; virtual void HandleRequest() override; + rclcpp::Node::SharedPtr node_; + rclcpp::Publisher::SharedPtr set_torque_publisher_; }; - class CallDataSaveConfig : CallData { + class CallDataSubscribeCurrentJoints + : CallData + { public: - CallDataSaveConfig(akushon_interfaces::proto::Config::AsyncService *service, - grpc::ServerCompletionQueue *cq, const std::string path); + CallDataSubscribeCurrentJoints( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node); protected: virtual void AddNextToCompletionQueue() override; virtual void WaitForRequest() override; virtual void HandleRequest() override; + rclcpp::Node::SharedPtr node_; + rclcpp::Subscription::SharedPtr current_joint_subscription_; + tachimawari_interfaces::msg::CurrentJoints curr_joints_; }; static inline std::unique_ptr cq_; @@ -122,9 +199,9 @@ class ConfigGrpc { std::shared_ptr thread_; akushon_interfaces::proto::Config::AsyncService service_; - rclcpp::TimerBase::SharedPtr timer_; + std::thread async_server; }; -} // namespace akushon +} // namespace akushon -#endif // AKUSHON__CONFIG__GRPC__CONFIG_HPP_ +#endif // AKUSHON__CONFIG__GRPC__CONFIG_HPP_ diff --git a/src/akushon/config/grpc/config.cpp b/src/akushon/config/grpc/config.cpp index c982fb6..1cfd91b 100644 --- a/src/akushon/config/grpc/config.cpp +++ b/src/akushon/config/grpc/config.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include "akushon/config/utils/config.hpp" @@ -30,23 +31,26 @@ using grpc::ServerBuilder; using namespace std::chrono_literals; -namespace akushon { - +namespace akushon +{ ConfigGrpc::ConfigGrpc() {} -ConfigGrpc::ConfigGrpc(const std::string &path) : path(path) {} +ConfigGrpc::ConfigGrpc(const std::string & path) : path(path) {} -ConfigGrpc::~ConfigGrpc() { +ConfigGrpc::~ConfigGrpc() +{ server_->Shutdown(); cq_->Shutdown(); } -void ConfigGrpc::SignIntHandler(int signum) { +void ConfigGrpc::SignIntHandler(int signum) +{ server_->Shutdown(); cq_->Shutdown(); - exit(signum); + exit(signum); } -void ConfigGrpc::Run(uint16_t port, const std::string path) { +void ConfigGrpc::Run(uint16_t port, const std::string path) +{ std::string server_address = absl::StrFormat("0.0.0.0:%d", port); ServerBuilder builder; @@ -58,33 +62,34 @@ void ConfigGrpc::Run(uint16_t port, const std::string path) { std::cout << "Server listening on " << server_address << std::endl; signal(SIGINT, SignIntHandler); - thread_ = std::make_shared([&path, this]() { + async_server = std::thread([&path, this]() { new ConfigGrpc::CallDataGetConfig(&service_, cq_.get(), path); new ConfigGrpc::CallDataSaveConfig(&service_, cq_.get(), path); - void *tag; // uniquely identifies a request. + void * tag; // uniquely identifies a request. bool ok = true; - while (true) { + while (true) { this->cq_->Next(&tag, &ok); if (ok) { - static_cast(tag)->Proceed(); + static_cast(tag)->Proceed(); } - } }); - thread_->join(); + std::this_thread::sleep_for(200ms); } ConfigGrpc::CallDataBase::CallDataBase() {} template ConfigGrpc::CallData::CallData( - akushon_interfaces::proto::Config::AsyncService *service, - grpc::ServerCompletionQueue *cq, const std::string path) - : status_(CREATE), service_(service), cq_(cq), responder_(&ctx_), - path_(path) {} + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path) +: status_(CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path) +{ +} template -void ConfigGrpc::CallData::Proceed() { +void ConfigGrpc::CallData::Proceed() +{ if (status_ == CREATE) { status_ = PROCESS; WaitForRequest(); @@ -94,54 +99,61 @@ void ConfigGrpc::CallData::Proceed() { status_ = FINISH; responder_.Finish(reply_, grpc::Status::OK, this); } else { - GPR_ASSERT(status_ == FINISH); + GPR_ASSERT(status_ == FINISH); delete this; } } ConfigGrpc::CallDataGetConfig::CallDataGetConfig( - akushon_interfaces::proto::Config::AsyncService *service, - grpc::ServerCompletionQueue *cq, const std::string path) - : CallData(service, cq, path) { + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path) +: CallData(service, cq, path) +{ Proceed(); } -void ConfigGrpc::CallDataGetConfig::AddNextToCompletionQueue() { +void ConfigGrpc::CallDataGetConfig::AddNextToCompletionQueue() +{ new ConfigGrpc::CallDataGetConfig(service_, cq_, path_); } -void ConfigGrpc::CallDataGetConfig::WaitForRequest() { +void ConfigGrpc::CallDataGetConfig::WaitForRequest() +{ service_->RequestGetConfig(&ctx_, &request_, &responder_, cq_, cq_, this); } -void ConfigGrpc::CallDataGetConfig::HandleRequest() { +void ConfigGrpc::CallDataGetConfig::HandleRequest() +{ Config config(path_); - reply_.set_json(config.get_config()); - RCLCPP_INFO(rclcpp::get_logger("GetConfig"), "config has been sent!"); + reply_.set_json_actions(config.get_config()); + RCLCPP_INFO(rclcpp::get_logger("GetConfig"), "config has been sent!"); } ConfigGrpc::CallDataSaveConfig::CallDataSaveConfig( - akushon_interfaces::proto::Config::AsyncService *service, - grpc::ServerCompletionQueue *cq, const std::string path) - : CallData(service, cq, path) { + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path) +: CallData(service, cq, path) +{ Proceed(); } -void ConfigGrpc::CallDataSaveConfig::AddNextToCompletionQueue() { +void ConfigGrpc::CallDataSaveConfig::AddNextToCompletionQueue() +{ new ConfigGrpc::CallDataSaveConfig(service_, cq_, path_); } -void ConfigGrpc::CallDataSaveConfig::WaitForRequest() { +void ConfigGrpc::CallDataSaveConfig::WaitForRequest() +{ service_->RequestSaveConfig(&ctx_, &request_, &responder_, cq_, cq_, this); } -void ConfigGrpc::CallDataSaveConfig::HandleRequest() { +void ConfigGrpc::CallDataSaveConfig::HandleRequest() +{ Config config(path_); try { - nlohmann::json akushon_data = - nlohmann::json::parse(request_.json()); - + nlohmann::json akushon_data = nlohmann::json::parse(request_.json_actions()); + config.save_config(akushon_data); RCLCPP_INFO(rclcpp::get_logger("SaveConfig"), "config has been saved! "); @@ -153,4 +165,175 @@ void ConfigGrpc::CallDataSaveConfig::HandleRequest() { } } -} // namespace aruku \ No newline at end of file +ConfigGrpc::CallDataPublishSetJoints::CallDataPublishSetJoints( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node) +: CallData(service, cq, path), node_(node) +{ + set_joints_publisher_ = + node_->create_publisher("/joint/set_joints", 10); + Proceed(); +} + +void ConfigGrpc::CallDataPublishSetJoints::AddNextToCompletionQueue() +{ + new ConfigGrpc::CallDataPublishSetJoints(service_, cq_, path_, node_); +} + +void ConfigGrpc::CallDataPublishSetJoints::WaitForRequest() +{ + service_->RequestPublishSetJoints(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void ConfigGrpc::CallDataPublishSetJoints::HandleRequest() +{ + try { + int control_type = request_.control_type(); + tachimawari_interfaces::msg::SetJoints set_joints_; + set_joints_.control_type = control_type; + // set_joints_.joints.push_back(joint_actions); + nlohmann::json publish_set_joint = nlohmann::json::parse(request_.joints_actions()); + for (const auto & items_json_ : publish_set_joint.items()) { + nlohmann::json obj = items_json_.value(); + tachimawari_interfaces::msg::Joint joint; + joint.id = (uint8_t)atoi(obj.at("id").get().c_str()); + joint.position = (float)atoi(obj.at("position").get().c_str()); + set_joints_.joints.push_back(joint); + } + set_joints_publisher_->publish(set_joints_); + + // config.save_config(akushon_data); + RCLCPP_INFO(rclcpp::get_logger("PublishSetJoints"), "set joints has been published! "); + + } catch (std::ofstream::failure f) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), f.what()); + + } catch (nlohmann::json::exception e) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), e.what()); + } +} + +ConfigGrpc::CallDataRunAction::CallDataRunAction( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node) +: CallData(service, cq, path), node_(node) +{ + run_action_publisher_ = + node_->create_publisher("/action/run_action", 10); + Proceed(); +} + +void ConfigGrpc::CallDataRunAction::AddNextToCompletionQueue() +{ + new ConfigGrpc::CallDataRunAction(service_, cq_, path_, node_); +} + +void ConfigGrpc::CallDataRunAction::WaitForRequest() +{ + service_->RequestRunAction(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void ConfigGrpc::CallDataRunAction::HandleRequest() +{ + akushon_interfaces::msg::RunAction run_action; + run_action.control_type = request_.control_type(); + run_action.action_name = request_.action_name(); + run_action.json = request_.json_action(); + run_action_publisher_->publish(run_action); + RCLCPP_INFO(rclcpp::get_logger("PublishSetJoints"), "run action config has been published!"); +} + +ConfigGrpc::CallDataPublishSetTorques::CallDataPublishSetTorques( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node) +: CallData(service, cq, path), node_(node) +{ + set_torque_publisher_ = + node_->create_publisher("/joint/set_torques", 10); + Proceed(); +} + +void ConfigGrpc::CallDataPublishSetTorques::AddNextToCompletionQueue() +{ + new ConfigGrpc::CallDataPublishSetTorques(service_, cq_, path_, node_); +} + +void ConfigGrpc::CallDataPublishSetTorques::WaitForRequest() +{ + service_->RequestSetTorques(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void ConfigGrpc::CallDataPublishSetTorques::HandleRequest() +{ + try { + tachimawari_interfaces::msg::SetTorques set_torque; + set_torque.torque_enable = request_.torque_enable(); + + nlohmann::json publish_set_joint = nlohmann::json::parse(request_.ids()); + for (const auto & items : publish_set_joint.items()) { + set_torque.ids.push_back((uint8_t)atoi(items.value().get().c_str())); + } + set_torque_publisher_->publish(set_torque); + + RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "set torques has been published!"); + + } catch (std::ofstream::failure f) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what()); + + } catch (nlohmann::json::exception e) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what()); + } +} + +ConfigGrpc::CallDataSubscribeCurrentJoints::CallDataSubscribeCurrentJoints( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node) +: CallData(service, cq, path), node_(node) +{ + current_joint_subscription_ = + node_->create_subscription( + "/joint/current_joints", 10, + [this](const tachimawari_interfaces::msg::CurrentJoints::SharedPtr curr_joints) { + RCLCPP_INFO(rclcpp::get_logger("SubscribeCurrentJoints"), "current joints received from tachimawari"); + curr_joints_.joints = curr_joints->joints; + }); + Proceed(); +} // namespace akushon + +void ConfigGrpc::CallDataSubscribeCurrentJoints::AddNextToCompletionQueue() +{ + new ConfigGrpc::CallDataSubscribeCurrentJoints(service_, cq_, path_, node_); +} + +void ConfigGrpc::CallDataSubscribeCurrentJoints::WaitForRequest() +{ + service_->RequestSubscribeCurrentJoints(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void ConfigGrpc::CallDataSubscribeCurrentJoints::HandleRequest() +{ + try { + + nlohmann::json curr_joints; + + // curr_joints["control_type"] = curr_joints_->joints; + for (const auto & items : curr_joints_.joints) { + nlohmann::json obj; + obj["id"] = std::to_string(items.id); + obj["position"] = std::to_string(items.position); + curr_joints.push_back(obj); + } + + reply_.set_msg_joints(curr_joints.dump()); + + RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "curr joints has been sended!"); + + } catch (std::ofstream::failure f) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what()); + + } catch (nlohmann::json::exception e) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what()); + } +} + +} // namespace akushon diff --git a/src/akushon/config/node/config_node.cpp b/src/akushon/config/node/config_node.cpp index 271d9ee..8bb984c 100644 --- a/src/akushon/config/node/config_node.cpp +++ b/src/akushon/config/node/config_node.cpp @@ -34,23 +34,24 @@ namespace akushon ConfigNode::ConfigNode(rclcpp::Node::SharedPtr node, const std::string & path) : config_util(path) { - // get_actions_service = node->create_service( - // get_node_prefix() + "/get_actions", - // [this](std::shared_ptr request, - // std::shared_ptr response) { - // response->json = this->config_util.get_config(); - // } - // ); - - // save_actions_service = node->create_service( - // get_node_prefix() + "/save_actions", - // [this](std::shared_ptr request, - // std::shared_ptr response) { - // this->config_util.save_config(request->json); - // response->status = "SAVED"; - // } - // ); + get_actions_service = node->create_service( + get_node_prefix() + "/get_actions", + [this](std::shared_ptr request, + std::shared_ptr response) { + response->json = this->config_util.get_config(); + } + ); + + save_actions_service = node->create_service( + get_node_prefix() + "/save_actions", + [this](std::shared_ptr request, + std::shared_ptr response) { + this->config_util.save_config(request->json); + response->status = "SAVED"; + } + ); config_grpc.Run(5060, path); + RCLCPP_INFO(rclcpp::get_logger("GrpcServers"), "grpc running"); } std::string ConfigNode::get_node_prefix() const From 41d7535e9a448908c4807d5c13eb34f9a68ba288 Mon Sep 17 00:00:00 2001 From: marfanr Date: Wed, 4 Oct 2023 18:43:31 +0700 Subject: [PATCH 03/17] Fix/forgot to enable the new RPC --- include/akushon/config/grpc/config.hpp | 2 +- src/akushon/config/grpc/config.cpp | 7 +++++-- src/akushon/config/node/config_node.cpp | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/akushon/config/grpc/config.hpp b/include/akushon/config/grpc/config.hpp index 12470b7..3df6566 100644 --- a/include/akushon/config/grpc/config.hpp +++ b/include/akushon/config/grpc/config.hpp @@ -55,7 +55,7 @@ class ConfigGrpc ~ConfigGrpc(); - void Run(uint16_t port, const std::string path); + void Run(uint16_t port, const std::string path, rclcpp::Node::SharedPtr node); private: std::string path; diff --git a/src/akushon/config/grpc/config.cpp b/src/akushon/config/grpc/config.cpp index 1cfd91b..7f8d4bc 100644 --- a/src/akushon/config/grpc/config.cpp +++ b/src/akushon/config/grpc/config.cpp @@ -49,7 +49,7 @@ void ConfigGrpc::SignIntHandler(int signum) exit(signum); } -void ConfigGrpc::Run(uint16_t port, const std::string path) +void ConfigGrpc::Run(uint16_t port, const std::string path, rclcpp::Node::SharedPtr node) { std::string server_address = absl::StrFormat("0.0.0.0:%d", port); @@ -62,9 +62,12 @@ void ConfigGrpc::Run(uint16_t port, const std::string path) std::cout << "Server listening on " << server_address << std::endl; signal(SIGINT, SignIntHandler); - async_server = std::thread([&path, this]() { + async_server = std::thread([&path, this, &node]() { new ConfigGrpc::CallDataGetConfig(&service_, cq_.get(), path); new ConfigGrpc::CallDataSaveConfig(&service_, cq_.get(), path); + new ConfigGrpc::CallDataPublishSetJoints(&service_, cq_.get(), path, node); + new ConfigGrpc::CallDataPublishSetTorques(&service_, cq_.get(), path, node); + new ConfigGrpc::CallDataSubscribeCurrentJoints(&service_, cq_.get(), path, node); void * tag; // uniquely identifies a request. bool ok = true; while (true) { diff --git a/src/akushon/config/node/config_node.cpp b/src/akushon/config/node/config_node.cpp index 8bb984c..ebc73b2 100644 --- a/src/akushon/config/node/config_node.cpp +++ b/src/akushon/config/node/config_node.cpp @@ -50,7 +50,7 @@ ConfigNode::ConfigNode(rclcpp::Node::SharedPtr node, const std::string & path) response->status = "SAVED"; } ); - config_grpc.Run(5060, path); + config_grpc.Run(5060, path, node); RCLCPP_INFO(rclcpp::get_logger("GrpcServers"), "grpc running"); } From a2129a056341e9683f9360f37cf8e6f5b94ff4cb Mon Sep 17 00:00:00 2001 From: marfanr Date: Fri, 6 Oct 2023 11:50:46 +0700 Subject: [PATCH 04/17] Fix/fix json format for ids in SetTorques RPC --- include/akushon/config/grpc/config.hpp | 1 + src/akushon/config/grpc/config.cpp | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/include/akushon/config/grpc/config.hpp b/include/akushon/config/grpc/config.hpp index 3df6566..befb311 100644 --- a/include/akushon/config/grpc/config.hpp +++ b/include/akushon/config/grpc/config.hpp @@ -175,6 +175,7 @@ class ConfigGrpc virtual void HandleRequest() override; rclcpp::Node::SharedPtr node_; rclcpp::Publisher::SharedPtr set_torque_publisher_; + }; class CallDataSubscribeCurrentJoints diff --git a/src/akushon/config/grpc/config.cpp b/src/akushon/config/grpc/config.cpp index 7f8d4bc..a3f0c38 100644 --- a/src/akushon/config/grpc/config.cpp +++ b/src/akushon/config/grpc/config.cpp @@ -244,6 +244,7 @@ void ConfigGrpc::CallDataRunAction::HandleRequest() run_action.json = request_.json_action(); run_action_publisher_->publish(run_action); RCLCPP_INFO(rclcpp::get_logger("PublishSetJoints"), "run action config has been published!"); + } ConfigGrpc::CallDataPublishSetTorques::CallDataPublishSetTorques( @@ -272,12 +273,12 @@ void ConfigGrpc::CallDataPublishSetTorques::HandleRequest() tachimawari_interfaces::msg::SetTorques set_torque; set_torque.torque_enable = request_.torque_enable(); - nlohmann::json publish_set_joint = nlohmann::json::parse(request_.ids()); - for (const auto & items : publish_set_joint.items()) { - set_torque.ids.push_back((uint8_t)atoi(items.value().get().c_str())); - } - set_torque_publisher_->publish(set_torque); + nlohmann::json publish_set_torque = nlohmann::json::parse(request_.ids()); + for (const auto & items : publish_set_torque["id"].items()) { + set_torque.ids.push_back((uint8_t)items.value().get()); + } + set_torque_publisher_->publish(set_torque); RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "set torques has been published!"); } catch (std::ofstream::failure f) { From 7e8f0c789c5caa7fd64e618570acbb4227bd4282 Mon Sep 17 00:00:00 2001 From: marfanr Date: Fri, 6 Oct 2023 17:37:41 +0700 Subject: [PATCH 05/17] Fix/forgot to register a RPC --- src/akushon/config/grpc/config.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/akushon/config/grpc/config.cpp b/src/akushon/config/grpc/config.cpp index a3f0c38..95ae740 100644 --- a/src/akushon/config/grpc/config.cpp +++ b/src/akushon/config/grpc/config.cpp @@ -67,6 +67,7 @@ void ConfigGrpc::Run(uint16_t port, const std::string path, rclcpp::Node::Shared new ConfigGrpc::CallDataSaveConfig(&service_, cq_.get(), path); new ConfigGrpc::CallDataPublishSetJoints(&service_, cq_.get(), path, node); new ConfigGrpc::CallDataPublishSetTorques(&service_, cq_.get(), path, node); + new ConfigGrpc::CallDataRunAction(&service_, cq_.get(), path, node); new ConfigGrpc::CallDataSubscribeCurrentJoints(&service_, cq_.get(), path, node); void * tag; // uniquely identifies a request. bool ok = true; From 0291a4ba9ca1299ef1f1f90897a2353225ac482c Mon Sep 17 00:00:00 2001 From: marfanr Date: Fri, 6 Oct 2023 18:46:48 +0700 Subject: [PATCH 06/17] Fix/fix json parsing problem --- src/akushon/config/grpc/config.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/akushon/config/grpc/config.cpp b/src/akushon/config/grpc/config.cpp index 95ae740..bfb8562 100644 --- a/src/akushon/config/grpc/config.cpp +++ b/src/akushon/config/grpc/config.cpp @@ -197,11 +197,11 @@ void ConfigGrpc::CallDataPublishSetJoints::HandleRequest() set_joints_.control_type = control_type; // set_joints_.joints.push_back(joint_actions); nlohmann::json publish_set_joint = nlohmann::json::parse(request_.joints_actions()); - for (const auto & items_json_ : publish_set_joint.items()) { + for (const auto & items_json_ : publish_set_joint["joints"].items()) { nlohmann::json obj = items_json_.value(); tachimawari_interfaces::msg::Joint joint; - joint.id = (uint8_t)atoi(obj.at("id").get().c_str()); - joint.position = (float)atoi(obj.at("position").get().c_str()); + joint.id = (uint8_t)obj.at("id").get(); + joint.position = (float)obj.at("position").get(); set_joints_.joints.push_back(joint); } set_joints_publisher_->publish(set_joints_); @@ -299,7 +299,7 @@ ConfigGrpc::CallDataSubscribeCurrentJoints::CallDataSubscribeCurrentJoints( node_->create_subscription( "/joint/current_joints", 10, [this](const tachimawari_interfaces::msg::CurrentJoints::SharedPtr curr_joints) { - RCLCPP_INFO(rclcpp::get_logger("SubscribeCurrentJoints"), "current joints received from tachimawari"); + // RCLCPP_INFO(rclcpp::get_logger("SubscribeCurrentJoints"), "current joints received from tachimawari"); curr_joints_.joints = curr_joints->joints; }); Proceed(); From 1c7559c2400d7f1de8c544d78924aa7425955fdc Mon Sep 17 00:00:00 2001 From: marfanr Date: Sun, 29 Oct 2023 17:39:22 +0700 Subject: [PATCH 07/17] feat: refractor akushon --- CMakeLists.txt | 8 + include/akushon/config/grpc/call_data.hpp | 61 ++++ .../akushon/config/grpc/call_data_base.hpp | 38 +++ .../config/grpc/call_data_get_config.hpp | 43 +++ .../grpc/call_data_publish_set_joints.hpp | 47 +++ .../grpc/call_data_publish_set_torques.hpp | 47 +++ .../config/grpc/call_data_run_action.hpp | 47 +++ .../config/grpc/call_data_save_config.hpp | 43 +++ .../call_data_subscribe_current_joints.hpp | 50 ++++ include/akushon/config/grpc/config.hpp | 141 +-------- src/akushon/config/grpc/call_data.cpp | 58 ++++ src/akushon/config/grpc/call_data_base.cpp | 26 ++ .../config/grpc/call_data_get_config.cpp | 54 ++++ .../grpc/call_data_publish_set_joints.cpp | 71 +++++ .../grpc/call_data_publish_set_torques.cpp | 69 +++++ .../config/grpc/call_data_run_action.cpp | 56 ++++ .../config/grpc/call_data_save_config.cpp | 61 ++++ .../call_data_subscribe_current_joints.cpp | 75 +++++ src/akushon/config/grpc/config.cpp | 280 +----------------- 19 files changed, 868 insertions(+), 407 deletions(-) create mode 100644 include/akushon/config/grpc/call_data.hpp create mode 100644 include/akushon/config/grpc/call_data_base.hpp create mode 100644 include/akushon/config/grpc/call_data_get_config.hpp create mode 100644 include/akushon/config/grpc/call_data_publish_set_joints.hpp create mode 100644 include/akushon/config/grpc/call_data_publish_set_torques.hpp create mode 100644 include/akushon/config/grpc/call_data_run_action.hpp create mode 100644 include/akushon/config/grpc/call_data_save_config.hpp create mode 100644 include/akushon/config/grpc/call_data_subscribe_current_joints.hpp create mode 100644 src/akushon/config/grpc/call_data.cpp create mode 100644 src/akushon/config/grpc/call_data_base.cpp create mode 100644 src/akushon/config/grpc/call_data_get_config.cpp create mode 100644 src/akushon/config/grpc/call_data_publish_set_joints.cpp create mode 100644 src/akushon/config/grpc/call_data_publish_set_torques.cpp create mode 100644 src/akushon/config/grpc/call_data_run_action.cpp create mode 100644 src/akushon/config/grpc/call_data_save_config.cpp create mode 100644 src/akushon/config/grpc/call_data_subscribe_current_joints.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 90834bf..aab4ec7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -76,6 +76,14 @@ add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/config/node/config_node.cpp" "src/${PROJECT_NAME}/config/utils/config.cpp" "src/${PROJECT_NAME}/config/grpc/config.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data_base.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data_get_config.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data_save_config.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data_publish_set_joints.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data_publish_set_torques.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data_subscribe_current_joints.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data_run_action.cpp" "src/${PROJECT_NAME}/node/akushon_node.cpp" "${akushon_proto_srcs}" "${akushon_proto_hdrs}" diff --git a/include/akushon/config/grpc/call_data.hpp b/include/akushon/config/grpc/call_data.hpp new file mode 100644 index 0000000..356ec8a --- /dev/null +++ b/include/akushon/config/grpc/call_data.hpp @@ -0,0 +1,61 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ +#define AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ + +#include "akushon.grpc.pb.h" +#include "akushon.pb.h" +#include "grpc/support/log.h" +#include "grpcpp/grpcpp.h" +#include "akushon/config/grpc/call_data_base.hpp" + +namespace akushon +{ +template +class CallData : CallDataBase +{ +public: + CallData( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path); + + virtual void Proceed() override; + +protected: + virtual void AddNextToCompletionQueue() = 0; + + enum CallStatus { CREATE, PROCESS, FINISH }; + + CallStatus status_; // The current serving state. + + akushon_interfaces::proto::Config::AsyncService * service_; + + const std::string path_; + + grpc::ServerCompletionQueue * cq_; + grpc::ServerContext ctx_; + ConfigRequest request_; + ConfigReply reply_; + grpc::ServerAsyncResponseWriter responder_; +}; +} // namespace akushon + +#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ \ No newline at end of file diff --git a/include/akushon/config/grpc/call_data_base.hpp b/include/akushon/config/grpc/call_data_base.hpp new file mode 100644 index 0000000..cf9b317 --- /dev/null +++ b/include/akushon/config/grpc/call_data_base.hpp @@ -0,0 +1,38 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef __AKUSHON__CONFIG__GRPC__CALL_DATA_BASE_HPP__ +#define __AKUSHON__CONFIG__GRPC__CALL_DATA_BASE_HPP__ +namespace akushon +{ +class CallDataBase +{ +public: + CallDataBase(); + + virtual void Proceed() = 0; + +protected: + virtual void WaitForRequest() = 0; + virtual void HandleRequest() = 0; +}; +} // namespace akushon + +#endif // __AKUSHON__CONFIG__GRPC__CALL_DATA_BASE_HPP__ diff --git a/include/akushon/config/grpc/call_data_get_config.hpp b/include/akushon/config/grpc/call_data_get_config.hpp new file mode 100644 index 0000000..26a9732 --- /dev/null +++ b/include/akushon/config/grpc/call_data_get_config.hpp @@ -0,0 +1,43 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef AKUSHON__CONFIG__GRPC__CALL_DATA_GET_CONFIG_HPP_ +#define AKUSHON__CONFIG__GRPC__CALL_DATA_GET_CONFIG_HPP_ + +#include "akushon/config/grpc/call_data.hpp" + +namespace akushon +{ +class CallDataGetConfig +: CallData +{ +public: + CallDataGetConfig( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path); + +protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; +}; +} // namespace akushon + +#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_GET_CONFIG_HPP_ \ No newline at end of file diff --git a/include/akushon/config/grpc/call_data_publish_set_joints.hpp b/include/akushon/config/grpc/call_data_publish_set_joints.hpp new file mode 100644 index 0000000..39edd34 --- /dev/null +++ b/include/akushon/config/grpc/call_data_publish_set_joints.hpp @@ -0,0 +1,47 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef AKUSHON__CONFIG__GRPC__CALL_DATA_PUBLISH_SET_JOINTS_HPP_ +#define AKUSHON__CONFIG__GRPC__CALL_DATA_PUBLISH_SET_JOINTS_HPP_ + +#include "akushon/config/grpc/call_data.hpp" +#include "tachimawari_interfaces/msg/set_joints.hpp" +#include "rclcpp/rclcpp.hpp" + +namespace akushon +{ +class CallDataPublishSetJoints +: CallData +{ +public: + CallDataPublishSetJoints( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node); + +protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + rclcpp::Node::SharedPtr node_; + rclcpp::Publisher::SharedPtr set_joints_publisher_; +}; +} // namespace akushon + +#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_PUBLISH_SET_JOINTS_HPP_ \ No newline at end of file diff --git a/include/akushon/config/grpc/call_data_publish_set_torques.hpp b/include/akushon/config/grpc/call_data_publish_set_torques.hpp new file mode 100644 index 0000000..3c06d7b --- /dev/null +++ b/include/akushon/config/grpc/call_data_publish_set_torques.hpp @@ -0,0 +1,47 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef AKUSHON__CONFIG__GRPC__CALL_DATA_PUBLISH_SET_TORQUES_HPP_ +#define AKUSHON__CONFIG__GRPC__CALL_DATA_PUBLISH_SET_TORQUES_HPP_ + +#include "akushon/config/grpc/call_data.hpp" +#include "tachimawari_interfaces/msg/set_torques.hpp" +#include "rclcpp/rclcpp.hpp" + +namespace akushon +{ +class CallDataPublishSetTorques +: CallData +{ +public: + CallDataPublishSetTorques( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node); + +protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + rclcpp::Node::SharedPtr node_; + rclcpp::Publisher::SharedPtr set_torque_publisher_; +}; +} // namespace akushon + +#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_PUBLISH_SET_TORQUES_HPP_ diff --git a/include/akushon/config/grpc/call_data_run_action.hpp b/include/akushon/config/grpc/call_data_run_action.hpp new file mode 100644 index 0000000..15fa073 --- /dev/null +++ b/include/akushon/config/grpc/call_data_run_action.hpp @@ -0,0 +1,47 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef AKUSHON__CONFIG__GRPC__CALL_DATA_RUN_ACTION_HPP_ +#define AKUSHON__CONFIG__GRPC__CALL_DATA_RUN_ACTION_HPP_ + +#include "akushon/config/grpc/call_data.hpp" +#include "akushon_interfaces/msg/run_action.hpp" +#include "rclcpp/rclcpp.hpp" + +namespace akushon +{ +class CallDataRunAction +: CallData +{ +public: + CallDataRunAction( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node); + +protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + rclcpp::Node::SharedPtr node_; + rclcpp::Publisher::SharedPtr run_action_publisher_; +}; +} // namespace akushon + +#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_RUN_ACTION_HPP_ \ No newline at end of file diff --git a/include/akushon/config/grpc/call_data_save_config.hpp b/include/akushon/config/grpc/call_data_save_config.hpp new file mode 100644 index 0000000..28b5750 --- /dev/null +++ b/include/akushon/config/grpc/call_data_save_config.hpp @@ -0,0 +1,43 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef AKUSHON__CONFIG__GRPC__SAVE_CONFIG_HPP_ +#define AKUSHON__CONFIG__GRPC__SAVE_CONFIG_HPP_ + +#include "akushon/config/grpc/call_data.hpp" + +namespace akushon +{ +class CallDataSaveConfig +: CallData +{ +public: + CallDataSaveConfig( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path); + +protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; +}; +} // namespace akushon + +#endif // AKUSHON__CONFIG__GRPC__SAVE_CONFIG_HPP_ \ No newline at end of file diff --git a/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp b/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp new file mode 100644 index 0000000..68ecb3e --- /dev/null +++ b/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp @@ -0,0 +1,50 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#ifndef AKUSHON__CONFIG__GRPC__CALL_DATA_SUBSCRIBE_CURRENT_JOINTS_HPP_ +#define AKUSHON__CONFIG__GRPC__CALL_DATA_SUBSCRIBE_CURRENT_JOINTS_HPP_ + +#include "akushon/config/grpc/call_data.hpp" +#include "akushon_interfaces/msg/run_action.hpp" +#include "tachimawari_interfaces/msg/current_joints.hpp" +#include "rclcpp/rclcpp.hpp" + +namespace akushon +{ +class CallDataSubscribeCurrentJoints +: CallData +{ +public: + CallDataSubscribeCurrentJoints( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node); + +protected: + virtual void AddNextToCompletionQueue() override; + virtual void WaitForRequest() override; + virtual void HandleRequest() override; + rclcpp::Node::SharedPtr node_; + rclcpp::Subscription::SharedPtr + current_joint_subscription_; + tachimawari_interfaces::msg::CurrentJoints curr_joints_; +}; +} // namespace akushon + +#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_SUBSCRIBE_CURRENT_JOINTS_HPP_ \ No newline at end of file diff --git a/include/akushon/config/grpc/config.hpp b/include/akushon/config/grpc/config.hpp index befb311..667bcb4 100644 --- a/include/akushon/config/grpc/config.hpp +++ b/include/akushon/config/grpc/config.hpp @@ -36,12 +36,7 @@ #include "akushon.pb.h" #include "grpc/support/log.h" #include "grpcpp/grpcpp.h" -#include "nlohmann/json.hpp" #include "rclcpp/rclcpp.hpp" -#include "tachimawari_interfaces/msg/set_joints.hpp" -#include "tachimawari_interfaces/msg/set_torques.hpp" -#include "tachimawari_interfaces/msg/current_joints.hpp" -#include "akushon_interfaces/msg/run_action.hpp" using akushon_interfaces::proto::Config; @@ -59,141 +54,7 @@ class ConfigGrpc private: std::string path; - static void SignIntHandler(int signum); - - class CallDataBase - { - public: - CallDataBase(); - - virtual void Proceed() = 0; - - protected: - virtual void WaitForRequest() = 0; - virtual void HandleRequest() = 0; - }; - - template - class CallData : CallDataBase - { - public: - CallData( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path); - - virtual void Proceed() override; - - protected: - virtual void AddNextToCompletionQueue() = 0; - - enum CallStatus { CREATE, PROCESS, FINISH }; - - CallStatus status_; // The current serving state. - - akushon_interfaces::proto::Config::AsyncService * service_; - - const std::string path_; - - grpc::ServerCompletionQueue * cq_; - grpc::ServerContext ctx_; - ConfigRequest request_; - ConfigReply reply_; - grpc::ServerAsyncResponseWriter responder_; - }; - - class CallDataGetConfig - : CallData - { - public: - CallDataGetConfig( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path); - - protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; - }; - - class CallDataSaveConfig - : CallData - { - public: - CallDataSaveConfig( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path); - - protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; - }; - - class CallDataPublishSetJoints - : CallData - { - public: - CallDataPublishSetJoints( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node); - - protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; - rclcpp::Node::SharedPtr node_; - rclcpp::Publisher::SharedPtr set_joints_publisher_; - }; - - class CallDataRunAction - : CallData - { - public: - CallDataRunAction( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node); - - protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; - rclcpp::Node::SharedPtr node_; - rclcpp::Publisher::SharedPtr run_action_publisher_; - }; - - class CallDataPublishSetTorques - : CallData - { - public: - CallDataPublishSetTorques( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node); - - protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; - rclcpp::Node::SharedPtr node_; - rclcpp::Publisher::SharedPtr set_torque_publisher_; - - }; - - class CallDataSubscribeCurrentJoints - : CallData - { - public: - CallDataSubscribeCurrentJoints( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node); - - protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; - rclcpp::Node::SharedPtr node_; - rclcpp::Subscription::SharedPtr current_joint_subscription_; - tachimawari_interfaces::msg::CurrentJoints curr_joints_; - }; + static void SignIntHandler(int signum); static inline std::unique_ptr cq_; static inline std::unique_ptr server_; diff --git a/src/akushon/config/grpc/call_data.cpp b/src/akushon/config/grpc/call_data.cpp new file mode 100644 index 0000000..e24fac8 --- /dev/null +++ b/src/akushon/config/grpc/call_data.cpp @@ -0,0 +1,58 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/call_data.hpp" + +namespace akushon +{ +template +CallData::CallData( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path) +: status_(CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path) +{ +} + +template +void CallData::Proceed() +{ + if (status_ == CREATE) { + status_ = PROCESS; + WaitForRequest(); + } else if (status_ == PROCESS) { + AddNextToCompletionQueue(); + HandleRequest(); + status_ = FINISH; + responder_.Finish(reply_, grpc::Status::OK, this); + } else { + GPR_ASSERT(status_ == FINISH); + delete this; + } +} + +template class CallData; +template class CallData; +template class CallData; +template class CallData< + akushon_interfaces::proto::SetTorquesData, akushon_interfaces::proto::Empty>; +template class CallData; +template class CallData< + akushon_interfaces::proto::ConfigRunAction, akushon_interfaces::proto::Empty>; +} // namespace akushon \ No newline at end of file diff --git a/src/akushon/config/grpc/call_data_base.cpp b/src/akushon/config/grpc/call_data_base.cpp new file mode 100644 index 0000000..0d7fc35 --- /dev/null +++ b/src/akushon/config/grpc/call_data_base.cpp @@ -0,0 +1,26 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/call_data_base.hpp" + +namespace akushon +{ +CallDataBase::CallDataBase() {} +} \ No newline at end of file diff --git a/src/akushon/config/grpc/call_data_get_config.cpp b/src/akushon/config/grpc/call_data_get_config.cpp new file mode 100644 index 0000000..9fc878c --- /dev/null +++ b/src/akushon/config/grpc/call_data_get_config.cpp @@ -0,0 +1,54 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/call_data_get_config.hpp" +#include "akushon/config/utils/config.hpp" +#include "rclcpp/rclcpp.hpp" + +namespace akushon +{ + +CallDataGetConfig::CallDataGetConfig( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path) +: CallData(service, cq, path) +{ + Proceed(); +} + +void CallDataGetConfig::AddNextToCompletionQueue() +{ + new CallDataGetConfig(service_, cq_, path_); +} + +void CallDataGetConfig::WaitForRequest() +{ + service_->RequestGetConfig(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void CallDataGetConfig::HandleRequest() +{ + Config config(path_); + + reply_.set_json_actions(config.get_config()); + RCLCPP_INFO(rclcpp::get_logger("GetConfig"), "config has been sent!"); +} + +} // namespace akushon \ No newline at end of file diff --git a/src/akushon/config/grpc/call_data_publish_set_joints.cpp b/src/akushon/config/grpc/call_data_publish_set_joints.cpp new file mode 100644 index 0000000..caa92a2 --- /dev/null +++ b/src/akushon/config/grpc/call_data_publish_set_joints.cpp @@ -0,0 +1,71 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/call_data_publish_set_joints.hpp" +#include "akushon/config/utils/config.hpp" +#include "nlohmann/json.hpp" + +namespace akushon +{ +CallDataPublishSetJoints::CallDataPublishSetJoints( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node) +: CallData(service, cq, path), node_(node) +{ + set_joints_publisher_ = + node_->create_publisher("/joint/set_joints", 10); + Proceed(); +} + +void CallDataPublishSetJoints::AddNextToCompletionQueue() +{ + new CallDataPublishSetJoints(service_, cq_, path_, node_); +} + +void CallDataPublishSetJoints::WaitForRequest() +{ + service_->RequestPublishSetJoints(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void CallDataPublishSetJoints::HandleRequest() +{ + try { + int control_type = request_.control_type(); + tachimawari_interfaces::msg::SetJoints set_joints_; + set_joints_.control_type = control_type; + // set_joints_.joints.push_back(joint_actions); + nlohmann::json publish_set_joint = nlohmann::json::parse(request_.joints_actions()); + for (const auto & items_json_ : publish_set_joint["joints"].items()) { + nlohmann::json obj = items_json_.value(); + tachimawari_interfaces::msg::Joint joint; + joint.id = (uint8_t)obj.at("id").get(); + joint.position = (float)obj.at("position").get(); + set_joints_.joints.push_back(joint); + } + set_joints_publisher_->publish(set_joints_); + + RCLCPP_INFO(rclcpp::get_logger("PublishSetJoints"), "set joints has been published!"); + } catch (std::ofstream::failure f) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), f.what()); + } catch (nlohmann::json::exception e) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), e.what()); + } +} +} // namespace akushon diff --git a/src/akushon/config/grpc/call_data_publish_set_torques.cpp b/src/akushon/config/grpc/call_data_publish_set_torques.cpp new file mode 100644 index 0000000..d9d04ed --- /dev/null +++ b/src/akushon/config/grpc/call_data_publish_set_torques.cpp @@ -0,0 +1,69 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/call_data_publish_set_torques.hpp" +#include "akushon/config/utils/config.hpp" +#include "rclcpp/rclcpp.hpp" +#include "nlohmann/json.hpp" + +namespace akushon +{ +CallDataPublishSetTorques::CallDataPublishSetTorques( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node) +: CallData(service, cq, path), node_(node) +{ + set_torque_publisher_ = + node_->create_publisher("/joint/set_torques", 10); + Proceed(); +} + +void CallDataPublishSetTorques::AddNextToCompletionQueue() +{ + new CallDataPublishSetTorques(service_, cq_, path_, node_); +} + +void CallDataPublishSetTorques::WaitForRequest() +{ + service_->RequestSetTorques(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void CallDataPublishSetTorques::HandleRequest() +{ + try { + tachimawari_interfaces::msg::SetTorques set_torque; + set_torque.torque_enable = request_.torque_enable(); + + nlohmann::json publish_set_torque = nlohmann::json::parse(request_.ids()); + + for (const auto & items : publish_set_torque["id"].items()) { + set_torque.ids.push_back((uint8_t)items.value().get()); + } + set_torque_publisher_->publish(set_torque); + RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "set torques has been published!"); + + } catch (std::ofstream::failure f) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what()); + + } catch (nlohmann::json::exception e) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what()); + } +} +} // namespace akushon diff --git a/src/akushon/config/grpc/call_data_run_action.cpp b/src/akushon/config/grpc/call_data_run_action.cpp new file mode 100644 index 0000000..1859b2f --- /dev/null +++ b/src/akushon/config/grpc/call_data_run_action.cpp @@ -0,0 +1,56 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/call_data_run_action.hpp" +#include "akushon/config/utils/config.hpp" +#include "rclcpp/rclcpp.hpp" + +namespace akushon +{ +CallDataRunAction::CallDataRunAction( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node) +: CallData(service, cq, path), node_(node) +{ + run_action_publisher_ = + node_->create_publisher("/action/run_action", 10); + Proceed(); +} + +void CallDataRunAction::AddNextToCompletionQueue() +{ + new CallDataRunAction(service_, cq_, path_, node_); +} + +void CallDataRunAction::WaitForRequest() +{ + service_->RequestRunAction(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void CallDataRunAction::HandleRequest() +{ + akushon_interfaces::msg::RunAction run_action; + run_action.control_type = request_.control_type(); + run_action.action_name = request_.action_name(); + run_action.json = request_.json_action(); + run_action_publisher_->publish(run_action); + RCLCPP_INFO(rclcpp::get_logger("PublishSetJoints"), "run action config has been published!"); +} +} // namespace akushon diff --git a/src/akushon/config/grpc/call_data_save_config.cpp b/src/akushon/config/grpc/call_data_save_config.cpp new file mode 100644 index 0000000..451aa7a --- /dev/null +++ b/src/akushon/config/grpc/call_data_save_config.cpp @@ -0,0 +1,61 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/call_data_save_config.hpp" +#include "akushon/config/utils/config.hpp" +#include "rclcpp/rclcpp.hpp" +#include "nlohmann/json.hpp" + +namespace akushon +{ + +CallDataSaveConfig::CallDataSaveConfig( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path) +: CallData(service, cq, path) +{ + Proceed(); +} + +void CallDataSaveConfig::AddNextToCompletionQueue() +{ + new CallDataSaveConfig(service_, cq_, path_); +} + +void CallDataSaveConfig::WaitForRequest() +{ + service_->RequestSaveConfig(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void CallDataSaveConfig::HandleRequest() +{ + Config config(path_); + try { + nlohmann::json akushon_data = nlohmann::json::parse(request_.json_actions()); + config.save_config(akushon_data); + RCLCPP_INFO(rclcpp::get_logger("SaveConfig"), "config has been saved!"); + } catch (std::ofstream::failure f) { + RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), f.what()); + } catch (nlohmann::json::exception e) { + RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), e.what()); + } +} + +} // namespace akushon \ No newline at end of file diff --git a/src/akushon/config/grpc/call_data_subscribe_current_joints.cpp b/src/akushon/config/grpc/call_data_subscribe_current_joints.cpp new file mode 100644 index 0000000..cedc3de --- /dev/null +++ b/src/akushon/config/grpc/call_data_subscribe_current_joints.cpp @@ -0,0 +1,75 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/call_data_subscribe_current_joints.hpp" + +#include +#include + +#include "akushon/config/utils/config.hpp" +#include "nlohmann/json.hpp" + +namespace akushon +{ +CallDataSubscribeCurrentJoints::CallDataSubscribeCurrentJoints( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string path, rclcpp::Node::SharedPtr node) +: CallData(service, cq, path), node_(node) +{ + Proceed(); +} // namespace akushon + +void CallDataSubscribeCurrentJoints::AddNextToCompletionQueue() +{ + new CallDataSubscribeCurrentJoints(service_, cq_, path_, node_); +} + +void CallDataSubscribeCurrentJoints::WaitForRequest() +{ + service_->RequestSubscribeCurrentJoints(&ctx_, &request_, &responder_, cq_, cq_, this); +} + +void CallDataSubscribeCurrentJoints::HandleRequest() +{ + try { + current_joint_subscription_ = + node_->create_subscription( + "/joint/current_joints", 10, + [this](const tachimawari_interfaces::msg::CurrentJoints::SharedPtr curr_joints) { + curr_joints_.joints = curr_joints->joints; + }); + nlohmann::json curr_joints; + + for (const auto & items : curr_joints_.joints) { + nlohmann::json obj; + obj["id"] = std::to_string(items.id); + obj["position"] = std::to_string(items.position); + curr_joints.push_back(obj); + } + reply_.set_msg_joints(curr_joints.dump()); + RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "curr joints has been sended!"); + current_joint_subscription_.reset(); + } catch (std::ofstream::failure f) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what()); + } catch (nlohmann::json::exception e) { + RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what()); + } +} +} // namespace akushon diff --git a/src/akushon/config/grpc/config.cpp b/src/akushon/config/grpc/config.cpp index bfb8562..e14295a 100644 --- a/src/akushon/config/grpc/config.cpp +++ b/src/akushon/config/grpc/config.cpp @@ -27,6 +27,12 @@ #include "akushon/config/utils/config.hpp" #include "rclcpp/rclcpp.hpp" +#include "akushon/config/grpc/call_data_get_config.hpp" +#include "akushon/config/grpc/call_data_save_config.hpp" +#include "akushon/config/grpc/call_data_publish_set_joints.hpp" +#include "akushon/config/grpc/call_data_publish_set_torques.hpp" +#include "akushon/config/grpc/call_data_subscribe_current_joints.hpp" +#include "akushon/config/grpc/call_data_run_action.hpp" using grpc::ServerBuilder; using namespace std::chrono_literals; @@ -63,282 +69,22 @@ void ConfigGrpc::Run(uint16_t port, const std::string path, rclcpp::Node::Shared signal(SIGINT, SignIntHandler); async_server = std::thread([&path, this, &node]() { - new ConfigGrpc::CallDataGetConfig(&service_, cq_.get(), path); - new ConfigGrpc::CallDataSaveConfig(&service_, cq_.get(), path); - new ConfigGrpc::CallDataPublishSetJoints(&service_, cq_.get(), path, node); - new ConfigGrpc::CallDataPublishSetTorques(&service_, cq_.get(), path, node); - new ConfigGrpc::CallDataRunAction(&service_, cq_.get(), path, node); - new ConfigGrpc::CallDataSubscribeCurrentJoints(&service_, cq_.get(), path, node); + new CallDataGetConfig(&service_, cq_.get(), path); + new CallDataSaveConfig(&service_, cq_.get(), path); + new CallDataPublishSetJoints(&service_, cq_.get(), path, node); + new CallDataPublishSetTorques(&service_, cq_.get(), path, node); + new CallDataRunAction(&service_, cq_.get(), path, node); + new CallDataSubscribeCurrentJoints(&service_, cq_.get(), path, node); void * tag; // uniquely identifies a request. bool ok = true; while (true) { this->cq_->Next(&tag, &ok); if (ok) { - static_cast(tag)->Proceed(); + static_cast(tag)->Proceed(); } } }); std::this_thread::sleep_for(200ms); } -ConfigGrpc::CallDataBase::CallDataBase() {} - -template -ConfigGrpc::CallData::CallData( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path) -: status_(CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path) -{ -} - -template -void ConfigGrpc::CallData::Proceed() -{ - if (status_ == CREATE) { - status_ = PROCESS; - WaitForRequest(); - } else if (status_ == PROCESS) { - AddNextToCompletionQueue(); - HandleRequest(); - status_ = FINISH; - responder_.Finish(reply_, grpc::Status::OK, this); - } else { - GPR_ASSERT(status_ == FINISH); - delete this; - } -} - -ConfigGrpc::CallDataGetConfig::CallDataGetConfig( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path) -: CallData(service, cq, path) -{ - Proceed(); -} - -void ConfigGrpc::CallDataGetConfig::AddNextToCompletionQueue() -{ - new ConfigGrpc::CallDataGetConfig(service_, cq_, path_); -} - -void ConfigGrpc::CallDataGetConfig::WaitForRequest() -{ - service_->RequestGetConfig(&ctx_, &request_, &responder_, cq_, cq_, this); -} - -void ConfigGrpc::CallDataGetConfig::HandleRequest() -{ - Config config(path_); - - reply_.set_json_actions(config.get_config()); - RCLCPP_INFO(rclcpp::get_logger("GetConfig"), "config has been sent!"); -} - -ConfigGrpc::CallDataSaveConfig::CallDataSaveConfig( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path) -: CallData(service, cq, path) -{ - Proceed(); -} - -void ConfigGrpc::CallDataSaveConfig::AddNextToCompletionQueue() -{ - new ConfigGrpc::CallDataSaveConfig(service_, cq_, path_); -} - -void ConfigGrpc::CallDataSaveConfig::WaitForRequest() -{ - service_->RequestSaveConfig(&ctx_, &request_, &responder_, cq_, cq_, this); -} - -void ConfigGrpc::CallDataSaveConfig::HandleRequest() -{ - Config config(path_); - try { - nlohmann::json akushon_data = nlohmann::json::parse(request_.json_actions()); - - config.save_config(akushon_data); - RCLCPP_INFO(rclcpp::get_logger("SaveConfig"), "config has been saved! "); - - } catch (std::ofstream::failure f) { - RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), f.what()); - - } catch (nlohmann::json::exception e) { - RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), e.what()); - } -} - -ConfigGrpc::CallDataPublishSetJoints::CallDataPublishSetJoints( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node) -: CallData(service, cq, path), node_(node) -{ - set_joints_publisher_ = - node_->create_publisher("/joint/set_joints", 10); - Proceed(); -} - -void ConfigGrpc::CallDataPublishSetJoints::AddNextToCompletionQueue() -{ - new ConfigGrpc::CallDataPublishSetJoints(service_, cq_, path_, node_); -} - -void ConfigGrpc::CallDataPublishSetJoints::WaitForRequest() -{ - service_->RequestPublishSetJoints(&ctx_, &request_, &responder_, cq_, cq_, this); -} - -void ConfigGrpc::CallDataPublishSetJoints::HandleRequest() -{ - try { - int control_type = request_.control_type(); - tachimawari_interfaces::msg::SetJoints set_joints_; - set_joints_.control_type = control_type; - // set_joints_.joints.push_back(joint_actions); - nlohmann::json publish_set_joint = nlohmann::json::parse(request_.joints_actions()); - for (const auto & items_json_ : publish_set_joint["joints"].items()) { - nlohmann::json obj = items_json_.value(); - tachimawari_interfaces::msg::Joint joint; - joint.id = (uint8_t)obj.at("id").get(); - joint.position = (float)obj.at("position").get(); - set_joints_.joints.push_back(joint); - } - set_joints_publisher_->publish(set_joints_); - - // config.save_config(akushon_data); - RCLCPP_INFO(rclcpp::get_logger("PublishSetJoints"), "set joints has been published! "); - - } catch (std::ofstream::failure f) { - RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), f.what()); - - } catch (nlohmann::json::exception e) { - RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), e.what()); - } -} - -ConfigGrpc::CallDataRunAction::CallDataRunAction( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node) -: CallData(service, cq, path), node_(node) -{ - run_action_publisher_ = - node_->create_publisher("/action/run_action", 10); - Proceed(); -} - -void ConfigGrpc::CallDataRunAction::AddNextToCompletionQueue() -{ - new ConfigGrpc::CallDataRunAction(service_, cq_, path_, node_); -} - -void ConfigGrpc::CallDataRunAction::WaitForRequest() -{ - service_->RequestRunAction(&ctx_, &request_, &responder_, cq_, cq_, this); -} - -void ConfigGrpc::CallDataRunAction::HandleRequest() -{ - akushon_interfaces::msg::RunAction run_action; - run_action.control_type = request_.control_type(); - run_action.action_name = request_.action_name(); - run_action.json = request_.json_action(); - run_action_publisher_->publish(run_action); - RCLCPP_INFO(rclcpp::get_logger("PublishSetJoints"), "run action config has been published!"); - -} - -ConfigGrpc::CallDataPublishSetTorques::CallDataPublishSetTorques( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node) -: CallData(service, cq, path), node_(node) -{ - set_torque_publisher_ = - node_->create_publisher("/joint/set_torques", 10); - Proceed(); -} - -void ConfigGrpc::CallDataPublishSetTorques::AddNextToCompletionQueue() -{ - new ConfigGrpc::CallDataPublishSetTorques(service_, cq_, path_, node_); -} - -void ConfigGrpc::CallDataPublishSetTorques::WaitForRequest() -{ - service_->RequestSetTorques(&ctx_, &request_, &responder_, cq_, cq_, this); -} - -void ConfigGrpc::CallDataPublishSetTorques::HandleRequest() -{ - try { - tachimawari_interfaces::msg::SetTorques set_torque; - set_torque.torque_enable = request_.torque_enable(); - - nlohmann::json publish_set_torque = nlohmann::json::parse(request_.ids()); - - for (const auto & items : publish_set_torque["id"].items()) { - set_torque.ids.push_back((uint8_t)items.value().get()); - } - set_torque_publisher_->publish(set_torque); - RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "set torques has been published!"); - - } catch (std::ofstream::failure f) { - RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what()); - - } catch (nlohmann::json::exception e) { - RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what()); - } -} - -ConfigGrpc::CallDataSubscribeCurrentJoints::CallDataSubscribeCurrentJoints( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node) -: CallData(service, cq, path), node_(node) -{ - current_joint_subscription_ = - node_->create_subscription( - "/joint/current_joints", 10, - [this](const tachimawari_interfaces::msg::CurrentJoints::SharedPtr curr_joints) { - // RCLCPP_INFO(rclcpp::get_logger("SubscribeCurrentJoints"), "current joints received from tachimawari"); - curr_joints_.joints = curr_joints->joints; - }); - Proceed(); -} // namespace akushon - -void ConfigGrpc::CallDataSubscribeCurrentJoints::AddNextToCompletionQueue() -{ - new ConfigGrpc::CallDataSubscribeCurrentJoints(service_, cq_, path_, node_); -} - -void ConfigGrpc::CallDataSubscribeCurrentJoints::WaitForRequest() -{ - service_->RequestSubscribeCurrentJoints(&ctx_, &request_, &responder_, cq_, cq_, this); -} - -void ConfigGrpc::CallDataSubscribeCurrentJoints::HandleRequest() -{ - try { - - nlohmann::json curr_joints; - - // curr_joints["control_type"] = curr_joints_->joints; - for (const auto & items : curr_joints_.joints) { - nlohmann::json obj; - obj["id"] = std::to_string(items.id); - obj["position"] = std::to_string(items.position); - curr_joints.push_back(obj); - } - - reply_.set_msg_joints(curr_joints.dump()); - - RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "curr joints has been sended!"); - - } catch (std::ofstream::failure f) { - RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what()); - - } catch (nlohmann::json::exception e) { - RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what()); - } -} - } // namespace akushon From a327149eb3e8809345f55d2ba97f0dfc66dffacd Mon Sep 17 00:00:00 2001 From: marfanr Date: Fri, 3 Nov 2023 18:53:25 +0700 Subject: [PATCH 08/17] feat: move proto compile part to akushon interfaces --- CMakeLists.txt | 10 ++++------ include/akushon/config/grpc/call_data.hpp | 4 ++-- include/akushon/config/grpc/config.hpp | 4 ++-- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index aab4ec7..ab138b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,13 +85,10 @@ add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/config/grpc/call_data_subscribe_current_joints.cpp" "src/${PROJECT_NAME}/config/grpc/call_data_run_action.cpp" "src/${PROJECT_NAME}/node/akushon_node.cpp" - "${akushon_proto_srcs}" - "${akushon_proto_hdrs}" - "${akushon_grpc_srcs}" - "${akushon_grpc_hdrs}") + ) target_include_directories(${PROJECT_NAME} PUBLIC - $ + # $ $ $) @@ -117,7 +114,8 @@ install(TARGETS ${PROJECT_NAME} EXPORT export_${PROJECT_NAME} ARCHIVE DESTINATION "lib" LIBRARY DESTINATION "lib" - RUNTIME DESTINATION "bin") + RUNTIME DESTINATION "bin" + INCLUDES DESTINATION "include") add_executable(action "src/action_main.cpp") target_include_directories(action PUBLIC diff --git a/include/akushon/config/grpc/call_data.hpp b/include/akushon/config/grpc/call_data.hpp index 356ec8a..613cc0a 100644 --- a/include/akushon/config/grpc/call_data.hpp +++ b/include/akushon/config/grpc/call_data.hpp @@ -21,8 +21,8 @@ #ifndef AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ #define AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ -#include "akushon.grpc.pb.h" -#include "akushon.pb.h" +#include "akushon_interfaces/akushon.grpc.pb.h" +#include "akushon_interfaces/akushon.pb.h" #include "grpc/support/log.h" #include "grpcpp/grpcpp.h" #include "akushon/config/grpc/call_data_base.hpp" diff --git a/include/akushon/config/grpc/config.hpp b/include/akushon/config/grpc/config.hpp index 667bcb4..b4e617e 100644 --- a/include/akushon/config/grpc/config.hpp +++ b/include/akushon/config/grpc/config.hpp @@ -32,8 +32,8 @@ #include "absl/flags/flag.h" #include "absl/flags/parse.h" #include "absl/strings/str_format.h" -#include "akushon.grpc.pb.h" -#include "akushon.pb.h" +#include "akushon_interfaces/akushon.grpc.pb.h" +#include "akushon_interfaces/akushon.pb.h" #include "grpc/support/log.h" #include "grpcpp/grpcpp.h" #include "rclcpp/rclcpp.hpp" From 1d11b8e511b7966e46fdaa8e8604648c054a3b90 Mon Sep 17 00:00:00 2001 From: marfanr Date: Tue, 12 Dec 2023 12:11:20 +0000 Subject: [PATCH 09/17] feat: fix problem on running grpc --- CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab138b3..f7bb60f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,6 +8,7 @@ endif() if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 17) endif() +set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" ) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic -fPIC) From 1e8d44d67ea1aaab74dab6b275bf370cae92359c Mon Sep 17 00:00:00 2001 From: marfanr Date: Tue, 26 Dec 2023 11:26:14 +0000 Subject: [PATCH 10/17] feat: refractor and resolve pull request --- CMakeLists.txt | 21 ++----- include/akushon/config/grpc/call_data.hpp | 24 ++++++-- .../config/grpc/call_data_get_config.hpp | 8 +-- .../grpc/call_data_publish_set_joints.hpp | 8 +-- .../grpc/call_data_publish_set_torques.hpp | 2 +- .../config/grpc/call_data_run_action.hpp | 8 +-- .../config/grpc/call_data_save_config.hpp | 8 +-- .../call_data_subscribe_current_joints.hpp | 8 +-- include/akushon/config/grpc/config.hpp | 5 +- src/akushon/config/grpc/call_data.cpp | 58 ------------------- .../config/grpc/call_data_get_config.cpp | 2 +- .../grpc/call_data_publish_set_joints.cpp | 4 +- .../grpc/call_data_publish_set_torques.cpp | 5 +- .../config/grpc/call_data_run_action.cpp | 2 +- .../config/grpc/call_data_save_config.cpp | 4 +- .../call_data_subscribe_current_joints.cpp | 4 +- src/akushon/config/grpc/config.cpp | 17 +++--- 17 files changed, 60 insertions(+), 128 deletions(-) delete mode 100644 src/akushon/config/grpc/call_data.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f7bb60f..28ffccb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,6 @@ endif() if(NOT CMAKE_CXX_STANDARD) set(CMAKE_CXX_STANDARD 17) endif() -set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC" ) if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") add_compile_options(-Wall -Wextra -Wpedantic -fPIC) @@ -27,25 +26,13 @@ set(protobuf_MODULE_COMPATIBLE TRUE) find_package(Protobuf CONFIG REQUIRED) message(STATUS "Using protobuf ${Protobuf_VERSION}") -set(_PROTOBUF_LIBPROTOBUF protobuf::libprotobuf) -set(_REFLECTION gRPC::grpc++_reflection) -if(CMAKE_CROSSCOMPILING) - find_program(_PROTOBUF_PROTOC protoc) -else() - set(_PROTOBUF_PROTOC $) -endif() - # Find gRPC installation # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation. find_package(gRPC CONFIG REQUIRED) message(STATUS "Using gRPC ${gRPC_VERSION}") set(_GRPC_GRPCPP gRPC::grpc++) -if(CMAKE_CROSSCOMPILING) - find_program(_GRPC_CPP_PLUGIN_EXECUTABLE grpc_cpp_plugin) -else() - set(_GRPC_CPP_PLUGIN_EXECUTABLE $) -endif() +set(_GRPC_CPP_PLUGIN_EXECUTABLE $) # Proto file get_filename_component(akushon_proto "../akushon_interfaces/proto/akushon.proto" ABSOLUTE) @@ -104,9 +91,9 @@ ament_target_dependencies(${PROJECT_NAME} gRPC) target_link_libraries(${PROJECT_NAME} - ${_REFLECTION} + gRPC::grpc++_reflection ${_GRPC_GRPCPP} - ${_PROTOBUF_LIBPROTOBUF} + protobuf::libprotobuf ) install(DIRECTORY "include" DESTINATION ".") @@ -118,6 +105,8 @@ install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "bin" INCLUDES DESTINATION "include") +target_compile_options(${PROJECT_NAME} PRIVATE -fPIC) + add_executable(action "src/action_main.cpp") target_include_directories(action PUBLIC $ diff --git a/include/akushon/config/grpc/call_data.hpp b/include/akushon/config/grpc/call_data.hpp index 613cc0a..fb93cc3 100644 --- a/include/akushon/config/grpc/call_data.hpp +++ b/include/akushon/config/grpc/call_data.hpp @@ -35,20 +35,34 @@ class CallData : CallDataBase public: CallData( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path); - - virtual void Proceed() override; + const std::string& path) : status_(CallStatus::CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path) +{ +} + void Proceed() override { + if (status_ == CallStatus::CREATE) { + status_ = CallStatus::PROCESS; + WaitForRequest(); + } else if (status_ == CallStatus::PROCESS) { + AddNextToCompletionQueue(); + HandleRequest(); + status_ = CallStatus::FINISH; + responder_.Finish(reply_, grpc::Status::OK, this); + } else { + GPR_ASSERT(status_ == CallStatus::FINISH); + delete this; + } + } protected: virtual void AddNextToCompletionQueue() = 0; - enum CallStatus { CREATE, PROCESS, FINISH }; + enum class CallStatus { CREATE, PROCESS, FINISH }; CallStatus status_; // The current serving state. akushon_interfaces::proto::Config::AsyncService * service_; - const std::string path_; + const std::string& path_; grpc::ServerCompletionQueue * cq_; grpc::ServerContext ctx_; diff --git a/include/akushon/config/grpc/call_data_get_config.hpp b/include/akushon/config/grpc/call_data_get_config.hpp index 26a9732..0c77422 100644 --- a/include/akushon/config/grpc/call_data_get_config.hpp +++ b/include/akushon/config/grpc/call_data_get_config.hpp @@ -31,12 +31,12 @@ class CallDataGetConfig public: CallDataGetConfig( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path); + const std::string& path); protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; + void AddNextToCompletionQueue() override; + void WaitForRequest() override; + void HandleRequest() override; }; } // namespace akushon diff --git a/include/akushon/config/grpc/call_data_publish_set_joints.hpp b/include/akushon/config/grpc/call_data_publish_set_joints.hpp index 39edd34..3622f9e 100644 --- a/include/akushon/config/grpc/call_data_publish_set_joints.hpp +++ b/include/akushon/config/grpc/call_data_publish_set_joints.hpp @@ -33,12 +33,12 @@ class CallDataPublishSetJoints public: CallDataPublishSetJoints( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node); + const std::string& path, rclcpp::Node::SharedPtr& node); protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; + void AddNextToCompletionQueue() override; + void WaitForRequest() override; + void HandleRequest() override; rclcpp::Node::SharedPtr node_; rclcpp::Publisher::SharedPtr set_joints_publisher_; }; diff --git a/include/akushon/config/grpc/call_data_publish_set_torques.hpp b/include/akushon/config/grpc/call_data_publish_set_torques.hpp index 3c06d7b..1cba37e 100644 --- a/include/akushon/config/grpc/call_data_publish_set_torques.hpp +++ b/include/akushon/config/grpc/call_data_publish_set_torques.hpp @@ -33,7 +33,7 @@ class CallDataPublishSetTorques public: CallDataPublishSetTorques( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node); + const std::string& path, rclcpp::Node::SharedPtr& node); protected: virtual void AddNextToCompletionQueue() override; diff --git a/include/akushon/config/grpc/call_data_run_action.hpp b/include/akushon/config/grpc/call_data_run_action.hpp index 15fa073..026051b 100644 --- a/include/akushon/config/grpc/call_data_run_action.hpp +++ b/include/akushon/config/grpc/call_data_run_action.hpp @@ -33,12 +33,12 @@ class CallDataRunAction public: CallDataRunAction( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node); + const std::string& path, rclcpp::Node::SharedPtr& node); protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; + void AddNextToCompletionQueue() override; + void WaitForRequest() override; + void HandleRequest() override; rclcpp::Node::SharedPtr node_; rclcpp::Publisher::SharedPtr run_action_publisher_; }; diff --git a/include/akushon/config/grpc/call_data_save_config.hpp b/include/akushon/config/grpc/call_data_save_config.hpp index 28b5750..7c6ee50 100644 --- a/include/akushon/config/grpc/call_data_save_config.hpp +++ b/include/akushon/config/grpc/call_data_save_config.hpp @@ -31,12 +31,12 @@ class CallDataSaveConfig public: CallDataSaveConfig( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path); + const std::string& path); protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; + void AddNextToCompletionQueue() override; + void WaitForRequest() override; + void HandleRequest() override; }; } // namespace akushon diff --git a/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp b/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp index 68ecb3e..ab19ef5 100644 --- a/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp +++ b/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp @@ -34,12 +34,12 @@ class CallDataSubscribeCurrentJoints public: CallDataSubscribeCurrentJoints( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node); + const std::string& path, rclcpp::Node::SharedPtr& node); protected: - virtual void AddNextToCompletionQueue() override; - virtual void WaitForRequest() override; - virtual void HandleRequest() override; + void AddNextToCompletionQueue() override; + void WaitForRequest() override; + void HandleRequest() override; rclcpp::Node::SharedPtr node_; rclcpp::Subscription::SharedPtr current_joint_subscription_; diff --git a/include/akushon/config/grpc/config.hpp b/include/akushon/config/grpc/config.hpp index b4e617e..75317e7 100644 --- a/include/akushon/config/grpc/config.hpp +++ b/include/akushon/config/grpc/config.hpp @@ -50,11 +50,10 @@ class ConfigGrpc ~ConfigGrpc(); - void Run(uint16_t port, const std::string path, rclcpp::Node::SharedPtr node); + void Run(uint16_t port, const std::string& path, rclcpp::Node::SharedPtr& node); private: - std::string path; - static void SignIntHandler(int signum); + std::string path; static inline std::unique_ptr cq_; static inline std::unique_ptr server_; diff --git a/src/akushon/config/grpc/call_data.cpp b/src/akushon/config/grpc/call_data.cpp deleted file mode 100644 index e24fac8..0000000 --- a/src/akushon/config/grpc/call_data.cpp +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) 2023 Ichiro ITS -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include "akushon/config/grpc/call_data.hpp" - -namespace akushon -{ -template -CallData::CallData( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path) -: status_(CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path) -{ -} - -template -void CallData::Proceed() -{ - if (status_ == CREATE) { - status_ = PROCESS; - WaitForRequest(); - } else if (status_ == PROCESS) { - AddNextToCompletionQueue(); - HandleRequest(); - status_ = FINISH; - responder_.Finish(reply_, grpc::Status::OK, this); - } else { - GPR_ASSERT(status_ == FINISH); - delete this; - } -} - -template class CallData; -template class CallData; -template class CallData; -template class CallData< - akushon_interfaces::proto::SetTorquesData, akushon_interfaces::proto::Empty>; -template class CallData; -template class CallData< - akushon_interfaces::proto::ConfigRunAction, akushon_interfaces::proto::Empty>; -} // namespace akushon \ No newline at end of file diff --git a/src/akushon/config/grpc/call_data_get_config.cpp b/src/akushon/config/grpc/call_data_get_config.cpp index 9fc878c..db0f8b2 100644 --- a/src/akushon/config/grpc/call_data_get_config.cpp +++ b/src/akushon/config/grpc/call_data_get_config.cpp @@ -27,7 +27,7 @@ namespace akushon CallDataGetConfig::CallDataGetConfig( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path) + const std::string& path) : CallData(service, cq, path) { Proceed(); diff --git a/src/akushon/config/grpc/call_data_publish_set_joints.cpp b/src/akushon/config/grpc/call_data_publish_set_joints.cpp index caa92a2..2d89890 100644 --- a/src/akushon/config/grpc/call_data_publish_set_joints.cpp +++ b/src/akushon/config/grpc/call_data_publish_set_joints.cpp @@ -26,7 +26,7 @@ namespace akushon { CallDataPublishSetJoints::CallDataPublishSetJoints( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node) + const std::string& path, rclcpp::Node::SharedPtr& node) : CallData(service, cq, path), node_(node) { set_joints_publisher_ = @@ -62,8 +62,6 @@ void CallDataPublishSetJoints::HandleRequest() set_joints_publisher_->publish(set_joints_); RCLCPP_INFO(rclcpp::get_logger("PublishSetJoints"), "set joints has been published!"); - } catch (std::ofstream::failure f) { - RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), f.what()); } catch (nlohmann::json::exception e) { RCLCPP_ERROR(rclcpp::get_logger("PublishSetJoints"), e.what()); } diff --git a/src/akushon/config/grpc/call_data_publish_set_torques.cpp b/src/akushon/config/grpc/call_data_publish_set_torques.cpp index d9d04ed..2f9f79f 100644 --- a/src/akushon/config/grpc/call_data_publish_set_torques.cpp +++ b/src/akushon/config/grpc/call_data_publish_set_torques.cpp @@ -27,7 +27,7 @@ namespace akushon { CallDataPublishSetTorques::CallDataPublishSetTorques( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node) + const std::string& path, rclcpp::Node::SharedPtr& node) : CallData(service, cq, path), node_(node) { set_torque_publisher_ = @@ -59,9 +59,6 @@ void CallDataPublishSetTorques::HandleRequest() set_torque_publisher_->publish(set_torque); RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "set torques has been published!"); - } catch (std::ofstream::failure f) { - RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what()); - } catch (nlohmann::json::exception e) { RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what()); } diff --git a/src/akushon/config/grpc/call_data_run_action.cpp b/src/akushon/config/grpc/call_data_run_action.cpp index 1859b2f..a6d5510 100644 --- a/src/akushon/config/grpc/call_data_run_action.cpp +++ b/src/akushon/config/grpc/call_data_run_action.cpp @@ -26,7 +26,7 @@ namespace akushon { CallDataRunAction::CallDataRunAction( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node) + const std::string& path, rclcpp::Node::SharedPtr& node) : CallData(service, cq, path), node_(node) { run_action_publisher_ = diff --git a/src/akushon/config/grpc/call_data_save_config.cpp b/src/akushon/config/grpc/call_data_save_config.cpp index 451aa7a..4f88bc6 100644 --- a/src/akushon/config/grpc/call_data_save_config.cpp +++ b/src/akushon/config/grpc/call_data_save_config.cpp @@ -28,7 +28,7 @@ namespace akushon CallDataSaveConfig::CallDataSaveConfig( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path) + const std::string& path) : CallData(service, cq, path) { Proceed(); @@ -51,8 +51,6 @@ void CallDataSaveConfig::HandleRequest() nlohmann::json akushon_data = nlohmann::json::parse(request_.json_actions()); config.save_config(akushon_data); RCLCPP_INFO(rclcpp::get_logger("SaveConfig"), "config has been saved!"); - } catch (std::ofstream::failure f) { - RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), f.what()); } catch (nlohmann::json::exception e) { RCLCPP_ERROR(rclcpp::get_logger("SaveConfig"), e.what()); } diff --git a/src/akushon/config/grpc/call_data_subscribe_current_joints.cpp b/src/akushon/config/grpc/call_data_subscribe_current_joints.cpp index cedc3de..8c564a1 100644 --- a/src/akushon/config/grpc/call_data_subscribe_current_joints.cpp +++ b/src/akushon/config/grpc/call_data_subscribe_current_joints.cpp @@ -30,7 +30,7 @@ namespace akushon { CallDataSubscribeCurrentJoints::CallDataSubscribeCurrentJoints( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string path, rclcpp::Node::SharedPtr node) + const std::string& path, rclcpp::Node::SharedPtr& node) : CallData(service, cq, path), node_(node) { Proceed(); @@ -66,8 +66,6 @@ void CallDataSubscribeCurrentJoints::HandleRequest() reply_.set_msg_joints(curr_joints.dump()); RCLCPP_INFO(rclcpp::get_logger("PublishSetTorques"), "curr joints has been sended!"); current_joint_subscription_.reset(); - } catch (std::ofstream::failure f) { - RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), f.what()); } catch (nlohmann::json::exception e) { RCLCPP_ERROR(rclcpp::get_logger("PublishSetTorques"), e.what()); } diff --git a/src/akushon/config/grpc/config.cpp b/src/akushon/config/grpc/config.cpp index e14295a..b3589d5 100644 --- a/src/akushon/config/grpc/config.cpp +++ b/src/akushon/config/grpc/config.cpp @@ -48,14 +48,7 @@ ConfigGrpc::~ConfigGrpc() cq_->Shutdown(); } -void ConfigGrpc::SignIntHandler(int signum) -{ - server_->Shutdown(); - cq_->Shutdown(); - exit(signum); -} - -void ConfigGrpc::Run(uint16_t port, const std::string path, rclcpp::Node::SharedPtr node) +void ConfigGrpc::Run(uint16_t port, const std::string& path, rclcpp::Node::SharedPtr& node) { std::string server_address = absl::StrFormat("0.0.0.0:%d", port); @@ -67,8 +60,12 @@ void ConfigGrpc::Run(uint16_t port, const std::string path, rclcpp::Node::Shared server_ = builder.BuildAndStart(); std::cout << "Server listening on " << server_address << std::endl; - signal(SIGINT, SignIntHandler); - async_server = std::thread([&path, this, &node]() { + std::signal(SIGINT, [](int signum) { + server_->Shutdown(); + cq_->Shutdown(); + exit(signum); + }); + async_server = std::thread([path, this, &node]() { new CallDataGetConfig(&service_, cq_.get(), path); new CallDataSaveConfig(&service_, cq_.get(), path); new CallDataPublishSetJoints(&service_, cq_.get(), path, node); From 998e4cde71db73740b4b4c7752c0dd172a4e631b Mon Sep 17 00:00:00 2001 From: marfanr Date: Tue, 26 Dec 2023 11:38:45 +0000 Subject: [PATCH 11/17] feat: refractor and resolve pull request --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 28ffccb..dd8bd9e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,8 +64,7 @@ add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/config/node/config_node.cpp" "src/${PROJECT_NAME}/config/utils/config.cpp" "src/${PROJECT_NAME}/config/grpc/config.cpp" - "src/${PROJECT_NAME}/config/grpc/call_data_base.cpp" - "src/${PROJECT_NAME}/config/grpc/call_data.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data_base.cpp" "src/${PROJECT_NAME}/config/grpc/call_data_get_config.cpp" "src/${PROJECT_NAME}/config/grpc/call_data_save_config.cpp" "src/${PROJECT_NAME}/config/grpc/call_data_publish_set_joints.cpp" From 0bac70d4f333a9003a81ab45884e9d836ac798c3 Mon Sep 17 00:00:00 2001 From: marfanr Date: Tue, 26 Dec 2023 20:56:09 +0000 Subject: [PATCH 12/17] feat: remove proto compile part from CMakeLists --- CMakeLists.txt | 26 ++------------------------ 1 file changed, 2 insertions(+), 24 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dd8bd9e..f14009a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,29 +30,8 @@ message(STATUS "Using protobuf ${Protobuf_VERSION}") # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation. find_package(gRPC CONFIG REQUIRED) message(STATUS "Using gRPC ${gRPC_VERSION}") - -set(_GRPC_GRPCPP gRPC::grpc++) set(_GRPC_CPP_PLUGIN_EXECUTABLE $) -# Proto file -get_filename_component(akushon_proto "../akushon_interfaces/proto/akushon.proto" ABSOLUTE) -get_filename_component(akushon_proto_path "${akushon_proto}" PATH) - -# Generated sources -set(akushon_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/akushon.pb.cc") -set(akushon_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/akushon.pb.h") -set(akushon_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/akushon.grpc.pb.cc") -set(akushon_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/akushon.grpc.pb.h") -add_custom_command( - OUTPUT "${akushon_proto_srcs}" "${akushon_proto_hdrs}" "${akushon_grpc_srcs}" "${akushon_grpc_hdrs}" - COMMAND ${_PROTOBUF_PROTOC} - ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}" - --cpp_out "${CMAKE_CURRENT_BINARY_DIR}" - -I "${akushon_proto_path}" - --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}" - "${akushon_proto}" - DEPENDS "${akushon_proto}") - add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/action/model/action_name.cpp" "src/${PROJECT_NAME}/action/model/action.cpp" @@ -74,8 +53,7 @@ add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/node/akushon_node.cpp" ) -target_include_directories(${PROJECT_NAME} PUBLIC - # $ +target_include_directories(${PROJECT_NAME} PUBLIC $ $) @@ -91,7 +69,7 @@ ament_target_dependencies(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} gRPC::grpc++_reflection - ${_GRPC_GRPCPP} + gRPC::grpc++ protobuf::libprotobuf ) From c07f3605f64515688bce2c4f7110289cdd573283 Mon Sep 17 00:00:00 2001 From: marfanr Date: Mon, 1 Jan 2024 07:46:41 +0000 Subject: [PATCH 13/17] feat: refractor and resolve pull request --- CMakeLists.txt | 5 +- include/akushon/config/grpc/call_data.hpp | 26 ++------- .../config/grpc/call_data_save_config.hpp | 2 +- .../call_data_subscribe_current_joints.hpp | 2 +- src/akushon/config/grpc/call_data.cpp | 56 +++++++++++++++++++ .../config/grpc/call_data_get_config.cpp | 2 +- .../config/grpc/call_data_save_config.cpp | 2 +- 7 files changed, 67 insertions(+), 28 deletions(-) create mode 100644 src/akushon/config/grpc/call_data.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f14009a..38b2620 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,6 @@ find_package(std_msgs REQUIRED) find_package(tachimawari REQUIRED) find_package(tachimawari_interfaces REQUIRED) -set(protobuf_MODULE_COMPATIBLE TRUE) find_package(Protobuf CONFIG REQUIRED) message(STATUS "Using protobuf ${Protobuf_VERSION}") @@ -35,6 +34,7 @@ set(_GRPC_CPP_PLUGIN_EXECUTABLE $) add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/action/model/action_name.cpp" "src/${PROJECT_NAME}/action/model/action.cpp" + "src/${PROJECT_NAME}/config/grpc/call_data.cpp" "src/${PROJECT_NAME}/action/model/pose.cpp" "src/${PROJECT_NAME}/action/node/action_manager.cpp" "src/${PROJECT_NAME}/action/node/action_node.cpp" @@ -69,8 +69,7 @@ ament_target_dependencies(${PROJECT_NAME} target_link_libraries(${PROJECT_NAME} gRPC::grpc++_reflection - gRPC::grpc++ - protobuf::libprotobuf + gRPC::grpc++ ) install(DIRECTORY "include" DESTINATION ".") diff --git a/include/akushon/config/grpc/call_data.hpp b/include/akushon/config/grpc/call_data.hpp index fb93cc3..7b00e79 100644 --- a/include/akushon/config/grpc/call_data.hpp +++ b/include/akushon/config/grpc/call_data.hpp @@ -30,29 +30,13 @@ namespace akushon { template -class CallData : CallDataBase -{ +class CallData : CallDataBase { public: CallData( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string& path) : status_(CallStatus::CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path) -{ -} - void Proceed() override { - if (status_ == CallStatus::CREATE) { - status_ = CallStatus::PROCESS; - WaitForRequest(); - } else if (status_ == CallStatus::PROCESS) { - AddNextToCompletionQueue(); - HandleRequest(); - status_ = CallStatus::FINISH; - responder_.Finish(reply_, grpc::Status::OK, this); - } else { - GPR_ASSERT(status_ == CallStatus::FINISH); - delete this; - } - } - + const std::string& path); + void Proceed() override; + protected: virtual void AddNextToCompletionQueue() = 0; @@ -72,4 +56,4 @@ class CallData : CallDataBase }; } // namespace akushon -#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ \ No newline at end of file +#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ diff --git a/include/akushon/config/grpc/call_data_save_config.hpp b/include/akushon/config/grpc/call_data_save_config.hpp index 7c6ee50..9643848 100644 --- a/include/akushon/config/grpc/call_data_save_config.hpp +++ b/include/akushon/config/grpc/call_data_save_config.hpp @@ -40,4 +40,4 @@ class CallDataSaveConfig }; } // namespace akushon -#endif // AKUSHON__CONFIG__GRPC__SAVE_CONFIG_HPP_ \ No newline at end of file +#endif // AKUSHON__CONFIG__GRPC__SAVE_CONFIG_HPP_ diff --git a/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp b/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp index ab19ef5..4e00f1e 100644 --- a/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp +++ b/include/akushon/config/grpc/call_data_subscribe_current_joints.hpp @@ -47,4 +47,4 @@ class CallDataSubscribeCurrentJoints }; } // namespace akushon -#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_SUBSCRIBE_CURRENT_JOINTS_HPP_ \ No newline at end of file +#endif // AKUSHON__CONFIG__GRPC__CALL_DATA_SUBSCRIBE_CURRENT_JOINTS_HPP_ diff --git a/src/akushon/config/grpc/call_data.cpp b/src/akushon/config/grpc/call_data.cpp new file mode 100644 index 0000000..944a761 --- /dev/null +++ b/src/akushon/config/grpc/call_data.cpp @@ -0,0 +1,56 @@ +// Copyright (c) 2023 Ichiro ITS +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "akushon/config/grpc/call_data.hpp" + +namespace akushon +{ +template +CallData::CallData( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string& path) : status_(CallStatus::CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path) { +} + +template +void CallData::Proceed() +{ + if (status_ == CallStatus::CREATE) { + status_ = CallStatus::PROCESS; + WaitForRequest(); + } else if (status_ == CallStatus::PROCESS) { + AddNextToCompletionQueue(); + HandleRequest(); + status_ = CallStatus::FINISH; + responder_.Finish(reply_, grpc::Status::OK, this); + } else { + GPR_ASSERT(status_ == CallStatus::FINISH); + delete this; + } +} + +template class CallData; +template class CallData; +template class CallData; +template class CallData< + akushon_interfaces::proto::SetTorquesData, akushon_interfaces::proto::Empty>; +template class CallData; +template class CallData< + akushon_interfaces::proto::ConfigRunAction, akushon_interfaces::proto::Empty>; +} diff --git a/src/akushon/config/grpc/call_data_get_config.cpp b/src/akushon/config/grpc/call_data_get_config.cpp index db0f8b2..485191c 100644 --- a/src/akushon/config/grpc/call_data_get_config.cpp +++ b/src/akushon/config/grpc/call_data_get_config.cpp @@ -51,4 +51,4 @@ void CallDataGetConfig::HandleRequest() RCLCPP_INFO(rclcpp::get_logger("GetConfig"), "config has been sent!"); } -} // namespace akushon \ No newline at end of file +} // namespace akushon diff --git a/src/akushon/config/grpc/call_data_save_config.cpp b/src/akushon/config/grpc/call_data_save_config.cpp index 4f88bc6..7e54d06 100644 --- a/src/akushon/config/grpc/call_data_save_config.cpp +++ b/src/akushon/config/grpc/call_data_save_config.cpp @@ -56,4 +56,4 @@ void CallDataSaveConfig::HandleRequest() } } -} // namespace akushon \ No newline at end of file +} // namespace akushon From e40d1deefebc81bf49a1c26dcafbaaa81b401b9c Mon Sep 17 00:00:00 2001 From: marfanr Date: Mon, 8 Jan 2024 11:05:25 +0000 Subject: [PATCH 14/17] feat: refractor and resolve pull request --- CMakeLists.txt | 2 +- include/akushon/config/grpc/call_data.hpp | 42 ++++++++++++++--- src/akushon/config/grpc/call_data.cpp | 56 ----------------------- 3 files changed, 37 insertions(+), 63 deletions(-) delete mode 100644 src/akushon/config/grpc/call_data.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 38b2620..b816664 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,7 +67,7 @@ ament_target_dependencies(${PROJECT_NAME} tachimawari_interfaces gRPC) - target_link_libraries(${PROJECT_NAME} +target_link_libraries(${PROJECT_NAME} gRPC::grpc++_reflection gRPC::grpc++ ) diff --git a/include/akushon/config/grpc/call_data.hpp b/include/akushon/config/grpc/call_data.hpp index 7b00e79..0ce97ee 100644 --- a/include/akushon/config/grpc/call_data.hpp +++ b/include/akushon/config/grpc/call_data.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2023 Ichiro ITS +// Copyright (c) 2024 Ichiro ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,22 +21,23 @@ #ifndef AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ #define AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ +#include "akushon/config/grpc/call_data_base.hpp" #include "akushon_interfaces/akushon.grpc.pb.h" #include "akushon_interfaces/akushon.pb.h" #include "grpc/support/log.h" #include "grpcpp/grpcpp.h" -#include "akushon/config/grpc/call_data_base.hpp" namespace akushon { template -class CallData : CallDataBase { +class CallData : CallDataBase +{ public: CallData( akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string& path); + const std::string & path); void Proceed() override; - + protected: virtual void AddNextToCompletionQueue() = 0; @@ -46,7 +47,7 @@ class CallData : CallDataBase { akushon_interfaces::proto::Config::AsyncService * service_; - const std::string& path_; + const std::string & path_; grpc::ServerCompletionQueue * cq_; grpc::ServerContext ctx_; @@ -54,6 +55,35 @@ class CallData : CallDataBase { ConfigReply reply_; grpc::ServerAsyncResponseWriter responder_; }; + +template +CallData::CallData( + akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, + const std::string & path) +: status_(CallStatus::CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path) +{ +} + +template +void CallData::Proceed() +{ + switch (status_) { + case CallStatus::CREATE: + status_ = CallStatus::PROCESS; + WaitForRequest(); + break; + case CallStatus::PROCESS: + AddNextToCompletionQueue(); + HandleRequest(); + status_ = CallStatus::FINISH; + responder_.Finish(reply_, grpc::Status::OK, this); + break; + default: + delete this; + break; + } +} + } // namespace akushon #endif // AKUSHON__CONFIG__GRPC__CALL_DATA_HPP_ diff --git a/src/akushon/config/grpc/call_data.cpp b/src/akushon/config/grpc/call_data.cpp deleted file mode 100644 index 944a761..0000000 --- a/src/akushon/config/grpc/call_data.cpp +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright (c) 2023 Ichiro ITS -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -#include "akushon/config/grpc/call_data.hpp" - -namespace akushon -{ -template -CallData::CallData( - akushon_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq, - const std::string& path) : status_(CallStatus::CREATE), service_(service), cq_(cq), responder_(&ctx_), path_(path) { -} - -template -void CallData::Proceed() -{ - if (status_ == CallStatus::CREATE) { - status_ = CallStatus::PROCESS; - WaitForRequest(); - } else if (status_ == CallStatus::PROCESS) { - AddNextToCompletionQueue(); - HandleRequest(); - status_ = CallStatus::FINISH; - responder_.Finish(reply_, grpc::Status::OK, this); - } else { - GPR_ASSERT(status_ == CallStatus::FINISH); - delete this; - } -} - -template class CallData; -template class CallData; -template class CallData; -template class CallData< - akushon_interfaces::proto::SetTorquesData, akushon_interfaces::proto::Empty>; -template class CallData; -template class CallData< - akushon_interfaces::proto::ConfigRunAction, akushon_interfaces::proto::Empty>; -} From 8d1c0dcb9c56e6c4f4a74c7797b432be7e47e3ba Mon Sep 17 00:00:00 2001 From: marfanr Date: Mon, 8 Jan 2024 11:07:49 +0000 Subject: [PATCH 15/17] feat: forgot remove call_data.cpp from CMakeLists.txt after remove this file --- CMakeLists.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b816664..b036bd5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,8 +33,7 @@ set(_GRPC_CPP_PLUGIN_EXECUTABLE $) add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/action/model/action_name.cpp" - "src/${PROJECT_NAME}/action/model/action.cpp" - "src/${PROJECT_NAME}/config/grpc/call_data.cpp" + "src/${PROJECT_NAME}/action/model/action.cpp" "src/${PROJECT_NAME}/action/model/pose.cpp" "src/${PROJECT_NAME}/action/node/action_manager.cpp" "src/${PROJECT_NAME}/action/node/action_node.cpp" From a59058cf2eebde0a9c56c44911c3ac72bf930c77 Mon Sep 17 00:00:00 2001 From: marfanr Date: Mon, 8 Jan 2024 11:55:48 +0000 Subject: [PATCH 16/17] feat: remove unused include header on config.hpp --- include/akushon/config/grpc/config.hpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/include/akushon/config/grpc/config.hpp b/include/akushon/config/grpc/config.hpp index 75317e7..c254e49 100644 --- a/include/akushon/config/grpc/config.hpp +++ b/include/akushon/config/grpc/config.hpp @@ -1,4 +1,4 @@ -// Copyright (c) 2023 Ichiro ITS +// Copyright (c) 2024 Ichiro ITS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -21,20 +21,11 @@ #ifndef AKUSHON__CONFIG__GRPC__CONFIG_HPP_ #define AKUSHON__CONFIG__GRPC__CONFIG_HPP_ -#include -#include -#include -#include #include -#include #include -#include "absl/flags/flag.h" -#include "absl/flags/parse.h" -#include "absl/strings/str_format.h" #include "akushon_interfaces/akushon.grpc.pb.h" #include "akushon_interfaces/akushon.pb.h" -#include "grpc/support/log.h" #include "grpcpp/grpcpp.h" #include "rclcpp/rclcpp.hpp" @@ -50,10 +41,10 @@ class ConfigGrpc ~ConfigGrpc(); - void Run(uint16_t port, const std::string& path, rclcpp::Node::SharedPtr& node); + void Run(uint16_t port, const std::string & path, rclcpp::Node::SharedPtr & node); private: - std::string path; + std::string path; static inline std::unique_ptr cq_; static inline std::unique_ptr server_; From 0a702ddd6245f709c92277ada0cd2b2581e64eae Mon Sep 17 00:00:00 2001 From: marfanr Date: Mon, 15 Jan 2024 11:15:39 +0700 Subject: [PATCH 17/17] feat: remove unused on CMakeLists.txt --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b036bd5..efd09bd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,7 +29,6 @@ message(STATUS "Using protobuf ${Protobuf_VERSION}") # Looks for gRPCConfig.cmake file installed by gRPC's cmake installation. find_package(gRPC CONFIG REQUIRED) message(STATUS "Using gRPC ${gRPC_VERSION}") -set(_GRPC_CPP_PLUGIN_EXECUTABLE $) add_library(${PROJECT_NAME} SHARED "src/${PROJECT_NAME}/action/model/action_name.cpp"