Skip to content

Commit 4563209

Browse files
isamu-takagia-maumau
authored andcommitted
feat(default_ad_api): add heratbeat api (autowarefoundation#6969)
* feat(default_ad_api): add heratbeat api Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp> * fix node dying Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp> --------- Signed-off-by: Takagi, Isamu <isamu.takagi@tier4.jp>
1 parent 800e6e8 commit 4563209

File tree

5 files changed

+121
-0
lines changed

5 files changed

+121
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2024 The Autoware Contributors
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_AD_API_SPECS__SYSTEM_HPP_
16+
#define AUTOWARE_AD_API_SPECS__SYSTEM_HPP_
17+
18+
#include <rclcpp/qos.hpp>
19+
20+
#include <autoware_adapi_v1_msgs/msg/heartbeat.hpp>
21+
22+
namespace autoware_ad_api::system
23+
{
24+
25+
struct Heartbeat
26+
{
27+
using Message = autoware_adapi_v1_msgs::msg::Heartbeat;
28+
static constexpr char name[] = "/api/system/heartbeat";
29+
static constexpr size_t depth = 10;
30+
static constexpr auto reliability = RMW_QOS_POLICY_RELIABILITY_BEST_EFFORT;
31+
static constexpr auto durability = RMW_QOS_POLICY_DURABILITY_VOLATILE;
32+
};
33+
34+
} // namespace autoware_ad_api::system
35+
36+
#endif // AUTOWARE_AD_API_SPECS__SYSTEM_HPP_

system/default_ad_api/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ autoware_package()
66

77
ament_auto_add_library(${PROJECT_NAME} SHARED
88
src/fail_safe.cpp
9+
src/heartbeat.cpp
910
src/interface.cpp
1011
src/localization.cpp
1112
src/motion.cpp
@@ -23,6 +24,7 @@ ament_auto_add_library(${PROJECT_NAME} SHARED
2324
rclcpp_components_register_nodes(${PROJECT_NAME}
2425
"default_ad_api::AutowareStateNode"
2526
"default_ad_api::FailSafeNode"
27+
"default_ad_api::HeartbeatNode"
2628
"default_ad_api::InterfaceNode"
2729
"default_ad_api::LocalizationNode"
2830
"default_ad_api::MotionNode"

system/default_ad_api/launch/default_ad_api.launch.py

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def generate_launch_description():
4343
components = [
4444
create_api_node("autoware_state", "AutowareStateNode"),
4545
create_api_node("fail_safe", "FailSafeNode"),
46+
create_api_node("heartbeat", "HeartbeatNode"),
4647
create_api_node("interface", "InterfaceNode"),
4748
create_api_node("localization", "LocalizationNode"),
4849
create_api_node("motion", "MotionNode"),
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2024 The Autoware Contributors
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 "heartbeat.hpp"
16+
17+
#include <utility>
18+
19+
namespace default_ad_api
20+
{
21+
22+
HeartbeatNode::HeartbeatNode(const rclcpp::NodeOptions & options) : Node("heartbeat", options)
23+
{
24+
// Move this function so that the timer no longer holds it as a reference.
25+
const auto on_timer = [this]() {
26+
autoware_ad_api::system::Heartbeat::Message heartbeat;
27+
heartbeat.stamp = now();
28+
heartbeat.seq = ++sequence_; // Wraps at 65535.
29+
pub_->publish(heartbeat);
30+
};
31+
32+
const auto adaptor = component_interface_utils::NodeAdaptor(this);
33+
adaptor.init_pub(pub_);
34+
35+
const auto period = rclcpp::Rate(10.0).period();
36+
timer_ = rclcpp::create_timer(this, get_clock(), period, std::move(on_timer));
37+
}
38+
39+
} // namespace default_ad_api
40+
41+
#include <rclcpp_components/register_node_macro.hpp>
42+
RCLCPP_COMPONENTS_REGISTER_NODE(default_ad_api::HeartbeatNode)
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2024 The Autoware Contributors
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 HEARTBEAT_HPP_
16+
#define HEARTBEAT_HPP_
17+
18+
#include <autoware_ad_api_specs/system.hpp>
19+
#include <rclcpp/rclcpp.hpp>
20+
21+
// This file should be included after messages.
22+
#include "utils/types.hpp"
23+
24+
namespace default_ad_api
25+
{
26+
27+
class HeartbeatNode : public rclcpp::Node
28+
{
29+
public:
30+
explicit HeartbeatNode(const rclcpp::NodeOptions & options);
31+
32+
private:
33+
rclcpp::TimerBase::SharedPtr timer_;
34+
Pub<autoware_ad_api::system::Heartbeat> pub_;
35+
uint16_t sequence_ = 0;
36+
};
37+
38+
} // namespace default_ad_api
39+
40+
#endif // HEARTBEAT_HPP_

0 commit comments

Comments
 (0)