Код: Выделить всё
void feedback_callback(
GoalHandleRecord::SharedPtr,
const std::shared_ptr feedback) {
RCLCPP_INFO(this->get_logger(), "Feedback received: %f",
feedback->current_total);
if (feedback->current_total >= 4.5 && !is_lap_completed_) {
if (this->goal_handle_) {
RCLCPP_INFO(this->get_logger(), "Attempting to cancel goal...");
auto cancel_future = action_client_->async_cancel_goal(this->goal_handle_);
}
}}
Другие соответствующие части кода:
клиент:
Код: Выделить всё
void result_callback(const GoalHandleRecord::WrappedResult &result) {
this->goal_done_ = true;
last_result_ = result; // Store the result for later access
switch (result.code) {
...
case rclcpp_action::ResultCode::CANCELED:
if (result.result) {
RCLCPP_INFO(this->get_logger(), "Total odometry points2:");
for (const auto &point : last_result_.result->list_of_odoms) {
RCLCPP_INFO(this->get_logger(), "Point2(x: %f, y: %f, z: %f)",
point.x, point.y, point.z);
}
}
RCLCPP_ERROR(this->get_logger(), "Goal was canceled");
break;
}
}
Код: Выделить всё
rclcpp_action::CancelResponse
handle_cancel(const std::shared_ptr goal_handle) {
RCLCPP_INFO(this->get_logger(), "Received request to cancel goal");
(void)goal_handle;
return rclcpp_action::CancelResponse::ACCEPT;
}
...
void execute(const std::shared_ptr goal_handle) {
RCLCPP_INFO(this->get_logger(), "Executing goal");
auto feedback = std::make_shared();
auto &message = feedback->current_total;
auto result = std::make_shared();
rclcpp::Rate loop_rate(1);
// Reset distance and initialization flag at the start of the goal
total_distance_ = 0.0;
is_initial_position_set_ = false;
while (rclcpp::ok()) {
// Check if there is a cancel request
if (goal_handle->is_canceling()) {
result->list_of_odoms = this->list_of_odoms_;
goal_handle->canceled(result);
RCLCPP_INFO(this->get_logger(), "Goal canceled");
return;
}
// Publish feedback
message = total_distance_;
goal_handle->publish_feedback(feedback);
loop_rate.sleep();
}
if (rclcpp::ok()) {
result->list_of_odoms = this->list_of_odoms_;
goal_handle->succeed(result);
RCLCPP_INFO(this->get_logger(), "Goal succeeded");
}
}
Подробнее здесь: https://stackoverflow.com/questions/792 ... ros2-and-c
Мобильная версия