From 5eb20141136401037584650c367a0d2637950bf8 Mon Sep 17 00:00:00 2001 From: Cx330 <1487537121@qq.com> Date: Sun, 12 Oct 2025 13:34:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=A8=8B=E5=BA=8F=EF=BC=8C?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=B0=8F=E4=B9=8C=E9=BE=9F=E5=AE=9E=E6=97=B6?= =?UTF-8?q?=E5=9D=90=E6=A0=87=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Aee1220310013/scripts/client.py | 20 ++++++++++++++++---- src/Aee1220310013/scripts/server.py | 4 ++-- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Aee1220310013/scripts/client.py b/src/Aee1220310013/scripts/client.py index 5d66083..1f0cd40 100755 --- a/src/Aee1220310013/scripts/client.py +++ b/src/Aee1220310013/scripts/client.py @@ -2,14 +2,23 @@ import rospy from geometry_msgs.msg import Twist from turtlesim.srv import TeleportAbsolute +from turtlesim.msg import Pose from Aee1220310013.srv import ProcessID +current_pose = None # 全局变量保存乌龟位姿 + +# 位姿回调函数 +def pose_callback(msg): + global current_pose + current_pose = msg + print(f"当前位置: x={msg.x:.2f}, y={msg.y:.2f}, 方向={msg.theta:.2f}") + def main(): rospy.init_node('turtle_controller_client') student_id = input("输入学号:").strip() - # 调用服务获取参数 + # 处理学号 rospy.wait_for_service('/process_id') try: process_id = rospy.ServiceProxy('/process_id', ProcessID) @@ -19,14 +28,17 @@ def main(): return x, y, r, direction = resp.x, resp.y, resp.radius, resp.direction - print(f"服务返回: x={x}, y={y}, 半径={r}, 方向={direction}") + print(f"x={x}, y={y}, 半径={r}, 方向={direction}") - # 移动乌龟到 (x, y) + # 移动乌龟到 (x, y) rospy.wait_for_service('/turtle1/teleport_absolute') teleport = rospy.ServiceProxy('/turtle1/teleport_absolute', TeleportAbsolute) teleport(x, y, 0) print(f"乌龟已跳转到 ({x}, {y})") + # 乌龟位置 + rospy.Subscriber('/turtle1/pose', Pose, pose_callback) + # 画圆 pub = rospy.Publisher('/turtle1/cmd_vel', Twist, queue_size=10) rate = rospy.Rate(10) @@ -38,7 +50,7 @@ def main(): vel_msg.linear.x = linear_speed vel_msg.angular.z = direction * angular_speed - print("开始画圆,按 Ctrl+Z 停止。") + print("开始画圆,按 Ctrl+C 停止。") while not rospy.is_shutdown(): pub.publish(vel_msg) diff --git a/src/Aee1220310013/scripts/server.py b/src/Aee1220310013/scripts/server.py index dc2bb27..51dbf28 100755 --- a/src/Aee1220310013/scripts/server.py +++ b/src/Aee1220310013/scripts/server.py @@ -24,7 +24,7 @@ def handle_process_id(req): total_three = sum(int(d) for d in last_three) radius = total_three % 3 if radius == 0: - radius = 3 + radius = 3 / 2 # 方向(奇数顺时针,偶数逆时针) direction = -1 if last_one % 2 else 1 @@ -35,7 +35,7 @@ def handle_process_id(req): def id_processor_server(): rospy.init_node('id_processor_server') service = rospy.Service('/process_id', ProcessID, handle_process_id) - rospy.loginfo("服务端已启动:/process_id") + rospy.loginfo("服务端已启动。") rospy.spin() if __name__ == "__main__":