|
| 1 | +// Copyright 2024 Intelligent Robotics Lab - Gentlebots |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "arm/manipulation/play_motion_predefined.hpp" |
| 16 | + |
| 17 | +#include <string> |
| 18 | + |
| 19 | +#include "behaviortree_cpp_v3/behavior_tree.h" |
| 20 | +#include "play_motion2_msgs/action/play_motion2.hpp" |
| 21 | + |
| 22 | +namespace manipulation |
| 23 | +{ |
| 24 | + |
| 25 | +using namespace std::chrono_literals; |
| 26 | +using namespace std::placeholders; |
| 27 | + |
| 28 | +PlayMotionPredefined::PlayMotionPredefined( |
| 29 | + const std::string & xml_tag_name, const std::string & action_name, |
| 30 | + const BT::NodeConfiguration & conf) |
| 31 | +: manipulation::BtActionNode< |
| 32 | + play_motion2_msgs::action::PlayMotion2, |
| 33 | + rclcpp_cascade_lifecycle::CascadeLifecycleNode>(xml_tag_name, action_name, conf) |
| 34 | +{ |
| 35 | +} |
| 36 | + |
| 37 | +void PlayMotionPredefined::on_tick() |
| 38 | +{ |
| 39 | + RCLCPP_DEBUG(node_->get_logger(), "PlayMotionPredefined ticked"); |
| 40 | + |
| 41 | + // Read ports |
| 42 | + getInput("motion_name", motion_name_); |
| 43 | + getInput("skip_planning", skip_planning_); |
| 44 | + |
| 45 | + // Fill goal |
| 46 | + goal_.motion_name = motion_name_; |
| 47 | + goal_.skip_planning = skip_planning_; |
| 48 | + |
| 49 | + RCLCPP_INFO( |
| 50 | + node_->get_logger(), "Requesting PlayMotion2 '%s' (skip_planning=%s)", |
| 51 | + goal_.motion_name.c_str(), skip_planning_ ? "true" : "false"); |
| 52 | +} |
| 53 | + |
| 54 | +BT::NodeStatus PlayMotionPredefined::on_success() |
| 55 | +{ |
| 56 | + if (result_.result && result_.result->success) { |
| 57 | + return BT::NodeStatus::SUCCESS; |
| 58 | + } else { |
| 59 | + if (result_.result) { |
| 60 | + RCLCPP_ERROR(node_->get_logger(), "PlayMotion2 failed: %s", result_.result->error.c_str()); |
| 61 | + } else { |
| 62 | + RCLCPP_ERROR(node_->get_logger(), "PlayMotion2 failed: no result received"); |
| 63 | + } |
| 64 | + return BT::NodeStatus::FAILURE; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace manipulation |
| 69 | + |
| 70 | +#include "behaviortree_cpp_v3/bt_factory.h" |
| 71 | +BT_REGISTER_NODES(factory) |
| 72 | +{ |
| 73 | + BT::NodeBuilder builder = [](const std::string & name, const BT::NodeConfiguration & config) { |
| 74 | + // Default action server name as per play_motion2: "/play_motion2" |
| 75 | + return std::make_unique<manipulation::PlayMotionPredefined>(name, "/play_motion2", config); |
| 76 | + }; |
| 77 | + |
| 78 | + factory.registerBuilder<manipulation::PlayMotionPredefined>("PlayMotionPredefined", builder); |
| 79 | +} |
0 commit comments