22
33#include " nav2_util/simple_action_server.hpp"
44#include " pose_3d.hpp"
5+ #include " tf2/LinearMath/Matrix3x3.h"
6+ #include " tf2_ros/transform_broadcaster.h"
7+ #include " tf2_ros/transform_listener.h"
8+ #include " tf2/convert.h"
9+ #include " tf2_geometry_msgs/tf2_geometry_msgs.h"
510
611#define LOG (level, ...) RCLCPP_##level(this ->get_logger (), __VA_ARGS__)
712
@@ -26,11 +31,11 @@ TaskMonitor::TaskMonitor() : Node("task_monitor") {
2631 // Subscriber to get the robot pose from amcl node
2732 robot_pose_sub_ = this ->create_subscription <geometry_msgs::msg::PoseWithCovarianceStamped>(
2833 " amcl_pose" , rclcpp::QoS (rclcpp::KeepLast (1 )).transient_local ().reliable (),
29- std::bind (&TaskMonitor::AmclPoseCallback () , this , std::placeholders::_1));
34+ std::bind (&TaskMonitor::AmclPoseCallback, this , std::placeholders::_1));
3035
3136 // Action server to initialize navigation properly
3237 initialize_navigation_action_server_ = std::make_unique<InitializeNavigationActionServer>(
33- rclcpp_node_ , " initialize_navigation" , std::bind (&TaskMonitor::SetInitialPose, this ));
38+ this -> create_sub_node ( " action_sub_node " ) , " initialize_navigation" , std::bind (&TaskMonitor::SetInitialPose, this ));
3439
3540 initial_pose_ = GetInitialPose ();
3641}
@@ -56,25 +61,29 @@ geometry_msgs::msg::PoseWithCovarianceStamped TaskMonitor::GetInitialPose() {
5661 return initial_pose_with_covariance_msg;
5762}
5863
59- void TaskMonitor::AmclPoseCallback (const geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr msg)
60- robot_pose_ = msg->pose ;
64+ void TaskMonitor::AmclPoseCallback (const geometry_msgs::msg::PoseWithCovarianceStamped::SharedPtr msg) {
65+ if (msg) {
66+ robot_pose_ = *msg;
67+ }
6168}
6269
6370bool TaskMonitor::IsRobotAtPose (geometry_msgs::msg::PoseWithCovarianceStamped desired_pose) {
6471 // Get the relative tf from the actual robot pose to the desired pose
65- tf2::Transform robot_pose_tf = tf2::fromMsg (robot_pose_);
66- tf2::Transform desired_pose_tf = tf2::fromMsg (desired_pose);
72+ tf2::Stamped<tf2::Transform> robot_pose_tf, desired_pose_tf;
73+ tf2::fromMsg (robot_pose_, robot_pose_tf);
74+ tf2::fromMsg (desired_pose, desired_pose_tf);
6775 tf2::Transform relative_tf = robot_pose_tf.inverseTimes (desired_pose_tf);
6876
6977 // Check that location coordinates are lower than the distance tolerance
7078 auto relative_translation = relative_tf.getOrigin ();
71- for (auto & coordinate : relative_translation) {
79+ std::vector<double > relative_translation_vector{relative_translation.getX (), relative_translation.getY (), relative_translation.getZ ()};
80+ for (auto &coordinate : relative_translation_vector) {
7281 if (coordinate > pose_distance_tolerance_)
7382 return false ;
7483 }
7584
7685 // Check that the RPY angles are lower than the angle tolerance
77- tf2::Matrix3x3 relative_orientation_matrix (relative_tf.getOrientation ());
86+ tf2::Matrix3x3 relative_orientation_matrix (relative_tf.getRotation ());
7887 std::vector<double > relative_orientation_vector;
7988 relative_orientation_vector.reserve (3 );
8089 relative_orientation_matrix.getRPY (relative_orientation_vector[0 ], relative_orientation_vector[1 ], relative_orientation_vector[2 ]);
@@ -88,13 +97,13 @@ bool TaskMonitor::IsRobotAtPose(geometry_msgs::msg::PoseWithCovarianceStamped de
8897
8998void TaskMonitor::SetInitialPose () {
9099 LOG (INFO, " Initialize navigation service called" );
91- auto start_time = rclcpp::Time:: now ();
100+ auto start_time = this -> now ();
92101 initial_pose_ = initialize_navigation_action_server_->get_current_goal ()->pose ;
93102 timeout_s_ = initialize_navigation_action_server_->get_current_goal ()->timeout_s ;
94103 initial_pose_pub_->publish (initial_pose_);
95104
96105 // Check that the robot is at the desired pose
97- while ((rclcpp::Time:: now () - start_time).seconds () < timeout_s_) {
106+ while ((this -> now () - start_time).seconds () < timeout_s_) {
98107 if (IsRobotAtPose (initial_pose_)) {
99108 LOG (INFO, " Robot is at initial pose" );
100109 initialize_navigation_action_server_->succeeded_current ();
0 commit comments