1515#include " arm/manipulation/point_at.hpp"
1616
1717#include < math.h>
18+ #include < tf2_geometry_msgs/tf2_geometry_msgs.hpp>
1819
1920namespace manipulation
2021{
@@ -23,15 +24,19 @@ using namespace std::chrono_literals;
2324using namespace std ::placeholders;
2425
2526PointAt::PointAt (
26- const std::string & xml_tag_name, const std::string & action_name,
27+ const std::string & xml_tag_name,
2728 const BT::NodeConfiguration & conf)
28- : manipulation::BtActionNode<
29- manipulation_interfaces::action::MoveEndEffector,
30- rclcpp_cascade_lifecycle::CascadeLifecycleNode>(xml_tag_name, action_name, conf)
29+ : BT::ActionNodeBase(xml_tag_name, conf)
3130{
31+ config ().blackboard ->get (" node" , node_);
3232}
3333
34- void PointAt::on_tick ()
34+ void PointAt::halt ()
35+ {
36+ RCLCPP_INFO (node_->get_logger (), " PointAt halted" );
37+ }
38+
39+ BT::NodeStatus PointAt::tick ()
3540{
3641 RCLCPP_DEBUG (node_->get_logger (), " PointAt ticked" );
3742
@@ -43,9 +48,10 @@ void PointAt::on_tick()
4348 getInput (" low_z" , low_z_);
4449 getInput (" high_z" , high_z_);
4550
51+ geometry_msgs::msg::Pose target_pose;
52+
4653 if (tf_frame_.empty () && pose_to_point_) {
47- goal_.pose = *pose_to_point_;
48- return ;
54+ target_pose = pose_to_point_->pose ;
4955 } else if (!tf_frame_.empty ()) {
5056 try {
5157 transform_ = tf_buffer_->lookupTransform (
@@ -54,62 +60,79 @@ void PointAt::on_tick()
5460 RCLCPP_ERROR (
5561 node_->get_logger (), " Could not transform %s to %s: %s" , tf_frame_.c_str (),
5662 base_frame_.c_str (), ex.what ());
57- setStatus (BT::NodeStatus::FAILURE);
58- return ;
63+ return BT::NodeStatus::FAILURE;
5964 }
60- // goal_.pose.pose.position.x = 0.5;
61- // auto y = 0.5*std::cos(std::atan2(transform_.transform.translation.y, transform_.transform.translation.x)) * std::sin(std::atan2(transform_.transform.translation.y, transform_.transform.translation.x));
62- // goal_.pose.pose.position.y = (std::isnan(y) || std::isinf(y)) ? 0.0 : (y);
63- // goal_.pose.pose.position.z = 0.5;
64- // goal_.pose.pose.orientation = transform_.transform.rotation;
65- auto angle = std::atan2 (transform_.transform .translation .y , transform_.transform .translation .x );
66- auto pitch = std::atan2 (transform_.transform .translation .z , transform_.transform .translation .x );
67- double desired_radius = 0.9 ; // 0.5
65+
66+ // Calculate pointing pose
67+ auto angle = std::atan2 (
68+ transform_.transform .translation .y ,
69+ transform_.transform .translation .x );
70+ double desired_radius = 0.9 ;
6871 auto x_point = desired_radius * std::cos (angle);
6972 auto y_point = desired_radius * std::sin (angle);
70- goal_. pose . pose . position . x = ( std::isnan (x_point) || std::isinf (x_point)) ? 0.0 : (x_point);
71- goal_. pose . pose . position .y = (std::isnan (y_point ) || std::isinf (y_point )) ? 0.0 : (y_point) ;
72- // if (transform_.transform.translation.z < low_z_) put 1.0 elif (transform_.transform.translation.z > high_z_) put 1 .0 else put transform_.transform.translation.z
73- goal_. pose . pose .position .z = (transform_.transform .translation .z < low_z_) ? low_z_ :
73+
74+ target_pose. position .x = (std::isnan (x_point ) || std::isinf (x_point )) ? 0.0 : x_point ;
75+ target_pose. position . y = ( std::isnan (y_point) || std::isinf (y_point)) ? 0 .0 : y_point;
76+ target_pose .position .z = (transform_.transform .translation .z < low_z_) ? low_z_ :
7477 (transform_.transform .translation .z > high_z_) ?
7578 high_z_ :
7679 transform_.transform .translation .z ;
77- goal_. pose . header . frame_id = base_frame_;
78- auto orientation = tf2::Quaternion (0 , 0 , 0 , 1 );
80+
81+ tf2::Quaternion orientation (0 , 0 , 0 , 1 );
7982 orientation.setEuler (0 , 0 , angle);
80- goal_. pose . pose .orientation .x = orientation.x ();
81- goal_. pose . pose .orientation .y = orientation.y ();
82- goal_. pose . pose .orientation .z = orientation.z ();
83- goal_. pose . pose .orientation .w = orientation.w ();
83+ target_pose .orientation .x = orientation.x ();
84+ target_pose .orientation .y = orientation.y ();
85+ target_pose .orientation .z = orientation.z ();
86+ target_pose .orientation .w = orientation.w ();
8487
8588 RCLCPP_INFO (
8689 node_->get_logger (), " Pointing from %s to %s" , base_frame_.c_str (),
8790 transform_.header .frame_id .c_str ());
8891 RCLCPP_INFO (node_->get_logger (), " Pointing at %s" , tf_frame_.c_str ());
8992 RCLCPP_INFO (
90- node_->get_logger (), " Pointing to %f %f %f" , goal_.pose .pose .position .x ,
91- goal_.pose .pose .position .y , goal_.pose .pose .position .z );
92- return ;
93+ node_->get_logger (), " Pointing to %f %f %f" , target_pose.position .x ,
94+ target_pose.position .y , target_pose.position .z );
95+ } else {
96+ RCLCPP_ERROR (node_->get_logger (), " No pose_to_point or tf_frame provided" );
97+ return BT::NodeStatus::FAILURE;
9398 }
94- }
9599
96- BT::NodeStatus PointAt::on_success ()
97- {
98- if (result_.result ->success ) {
99- return BT::NodeStatus::SUCCESS;
100- } else {
101- RCLCPP_ERROR (node_->get_logger (), " PointAt failed" );
100+ // Use MoveIt to plan and execute
101+ try {
102+ using moveit::planning_interface::MoveGroupInterface;
103+ auto move_group_interface = MoveGroupInterface (node_, " arm_torso" );
104+
105+ // Set the target pose
106+ move_group_interface.setPoseTarget (target_pose);
107+
108+ // Create a plan to that target pose
109+ moveit::planning_interface::MoveGroupInterface::Plan plan;
110+ bool success = static_cast <bool >(move_group_interface.plan (plan));
111+
112+ if (success) {
113+ RCLCPP_INFO (node_->get_logger (), " Plan found, executing..." );
114+ auto execute_result = move_group_interface.execute (plan);
115+ if (execute_result == moveit::core::MoveItErrorCode::SUCCESS) {
116+ RCLCPP_INFO (node_->get_logger (), " PointAt execution successful" );
117+ return BT::NodeStatus::SUCCESS;
118+ } else {
119+ RCLCPP_ERROR (node_->get_logger (), " Execution failed!" );
120+ return BT::NodeStatus::FAILURE;
121+ }
122+ } else {
123+ RCLCPP_ERROR (node_->get_logger (), " Planning failed!" );
124+ return BT::NodeStatus::FAILURE;
125+ }
126+ } catch (const std::exception & ex) {
127+ RCLCPP_ERROR (node_->get_logger (), " Exception during MoveIt planning: %s" , ex.what ());
102128 return BT::NodeStatus::FAILURE;
103129 }
104130}
105131
106132} // namespace manipulation
133+
107134#include " behaviortree_cpp_v3/bt_factory.h"
108135BT_REGISTER_NODES (factory)
109136{
110- BT::NodeBuilder builder = [](const std::string & name, const BT::NodeConfiguration & config) {
111- return std::make_unique<manipulation::PointAt>(name, " move_end_effector" , config);
112- };
113-
114- factory.registerBuilder <manipulation::PointAt>(" PointAt" , builder);
137+ factory.registerNodeType <manipulation::PointAt>(" PointAt" );
115138}
0 commit comments