40 cmd_vel_pub_ = nh->create_publisher<geometry_msgs::msg::Twist>(
"/cmd_vel", rclcpp::QoS(1));
46 geometry_msgs::msg::Twist cmd_vel;
49 geometry_msgs::msg::Pose currentPose = pose->toPoseMsg();
51 rclcpp::Rate loop_rate(10);
52 double countAngle = 0;
53 auto prevyaw = tf2::getYaw(currentPose.orientation);
56 double kp_linear = 0.5;
57 double ki_linear = 0.0;
58 double kd_linear = 0.1;
60 double kp_angular = 0.5;
61 double ki_angular = 0.0;
62 double kd_angular = 0.1;
66 RCLCPP_INFO_STREAM_THROTTLE(
68 "CbPositionControlFreeSpace, current pose: " << currentPose.position.x <<
", "
69 << currentPose.position.y <<
", "
70 << tf2::getYaw(currentPose.orientation));
72 RCLCPP_INFO_STREAM_THROTTLE(
74 "CbPositionControlFreeSpace, target pose: " <<
target_pose_.position.x <<
", "
79 currentPose = pose->toPoseMsg();
85 double error_x =
target_pose_.position.x - currentPose.position.x;
86 double error_y =
target_pose_.position.y - currentPose.position.y;
89 double distance_to_target = std::sqrt(error_x * error_x + error_y * error_y);
92 getLogger(),
"[" <<
getName() <<
"] distance to target: " << distance_to_target
98 RCLCPP_INFO(
getLogger(),
"Goal reached!");
100 geometry_msgs::msg::Twist cmd_vel_msg;
101 cmd_vel_msg.linear.x = 0.0;
102 cmd_vel_msg.angular.z = 0.0;
109 double desired_yaw = std::atan2(error_y, error_x);
113 double yaw_error = desired_yaw - (tf2::getYaw(currentPose.orientation) + M_PI);
116 while (yaw_error > M_PI) yaw_error -= 2 * M_PI;
117 while (yaw_error < -M_PI) yaw_error += 2 * M_PI;
120 double cmd_linear_x = kp_linear * distance_to_target + ki_linear *
integral_linear_ +
136 geometry_msgs::msg::Twist cmd_vel_msg;
138 cmd_vel_msg.linear.x = cmd_linear_x;
139 cmd_vel_msg.angular.z = cmd_angular_z;
192 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] Finished behavior execution");