38 cmd_vel_pub_ = nh->create_publisher<geometry_msgs::msg::Twist>(
"/cmd_vel", rclcpp::QoS(1));
44 geometry_msgs::msg::Twist cmd_vel;
47 geometry_msgs::msg::Pose currentPose = pose->toPoseMsg();
49 rclcpp::Rate loop_rate(10);
52 double kp_linear = 0.5;
53 double ki_linear = 0.0;
54 double kd_linear = 0.1;
56 double kp_angular = 0.5;
57 double ki_angular = 0.0;
58 double kd_angular = 0.1;
63 RCLCPP_INFO_STREAM_THROTTLE(
65 "CbPositionControlFreeSpace, current pose: " << currentPose.position.x <<
", "
66 << currentPose.position.y <<
", "
67 << tf2::getYaw(currentPose.orientation));
69 RCLCPP_INFO_STREAM_THROTTLE(
71 "CbPositionControlFreeSpace, target pose: " <<
target_pose_.position.x <<
", "
76 currentPose = pose->toPoseMsg();
82 double error_x =
target_pose_.position.x - currentPose.position.x;
83 double error_y =
target_pose_.position.y - currentPose.position.y;
86 double distance_to_target = std::sqrt(error_x * error_x + error_y * error_y);
89 getLogger(),
"[" <<
getName() <<
"] distance to target: " << distance_to_target
95 RCLCPP_INFO(
getLogger(),
"Goal reached!");
101 double desired_yaw = std::atan2(error_y, error_x);
106 double yaw_error = desired_yaw - tf2::getYaw(currentPose.orientation);
109 while (yaw_error > M_PI) yaw_error -= 2 * M_PI;
110 while (yaw_error < -M_PI) yaw_error += 2 * M_PI;
113 double cmd_linear_x = kp_linear * distance_to_target + ki_linear *
integral_linear_ +
129 geometry_msgs::msg::Twist cmd_vel_msg;
131 cmd_vel_msg.linear.x = cmd_linear_x;
132 cmd_vel_msg.angular.z = cmd_angular_z;
147 if (rclcpp::ok())
cmd_vel_pub_->publish(geometry_msgs::msg::Twist());
149 RCLCPP_INFO_STREAM(
getLogger(),
"[" <<
getName() <<
"] Finished behavior execution");