جارٍ تحميل الدرس...
في نافذة الطباعة اختر «حفظ كملف PDF» كوجهة.
نموذج النشر والاشتراك (Pub/Sub) في التطبيق العملي
Topic)Publisher
Topic
Subscriber
Talker رسائل إلى /chatterListener في /chatter وتطبع ما تستقبلهros2 run demo_nodes_py talker
ros2 run demo_nodes_py listenerros2 topic list
ros2 topic echo /chatterturtlesim طريقة رسومية لمشاهدة النشر والاشتراك أثناء العملros2 run turtlesim turtlesim_node
ros2 run turtlesim turtle_teleop_keyturtle_teleop_key
/turtle1/cmd_vel
turtlesim_node
ros2 topic list
ros2 topic echo /turtle1/cmd_vel
ros2 topic info /turtle1/cmd_vel
ros2 interface show geometry_msgs/msg/Twisttopic infoTwistfloat64.package.xml: rclpy وgeometry_msgs وturtlesimTwist عبر مؤقت (Timer) لتحريك السلحفاة في دائرةclass DrawCircleNode(Node):
def __init__(self):
super().__init__("draw_circle")
self.cmd_vel_pub = self.create_publisher(Twist, "/turtle1/cmd_vel", 10)
self.timer = self.create_timer(0.5, self.send_velocity_command)
def send_velocity_command(self):
msg = Twist()
msg.linear.x = 2.0
msg.angular.z = 1.0
self.cmd_vel_pub.publish(msg)turtlesim/msg/Posex وy وtheta وlinear_velocity وangular_velocity./turtle1/pose يستخدم نوع الرسالة turtlesim/msg/Poserclpy وturtlesimclass PoseSubscriberNode(Node):
def __init__(self):
super().__init__("pose_subscriber")
self.subscription = self.create_subscription(
Pose, "/turtle1/pose", self.pose_callback, 10)
def pose_callback(self, msg):
self.get_logger().info(
f"x: {msg.x}, y: {msg.y}, theta: {msg.theta}")colcon build --symlink-install
source install/setup.bash
ros2 run my_first_py_pkg draw_circle
ros2 run my_first_py_pkg pose_subscriber # separate terminalrqt_graph العقد واتصالات المواضيع بصرياrqt_graph
ros2 topic list
ros2 topic echo /topicros2 topic info مع ros2 interface show نوع رسالة الموضوع وبنيتهاcreate_publisher مع create_timer = نشر دوريcreate_subscription مع دالة رد نداء (Callback) = الاستجابة للرسائل الواردةsetup.py قبل تشغيلهاrqt_graph وros2 topic echo أساسيتين لتشخيص أخطاء النشر والاشتراكMahmoud Abas|الـTopics في ROS 2، وnodes النشر (Publisher) والاشتراك (Subscriber)