Skip to content

Commit

Permalink
Merge pull request #20 from ichiro-its/feature/fix-using-grpc
Browse files Browse the repository at this point in the history
[Sprint  22 / PD-50] - [Feature] Add implementation GRPC
  • Loading branch information
marfanr authored Jun 5, 2024
2 parents e4a0005 + d6221c2 commit 304becf
Show file tree
Hide file tree
Showing 24 changed files with 900 additions and 55 deletions.
69 changes: 57 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,38 @@ find_package(keisan REQUIRED)
find_package(rclcpp REQUIRED)
find_package(tachimawari REQUIRED)
find_package(tachimawari_interfaces REQUIRED)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")

add_library(${PROJECT_NAME} SHARED
"src/${PROJECT_NAME}/config/node/config_node.cpp"
"src/${PROJECT_NAME}/config/utils/config.cpp"
"src/${PROJECT_NAME}/node/aruku_node.cpp"
"src/${PROJECT_NAME}/walking/node/walking_manager.cpp"
"src/${PROJECT_NAME}/walking/node/walking_node.cpp"
"src/${PROJECT_NAME}/walking/process/kinematic.cpp")
"src/${PROJECT_NAME}/walking/process/kinematic.cpp"
"src/${PROJECT_NAME}/config/grpc/config.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_set_config.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_save_config.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_publish_config.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_set_app_status.cpp"
)

add_library(${PROJECT_NAME}_exported SHARED
"src/${PROJECT_NAME}/walking/node/walking_manager.cpp"
"src/${PROJECT_NAME}/walking/node/walking_node.cpp"
"src/${PROJECT_NAME}/walking/process/kinematic.cpp"
)

target_include_directories(${PROJECT_NAME}_exported PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

target_include_directories(${PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

Expand All @@ -43,21 +65,43 @@ ament_target_dependencies(${PROJECT_NAME}
keisan
rclcpp
tachimawari
tachimawari_interfaces)
tachimawari_interfaces
gRPC
)

ament_target_dependencies(${PROJECT_NAME}_exported
ament_index_cpp
aruku_interfaces
kansei
kansei_interfaces
keisan
rclcpp
tachimawari
tachimawari_interfaces
)

target_link_libraries(${PROJECT_NAME}
gRPC::grpc++_reflection
gRPC::grpc++
)

install(DIRECTORY "include" DESTINATION ".")

install(TARGETS ${PROJECT_NAME}
EXPORT export_${PROJECT_NAME}
ARCHIVE DESTINATION "lib"
LIBRARY DESTINATION "lib"
RUNTIME DESTINATION "bin")
RUNTIME DESTINATION "bin"
)

add_executable(config "src/config_main.cpp")
target_include_directories(config PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
target_link_libraries(config ${PROJECT_NAME})
install(TARGETS ${PROJECT_NAME}_exported
EXPORT export_${PROJECT_NAME}_exported
ARCHIVE DESTINATION "lib"
LIBRARY DESTINATION "lib"
RUNTIME DESTINATION "bin"
)

target_compile_options(${PROJECT_NAME} PRIVATE -fPIC)

add_executable(control "src/control_main.cpp")
target_include_directories(control PUBLIC
Expand All @@ -78,11 +122,11 @@ target_include_directories(walking PUBLIC
target_link_libraries(walking ${PROJECT_NAME})

install(TARGETS
config
control
main
walking
DESTINATION lib/${PROJECT_NAME})
DESTINATION lib/${PROJECT_NAME}
)

if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
Expand All @@ -97,7 +141,8 @@ ament_export_dependencies(
keisan
rclcpp
tachimawari
tachimawari_interfaces)
tachimawari_interfaces
)
ament_export_include_directories("include")
ament_export_libraries(${PROJECT_NAME})
ament_export_libraries(${PROJECT_NAME}_exported)
ament_package()
86 changes: 86 additions & 0 deletions include/aruku/config/grpc/call_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// 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
// 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 __ARUKU__CONFIG__GRPC__CALL_DATA_HPP__
#define __ARUKU__CONFIG__GRPC__CALL_DATA_HPP__

#include "aruku/config/grpc/call_data_base.hpp"
#include "aruku_interfaces/aruku.grpc.pb.h"
#include "aruku_interfaces/aruku.pb.h"
#include "grpc/support/log.h"
#include "grpcpp/grpcpp.h"

namespace aruku {
template <class ConfigRequest, class ConfigReply>
class CallData : public CallDataBase {
public:
CallData(aruku_interfaces::proto::Config::AsyncService *service,
grpc::ServerCompletionQueue *cq, const std::string path);

void Proceed();

protected:
virtual void AddNextToCompletionQueue() = 0;

enum CallStatus { CREATE, PROCESS, FINISH };

CallStatus status_; // The current serving state.

aruku_interfaces::proto::Config::AsyncService *service_;

const std::string path_;

grpc::ServerCompletionQueue *cq_;
grpc::ServerContext ctx_;
ConfigRequest request_;
ConfigReply reply_;
grpc::ServerAsyncResponseWriter<ConfigReply> responder_;
};

template <class ConfigRequest, class ConfigReply>
CallData<ConfigRequest, ConfigReply>::CallData(
aruku_interfaces::proto::Config::AsyncService *service,
grpc::ServerCompletionQueue *cq, const std::string path)
: status_(CREATE), service_(service), cq_(cq), responder_(&ctx_),
path_(path) {}

template <class ConfigRequest, class ConfigReply>
void CallData<ConfigRequest, ConfigReply>::Proceed() {
switch (status_) {
case CREATE:
status_ = PROCESS;
WaitForRequest();
break;
case PROCESS:
AddNextToCompletionQueue();
HandleRequest();
status_ = FINISH;
responder_.Finish(reply_, grpc::Status::OK, this);
break;
default:
GPR_ASSERT(status_ == FINISH);
delete this;
break;
}
}

} // namespace aruku

#endif // __ARUKU__CONFIG__GRPC__CALL_DATA_HPP__
39 changes: 39 additions & 0 deletions include/aruku/config/grpc/call_data_base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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
// 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 ARUKU__CONFIG__GRPC__CALL_DATA_BASE_HPP_
#define ARUKU__CONFIG__GRPC__CALL_DATA_BASE_HPP_

namespace aruku
{
class CallDataBase
{
public:
CallDataBase();

virtual void Proceed() = 0;

protected:
virtual void WaitForRequest() = 0;
virtual void HandleRequest() = 0;
};
} // namespace aruku

#endif // ARUKU__CONFIG__GRPC__CALL_DATA_BASE_HPP_
43 changes: 43 additions & 0 deletions include/aruku/config/grpc/call_data_get_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// 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
// 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 ARUKU__CONFIG__GRPC__CALL_DATA_GET_CONFIG_HPP__
#define ARUKU__CONFIG__GRPC__CALL_DATA_GET_CONFIG_HPP__

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

namespace aruku
{
class CallDataGetConfig
: CallData<aruku_interfaces::proto::Empty, aruku_interfaces::proto::ConfigWalking>
{
public:
CallDataGetConfig(
aruku_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path);

protected:
void AddNextToCompletionQueue() override;
void WaitForRequest() ;
void HandleRequest() ;
};
} // namespace aruku

#endif // ARUKU__CONFIG__GRPC__CALL_DATA_GET_CONFIG_HPP__
47 changes: 47 additions & 0 deletions include/aruku/config/grpc/call_data_publish_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// 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
// 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 ARUKU__CONFIG__GRPC__CALL_DATA_PUBLISH_CONFIG_HPP__
#define ARUKU__CONFIG__GRPC__CALL_DATA_PUBLISH_CONFIG_HPP__

#include "aruku/config/grpc/call_data.hpp"
#include "aruku_interfaces/msg/set_config.hpp"
#include "rclcpp/rclcpp.hpp"

namespace aruku
{
class CallDataPublishConfig
: CallData<aruku_interfaces::proto::ConfigWalking, aruku_interfaces::proto::Empty>
{
public:
CallDataPublishConfig(
aruku_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path, rclcpp::Node::SharedPtr node);

protected:
rclcpp::Node::SharedPtr node_;
rclcpp::Publisher<aruku_interfaces::msg::SetConfig>::SharedPtr set_config_publisher_;
void AddNextToCompletionQueue() override;
void WaitForRequest();
void HandleRequest();
};
} // namespace aruku

#endif // ARUKU__CONFIG__GRPC__CALL_DATA_PUBLISH_CONFIG_HPP__
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2021 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
Expand All @@ -18,33 +18,28 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#include <string>
#include <memory>
#ifndef ARUKU__CONFIG__GRPC__CALL_DATA_SAVE_CONFIG_HPP__
#define ARUKU__CONFIG__GRPC__CALL_DATA_SAVE_CONFIG_HPP__

#include "aruku/node/aruku_node.hpp"
#include "aruku/config/grpc/call_data.hpp"
#include "rclcpp/rclcpp.hpp"

int main(int argc, char * argv[])
namespace aruku
{
rclcpp::init(argc, argv);

if (argc < 2) {
std::cerr << "Please specify the path!" << std::endl;
return 0;
}

std::string path = argv[1];
auto node = std::make_shared<rclcpp::Node>("aruku_node");
auto aruku_node = std::make_shared<aruku::ArukuNode>(node);

auto walking_manager = std::make_shared<aruku::WalkingManager>();
walking_manager->load_config(path);
class CallDataSaveConfig
: CallData<aruku_interfaces::proto::ConfigWalking, aruku_interfaces::proto::Empty>
{
public:
CallDataSaveConfig(
aruku_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path);

aruku_node->set_walking_manager(walking_manager);
aruku_node->run_config_service(path);
protected:
void AddNextToCompletionQueue() override;
void WaitForRequest();
void HandleRequest();
};

rclcpp::spin(node);
rclcpp::shutdown();
} // namespace aruku

return 0;
}
#endif // ARUKU__CONFIG__GRPC__CALL_DATA_SAVE_CONFIG_HPP__
Loading

0 comments on commit 304becf

Please sign in to comment.