19
19
#include < chrono>
20
20
#include < memory>
21
21
#include < utility>
22
+ #include < string>
22
23
23
24
#include " nav2_behaviors/timed_behavior.hpp"
24
25
#include " nav2_msgs/action/drive_on_heading.hpp"
@@ -60,17 +61,18 @@ class DriveOnHeading : public TimedBehavior<ActionT>
60
61
*/
61
62
ResultStatus onRun (const std::shared_ptr<const typename ActionT::Goal> command) override
62
63
{
64
+ std::string error_msg;
63
65
if (command->target .y != 0.0 || command->target .z != 0.0 ) {
64
- RCLCPP_INFO (
65
- this ->logger_ ,
66
- " DrivingOnHeading in Y and Z not supported, will only move in X." );
67
- return ResultStatus{Status::FAILED, ActionT::Result::INVALID_INPUT};
66
+ error_msg = " DrivingOnHeading in Y and Z not supported, will only move in X." ;
67
+ RCLCPP_INFO (this ->logger_ , error_msg.c_str ());
68
+ return ResultStatus{Status::FAILED, ActionT::Result::INVALID_INPUT, error_msg};
68
69
}
69
70
70
71
// Ensure that both the speed and direction have the same sign
71
72
if (!((command->target .x > 0.0 ) == (command->speed > 0.0 )) ) {
72
- RCLCPP_ERROR (this ->logger_ , " Speed and command sign did not match" );
73
- return ResultStatus{Status::FAILED, ActionT::Result::INVALID_INPUT};
73
+ error_msg = " Speed and command sign did not match" ;
74
+ RCLCPP_ERROR (this ->logger_ , error_msg.c_str ());
75
+ return ResultStatus{Status::FAILED, ActionT::Result::INVALID_INPUT, error_msg};
74
76
}
75
77
76
78
command_x_ = command->target .x ;
@@ -83,11 +85,12 @@ class DriveOnHeading : public TimedBehavior<ActionT>
83
85
initial_pose_, *this ->tf_ , this ->local_frame_ , this ->robot_base_frame_ ,
84
86
this ->transform_tolerance_ ))
85
87
{
86
- RCLCPP_ERROR (this ->logger_ , " Initial robot pose is not available." );
87
- return ResultStatus{Status::FAILED, ActionT::Result::TF_ERROR};
88
+ error_msg = " Initial robot pose is not available." ;
89
+ RCLCPP_ERROR (this ->logger_ , error_msg.c_str ());
90
+ return ResultStatus{Status::FAILED, ActionT::Result::TF_ERROR, error_msg};
88
91
}
89
92
90
- return ResultStatus{Status::SUCCEEDED, ActionT::Result::NONE};
93
+ return ResultStatus{Status::SUCCEEDED, ActionT::Result::NONE, " " };
91
94
}
92
95
93
96
/* *
@@ -99,19 +102,20 @@ class DriveOnHeading : public TimedBehavior<ActionT>
99
102
rclcpp::Duration time_remaining = end_time_ - this ->clock_ ->now ();
100
103
if (time_remaining.seconds () < 0.0 && command_time_allowance_.seconds () > 0.0 ) {
101
104
this ->stopRobot ();
102
- RCLCPP_WARN (
103
- this -> logger_ ,
104
- " Exceeded time allowance before reaching the DriveOnHeading goal - Exiting DriveOnHeading " );
105
- return ResultStatus{Status::FAILED, ActionT::Result::NONE};
105
+ std::string error_msg =
106
+ " Exceeded time allowance before reaching the DriveOnHeading goal - Exiting DriveOnHeading " ;
107
+ RCLCPP_WARN ( this -> logger_ , error_msg. c_str () );
108
+ return ResultStatus{Status::FAILED, ActionT::Result::NONE, error_msg };
106
109
}
107
110
108
111
geometry_msgs::msg::PoseStamped current_pose;
109
112
if (!nav2_util::getCurrentPose (
110
113
current_pose, *this ->tf_ , this ->local_frame_ , this ->robot_base_frame_ ,
111
114
this ->transform_tolerance_ ))
112
115
{
113
- RCLCPP_ERROR (this ->logger_ , " Current robot pose is not available." );
114
- return ResultStatus{Status::FAILED, ActionT::Result::TF_ERROR};
116
+ std::string error_msg = " Current robot pose is not available." ;
117
+ RCLCPP_ERROR (this ->logger_ , error_msg.c_str ());
118
+ return ResultStatus{Status::FAILED, ActionT::Result::TF_ERROR, error_msg};
115
119
}
116
120
117
121
double diff_x = initial_pose_.pose .position .x - current_pose.pose .position .x ;
@@ -123,7 +127,7 @@ class DriveOnHeading : public TimedBehavior<ActionT>
123
127
124
128
if (distance >= std::fabs (command_x_)) {
125
129
this ->stopRobot ();
126
- return ResultStatus{Status::SUCCEEDED, ActionT::Result::NONE};
130
+ return ResultStatus{Status::SUCCEEDED, ActionT::Result::NONE, " " };
127
131
}
128
132
129
133
auto cmd_vel = std::make_unique<geometry_msgs::msg::TwistStamped>();
@@ -140,13 +144,14 @@ class DriveOnHeading : public TimedBehavior<ActionT>
140
144
141
145
if (!isCollisionFree (distance, cmd_vel->twist , pose2d)) {
142
146
this ->stopRobot ();
143
- RCLCPP_WARN (this ->logger_ , " Collision Ahead - Exiting DriveOnHeading" );
144
- return ResultStatus{Status::FAILED, ActionT::Result::COLLISION_AHEAD};
147
+ std::string error_msg = " Collision Ahead - Exiting DriveOnHeading" ;
148
+ RCLCPP_WARN (this ->logger_ , error_msg.c_str ());
149
+ return ResultStatus{Status::FAILED, ActionT::Result::COLLISION_AHEAD, error_msg};
145
150
}
146
151
147
152
this ->vel_pub_ ->publish (std::move (cmd_vel));
148
153
149
- return ResultStatus{Status::RUNNING, ActionT::Result::NONE};
154
+ return ResultStatus{Status::RUNNING, ActionT::Result::NONE, " " };
150
155
}
151
156
152
157
/* *
0 commit comments