Skip to content

Commit

Permalink
feat: add autoware_node and autoware_test node (#113)
Browse files Browse the repository at this point in the history
Signed-off-by: M. Fatih Cırıt <[email protected]>
  • Loading branch information
xmfcx authored Dec 10, 2024
1 parent a61009d commit 1bba5d3
Show file tree
Hide file tree
Showing 13 changed files with 390 additions and 0 deletions.
26 changes: 26 additions & 0 deletions common/autoware_node/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
cmake_minimum_required(VERSION 3.8)
project(autoware_node)

find_package(autoware_cmake REQUIRED)
autoware_package()

ament_auto_add_library(${PROJECT_NAME} src/node.cpp)

if(BUILD_TESTING)
file(GLOB_RECURSE TEST_FILES test/*.cpp)

foreach(TEST_FILE ${TEST_FILES})
# Get the test name without directory and extension
get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE)

# Add each test separately
ament_add_ros_isolated_gtest(${TEST_NAME} ${TEST_FILE} TIMEOUT 10)
target_include_directories(${TEST_NAME} PRIVATE src/include)
target_link_libraries(${TEST_NAME} ${PROJECT_NAME})
ament_target_dependencies(${TEST_NAME}
rclcpp
rclcpp_lifecycle)
endforeach()
endif()

ament_auto_package(INSTALL_TO_SHARE)
23 changes: 23 additions & 0 deletions common/autoware_node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Autoware Node

## Abbreviations

- **AN:** Autoware Node

## Overview

AN is an `autoware.core` package designed to provide a base class for all future nodes in the
system.
It also inherits all lifecycle control capabilities of the base
class [LifecycleNode](https://docs.ros2.org/latest/api/rclcpp_lifecycle/classrclcpp__lifecycle_1_1LifecycleNode.html)

## Usage

Check the [autoware_test_node](../../demos/autoware_test_node/README.md) package for an example of how to use `autoware::Node`.

## Design

### Lifecycle

AN inherits from ROS 2 [rclcpp_lifecycle::LifecycleNode](https://design.ros2.org/articles/node_lifecycle.html) and has
all the basic functions of it.
41 changes: 41 additions & 0 deletions common/autoware_node/include/autoware/node/node.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2024 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AUTOWARE__NODE__NODE_HPP_
#define AUTOWARE__NODE__NODE_HPP_

#include "autoware/node/visibility_control.hpp"

#include <rclcpp_lifecycle/lifecycle_node.hpp>

#include <string>

namespace autoware::node
{
using CallbackReturn = rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

class Node : public rclcpp_lifecycle::LifecycleNode
{
public:
AUTOWARE_NODE_PUBLIC
explicit Node(
const std::string & node_name, const std::string & ns = "",
const rclcpp::NodeOptions & options = rclcpp::NodeOptions());

protected:
CallbackReturn on_shutdown(const rclcpp_lifecycle::State & state) override;
};
} // namespace autoware::node

#endif // AUTOWARE__NODE__NODE_HPP_
26 changes: 26 additions & 0 deletions common/autoware_node/include/autoware/node/visibility_control.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2024 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef AUTOWARE__NODE__VISIBILITY_CONTROL_HPP_
#define AUTOWARE__NODE__VISIBILITY_CONTROL_HPP_

#include "rcutils/visibility_control_macros.h"
#ifdef AUTOWARE_NODE_BUILDING_DLL
#define AUTOWARE_NODE_PUBLIC RCUTILS_EXPORT
#else
#define AUTOWARE_NODE_PUBLIC RCUTILS_IMPORT
#endif // !AUTOWARE_NODE_BUILDING_DLL
#define AUTOWARE_NODE_LOCAL RCUTILS_LOCAL

#endif // AUTOWARE__NODE__VISIBILITY_CONTROL_HPP_
21 changes: 21 additions & 0 deletions common/autoware_node/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>autoware_node</name>
<version>0.0.0</version>
<description>Autoware Node is an Autoware.Core package designed to provide a base class for all nodes in the system.</description>
<maintainer email="[email protected]">M. Fatih Cırıt</maintainer>
<license>Apache-2.0</license>

<buildtool_depend>ament_cmake_auto</buildtool_depend>
<buildtool_depend>autoware_cmake</buildtool_depend>

<depend>rclcpp_lifecycle</depend>

<test_depend>ament_cmake_ros</test_depend>
<test_depend>autoware_lint_common</test_depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
38 changes: 38 additions & 0 deletions common/autoware_node/src/node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright 2024 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <autoware/node/node.hpp>
#include <rclcpp/rclcpp.hpp>

#include <string>

namespace autoware::node
{
Node::Node(
const std::string & node_name, const std::string & ns, const rclcpp::NodeOptions & options)
: LifecycleNode(node_name, ns, options)
{
RCLCPP_DEBUG(
get_logger(), "Node %s constructor was called.",
get_node_base_interface()->get_fully_qualified_name());
}

CallbackReturn Node::on_shutdown(const rclcpp_lifecycle::State & state)
{
RCLCPP_DEBUG(
get_logger(), "Node %s shutdown was called with state %s.",
get_node_base_interface()->get_fully_qualified_name(), state.label().c_str());
return CallbackReturn::SUCCESS;
}
} // namespace autoware::node
60 changes: 60 additions & 0 deletions common/autoware_node/test/test_an_init_shutdown.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2024 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <autoware/node/node.hpp>
#include <rclcpp/rclcpp.hpp>

#include <lifecycle_msgs/msg/state.hpp>

#include <gtest/gtest.h>

#include <memory>

class AutowareNodeInitShutdown : public ::testing::Test
{
public:
void SetUp() override { rclcpp::init(0, nullptr); }

void TearDown() override { rclcpp::shutdown(); }

rclcpp::NodeOptions node_options_an_;
};

TEST_F(AutowareNodeInitShutdown, NodeInitShutdown)
{
autoware::node::Node::SharedPtr autoware_node =
std::make_shared<autoware::node::Node>("test_node", "test_ns", node_options_an_);

auto executor = std::make_shared<rclcpp::executors::SingleThreadedExecutor>();
executor->add_node(autoware_node->get_node_base_interface());

std::thread thread_spin = std::thread([&executor]() { executor->spin(); });

ASSERT_EQ(
autoware_node->get_current_state().id(),
lifecycle_msgs::msg::State::PRIMARY_STATE_UNCONFIGURED);

auto state = autoware_node->shutdown();

ASSERT_EQ(state.id(), lifecycle_msgs::msg::State::PRIMARY_STATE_FINALIZED);

// wait until executor is spinning
while (!executor->is_spinning()) {
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
executor->cancel(); // make sure cancel is called after spin
if (thread_spin.joinable()) {
thread_spin.join();
}
}
15 changes: 15 additions & 0 deletions demos/autoware_test_node/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.8)
project(autoware_test_node)

find_package(autoware_cmake REQUIRED)
autoware_package()

ament_auto_add_library(${PROJECT_NAME} SHARED
src/test_node.cpp)

rclcpp_components_register_node(${PROJECT_NAME}
PLUGIN "autoware::test_node::TestNode"
EXECUTABLE ${PROJECT_NAME}_node)

ament_auto_package(INSTALL_TO_SHARE
launch)
53 changes: 53 additions & 0 deletions demos/autoware_test_node/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# autoware_test_node

This package contains a simple example of how to use `autoware::Node`.

## Usage

```bash
ros2 launch autoware_test_node autoware_test_node.launch.xml
```

### Lifecycle control

Information on Lifecycle nodes can be found [here](https://design.ros2.org/articles/node_lifecycle.html).

Output a list of nodes with lifecycle:

```console
$ ros2 lifecycle nodes
/test_ns1/test_node1
```

Get the current state of a node:

```console
$ ros2 lifecycle get /test_ns1/test_node1
unconfigured [1]
```

List the available transitions for the node:

```console
$ ros2 lifecycle list /test_ns1/test_node1
- configure [1]
Start: unconfigured
Goal: configuring
- shutdown [5]
Start: unconfigured
Goal: shuttingdown
```

Shutdown the node:

```console
$ ros2 lifecycle set /test_ns1/test_node1 shutdown
Transitioning successful
```

```console
$ ros2 lifecycle get /test_ns1/test_node1
finalized [4]
```

The node will remain alive in the `finalized` state until it is killed by the user.
3 changes: 3 additions & 0 deletions demos/autoware_test_node/launch/autoware_test_node.launch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<launch>
<node pkg="autoware_test_node" exec="autoware_test_node_node" name="test_node1" namespace="test_ns1"/>
</launch>
21 changes: 21 additions & 0 deletions demos/autoware_test_node/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
<name>autoware_test_node</name>
<version>0.0.0</version>
<description>Test package for Autoware Node.</description>
<maintainer email="[email protected]">M. Fatih Cırıt</maintainer>
<license>Apache-2.0</license>

<buildtool_depend>ament_cmake_auto</buildtool_depend>
<buildtool_depend>autoware_cmake</buildtool_depend>

<depend>autoware_node</depend>
<depend>rclcpp</depend>
<depend>rclcpp_components</depend>
<depend>rclcpp_lifecycle</depend>

<export>
<build_type>ament_cmake</build_type>
</export>
</package>
32 changes: 32 additions & 0 deletions demos/autoware_test_node/src/include/test_node.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2024 The Autoware Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef TEST_NODE_HPP_
#define TEST_NODE_HPP_

#include <autoware/node/node.hpp>
#include <rclcpp_lifecycle/lifecycle_node.hpp>

namespace autoware::test_node
{

class TestNode : public autoware::node::Node
{
public:
explicit TestNode(const rclcpp::NodeOptions & options = rclcpp::NodeOptions());
};

} // namespace autoware::test_node

#endif // TEST_NODE_HPP_
Loading

0 comments on commit 1bba5d3

Please sign in to comment.