Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Commit

Permalink
Merge pull request #5 from ROBOTIS-Platform/develop
Browse files Browse the repository at this point in the history
Added lifecycle and action examples
  • Loading branch information
robotpilot authored Oct 7, 2019
2 parents 17a4719 + f65ee2e commit 1e29a5e
Show file tree
Hide file tree
Showing 32 changed files with 1,195 additions and 10 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ $ ros2 run examples_rclcpp client -a ${number} -b ${number} -o ${arithmetic_oper

${arithmetic_operator} : plus, minus, multiply, division

### Action Server
```bash
$ ros2 run examples_rclcpp action_server
```

### Action Client
```bash
$ ros2 run examples_rclcpp action_client -n ${number}
```

### Launch
```bash
$ ros2 launch examples_rclcpp pub.launch.py
Expand Down Expand Up @@ -155,3 +165,39 @@ $ ros2 run rviz2 rviz2 -d examples_tf2/rviz/arm.rviz
```bash
$ ros2 service call state std_srvs/srv/SetBool "data: false"
```

## examples_lifecycle

### Run examples_lifecycle
```bash
$ ros2 run examples_lifecycle robot
```

### Launch examples_lifecycle
```bash
$ ros2 launch examples_lifecycle bringup.launch.py auto_activate:=False
```

### Lifecycle client
#### Trigger lifecycle state transition
```bash
$ ros2 lifecycle set /example/robot configure
$ ros2 lifecycle set /example/robot activate
$ ros2 lifecycle set /example/robot deactivate
$ ros2 lifecycle set /example/robot shutdown
```

#### Get lifecycle state for one or more nodes
```
$ ros2 lifecycle get /example/robot
```

#### Output a list of available transitions
```
$ ros2 lifecycle list /example/robot
```

#### Output a list of nodes with lifecycle
```
$ ros2 lifecycle nodes
```
16 changes: 16 additions & 0 deletions examples_lifecycle/CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Changelog for package examples_lifecycle
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1.1.0 (2019-10-07)
------------------
* Added example package for lifecycle
* Contributors: Darby Lim

1.0.0 (2019-08-05)
------------------
* None

0.1.0 (2019-05-15)
------------------
* None
69 changes: 69 additions & 0 deletions examples_lifecycle/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
################################################################################
# Set minimum required version of cmake, project name and compile options
################################################################################
cmake_minimum_required(VERSION 3.5)
project(examples_lifecycle)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
endif()

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

################################################################################
# Find and load build settings from external packages
################################################################################
find_package(ament_cmake REQUIRED)
find_package(lifecycle_msgs REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_lifecycle REQUIRED)
find_package(std_msgs REQUIRED)

################################################################################
# Build
################################################################################
include_directories(
include
)

set(LIFECYCLE_NODE_NAME robot)

set(dependencies
"lifecycle_msgs"
"rclcpp"
"rclcpp_lifecycle"
"std_msgs"
)

add_executable(${LIFECYCLE_NODE_NAME} src/main.cpp src/robot.cpp)
ament_target_dependencies(${LIFECYCLE_NODE_NAME} ${dependencies})

################################################################################
# Install
################################################################################
install(TARGETS
${LIFECYCLE_NODE_NAME}
DESTINATION lib/${PROJECT_NAME}
)

install(DIRECTORY launch
DESTINATION share/${PROJECT_NAME}
)

################################################################################
# Test
################################################################################
if(BUILD_TESTING)
find_package(ament_cmake_pytest REQUIRED)
find_package(ament_cmake_gtest REQUIRED)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
endif()

################################################################################
# Macro for ament package
################################################################################
ament_export_include_directories(include)
ament_package()
1 change: 1 addition & 0 deletions examples_lifecycle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://design.ros2.org/articles/node_lifecycle.html
89 changes: 89 additions & 0 deletions examples_lifecycle/include/examples_lifecycle/robot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*******************************************************************************
* Copyright 2019 ROBOTIS CO., LTD.
*
* 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.
*******************************************************************************/

/* Authors: Darby Lim */

#ifndef EXAMPLES_LIFECYCLE_ROBOT_HPP_
#define EXAMPLES_LIFECYCLE_ROBOT_HPP_

// Load smart pointer
#include <memory>
// Load std::string
#include <string>
// Load move, forward, tuple and etc (https://en.cppreference.com/w/cpp/header/utility)
#include <utility>
// Load change_state service in lifecycle_msgs
#include <lifecycle_msgs/srv/change_state.hpp>
// Load RCLCPP Lifecycle Node Library (https://github.com/ros2/rclcpp/tree/master/rclcpp_lifecycle)
#include <rclcpp_lifecycle/lifecycle_node.hpp>
// Load string message in std_msgs
#include <std_msgs/msg/string.hpp>


using LifecycleReturn =
rclcpp_lifecycle::node_interfaces::LifecycleNodeInterface::CallbackReturn;

namespace robotis
{
class Robot : public rclcpp_lifecycle::LifecycleNode
{
public:
// 'explicit' makes block data conversion
explicit Robot(const bool & auto_activate);

// Specify overriding functions
LifecycleReturn on_configure(const rclcpp_lifecycle::State &) override;
LifecycleReturn on_activate(const rclcpp_lifecycle::State &) override;
LifecycleReturn on_deactivate(const rclcpp_lifecycle::State &) override;
LifecycleReturn on_cleanup(const rclcpp_lifecycle::State &) override;
LifecycleReturn on_shutdown(const rclcpp_lifecycle::State &) override;

private:
// Get all parameters when you set in launch file or yaml
void get_parameters();

// Initialization robot
void plugin_robot();

// Shutdown robot
void unplug_robot();

// Declare status of robot
void run();

// Declare status of robot
void stop();

// Declare robot name. This variable will be set by parameters
std::string robot_name_;

// Declare robot state. This variable will be changed by lifecycle state
std::string robot_state_;

// Declare topic message as unique pointer
std::unique_ptr<std_msgs::msg::String> msg_;

// Declare publisher as shared pointer
std::shared_ptr<rclcpp_lifecycle::LifecyclePublisher<std_msgs::msg::String>> pub_;

// Declare timer to publish data periodically
rclcpp::TimerBase::SharedPtr timer_;

// Declare client to change state
std::shared_ptr<rclcpp::Client<lifecycle_msgs::srv::ChangeState>> client_change_state_;
};
} // robotis
#endif // EXAMPLES_LIFECYCLE_ROBOT_HPP_
50 changes: 50 additions & 0 deletions examples_lifecycle/launch/bringup.launch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/usr/bin/env python3
#
# Copyright 2019 ROBOTIS CO., LTD.
#
# 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.
#
# Authors: Darby Lim

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.actions import LogInfo
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node


def generate_launch_description():
auto_activate = LaunchConfiguration('auto_activate', default='False')
namespace = LaunchConfiguration('ns', default='example')

return LaunchDescription([
LogInfo(msg=['Execute lifecycle node!!']),

DeclareLaunchArgument(
'auto_activate',
default_value=auto_activate,
description='Specifying auto activate node'),

DeclareLaunchArgument(
'ns',
default_value=namespace,
description='Specifying namespace to node'),

Node(
node_namespace=namespace,
package='examples_lifecycle',
node_executable='robot',
parameters=[{'robot_name': 'C3PO'}],
arguments=['-a', auto_activate],
output='screen'),
])
20 changes: 20 additions & 0 deletions examples_lifecycle/package.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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>examples_lifecycle</name>
<version>1.1.0</version>
<description>
Examples of ROS 2 RCLCPP Lifecycle nodes
</description>
<author email="[email protected]">Darby Lim</author>
<maintainer email="[email protected]">Pyo</maintainer>
<license>Apache 2.0</license>
<buildtool_depend>ament_cmake</buildtool_depend>
<depend>lifecycle_msgs</depend>
<depend>rclcpp</depend>
<depend>rclcpp_lifecycle</depend>
<depend>std_msgs</depend>
<export>
<build_type>ament_cmake</build_type>
</export>
</package>
Loading

0 comments on commit 1e29a5e

Please sign in to comment.