Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sprint 22/23 / PD-405] [Feature] Add implementation GRPC #13

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 57 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,58 @@ find_package(shisen_cpp REQUIRED)
find_package(sensor_msgs REQUIRED)
find_package(cv_bridge REQUIRED)

find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${Protobuf_VERSION}")

# 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}")

add_library(${PROJECT_NAME} SHARED
"src/${PROJECT_NAME}/config/grpc/config.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_get_color_setting.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_save_color_setting.cpp"
"src/${PROJECT_NAME}/config/grpc/call_data_set_color_setting.cpp"
"src/${PROJECT_NAME}/config/utils/config.cpp"
"src/${PROJECT_NAME}/detector/color_detector.cpp"
"src/${PROJECT_NAME}/detector/dnn_detector.cpp"
"src/${PROJECT_NAME}/detector/lbp_detector.cpp"
"src/${PROJECT_NAME}/node/ninshiki_cpp_node.cpp"
"src/${PROJECT_NAME}/utils/color.cpp"
"src/${PROJECT_NAME}/utils/contours.cpp"
"src/${PROJECT_NAME}/utils/circle.cpp"
"src/${PROJECT_NAME}/node/ninshiki_cpp_node.cpp"
"src/${PROJECT_NAME}/utils/utils.cpp")
"src/${PROJECT_NAME}/utils/utils.cpp"
)

add_library(${PROJECT_NAME}_exported SHARED
"src/${PROJECT_NAME}/detector/color_detector.cpp"
"src/${PROJECT_NAME}/detector/dnn_detector.cpp"
"src/${PROJECT_NAME}/detector/lbp_detector.cpp"
"src/${PROJECT_NAME}/utils/color.cpp"
"src/${PROJECT_NAME}/utils/contours.cpp"
"src/${PROJECT_NAME}/utils/utils.cpp"
)

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

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

ament_target_dependencies(${PROJECT_NAME}
keisan
ninshiki_interfaces
OpenCV
rclcpp
shisen_cpp
sensor_msgs
cv_bridge
gRPC)

ament_target_dependencies(${PROJECT_NAME}_exported
keisan
ninshiki_interfaces
OpenCV
Expand All @@ -46,13 +82,28 @@ ament_target_dependencies(${PROJECT_NAME}
sensor_msgs
cv_bridge)

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

install(DIRECTORY "include" DESTINATION ".")

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

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

target_compile_options(${PROJECT_NAME} PRIVATE -fPIC)

add_executable(detector "src/ninshiki_cpp_detector.cpp")
target_include_directories(detector PUBLIC
Expand All @@ -78,5 +129,5 @@ ament_export_dependencies(
sensor_msgs
cv_bridge)
ament_export_include_directories("include")
ament_export_libraries(${PROJECT_NAME})
ament_export_libraries(${PROJECT_NAME}_exported)
ament_package()
87 changes: 87 additions & 0 deletions include/ninshiki_cpp/config/grpc/call_data.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// 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 __NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_HPP__
#define __NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_HPP__

#include "ninshiki_cpp/config/grpc/call_data_base.hpp"
#include "ninshiki_interfaces/ninshiki.grpc.pb.h"
#include "ninshiki_interfaces/ninshiki.pb.h"
#include "grpc/support/log.h"
#include "grpcpp/grpcpp.h"

namespace ninshiki_cpp
{
template <class ConfigRequest, class ConfigReply>
class CallData : public CallDataBase
{
public:
CallData(
ninshiki_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.

ninshiki_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(
ninshiki_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()
{
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;
}
}

} // namespace ninshiki_cpp

#endif // __NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_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,32 +18,20 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#ifndef NINSHIKI_CPP__UTILS__CIRCLE_HPP_
#define NINSHIKI_CPP__UTILS__CIRCLE_HPP_
#ifndef NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_BASE_HPP_
#define NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_BASE_HPP_

#include <opencv2/opencv.hpp>
#include <vector>

namespace ninshiki_cpp::utils
namespace ninshiki_cpp
{

class Circle
class CallDataBase
{
private:

cv::Point2f center;
float radius;

public:
virtual void Proceed() = 0;

Circle(const std::vector<cv::Point> & contour);

void draw(cv::Mat & image, int line_size) const;

const cv::Point2f & get_center() const;
const float get_radius() const;
protected:
virtual void WaitForRequest() = 0;
virtual void HandleRequest() = 0;
};
} // namespace ninshiki_cpp

} // namespace ninshiki_cpp::utils

#endif // NINSHIKI_CPP__UTILS__CIRCLES_HPP_
#endif // NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_BASE_HPP_
43 changes: 43 additions & 0 deletions include/ninshiki_cpp/config/grpc/call_data_get_color_setting.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 NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_GET_COLOR_SETTING_HPP__
#define NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_GET_COLOR_SETTING_HPP__

#include <ninshiki_cpp/config/grpc/call_data.hpp>

namespace ninshiki_cpp
{
class CallDataGetColorSetting
: CallData<ninshiki_interfaces::proto::Empty, ninshiki_interfaces::proto::ConfigColor>
{
public:
CallDataGetColorSetting(
ninshiki_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path);

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

#endif // NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_GET_COLOR_SETTING_HPP__
45 changes: 45 additions & 0 deletions include/ninshiki_cpp/config/grpc/call_data_save_color_setting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_SAVE_COLOR_SETTING_HPP__
#define NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_SAVE_COLOR_SETTING_HPP__

#include <ninshiki_cpp/config/grpc/call_data.hpp>
#include <rclcpp/rclcpp.hpp>

namespace ninshiki_cpp
{
class CallDataSaveColorSetting
: CallData<ninshiki_interfaces::proto::ConfigColor, ninshiki_interfaces::proto::Empty>
{
public:
CallDataSaveColorSetting(
ninshiki_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path);

protected:
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
};

} // namespace ninshiki_cpp

#endif // NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_SAVE_COLOR_SETTING_HPP__
48 changes: 48 additions & 0 deletions include/ninshiki_cpp/config/grpc/call_data_set_color_setting.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// 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 NINSHIKI_CPP_CONFIG__GRPC__CALL_DATA_SET_COLOR_SETTING_HPP__
#define NINSHIKI_CPP_CONFIG__GRPC__CALL_DATA_SET_COLOR_SETTING_HPP__

#include <rclcpp/rclcpp.hpp>
#include <ninshiki_cpp/config/grpc/call_data.hpp>
#include <ninshiki_cpp/detector/color_detector.hpp>
#include <ninshiki_cpp/utils/color.hpp>

namespace ninshiki_cpp
{
class CallDataSetColorSetting
: CallData<ninshiki_interfaces::proto::ColorSetting, ninshiki_interfaces::proto::Empty>
{
public:
CallDataSetColorSetting(
ninshiki_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
const std::string & path, std::shared_ptr<ninshiki_cpp::detector::ColorDetector> color_detection);
using ColorDetector = ninshiki_cpp::detector::ColorDetector;

protected:
void AddNextToCompletionQueue() override;
void WaitForRequest() override;
void HandleRequest() override;
std::shared_ptr<ColorDetector> color_detection_;
};
} // namespace ninshiki_cpp

#endif // NINSHIKI_CPP__CONFIG__GRPC__CALL_DATA_SET_COLOR_SETTING_HPP__
Loading
Loading