How to preempt action by the client in ros2 using c++?
I am trying to do:
void send_cancel()
{
auto goal_handle = this->goal_handle_future.get();
auto cancel_result_future = this->client_ptr_->async_cancel_goal(goal_handle); //this line show how to cancel the action
}
Is there any possibility to cancel at some point according to some feedback received at feedback_callback?
I found another reference here, but it is a different scenario using not_composable:
// Copyright 2018 Open Source Robotics Foundation, Inc.
//
// 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 <inttypes.h>
#include <memory>
#include "example_interfaces/action/fibonacci.hpp"
#include "rclcpp/rclcpp.hpp"
// TODO(jacobperron): Remove this once it is included as part of 'rclcpp.hpp'
#include "rclcpp_action/rclcpp_action.hpp"
This file has been truncated. show original
I found the solution. The send_cancel must always be called inside the wall_timer !!
void feedback_callback(
GoalHandleMove::SharedPtr,
const std::shared_ptr feedback)
{
RCLCPP_INFO(
this->get_logger(),
"Feedback received: " +
feedback->feedback);
this->cont++;
if(this->cont==2){
this->timer_ = this->create_wall_timer(
std::chrono::milliseconds(500),
std::bind(&T3ActionClient::send_cancel, this));
}
}
1 Like
Nice!
It’s really good that you found the solution for yourself. Keep pushing your ROS learning! @vinicius.marques
1 Like