Skip to content

Commit 724c341

Browse files
tkimura4isamu-takagitier4-autoware-private-bot[bot]kenji-miyakepre-commit-ci[bot]
authored
feat: add autoware_api_utils package (autowarefoundation#106)
* Add autoware api mapping system (#9) * Apply api mapping to engage * Fix format * Add vehicle status API (#8) * Add vehicle status API * Fix logging * Fix lint * Add vehicle command API (#11) * sync main for develop (autowarefoundation#17) * Add CI (#12) * Add sync-public.yaml (#14) * Add sync-public.yaml Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Add sync-public-develop.yaml Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> * Change proposal sync app ID (autowarefoundation#18) Signed-off-by: Kenji Miyake <kenji.miyake@tier4.jp> Co-authored-by: Takagi, Isamu <43976882+isamu-takagi@users.noreply.github.com> Co-authored-by: Kenji Miyake <31987104+kenji-miyake@users.noreply.github.com> * Add fail safe state API (autowarefoundation#20) * Move autoware_api_utils * Move autoware_api_mapping * Fix package depend * Move autoware_api_generator Co-authored-by: autoware-iv-sync-ci[bot] <87871706+autoware-iv-sync-ci[bot]@users.noreply.github.com> Co-authored-by: Kenji Miyake <31987104+kenji-miyake@users.noreply.github.com> * ci(pre-commit): autofix * use rosidl_generator_traits::to_yaml * fix format * ci(pre-commit): autofix * change lint * ci(pre-commit): autofix Co-authored-by: Takagi, Isamu <43976882+isamu-takagi@users.noreply.github.com> Co-authored-by: autoware-iv-sync-ci[bot] <87871706+autoware-iv-sync-ci[bot]@users.noreply.github.com> Co-authored-by: Kenji Miyake <31987104+kenji-miyake@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 724c054 commit 724c341

File tree

8 files changed

+373
-0
lines changed

8 files changed

+373
-0
lines changed
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
cmake_minimum_required(VERSION 3.5)
2+
project(autoware_api_utils)
3+
4+
if(NOT CMAKE_CXX_STANDARD)
5+
set(CMAKE_CXX_STANDARD 17)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
set(CMAKE_CXX_EXTENSIONS OFF)
8+
endif()
9+
10+
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
11+
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
12+
endif()
13+
14+
find_package(ament_cmake_auto REQUIRED)
15+
ament_auto_find_build_dependencies()
16+
17+
if(BUILD_TESTING)
18+
find_package(ament_lint_auto REQUIRED)
19+
find_package(ament_cmake_gtest REQUIRED)
20+
include_directories(include)
21+
ament_lint_auto_find_test_dependencies()
22+
ament_add_gtest(${PROJECT_NAME}_test test/test.cpp)
23+
ament_target_dependencies(${PROJECT_NAME}_test rclcpp autoware_external_api_msgs)
24+
endif()
25+
26+
ament_auto_package()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2021 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE_API_UTILS__AUTOWARE_API_UTILS_HPP_
16+
#define AUTOWARE_API_UTILS__AUTOWARE_API_UTILS_HPP_
17+
18+
#include "autoware_api_utils/rclcpp/proxy.hpp"
19+
#include "autoware_api_utils/types/response.hpp"
20+
21+
#endif // AUTOWARE_API_UTILS__AUTOWARE_API_UTILS_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2021 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE_API_UTILS__RCLCPP__CLIENT_HPP_
16+
#define AUTOWARE_API_UTILS__RCLCPP__CLIENT_HPP_
17+
18+
#include "autoware_api_utils/types/response.hpp"
19+
#include "rclcpp/client.hpp"
20+
21+
#include <chrono>
22+
#include <utility>
23+
24+
namespace autoware_api_utils
25+
{
26+
template <typename ServiceT>
27+
class Client
28+
{
29+
public:
30+
RCLCPP_SMART_PTR_DEFINITIONS(Client)
31+
32+
using ResponseStatus = autoware_external_api_msgs::msg::ResponseStatus;
33+
using AutowareServiceResult = std::pair<ResponseStatus, typename ServiceT::Response::SharedPtr>;
34+
35+
Client(typename rclcpp::Client<ServiceT>::SharedPtr client, rclcpp::Logger logger)
36+
: client_(client), logger_(logger)
37+
{
38+
}
39+
40+
AutowareServiceResult call(
41+
const typename ServiceT::Request::SharedPtr & request,
42+
const std::chrono::nanoseconds & timeout = std::chrono::seconds(2))
43+
{
44+
RCLCPP_INFO(
45+
logger_, "client request: \n%s", rosidl_generator_traits::to_yaml(*request).c_str());
46+
47+
if (!client_->service_is_ready()) {
48+
RCLCPP_INFO(logger_, "client available");
49+
return {response_error("Internal service is not available."), nullptr};
50+
}
51+
52+
auto future = client_->async_send_request(request);
53+
if (future.wait_for(timeout) != std::future_status::ready) {
54+
RCLCPP_INFO(logger_, "client timeout");
55+
return {response_error("Internal service has timed out."), nullptr};
56+
}
57+
58+
RCLCPP_INFO(
59+
logger_, "client response: \n%s", rosidl_generator_traits::to_yaml(future.get()).c_str());
60+
return {response_success(), future.get()};
61+
}
62+
63+
private:
64+
RCLCPP_DISABLE_COPY(Client)
65+
66+
typename rclcpp::Client<ServiceT>::SharedPtr client_;
67+
rclcpp::Logger logger_;
68+
};
69+
70+
} // namespace autoware_api_utils
71+
72+
#endif // AUTOWARE_API_UTILS__RCLCPP__CLIENT_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2021 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE_API_UTILS__RCLCPP__PROXY_HPP_
16+
#define AUTOWARE_API_UTILS__RCLCPP__PROXY_HPP_
17+
18+
#include "autoware_api_utils/rclcpp/client.hpp"
19+
#include "autoware_api_utils/rclcpp/service.hpp"
20+
#include "rclcpp/rclcpp.hpp"
21+
22+
#include <string>
23+
#include <utility>
24+
25+
namespace autoware_api_utils
26+
{
27+
template <class NodeT>
28+
class ServiceProxyNodeInterface
29+
{
30+
public:
31+
// Use a raw pointer because shared_from_this cannot be used in constructor.
32+
explicit ServiceProxyNodeInterface(NodeT * node) { node_ = node; }
33+
34+
template <typename ServiceT, typename CallbackT>
35+
typename Service<ServiceT>::SharedPtr create_service(
36+
const std::string & service_name, CallbackT && callback,
37+
const rmw_qos_profile_t & qos_profile = rmw_qos_profile_services_default,
38+
rclcpp::CallbackGroup::SharedPtr group = nullptr)
39+
{
40+
auto wrapped_callback = Service<ServiceT>::template wrap<CallbackT>(
41+
std::forward<CallbackT>(callback), node_->get_logger());
42+
return Service<ServiceT>::make_shared(node_->template create_service<ServiceT>(
43+
service_name, std::move(wrapped_callback), qos_profile, group));
44+
}
45+
46+
template <typename ServiceT>
47+
typename Client<ServiceT>::SharedPtr create_client(
48+
const std::string & service_name,
49+
const rmw_qos_profile_t & qos_profile = rmw_qos_profile_services_default,
50+
rclcpp::CallbackGroup::SharedPtr group = nullptr)
51+
{
52+
return Client<ServiceT>::make_shared(
53+
node_->template create_client<ServiceT>(service_name, qos_profile, group),
54+
node_->get_logger());
55+
}
56+
57+
private:
58+
NodeT * node_;
59+
};
60+
61+
} // namespace autoware_api_utils
62+
63+
#endif // AUTOWARE_API_UTILS__RCLCPP__PROXY_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2021 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE_API_UTILS__RCLCPP__SERVICE_HPP_
16+
#define AUTOWARE_API_UTILS__RCLCPP__SERVICE_HPP_
17+
18+
#include "rclcpp/service.hpp"
19+
20+
namespace autoware_api_utils
21+
{
22+
template <typename ServiceT>
23+
class Service
24+
{
25+
public:
26+
RCLCPP_SMART_PTR_DEFINITIONS(Service)
27+
28+
explicit Service(typename rclcpp::Service<ServiceT>::SharedPtr service) : service_(service) {}
29+
30+
template <typename CallbackT>
31+
static auto wrap(CallbackT && callback, const rclcpp::Logger & logger)
32+
{
33+
auto wrapped_callback = [logger, callback](
34+
typename ServiceT::Request::SharedPtr request,
35+
typename ServiceT::Response::SharedPtr response) {
36+
RCLCPP_INFO(
37+
logger, "service request: \n%s", rosidl_generator_traits::to_yaml(*request).c_str());
38+
callback(request, response);
39+
RCLCPP_INFO(
40+
logger, "service response: \n%s", rosidl_generator_traits::to_yaml(*response).c_str());
41+
};
42+
return wrapped_callback;
43+
}
44+
45+
private:
46+
RCLCPP_DISABLE_COPY(Service)
47+
48+
typename rclcpp::Service<ServiceT>::SharedPtr service_;
49+
};
50+
51+
} // namespace autoware_api_utils
52+
53+
#endif // AUTOWARE_API_UTILS__RCLCPP__SERVICE_HPP_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2021 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef AUTOWARE_API_UTILS__TYPES__RESPONSE_HPP_
16+
#define AUTOWARE_API_UTILS__TYPES__RESPONSE_HPP_
17+
18+
#include "rclcpp/rclcpp.hpp"
19+
20+
#include "autoware_external_api_msgs/msg/response_status.hpp"
21+
22+
#include <string>
23+
24+
namespace autoware_api_utils
25+
{
26+
using ResponseStatus = autoware_external_api_msgs::msg::ResponseStatus;
27+
28+
inline bool is_success(const autoware_external_api_msgs::msg::ResponseStatus & status)
29+
{
30+
return status.code == autoware_external_api_msgs::msg::ResponseStatus::SUCCESS;
31+
}
32+
33+
inline bool is_ignored(const autoware_external_api_msgs::msg::ResponseStatus & status)
34+
{
35+
return status.code == autoware_external_api_msgs::msg::ResponseStatus::IGNORED;
36+
}
37+
38+
inline bool is_warn(const autoware_external_api_msgs::msg::ResponseStatus & status)
39+
{
40+
return status.code == autoware_external_api_msgs::msg::ResponseStatus::WARN;
41+
}
42+
43+
inline bool is_error(const autoware_external_api_msgs::msg::ResponseStatus & status)
44+
{
45+
return status.code == autoware_external_api_msgs::msg::ResponseStatus::ERROR;
46+
}
47+
48+
inline ResponseStatus response_success(const std::string & message = "")
49+
{
50+
return autoware_external_api_msgs::build<autoware_external_api_msgs::msg::ResponseStatus>()
51+
.code(autoware_external_api_msgs::msg::ResponseStatus::SUCCESS)
52+
.message(message);
53+
}
54+
55+
inline ResponseStatus response_ignored(const std::string & message = "")
56+
{
57+
return autoware_external_api_msgs::build<autoware_external_api_msgs::msg::ResponseStatus>()
58+
.code(autoware_external_api_msgs::msg::ResponseStatus::IGNORED)
59+
.message(message);
60+
}
61+
62+
inline ResponseStatus response_warn(const std::string & message = "")
63+
{
64+
return autoware_external_api_msgs::build<autoware_external_api_msgs::msg::ResponseStatus>()
65+
.code(autoware_external_api_msgs::msg::ResponseStatus::WARN)
66+
.message(message);
67+
}
68+
69+
inline ResponseStatus response_error(const std::string & message = "")
70+
{
71+
return autoware_external_api_msgs::build<autoware_external_api_msgs::msg::ResponseStatus>()
72+
.code(autoware_external_api_msgs::msg::ResponseStatus::ERROR)
73+
.message(message);
74+
}
75+
76+
} // namespace autoware_api_utils
77+
78+
#endif // AUTOWARE_API_UTILS__TYPES__RESPONSE_HPP_

common/autoware_api_utils/package.xml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0"?>
2+
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
3+
<package format="3">
4+
5+
<name>autoware_api_utils</name>
6+
<version>0.0.0</version>
7+
<description>The autoware_api_utils package</description>
8+
<maintainer email="isamu.takagi@tier4.jp">Takagi, Isamu</maintainer>
9+
<license>Apache License 2.0</license>
10+
11+
<buildtool_depend>ament_cmake_auto</buildtool_depend>
12+
13+
<depend>autoware_external_api_msgs</depend>
14+
<depend>rclcpp</depend>
15+
16+
<test_depend>ament_cmake_gtest</test_depend>
17+
<test_depend>ament_lint_auto</test_depend>
18+
<test_depend>autoware_external_api_msgs</test_depend>
19+
<test_depend>autoware_lint_common</test_depend>
20+
<test_depend>rclcpp</test_depend>
21+
22+
<member_of_group>rosidl_interface_packages</member_of_group>
23+
24+
<export>
25+
<build_type>ament_cmake</build_type>
26+
</export>
27+
28+
</package>
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2021 Tier IV, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "autoware_api_utils/autoware_api_utils.hpp"
16+
#include "gtest/gtest.h"
17+
#include "rclcpp/rclcpp.hpp"
18+
19+
TEST(autoware_api_utils, instantiate)
20+
{
21+
rclcpp::Node node("autoware_api_utils_test");
22+
autoware_api_utils::ServiceProxyNodeInterface proxy(&node);
23+
}
24+
25+
int main(int argc, char ** argv)
26+
{
27+
testing::InitGoogleTest(&argc, argv);
28+
rclcpp::init(argc, argv);
29+
bool result = RUN_ALL_TESTS();
30+
rclcpp::shutdown();
31+
return result;
32+
}

0 commit comments

Comments
 (0)