본 포스트는 ROS1에 대한 전반적인 내용 및 ROS2 Python에 대한 지식이 있다는 가정 하에 작성되었으며, The Construct Youtube 영상을 참고하였습니다.
또한, 시뮬레이션 환경은 turtlebot3 waffle_pi를 사용합니다 : https://github.com/ROBOTIS-GIT/turtlebot3_simulations
1. Action Server, Client 만들기
python과 내용은 같으므로 생략하고, 바로 예제 코드로 넘어갑니다.
1. 패키지 만들기
$ ros2 pkg create my_action --build-type ament_cmake --dependencies rclcpp rclcpp_action custom_interface
2. Action cpp 코드 작성
action_server.cpp
#include <functional>
#include <memory>
#include <thread>
#include "rclcpp/rclcpp.hpp"
#include "rclcpp_action/rclcpp_action.hpp"
#include "custom_interfaces/action/move.hpp"
#include "geometry_msgs/msg/twist.hpp"
class MyActionServer : public rclcpp::Node
{
public:
using Move = custom_interfaces::action::Move;
using GoalHandleMove = rclcpp_action::ServerGoalHandle<Move>;
explicit MyActionServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions())
: Node("my_action_server", options)
{
using namespace std::placeholders;
this->action_server_ = rclcpp_action::create_server<Move>(
this,
"move_robot_as",
std::bind(&MyActionServer::handle_goal, this, _1, _2),
std::bind(&MyActionServer::handle_cancel, this, _1),
std::bind(&MyActionServer::handle_accepted, this, _1));
publisher_ = this->create_publisher<geometry_msgs::msg::Twist>("cmd_vel", 10);
}
private:
rclcpp_action::Server<Move>::SharedPtr action_server_;
rclcpp::Publisher<geometry_msgs::msg::Twist>::SharedPtr publisher_;
rclcpp_action::GoalResponse handle_goal(
const rclcpp_action::GoalUUID & uuid,
std::shared_ptr<const Move::Goal> goal)
{
RCLCPP_INFO(this->get_logger(), "Received goal request with secs %d", goal->secs);
(void)uuid;
return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
}
rclcpp_action::CancelResponse handle_cancel(
const std::shared_ptr<GoalHandleMove> goal_handle)
{
RCLCPP_INFO(this->get_logger(), "Received request to cancel goal");
(void)goal_handle;
return rclcpp_action::CancelResponse::ACCEPT;
}
void handle_accepted(const std::shared_ptr<GoalHandleMove> goal_handle)
{
using namespace std::placeholders;
// executor를 막지 않고 빠르게 리턴 해야합니다.
std::thread{std::bind(&MyActionServer::execute, this, _1), goal_handle}.detach();
}
void execute(const std::shared_ptr<GoalHandleMove> goal_handle)
{
RCLCPP_INFO(this->get_logger(), "Executing goal");
const auto goal = goal_handle->get_goal();
auto feedback = std::make_shared<Move::Feedback>();
auto & message = feedback->feedback;
message = "Starting movement...";
auto result = std::make_shared<Move::Result>();
auto move = geometry_msgs::msg::Twist();
rclcpp::Rate loop_rate(1);
for (int i = 0; (i < goal->secs) && rclcpp::ok(); ++i) {
// 취소 요청이 있는지 확인
if (goal_handle->is_canceling()) {
result->status = message;
goal_handle->canceled(result);
RCLCPP_INFO(this->get_logger(), "Goal canceled");
return;
}
// 로봇 움직이면서 feedback 보내기
message = "Moving forward...";
move.linear.x = 0.3;
publisher_->publish(move);
goal_handle->publish_feedback(feedback);
RCLCPP_INFO(this->get_logger(), "Publish feedback");
loop_rate.sleep();
}
// goal이 끝났으면
if (rclcpp::ok()) {
result->status = "Finished action server. Robot moved during 5 seconds";
move.linear.x = 0.0;
publisher_->publish(move);
goal_handle->succeed(result);
RCLCPP_INFO(this->get_logger(), "Goal succeeded");
}
}
}; // class MyActionServer
int main(int argc, char ** argv)
{
rclcpp::init(argc, argv);
auto action_server = std::make_shared<MyActionServer>();
rclcpp::executors::MultiThreadedExecutor executor;
executor.add_node(action_server);
executor.spin();
rclcpp::shutdown();
return 0;
}
2. Action Client 코드 작성
action_client.cpp
#include <inttypes.h>
#include <memory>
#include <string>
#include <iostream>
#include "custom_interfaces/action/move.hpp"
#include "rclcpp/rclcpp.hpp"
#include "rclcpp_action/rclcpp_action.hpp"
class MyActionClient : public rclcpp::Node
{
public:
using Move = custom_interfaces::action::Move;
using GoalHandleMove = rclcpp_action::ClientGoalHandle<Move>;
explicit MyActionClient(const rclcpp::NodeOptions & node_options = rclcpp::NodeOptions())
: Node("my_action_client", node_options), goal_done_(false)
{
this->client_ptr_ = rclcpp_action::create_client<Move>(
this->get_node_base_interface(),
this->get_node_graph_interface(),
this->get_node_logging_interface(),
this->get_node_waitables_interface(),
"move_robot_as");
this->timer_ = this->create_wall_timer(
std::chrono::milliseconds(500),
std::bind(&MyActionClient::send_goal, this));
}
bool is_goal_done() const
{
return this->goal_done_;
}
void send_goal()
{
using namespace std::placeholders;
this->timer_->cancel();
this->goal_done_ = false;
if (!this->client_ptr_) {
RCLCPP_ERROR(this->get_logger(), "Action client not initialized");
}
if (!this->client_ptr_->wait_for_action_server(std::chrono::seconds(10))) {
RCLCPP_ERROR(this->get_logger(), "Action server not available after waiting");
this->goal_done_ = true;
return;
}
auto goal_msg = Move::Goal();
goal_msg.secs = 5;
RCLCPP_INFO(this->get_logger(), "Sending goal");
auto send_goal_options = rclcpp_action::Client<Move>::SendGoalOptions();
send_goal_options.goal_response_callback =
std::bind(&MyActionClient::goal_response_callback, this, _1);
send_goal_options.feedback_callback =
std::bind(&MyActionClient::feedback_callback, this, _1, _2);
send_goal_options.result_callback =
std::bind(&MyActionClient::result_callback, this, _1);
this->client_ptr_->async_send_goal(goal_msg, send_goal_options);
}
private:
rclcpp_action::Client<Move>::SharedPtr client_ptr_;
rclcpp::TimerBase::SharedPtr timer_;
bool goal_done_;
void goal_response_callback(const GoalHandleMove::SharedPtr & goal_handle)
{
if (!goal_handle) {
RCLCPP_ERROR(this->get_logger(), "Goal was rejected by server");
} else {
RCLCPP_INFO(this->get_logger(), "Goal accepted by server, waiting for result");
}
}
void feedback_callback(
GoalHandleMove::SharedPtr,
const std::shared_ptr<const Move::Feedback> feedback)
{
RCLCPP_INFO(
this->get_logger(), "Feedback received: %s", feedback->feedback.c_str());
}
void result_callback(const GoalHandleMove::WrappedResult & result)
{
this->goal_done_ = true;
switch (result.code) {
case rclcpp_action::ResultCode::SUCCEEDED:
break;
case rclcpp_action::ResultCode::ABORTED:
RCLCPP_ERROR(this->get_logger(), "Goal was aborted");
return;
case rclcpp_action::ResultCode::CANCELED:
RCLCPP_ERROR(this->get_logger(), "Goal was canceled");
return;
default:
RCLCPP_ERROR(this->get_logger(), "Unknown result code");
return;
}
RCLCPP_INFO(this->get_logger(), "Result received: %s", result.result->status.c_str());
}
}; // class MyActionClient
int main(int argc, char ** argv)
{
rclcpp::init(argc, argv);
auto action_client = std::make_shared<MyActionClient>();
rclcpp::executors::MultiThreadedExecutor executor;
executor.add_node(action_client);
while (!action_client->is_goal_done()) {
executor.spin();
}
rclcpp::shutdown();
return 0;
}
3. launch file 만들기
my_action_client.launch.py
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='my_action_cpp',
executable='action_client_node',
output='screen'),
])
my_action_server.launch.py
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='my_action_cpp',
executable='action_server_node',
output='screen'),
])
4. CMakeLists.txt 파일 수정
CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project(my_action_cpp)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()
# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(custom_interfaces REQUIRED)
find_package(geometry_msgs REQUIRED)
add_executable(action_client_node src/action_client.cpp)
ament_target_dependencies(action_client_node rclcpp rclcpp_action custom_interfaces)
add_executable(action_server_node src/action_server.cpp)
ament_target_dependencies(action_server_node rclcpp rclcpp_action custom_interfaces geometry_msgs)
install(TARGETS
action_client_node
action_server_node
DESTINATION lib/${PROJECT_NAME}
)
# Install launch files
install(DIRECTORY
launch
DESTINATION share/${PROJECT_NAME}/
)
if(BUILD_TESTING)
find_package(ament_lint_auto REQUIRED)
# the following line skips the linter which checks for copyrights
# comment the line when a copyright and license is added to all source files
set(ament_cmake_copyright_FOUND TRUE)
# the following line skips cpplint (only works in a git repo)
# comment the line when this package is in a git repo and when
# a copyright and license is added to all source files
set(ament_cmake_cpplint_FOUND TRUE)
ament_lint_auto_find_test_dependencies()
endif()
ament_package()
5. 빌드 후, 실행
Server 코드 설명
explicit MyActionServer(const rclcpp::NodeOptions & options = rclcpp::NodeOptions())
: Node("my_action_server", options)
{
using namespace std::placeholders;
this->action_server_ = rclcpp_action::create_server<Move>(
this,
"move_robot_as_2",
std::bind(&MyActionServer::handle_goal, this, _1, _2),
std::bind(&MyActionServer::handle_cancel, this, _1),
std::bind(&MyActionServer::handle_accepted, this, _1));
publisher_ = this->create_publisher<geometry_msgs::msg::Twist>("cmd_vel", 10);
}
- Move 타입의 커스텀 메시지를 사용하는 액션 서버를 만듭니다
- 이때, 객체를 선언하면서 액션 서버를 담고있는 node(this)를 넘겨줍니다.
- Client로부터 goal을 받을 때 호출할 Callback 함수는 handle_goal 입니다
- Client가 액션을 도중에 취소할 경우, 호출할 Callback 함수는 handle_cancel 입니다.
- goal을 받고, 이를 실행할 때 호출할 Callback 함수는 handle_accepted입니다.
rclcpp_action::GoalResponse handle_goal(
const rclcpp_action::GoalUUID & uuid,
std::shared_ptr<const Move::Goal> goal)
{
RCLCPP_INFO(this->get_logger(), "Received goal request with secs %d", goal->secs);
(void)uuid;
return rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE;
}
Client로부터 goal을 받을 때 호출할 Callback 함수입니다. goal을 제대로 받았으면, rclcpp_action::GoalResponse::ACCEPT_AND_EXECUTE를 리턴합니다.
rclcpp_action::CancelResponse handle_cancel(
const std::shared_ptr<GoalHandleMove> goal_handle)
{
RCLCPP_INFO(this->get_logger(), "Received request to cancel goal");
(void)goal_handle;
return rclcpp_action::CancelResponse::ACCEPT;
}
Client로부터 cancel을 받을 때 호출할 Callback 함수입니다. cancel 요청을 받으면 rclcpp_ation::CancelResponse::ACCEPT를 리턴합니다.
void handle_accepted(const std::shared_ptr<GoalHandleMove> goal_handle)
{
using namespace std::placeholders;
// this needs to return quickly to avoid blocking the executor, so spin up a new thread
std::thread{std::bind(&MyActionServer::execute, this, _1), goal_handle}.detach();
}
goal을 받았을 때, server node에서 메인으로 실행할 함수(메인함수가 아니라, Action에서 주요하게 쓰일 함수)를 쓰레드로 돌리기 위한 코드입니다. 이를 execute function이라고 합니다.
쓰레드로 돌리는 이유는 execute function을 실행하는데 시간이 오래 걸리기 때문입니다(우리 예제의 경우 5초동안 cmd_vel을 publish하는데, 이 동안 다른 모든게 멈추면 안되기 때문입니다)
void execute(const std::shared_ptr<GoalHandleMove> goal_handle)
{
RCLCPP_INFO(this->get_logger(), "Executing goal");
const auto goal = goal_handle->get_goal();
auto feedback = std::make_shared<Move::Feedback>();
auto & message = feedback->feedback;
message = "Starting movement...";
auto result = std::make_shared<Move::Result>();
auto move = geometry_msgs::msg::Twist();
rclcpp::Rate loop_rate(1);
for (int i = 0; (i < goal->secs) && rclcpp::ok(); ++i) {
// 취소 요청이 있는지 확인
if (goal_handle->is_canceling()) {
result->status = message;
goal_handle->canceled(result);
RCLCPP_INFO(this->get_logger(), "Goal canceled");
return;
}
// 로봇 움직이면서 feedback 보내기
message = "Moving forward...";
move.linear.x = 0.3;
publisher_->publish(move);
goal_handle->publish_feedback(feedback);
RCLCPP_INFO(this->get_logger(), "Publish feedback");
loop_rate.sleep();
}
// goal이 끝났으면
if (rclcpp::ok()) {
result->status = "Finished action server. Robot moved during 5 seconds";
move.linear.x = 0.0;
publisher_->publish(move);
goal_handle->succeed(result);
RCLCPP_INFO(this->get_logger(), "Goal succeeded");
}
}
}; // class MyActionServer
execute function입니다.
Client 코드 설명
this->timer_->cancel();
이 노드가 돌아가는동안 계속 timer callback을 보낼 것이 아니기 때문에, 한번만 하고 바로 꺼버립니다
auto send_goal_options = rclcpp_action::Client<Move>::SendGoalOptions();
send_goal_options.goal_response_callback =
std::bind(&MyActionClient::goal_response_callback, this, _1);
send_goal_options.feedback_callback =
std::bind(&MyActionClient::feedback_callback, this, _1, _2);
send_goal_options.result_callback =
std::bind(&MyActionClient::result_callback, this, _1);
this->client_ptr_->async_send_goal(goal_msg, send_goal_options);
goal options에다 callback 함수들을 정해주고, this->client_ptr_->async_send_goal(goal_msg, send_goal_options); 를 사용해서 Action server에 goal을 보낼것입니다.
custom Action은 python과 동일하므로 생략하겠습니다
'Study > [ROS2] ROS2 Basic' 카테고리의 다른 글
ROS2 with C++ - Lifecycle Nodes (0) | 2023.08.02 |
---|---|
ROS2 with C++ - Node composition (0) | 2023.07.30 |
ROS2 with C++ - Service (0) | 2023.07.30 |
ROS2 with C++ - Executors and Callbacks (0) | 2023.07.30 |
ROS2 with C++ - Topic (0) | 2023.07.28 |